]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4453 Added tests to cover permission template application
authorJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>
Thu, 11 Jul 2013 15:19:51 +0000 (17:19 +0200)
committerJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>
Thu, 11 Jul 2013 15:19:51 +0000 (17:19 +0200)
21 files changed:
sonar-core/src/main/java/org/sonar/core/permission/ComponentPermissionFacade.java
sonar-core/src/main/resources/org/sonar/core/user/PermissionTemplateMapper.xml
sonar-core/src/main/resources/org/sonar/core/user/UserMapper.xml
sonar-core/src/test/java/org/sonar/core/permission/ComponentPermissionFacadeTest.java
sonar-core/src/test/java/org/sonar/core/user/PermissionDaoTest.java
sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_apply_permission_template-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_apply_permission_template.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeGroupPermissionFromTemplate-result.xml
sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeGroupPermissionFromTemplate.xml
sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeNullGroupPermissionFromTemplate-result.xml
sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeNullGroupPermissionFromTemplate.xml
sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeUserPermissionFromTemplate-result.xml
sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeUserPermissionFromTemplate.xml
sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionTemplateService.java
sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateUpdater.java
sonar-server/src/main/webapp/WEB-INF/app/controllers/permission_templates_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/controllers/roles_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/helpers/roles_helper.rb
sonar-server/src/main/webapp/WEB-INF/app/views/permission_templates/_edit_groups.html.erb
sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionTemplateServiceTest.java
sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateTest.java

index 177a5c69f5fba8583ecda8b0d5f3f32444677f8f..e1dcb52b8d8421ccc99606e53dd445f87bc22693 100644 (file)
@@ -135,6 +135,7 @@ public class ComponentPermissionFacade implements TaskComponent, ServerComponent
   public void applyPermissionTemplate(Long templateId, Long resourceId) {
     SqlSession session = myBatis.openSession();
     try {
+      removeAllPermissions(resourceId, session);
       PermissionTemplateDto permissionTemplate = getPermissionTemplate(templateId);
       List<PermissionTemplateUserDto> usersPermissions = permissionTemplate.getUsersPermissions();
       if(usersPermissions != null) {
@@ -146,7 +147,8 @@ public class ComponentPermissionFacade implements TaskComponent, ServerComponent
       List<PermissionTemplateGroupDto> groupsPermissions = permissionTemplate.getGroupsPermissions();
       if(groupsPermissions != null) {
         for (PermissionTemplateGroupDto groupPermission : groupsPermissions) {
-          addGroupPermission(resourceId, groupPermission.getGroupName(), groupPermission.getPermission(), session);
+          String groupName = groupPermission.getGroupName() == null ? DefaultGroups.ANYONE : groupPermission.getGroupName();
+          addGroupPermission(resourceId, groupName, groupPermission.getPermission(), session);
         }
       }
       session.commit();
index de84f3485bd42d00a8fc995c3851df88a6e3c8e8..4e9dc2d47f0900364cd0354a22437f2b8a6d887f 100644 (file)
@@ -38,6 +38,7 @@
     DELETE FROM perm_templates_users
     WHERE template_id = #{templateId}
     AND user_id = #{userId}
+    AND permission_reference = #{permission}
   </delete>
 
   <insert id="insertGroupPermission" parameterType="PermissionTemplateGroup">
@@ -48,6 +49,7 @@
   <delete id="deleteGroupPermission" parameterType="PermissionTemplateGroup">
     DELETE FROM perm_templates_groups
     WHERE template_id = #{templateId}
+    AND permission_reference = #{permission}
     AND
     <choose>
       <when test="groupId != null">
index 3b5f9003cb6d0e99fb50a19f93e0c44550c5342c..4b3a0ad059938a36968e60c1be49bc16f9624097 100644 (file)
   </sql>
 
   <select id="selectUserByLogin" parameterType="string" resultType="User">
-    select
+    SELECT
     <include refid="userColumns"/>
-    from users u where u.login=#{id} and u.active=${_true}
+    FROM users u WHERE u.login=#{id} AND u.active=${_true}
   </select>
 
   <select id="selectUsersByLogins" parameterType="map" resultType="User">
-    select
+    SELECT
     <include refid="userColumns"/>
-    from users u where u.login in
+    FROM users u WHERE u.login IN
     <foreach item="login" index="index" collection="logins" open="(" separator="," close=")">
       #{login}
     </foreach>
   </select>
 
   <select id="selectUsers" parameterType="map" resultType="User">
-    select
+    SELECT
     <include refid="userColumns"/>
-    from users u
+    FROM users u
     <where>
       <if test="logins != null and logins.size() > 0">
-        u.login in
+        u.login IN
         <foreach item="login" index="index" collection="logins" open="(" separator="," close=")">
           #{login}
         </foreach>
       </if>
       <if test="includeDeactivated==false">
-        and u.active=${_true}
+        AND u.active=${_true}
       </if>
       <if test="searchText != null">
-        and (u.login like #{searchTextSql} escape '/' or u.name like #{searchTextSql} escape '/')
+        AND (u.login LIKE #{searchTextSql} ESCAPE '/' OR u.name LIKE #{searchTextSql} ESCAPE '/')
       </if>
     </where>
-    order by u.name
+    ORDER BY u.name
   </select>
 
 
   <select id="selectGroupByName" parameterType="string" resultType="Group">
-    select id, name, description, created_at AS "createdAt", updated_at AS "updatedAt"
-    from groups where name=#{id}
+    SELECT id, name, description, created_at AS "createdAt", updated_at AS "updatedAt"
+    FROM groups WHERE name=#{id}
   </select>
 
   <delete id="removeUserFromGroups" parameterType="long">
-    delete from groups_users where user_id=#{id}
+    DELETE FROM groups_users WHERE user_id=#{id}
   </delete>
 
   <delete id="deleteUserRoles" parameterType="long">
-    delete from user_roles where user_id=#{id}
+    DELETE FROM user_roles WHERE user_id=#{id}
   </delete>
 
   <delete id="deleteUserProperties" parameterType="long">
-    delete from properties where user_id=#{id}
+    DELETE FROM properties WHERE user_id=#{id}
   </delete>
 
   <delete id="deleteUserDashboards" parameterType="long">
-    delete from dashboards where user_id=#{id}
+    DELETE FROM dashboards WHERE user_id=#{id}
   </delete>
 
   <delete id="deleteUserActiveDashboards" parameterType="long">
-    delete from active_dashboards where user_id=#{id}
+    DELETE FROM active_dashboards WHERE user_id=#{id}
   </delete>
 
   <delete id="" parameterType="long">
-    delete from user_roles where user_id=#{id}
+    DELETE FROM user_roles WHERE user_id=#{id}
   </delete>
 
   <delete id="deleteUserMeasureFilters" parameterType="long">
-    delete from measure_filters where user_id=#{id}
+    DELETE FROM measure_filters WHERE user_id=#{id}
   </delete>
 
   <delete id="deleteUserMeasureFilterFavourites" parameterType="long">
-    delete from measure_filter_favourites where user_id=#{id}
+    DELETE FROM measure_filter_favourites WHERE user_id=#{id}
   </delete>
 
   <delete id="deleteUserIssueFilters" parameterType="String">
-    delete from issue_filters where user_login=#{id}
+    DELETE FROM issue_filters WHERE user_login=#{id}
   </delete>
 
   <delete id="deleteUserIssueFilterFavourites" parameterType="String">
-    delete from issue_filter_favourites where user_login=#{id}
+    DELETE FROM issue_filter_favourites WHERE user_login=#{id}
   </delete>
 
   <update id="deactivateUser" parameterType="long">
-    update users set active=${_false} where id=#{id}
+    UPDATE users SET active=${_false} WHERE id=#{id}
   </update>
 
 </mapper>
index 88fca000c36ad9fa59716fe7793a5d1017894cca..6760a01ee15ca2dac39db885c73046f9e72c3a09 100644 (file)
 
 package org.sonar.core.permission;
 
-public class ComponentPermissionFacadeTest {
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.core.persistence.AbstractDaoTestCase;
+import org.sonar.core.user.PermissionDao;
+import org.sonar.core.user.RoleDao;
+import org.sonar.core.user.UserDao;
+
+public class ComponentPermissionFacadeTest extends AbstractDaoTestCase {
+
+  private ComponentPermissionFacade permissionFacade;
+  private RoleDao roleDao;
+  private UserDao userDao;
+  private PermissionDao permissionDao;
+
+  @Before
+  public void setUp() {
+    roleDao = new RoleDao(getMyBatis());
+    userDao = new UserDao(getMyBatis());
+    permissionDao = new PermissionDao(getMyBatis());
+    permissionFacade = new ComponentPermissionFacade(getMyBatis(), roleDao, userDao, permissionDao);
+  }
+
+  @Test
+  public void should_apply_permission_template() throws Exception {
+    setupData("should_apply_permission_template");
+
+    permissionFacade.applyPermissionTemplate(1L, 123L);
+
+    checkTable("should_apply_permission_template", "group_roles", "group_id", "resource_id", "role");
+    checkTable("should_apply_permission_template", "user_roles", "group_id", "resource_id", "role");
+  }
 }
index 6f99afabffc3144eb98c187d1fe0d5f7d51b1c90..bfce78a6942590399b98071886876188f58a791b 100644 (file)
@@ -129,8 +129,8 @@ public class PermissionDaoTest extends AbstractDaoTestCase {
     permissionDao.deletePermissionTemplate(1L);
 
     checkTable("deletePermissionTemplate", "permission_templates", "id", "name", "description");
-    checkTable("deletePermissionTemplate", "perm_templates_users", "id", "template_id", "user_id", "permission");
-    checkTable("deletePermissionTemplate", "perm_templates_groups", "id", "template_id", "group_id", "permission");
+    checkTable("deletePermissionTemplate", "perm_templates_users", "id", "template_id", "user_id", "permission_reference");
+    checkTable("deletePermissionTemplate", "perm_templates_groups", "id", "template_id", "group_id", "permission_reference");
   }
 
   @Test
@@ -139,18 +139,18 @@ public class PermissionDaoTest extends AbstractDaoTestCase {
     permissionDao.addUserPermission(1L, 1L, "new_permission");
 
     checkTable("addUserPermissionToTemplate", "permission_templates", "id", "name", "description");
-    checkTable("addUserPermissionToTemplate", "perm_templates_users", "id", "template_id", "user_id", "permission");
-    checkTable("addUserPermissionToTemplate", "perm_templates_groups", "id", "template_id", "group_id", "permission");
+    checkTable("addUserPermissionToTemplate", "perm_templates_users", "id", "template_id", "user_id", "permission_reference");
+    checkTable("addUserPermissionToTemplate", "perm_templates_groups", "id", "template_id", "group_id", "permission_reference");
   }
 
   @Test
   public void should_remove_user_permission_from_template() throws Exception {
     setupData("removeUserPermissionFromTemplate");
-    permissionDao.removeUserPermission(1L, 2L, "existing_permission");
+    permissionDao.removeUserPermission(1L, 2L, "permission_to_remove");
 
     checkTable("removeUserPermissionFromTemplate", "permission_templates", "id", "name", "description");
-    checkTable("removeUserPermissionFromTemplate", "perm_templates_users", "id", "template_id", "user_id", "permission");
-    checkTable("removeUserPermissionFromTemplate", "perm_templates_groups", "id", "template_id", "group_id", "permission");
+    checkTable("removeUserPermissionFromTemplate", "perm_templates_users", "id", "template_id", "user_id", "permission_reference");
+    checkTable("removeUserPermissionFromTemplate", "perm_templates_groups", "id", "template_id", "group_id", "permission_reference");
   }
 
   @Test
@@ -159,18 +159,18 @@ public class PermissionDaoTest extends AbstractDaoTestCase {
     permissionDao.addGroupPermission(1L, 1L, "new_permission");
 
     checkTable("addGroupPermissionToTemplate", "permission_templates", "id", "name", "description");
-    checkTable("addGroupPermissionToTemplate", "perm_templates_users", "id", "template_id", "user_id", "permission");
-    checkTable("addGroupPermissionToTemplate", "perm_templates_groups", "id", "template_id", "group_id", "permission");
+    checkTable("addGroupPermissionToTemplate", "perm_templates_users", "id", "template_id", "user_id", "permission_reference");
+    checkTable("addGroupPermissionToTemplate", "perm_templates_groups", "id", "template_id", "group_id", "permission_reference");
   }
 
   @Test
   public void should_remove_group_permission_from_template() throws Exception {
     setupData("removeGroupPermissionFromTemplate");
-    permissionDao.removeGroupPermission(1L, 2L, "existing_permission");
+    permissionDao.removeGroupPermission(1L, 2L, "permission_to_remove");
 
     checkTable("removeGroupPermissionFromTemplate", "permission_templates", "id", "name", "description");
-    checkTable("removeGroupPermissionFromTemplate", "perm_templates_users", "id", "template_id", "user_id", "permission");
-    checkTable("removeGroupPermissionFromTemplate", "perm_templates_groups", "id", "template_id", "group_id", "permission");
+    checkTable("removeGroupPermissionFromTemplate", "perm_templates_users", "id", "template_id", "user_id", "permission_reference");
+    checkTable("removeGroupPermissionFromTemplate", "perm_templates_groups", "id", "template_id", "group_id", "permission_reference");
   }
 
   @Test
@@ -179,17 +179,17 @@ public class PermissionDaoTest extends AbstractDaoTestCase {
     permissionDao.addGroupPermission(1L, null, "new_permission");
 
     checkTable("addNullGroupPermissionToTemplate", "permission_templates", "id", "name", "description");
-    checkTable("addNullGroupPermissionToTemplate", "perm_templates_users", "id", "template_id", "user_id", "permission");
-    checkTable("addNullGroupPermissionToTemplate", "perm_templates_groups", "id", "template_id", "group_id", "permission");
+    checkTable("addNullGroupPermissionToTemplate", "perm_templates_users", "id", "template_id", "user_id", "permission_reference");
+    checkTable("addNullGroupPermissionToTemplate", "perm_templates_groups", "id", "template_id", "group_id", "permission_reference");
   }
 
   @Test
   public void should_remove_group_permission_with_null_name() throws Exception {
     setupData("removeNullGroupPermissionFromTemplate");
-    permissionDao.removeGroupPermission(1L, null, "existing_permission");
+    permissionDao.removeGroupPermission(1L, null, "permission_to_remove");
 
     checkTable("removeNullGroupPermissionFromTemplate", "permission_templates", "id", "name", "description");
-    checkTable("removeNullGroupPermissionFromTemplate", "perm_templates_users", "id", "template_id", "user_id", "permission");
-    checkTable("removeNullGroupPermissionFromTemplate", "perm_templates_groups", "id", "template_id", "group_id", "permission");
+    checkTable("removeNullGroupPermissionFromTemplate", "perm_templates_users", "id", "template_id", "user_id", "permission_reference");
+    checkTable("removeNullGroupPermissionFromTemplate", "perm_templates_groups", "id", "template_id", "group_id", "permission_reference");
   }
 }
diff --git a/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_apply_permission_template-result.xml b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_apply_permission_template-result.xml
new file mode 100644 (file)
index 0000000..ab25beb
--- /dev/null
@@ -0,0 +1,34 @@
+<dataset>
+
+  <groups id="100" name="sonar-administrators" />
+  <groups id="101" name="sonar-users" />
+
+  <users id="200" login="marius" name="Marius" email="[null]" active="[true]" />
+
+  <!-- on other resources -->
+  <group_roles id="1" group_id="100" resource_id="1" role="admin"/>
+  <group_roles id="2" group_id="101" resource_id="1" role="user"/>
+  <user_roles id="1" user_id="200" resource_id="1" role="admin"/>
+
+  <!-- new groups permissions : sonar-administrators (admin), sonar-users (user & codeviewer), Anyone (user & codeviewer) -->
+  <group_roles id="3" group_id="100" resource_id="123" role="admin"/>
+  <group_roles id="4" group_id="101" resource_id="123" role="user"/>
+  <group_roles id="5" group_id="[null]" resource_id="123" role="user"/>
+  <group_roles id="6" group_id="101" resource_id="123" role="codeviewer"/>
+  <group_roles id="7" group_id="[null]" resource_id="123" role="codeviewer"/>
+
+  <!-- new user permission : marius (admin) -->
+  <user_roles id="2" user_id="200" resource_id="123" role="admin"/>
+
+  <!-- default permission template for all qualifiers -->
+  <permission_templates id="1" name="default"/>
+
+  <perm_templates_groups id="1" template_id="1" group_id="100" permission_reference="admin"/>
+  <perm_templates_groups id="2" template_id="1" group_id="101" permission_reference="user"/>
+  <perm_templates_groups id="3" template_id="1" group_id="[null]" permission_reference="user"/>
+  <perm_templates_groups id="4" template_id="1" group_id="101" permission_reference="codeviewer"/>
+  <perm_templates_groups id="5" template_id="1" group_id="[null]" permission_reference="codeviewer"/>
+
+  <perm_templates_users id="1" template_id="1" user_id="200" permission_reference="admin"/>
+
+</dataset>
\ No newline at end of file
diff --git a/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_apply_permission_template.xml b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_apply_permission_template.xml
new file mode 100644 (file)
index 0000000..f962b00
--- /dev/null
@@ -0,0 +1,25 @@
+<dataset>
+
+  <groups id="100" name="sonar-administrators" />
+  <groups id="101" name="sonar-users" />
+
+  <users id="200" login="marius" name="Marius" email="[null]" active="[true]" />
+
+  <!-- on other resources -->
+  <group_roles id="1" group_id="100" resource_id="1" role="admin"/>
+  <group_roles id="2" group_id="101" resource_id="1" role="user"/>
+  <user_roles id="1" user_id="200" resource_id="1" role="admin"/>
+
+
+  <!-- default permission template for all qualifiers -->
+  <permission_templates id="1" name="default"/>
+
+  <perm_templates_groups id="1" template_id="1" group_id="100" permission_reference="admin"/>
+  <perm_templates_groups id="2" template_id="1" group_id="101" permission_reference="user"/>
+  <perm_templates_groups id="3" template_id="1" group_id="[null]" permission_reference="user"/>
+  <perm_templates_groups id="4" template_id="1" group_id="101" permission_reference="codeviewer"/>
+  <perm_templates_groups id="5" template_id="1" group_id="[null]" permission_reference="codeviewer"/>
+
+  <perm_templates_users id="1" template_id="1" user_id="200" permission_reference="admin"/>
+
+</dataset>
\ No newline at end of file
index d2b04dcce89bf32458a7d5a1e3b8b3dfe6283ca4..33773a8d4e076d4d96a2c019ff51b7f8e3406b15 100644 (file)
@@ -1,5 +1,6 @@
 <dataset>
   <permission_templates id="1" name="my template" description="my description"/>
   <perm_templates_users/>
-  <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission"/>
+  <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission_other_group"/>
+  <perm_templates_groups id="3" template_id="1" group_id="2" permission_reference="remaining_permission_same_group"/>
 </dataset>
\ No newline at end of file
index 2cb0ab460fdf07197dd1accfb99b9b625f4a0223..9df53ee7d2b7db3df54dcc8d3056d8b5293cea10 100644 (file)
@@ -1,6 +1,7 @@
 <dataset>
   <permission_templates id="1" name="my template" description="my description"/>
   <perm_templates_users/>
-  <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission"/>
-  <perm_templates_groups id="2" template_id="1" group_id="2" permission_reference="existing_permission"/>
+  <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission_other_group"/>
+  <perm_templates_groups id="2" template_id="1" group_id="2" permission_reference="permission_to_remove"/>
+  <perm_templates_groups id="3" template_id="1" group_id="2" permission_reference="remaining_permission_same_group"/>
 </dataset>
\ No newline at end of file
index d2b04dcce89bf32458a7d5a1e3b8b3dfe6283ca4..52fd267873b9b5e6292f8ed51e60476bad158b44 100644 (file)
@@ -1,5 +1,6 @@
 <dataset>
   <permission_templates id="1" name="my template" description="my description"/>
   <perm_templates_users/>
-  <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission"/>
+  <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission_other_group"/>
+  <perm_templates_groups id="3" template_id="1" group_id="[null]" permission_reference="remaining_permission_same_group"/>
 </dataset>
\ No newline at end of file
index 23bd158f3a1539020880a4c386c178d802b1c825..3e3f93359a2b23ffccbd01c4ca01c6a47dd79f19 100644 (file)
@@ -1,6 +1,7 @@
 <dataset>
   <permission_templates id="1" name="my template" description="my description"/>
   <perm_templates_users/>
-  <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission"/>
-  <perm_templates_groups id="2" template_id="1" group_id="[null]" permission_reference="existing_permission"/>
+  <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission_other_group"/>
+  <perm_templates_groups id="2" template_id="1" group_id="[null]" permission_reference="permission_to_remove"/>
+  <perm_templates_groups id="3" template_id="1" group_id="[null]" permission_reference="remaining_permission_same_group"/>
 </dataset>
\ No newline at end of file
index f4c500ba1ffbe137fa4171bd7c1f5e8170f3422f..925a1b25692d36b6a629757d66a54b8c27d190e7 100644 (file)
@@ -1,7 +1,8 @@
 <dataset>
   <permission_templates id="1" name="my template" description="my description"/>
 
-  <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="remaining_permission"/>
+  <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="remaining_permission_other_user"/>
+  <perm_templates_users id="3" template_id="1" user_id="2" permission_reference="remaining_permission_same_user"/>
 
   <perm_templates_groups/>
 </dataset>
\ No newline at end of file
index 219c1f07286be20d006228f4549271ff8d879700..c160e4edf300e94666616609dec1b811c5d6baf4 100644 (file)
@@ -1,8 +1,9 @@
 <dataset>
   <permission_templates id="1" name="my template" description="my description"/>
 
-  <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="remaining_permission"/>
-  <perm_templates_users id="2" template_id="1" user_id="2" permission_reference="existing_permission"/>
+  <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="remaining_permission_other_user"/>
+  <perm_templates_users id="2" template_id="1" user_id="2" permission_reference="permission_to_remove"/>
+  <perm_templates_users id="3" template_id="1" user_id="2" permission_reference="remaining_permission_same_user"/>
 
   <perm_templates_groups/>
 </dataset>
\ No newline at end of file
index 6f19069ca9099179d2c62d53cc57159be34c7bb9..306771b55ee94342ae4c337b418c270e5491bf7f 100644 (file)
@@ -34,7 +34,6 @@ import org.sonar.server.exceptions.ServerErrorException;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import java.util.List;
-import java.util.Map;
 
 /**
  * Used by ruby code <pre>Internal.permission_templates</pre>
@@ -135,12 +134,6 @@ public class InternalPermissionTemplateService implements ServerComponent {
       }
     };
     updater.executeUpdate();
-  }
-
-  public void applyPermissionTemplate(String templateName, Map<String, Object> params) {
-
-
-
   }
 
   private void validateTemplateName(Long templateId, String templateName) {
index 9ab56eadb04ad23fdb06ff4653f3ec74645aed95..ace626614b17b741c30f68320676a46776c345a5 100644 (file)
@@ -21,6 +21,7 @@
 package org.sonar.server.permission;
 
 import com.google.common.collect.Lists;
+import org.sonar.api.security.DefaultGroups;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.user.*;
 import org.sonar.server.exceptions.BadRequestException;
@@ -62,6 +63,9 @@ abstract class PermissionTemplateUpdater {
   }
 
   Long getGroupId() {
+    if(DefaultGroups.isAnyone(updatedReference)) {
+      return null;
+    }
     GroupDto groupDto = userDao.selectGroupByName(updatedReference);
     if(groupDto == null) {
       throw new BadRequestException("Unknown group: " + updatedReference);
index e1bb8d7229131cb31eddabbda3d294b4bf5621c5..b9a99cb8fb078ee22ad56890bf9eb339f80a3f1c 100644 (file)
@@ -67,8 +67,8 @@ class PermissionTemplatesController < ApplicationController
   def edit_groups
     @permission = params[:permission]
     @permission_template = Internal.permission_templates.selectPermissionTemplate(params[:name])
-    @groups_with_permission = @permission_template.getGroupsForPermission(params[:permission]).collect {|g| [g.groupName, g.groupName]}
-    @groups_without_permission = all_groups.select {|g| !g.nil?}.each.collect {|g| [g.name, g.name]} - @groups_with_permission
+    @groups_with_permission = @permission_template.getGroupsForPermission(params[:permission]).collect {|g| [group_ref(g.groupName), group_ref(g.groupName)]}
+    @groups_without_permission = all_groups.each.collect {|g| g.nil? ? ['Anyone', 'Anyone'] : [g.name, g.name]} - @groups_with_permission
 
     render :partial => 'permission_templates/edit_groups'
   end
@@ -112,8 +112,8 @@ class PermissionTemplatesController < ApplicationController
 
     selected_groups = params[:groups] || []
 
-    previous_groups_with_permission = @permission_template.getGroupsForPermission(params[:permission]).collect {|g| [g.groupName, g.groupName]}
-    new_groups_with_permission = all_groups.select {|g| !g.nil? && selected_groups.include?(g.name)}.collect {|g| [g.name, g.name]}
+    previous_groups_with_permission = @permission_template.getGroupsForPermission(params[:permission]).collect {|g| [group_ref(g.groupName), group_ref(g.groupName)]}
+    new_groups_with_permission = all_groups.collect {|g| g.nil? ? ['Anyone', 'Anyone'] : [g.name, g.name]}.select {|g| selected_groups.include?(g[1])}
 
     promoted_groups = new_groups_with_permission - previous_groups_with_permission
     demoted_groups = previous_groups_with_permission - new_groups_with_permission
index 9d6832a8aba0043cf2ab0b9e6db2d029f76f8c3f..343a34a93184ea7a1b5c155fe7e70ba7b165ba24 100644 (file)
@@ -122,9 +122,8 @@ class RolesController < ApplicationController
 
   def bulk_apply_template
     verify_post_request
-    require_parameters :name
+    require_parameters :template_id
     Internal.permissions.applyPermissionTemplate(params)
-
     redirect_to :action => 'projects'
   end
 
index 9d32cabffae8ff9553e619540294316a84b240e2..681eee71547dd63063edfb9ca9935ab2aacbcf41 100644 (file)
@@ -46,6 +46,10 @@ module RolesHelper
     group ? group.name : 'Anyone'
   end
 
+  def group_ref(group_name)
+    group_name.blank? ? 'Anyone' : group_name
+  end
+
   def default_project_group_names(role, qualifier)
     group_names=(controller.java_facade.getConfigurationValue("sonar.role.#{role}.#{qualifier}.defaultGroups")||'').split(',')
 
index 1284414c6d3bc6c6c3691e625f3d3baf539f1d9c..a5d0a387401731413ab98ff9053d0c059b7fffb6 100644 (file)
@@ -15,7 +15,7 @@
 
             <select name="from" id="from" size="30" style="margin: 5px 0; width: 300px;" multiple="multiple">
               <% @groups_without_permission.sort.each do |group| %>
-                <option value="<%= group[1] -%>"><%= h group[0] -%></option>
+                <option value="<%= group[1] -%>"><%= h(group[0].blank? ? 'Anyone' : group[0]) -%></option>
               <% end %>
             </select>
           </td>
index 1ea834643af88e453f0a172138e1eef20147cfa8..49d9e10a76d8c29afb83274f184e3bc31ba7e715 100644 (file)
@@ -248,6 +248,26 @@ public class InternalPermissionTemplateServiceTest {
     verify(permissionDao, times(1)).removeGroupPermission(1L, 1L, DEFAULT_PERMISSION);
   }
 
+  @Test
+  public void should_add_permission_to_anyone_group() throws Exception {
+    when(permissionDao.selectTemplateByName(DEFAULT_NAME)).thenReturn(DEFAULT_TEMPLATE);
+
+    permissionTemplateService.addGroupPermission(DEFAULT_NAME, DEFAULT_PERMISSION, "Anyone");
+
+    verify(permissionDao).addGroupPermission(1L, null, DEFAULT_PERMISSION);
+    verifyZeroInteractions(userDao);
+  }
+
+  @Test
+  public void should_remove_permission_from_anyone_group() throws Exception {
+    when(permissionDao.selectTemplateByName(DEFAULT_NAME)).thenReturn(DEFAULT_TEMPLATE);
+
+    permissionTemplateService.removeGroupPermission(DEFAULT_NAME, DEFAULT_PERMISSION, "Anyone");
+
+    verify(permissionDao).removeGroupPermission(1L, null, DEFAULT_PERMISSION);
+    verifyZeroInteractions(userDao);
+  }
+
   private PermissionTemplateUserDto buildUserPermission(String userName, String permission) {
     return new PermissionTemplateUserDto().setUserName(userName).setPermission(permission);
   }
index 6556607f6a5560859cd28de0d345925cc3c0987f..db52030e3ccec8b2ad8b730797758b544307be65 100644 (file)
@@ -44,7 +44,8 @@ public class PermissionTemplateTest {
       ))
       .setGroupsByPermission(Lists.newArrayList(
         new PermissionTemplateGroupDto().setId(1L).setGroupId(1L).setGroupName("group1").setPermission("permission3"),
-        new PermissionTemplateGroupDto().setId(2L).setGroupId(2L).setGroupName("group2").setPermission("permission3")
+        new PermissionTemplateGroupDto().setId(2L).setGroupId(2L).setGroupName("group2").setPermission("permission3"),
+        new PermissionTemplateGroupDto().setId(3L).setGroupId(null).setGroupName(null).setPermission("permission3")
       ));
 
     PermissionTemplate permissionTemplate = PermissionTemplate.create(permissionTemplateDto);
@@ -59,7 +60,7 @@ public class PermissionTemplateTest {
     assertThat(permissionTemplate.getUsersForPermission("permission2")).onProperty("userName").containsOnly("user1");
     assertThat(permissionTemplate.getUsersForPermission("permission2")).onProperty("userId").containsOnly(1L);
     assertThat(permissionTemplate.getUsersForPermission("permission2")).onProperty("userLogin").containsOnly("login1");
-    assertThat(permissionTemplate.getGroupsForPermission("permission3")).onProperty("groupName").containsOnly("group1", "group2");
-    assertThat(permissionTemplate.getGroupsForPermission("permission3")).onProperty("groupId").containsOnly(1L, 2L);
+    assertThat(permissionTemplate.getGroupsForPermission("permission3")).onProperty("groupName").containsOnly("group1", "group2", null);
+    assertThat(permissionTemplate.getGroupsForPermission("permission3")).onProperty("groupId").containsOnly(1L, 2L, null);
   }
 }