diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-07-06 18:14:14 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-07-06 18:14:32 +0200 |
commit | 4925839fd7c126f25c6352d6688b8fafd5cd81f0 (patch) | |
tree | f24e09117e6901660fd721bf1b3f59eba3a089f7 /plugins | |
parent | b3ca2478b4cb39787d1be6e2e91463b9869b8908 (diff) | |
download | sonarqube-4925839fd7c126f25c6352d6688b8fafd5cd81f0.tar.gz sonarqube-4925839fd7c126f25c6352d6688b8fafd5cd81f0.zip |
SONAR-3618 improve support of the Views plugin
Diffstat (limited to 'plugins')
3 files changed, 14 insertions, 98 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/security/DefaultResourcePermissioning.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/security/DefaultResourcePermissioning.java index 123869b49d0..8de64d8047c 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/security/DefaultResourcePermissioning.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/security/DefaultResourcePermissioning.java @@ -19,13 +19,9 @@ */ package org.sonar.plugins.core.security; -import com.google.common.annotations.VisibleForTesting; import org.apache.ibatis.session.SqlSession; import org.sonar.api.BatchExtension; -import org.sonar.api.Properties; -import org.sonar.api.Property; import org.sonar.api.config.Settings; -import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Resource; import org.sonar.api.security.DefaultGroups; import org.sonar.api.security.ResourcePermissioning; @@ -36,23 +32,6 @@ import org.sonar.core.user.*; /** * @since 3.2 */ -@Properties({ - @Property(key = "sonar.role." + UserRole.ADMIN + ".TRK.defaultGroups", - name = "Default groups for project administrators", - defaultValue = DefaultGroups.ADMINISTRATORS, - global = false, - project = false), - @Property(key = "sonar.role." + UserRole.USER + ".TRK.defaultGroups", - name = "Default groups for project users", - defaultValue = DefaultGroups.USERS + "," + DefaultGroups.ANYONE, - global = false, - project = false), - @Property(key = "sonar.role." + UserRole.CODEVIEWER + ".TRK.defaultGroups", - name = "Default groups for project code viewers", - defaultValue = DefaultGroups.USERS + "," + DefaultGroups.ANYONE, - global = false, - project = false) -}) public class DefaultResourcePermissioning implements ResourcePermissioning, BatchExtension { private final Settings settings; @@ -146,8 +125,7 @@ public class DefaultResourcePermissioning implements ResourcePermissioning, Batc UserMapper userMapper = session.getMapper(UserMapper.class); RoleMapper roleMapper = session.getMapper(RoleMapper.class); - String strategy = getStrategy(resource); - String[] groupNames = settings.getStringArrayBySeparator("sonar.role." + role + "." + strategy + ".defaultGroups", ","); + String[] groupNames = settings.getStringArrayBySeparator("sonar.role." + role + "." + resource.getQualifier() + ".defaultGroups", ","); for (String groupName : groupNames) { GroupRoleDto groupRole = new GroupRoleDto().setRole(role).setResourceId(new Long(resource.getId())); if (DefaultGroups.isAnyone(groupName)) { @@ -160,7 +138,7 @@ public class DefaultResourcePermissioning implements ResourcePermissioning, Batc } } - String[] logins = settings.getStringArrayBySeparator("sonar.role." + role + "." + strategy + ".defaultUsers", ","); + String[] logins = settings.getStringArrayBySeparator("sonar.role." + role + "." + resource.getQualifier() + ".defaultUsers", ","); for (String login : logins) { UserDto user = userMapper.selectUserByLogin(login); if (user != null) { @@ -168,28 +146,4 @@ public class DefaultResourcePermissioning implements ResourcePermissioning, Batc } } } - - /** - * This is workaround to support old versions of the Views plugin. - * If the Views plugin does not define default permissions, then the standard permissions are re-used for new views. - */ - @VisibleForTesting - String getStrategy(Resource resource) { - String qualifier = resource.getQualifier(); - String result; - if (Qualifiers.PROJECT.equals(qualifier)) { - result = qualifier; - - } else if (hasRoleSettings(UserRole.ADMIN, qualifier) || hasRoleSettings(UserRole.USER, qualifier) || hasRoleSettings(UserRole.CODEVIEWER, qualifier)) { - result = qualifier; - } else { - result = Qualifiers.PROJECT; - } - return result; - } - - private boolean hasRoleSettings(String role, String qualifier) { - return settings.getString("sonar.role." + role + "." + qualifier + ".defaultGroups") != null - || settings.getString("sonar.role." + role + "." + qualifier + ".defaultUsers") != null; - } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/security/DefaultResourcePermissioningTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/security/DefaultResourcePermissioningTest.java index a67277fc376..50afeaaf526 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/security/DefaultResourcePermissioningTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/security/DefaultResourcePermissioningTest.java @@ -20,19 +20,13 @@ package org.sonar.plugins.core.security; import org.junit.Test; -import org.sonar.api.Properties; -import org.sonar.api.Property; -import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.Settings; import org.sonar.api.resources.Project; -import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Resource; import org.sonar.api.security.DefaultGroups; import org.sonar.core.persistence.AbstractDaoTestCase; import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; public class DefaultResourcePermissioningTest extends AbstractDaoTestCase { @@ -83,8 +77,15 @@ public class DefaultResourcePermissioningTest extends AbstractDaoTestCase { public void grantDefaultRoles() { setupData("grantDefaultRoles"); - Settings settings = new Settings(new PropertyDefinitions(DefaultResourcePermissioning.class)); + Settings settings = new Settings(); + settings.setProperty("sonar.role.admin.TRK.defaultGroups", "sonar-administrators"); + settings.setProperty("sonar.role.admin.TRK.defaultUsers", ""); + settings.setProperty("sonar.role.user.TRK.defaultGroups", "Anyone,sonar-users"); + settings.setProperty("sonar.role.user.TRK.defaultUsers", ""); + settings.setProperty("sonar.role.codeviewer.TRK.defaultGroups", "Anyone,sonar-users"); + settings.setProperty("sonar.role.codeviewer.TRK.defaultUsers", ""); DefaultResourcePermissioning permissioning = new DefaultResourcePermissioning(settings, getMyBatis()); + permissioning.grantDefaultRoles(project); checkTables("grantDefaultRoles", "user_roles", "group_roles"); @@ -134,43 +135,4 @@ public class DefaultResourcePermissioningTest extends AbstractDaoTestCase { // does not exist assertThat(permissioning.hasRoles(new Project("not_found"))).isFalse(); } - - @Test - public void use_default_project_roles_when_old_version_of_views_plugin() { - DefaultResourcePermissioning permissioning = new DefaultResourcePermissioning(new Settings(), getMyBatis()); - Resource view = mock(Resource.class); - when(view.getQualifier()).thenReturn(Qualifiers.VIEW); - - assertThat(permissioning.getStrategy(view)).isEqualTo(Qualifiers.PROJECT); - } - - @Test - public void use_existing_view_roles() { - Settings settings = new Settings(); - settings.setProperty("sonar.role.admin.VW.defaultUsers", "sonar-administrators"); - - DefaultResourcePermissioning permissioning = new DefaultResourcePermissioning(settings, getMyBatis()); - Resource view = mock(Resource.class); - when(view.getQualifier()).thenReturn(Qualifiers.VIEW); - - assertThat(permissioning.getStrategy(view)).isEqualTo(Qualifiers.VIEW); - } - - @Test - public void use_existing_default_view_roles() { - Settings settings = new Settings(new PropertyDefinitions(RecentViewPlugin.class)); - - DefaultResourcePermissioning permissioning = new DefaultResourcePermissioning(settings, getMyBatis()); - Resource view = mock(Resource.class); - when(view.getQualifier()).thenReturn(Qualifiers.VIEW); - - assertThat(permissioning.getStrategy(view)).isEqualTo(Qualifiers.VIEW); - } - - @Properties({ - @Property(key = "sonar.role.user.VW.defaultUsers", defaultValue = "sonar-users", name = "") - }) - static class RecentViewPlugin { - - } }
\ No newline at end of file diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/security/DefaultResourcePermissioningTest/grantDefaultRoles-result.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/security/DefaultResourcePermissioningTest/grantDefaultRoles-result.xml index 23b0c67e69c..f4154d15024 100644 --- a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/security/DefaultResourcePermissioningTest/grantDefaultRoles-result.xml +++ b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/security/DefaultResourcePermissioningTest/grantDefaultRoles-result.xml @@ -12,9 +12,9 @@ new rows : 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"/> + <group_roles id="4" group_id="[null]" resource_id="123" role="user"/> + <group_roles id="5" group_id="101" resource_id="123" role="user"/> + <group_roles id="6" group_id="[null]" resource_id="123" role="codeviewer"/> + <group_roles id="7" group_id="101" resource_id="123" role="codeviewer"/> </dataset>
\ No newline at end of file |