aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2013-10-25 12:10:45 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2013-10-25 12:10:54 +0200
commitddf293f0b9827b26b2efb96526ecc7d242750cba (patch)
tree39893072eb67907fea474ed6659f2d7a19e9d7a5
parent026762a32c9494e88c3cc18c787327e20f3dd0cc (diff)
downloadsonarqube-ddf293f0b9827b26b2efb96526ecc7d242750cba.tar.gz
sonarqube-ddf293f0b9827b26b2efb96526ecc7d242750cba.zip
SONAR-4725 web service for management of user group members
-rw-r--r--sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java2
-rw-r--r--sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql1
-rw-r--r--sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl2
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/controllers/groups_controller.rb80
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/models/group.rb2
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/db/migrate/460_add_unique_constraint_to_groups_users.rb29
6 files changed, 114 insertions, 2 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java
index 49d3e990ea2..61afc2419b7 100644
--- a/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java
+++ b/sonar-core/src/main/java/org/sonar/core/persistence/DatabaseVersion.java
@@ -33,7 +33,7 @@ import java.util.List;
*/
public class DatabaseVersion implements BatchComponent, ServerComponent {
- public static final int LAST_VERSION = 444;
+ public static final int LAST_VERSION = 460;
public static enum Status {
UP_TO_DATE, REQUIRES_UPGRADE, REQUIRES_DOWNGRADE, FRESH_INSTALL
diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql
index 1ffcfbd5558..b06bae5d988 100644
--- a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql
+++ b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql
@@ -182,6 +182,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('441');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('442');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('443');
INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('444');
+INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('460');
INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT, REMEMBER_TOKEN, REMEMBER_TOKEN_EXPIRES_AT) VALUES (1, 'admin', 'Administrator', '', 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '2011-09-26 22:27:48.0', '2011-09-26 22:27:48.0', null, null);
ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2;
diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl
index 5551481cb82..dc3419d5fcf 100644
--- a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl
+++ b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl
@@ -583,6 +583,8 @@ CREATE INDEX "INDEX_GROUPS_USERS_ON_GROUP_ID" ON "GROUPS_USERS" ("GROUP_ID");
CREATE INDEX "INDEX_GROUPS_USERS_ON_USER_ID" ON "GROUPS_USERS" ("USER_ID");
+CREATE UNIQUE INDEX "GROUPS_USERS_UNIQUE" ON "GROUPS_USERS" ("GROUP_ID", "USER_ID");
+
CREATE INDEX "DEPS_TO_SID" ON "DEPENDENCIES" ("TO_SNAPSHOT_ID");
CREATE INDEX "DEPS_FROM_SID" ON "DEPENDENCIES" ("FROM_SNAPSHOT_ID");
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