diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-10-25 12:10:45 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-10-25 12:10:54 +0200 |
commit | ddf293f0b9827b26b2efb96526ecc7d242750cba (patch) | |
tree | 39893072eb67907fea474ed6659f2d7a19e9d7a5 /sonar-server | |
parent | 026762a32c9494e88c3cc18c787327e20f3dd0cc (diff) | |
download | sonarqube-ddf293f0b9827b26b2efb96526ecc7d242750cba.tar.gz sonarqube-ddf293f0b9827b26b2efb96526ecc7d242750cba.zip |
SONAR-4725 web service for management of user group members
Diffstat (limited to 'sonar-server')
3 files changed, 110 insertions, 1 deletions
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/groups_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/groups_controller.rb index 7b50c340733..0cec72ec75f 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/groups_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/groups_controller.rb @@ -102,6 +102,86 @@ class GroupsController < ApplicationController end + # Used for selection of group members + def search_users + require_parameters :group, :page, :pageSize + + group_id = params[:group].to_i + selected = params[:selected]||'all' + query = params[:query] + page_id = params[:page].to_i + page_size = [params[:pageSize].to_i, 1000].min + + conditions = ['users.active=?'] + condition_values = [true] + if selected=='selected' + conditions << "groups_users.group_id=?" + condition_values << group_id + elsif selected=='deselected' + conditions << "groups_users.group_id is null" + end + if query + conditions << "users.name like ?" + condition_values << "%#{query}%" + end + + users = User.find(:all, + :select => 'users.id,users.name,groups_users.group_id', + :joins => "left join groups_users on users.id=groups_users.user_id and groups_users.group_id=#{group_id}", + :conditions => [conditions.join(' and ')].concat(condition_values), + :offset => (page_id-1) * page_size, + :limit => page_size + 1, + :order => 'users.name') + + more = false + if users.size>page_size + users = users[0...page_size] + more = true + end + + respond_to do |format| + format.json { + render :json => { + :more => more, + :results => users.map {|user| {:id => user.id, :value => user.name, :selected => (user.group_id != nil)}} + } + } + end + end + + def add_member + verify_post_request + require_parameters :group, :user + + user = User.find(:first, :conditions => {:id => params[:user], :active => true}) + group = Group.find(params[:group]) + status = 400 + if user && group + group.users << user + status = 200 if group.save + end + render :status => status, :text => '{}' + end + + def remove_member + verify_post_request + require_parameters :group, :user + + user_id = params[:user].to_i + group = Group.find(params[:group]) + status = 400 + if group + user = group.users.find(user_id) + if user + group.users.delete(user) + status = 200 if group.save + else + status = 200 + end + end + render :status => status, :text => '{}' + end + private def to_index(errors, id) diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/group.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/group.rb index f8ac4a0ee38..4cb1edef143 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/group.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/group.rb @@ -21,7 +21,7 @@ class Group < ActiveRecord::Base ANYONE = 'anyone' - has_and_belongs_to_many :users + has_and_belongs_to_many :users, :uniq => true has_many :group_roles, :dependent => :delete_all validates_presence_of :name diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/460_add_unique_constraint_to_groups_users.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/460_add_unique_constraint_to_groups_users.rb new file mode 100644 index 00000000000..efa8fae0426 --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/460_add_unique_constraint_to_groups_users.rb @@ -0,0 +1,29 @@ +# +# Sonar, entreprise quality control tool. +# Copyright (C) 2008-2013 SonarSource +# mailto:contact AT sonarsource DOT com +# +# SonarQube is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 3 of the License, or (at your option) any later version. +# +# SonarQube is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# + +# +# Version 4.1 +# +class AddUniqueConstraintToGroupsUsers < ActiveRecord::Migration + + def self.up + add_index :groups_users, [:group_id, :user_id], :name => 'GROUPS_USERS_UNIQUE', :unique => true + end +end |