import org.sonar.plugins.core.filters.ProjectFilter;
import org.sonar.plugins.core.filters.TreeMapFilter;
import org.sonar.plugins.core.security.ApplyProjectRolesDecorator;
-import org.sonar.plugins.core.security.DefaultResourcePermissioning;
+import org.sonar.plugins.core.security.DefaultResourcePermissions;
import org.sonar.plugins.core.sensors.*;
import org.sonar.plugins.core.testdetailsviewer.TestsViewerDefinition;
import org.sonar.plugins.core.timemachine.*;
ItLineCoverageDecorator.class,
ItCoverageDecorator.class,
ItBranchCoverageDecorator.class,
- DefaultResourcePermissioning.class,
+ DefaultResourcePermissions.class,
ApplyProjectRolesDecorator.class,
ExcludedResourceFilter.class,
CommentDensityDecorator.class,
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Resource;
-import org.sonar.api.security.ResourcePermissioning;
+import org.sonar.api.security.ResourcePermissions;
import java.util.Set;
public class ApplyProjectRolesDecorator implements Decorator {
- private final ResourcePermissioning resourcePermissioning;
+ private final ResourcePermissions resourcePermissions;
private final Set<String> QUALIFIERS = ImmutableSet.of(Qualifiers.PROJECT, Qualifiers.VIEW, Qualifiers.SUBVIEW);
- public ApplyProjectRolesDecorator(ResourcePermissioning resourcePermissioning) {
- this.resourcePermissioning = resourcePermissioning;
+ public ApplyProjectRolesDecorator(ResourcePermissions resourcePermissions) {
+ this.resourcePermissions = resourcePermissions;
}
public boolean shouldExecuteOnProject(Project project) {
public void decorate(Resource resource, DecoratorContext context) {
if (shouldDecorateResource(resource)) {
LoggerFactory.getLogger(ApplyProjectRolesDecorator.class).info("Grant default permissions to {}", resource.getKey());
- resourcePermissioning.grantDefaultRoles(resource);
+ resourcePermissions.grantDefaultRoles(resource);
}
}
private boolean shouldDecorateResource(Resource resource) {
- return resource.getId() != null && QUALIFIERS.contains(resource.getQualifier()) && !resourcePermissioning.hasRoles(resource);
+ return resource.getId() != null && QUALIFIERS.contains(resource.getQualifier()) && !resourcePermissions.hasRoles(resource);
}
}
+++ /dev/null
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar 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.
- *
- * Sonar 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 Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.plugins.core.security;
-
-import org.apache.ibatis.session.SqlSession;
-import org.sonar.api.BatchExtension;
-import org.sonar.api.config.Settings;
-import org.sonar.api.resources.Resource;
-import org.sonar.api.security.DefaultGroups;
-import org.sonar.api.security.ResourcePermissioning;
-import org.sonar.api.web.UserRole;
-import org.sonar.core.persistence.MyBatis;
-import org.sonar.core.user.*;
-
-/**
- * @since 3.2
- */
-public class DefaultResourcePermissioning implements ResourcePermissioning, BatchExtension {
-
- private final Settings settings;
- private final MyBatis myBatis;
-
- public DefaultResourcePermissioning(Settings settings, MyBatis myBatis) {
- this.settings = settings;
- this.myBatis = myBatis;
- }
-
- public boolean hasRoles(Resource resource) {
- if (resource.getId() != null) {
- SqlSession session = myBatis.openSession();
- try {
- RoleMapper roleMapper = session.getMapper(RoleMapper.class);
- Long resourceId = Long.valueOf(resource.getId());
- return roleMapper.countGroupRoles(resourceId) + roleMapper.countUserRoles(resourceId) > 0;
-
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
- return false;
- }
-
- public void grantUserRole(Resource resource, String login, String role) {
- if (resource.getId() != null) {
- SqlSession session = myBatis.openSession();
- try {
- UserDto user = session.getMapper(UserMapper.class).selectUserByLogin(login);
- if (user != null) {
- UserRoleDto userRole = new UserRoleDto()
- .setRole(role)
- .setUserId(user.getId())
- .setResourceId(Long.valueOf(resource.getId()));
- session.getMapper(RoleMapper.class).insertUserRole(userRole);
- session.commit();
- }
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
- }
-
- public void grantGroupRole(Resource resource, String groupName, String role) {
- if (resource.getId() != null) {
- SqlSession session = myBatis.openSession();
- try {
- GroupRoleDto groupRole = new GroupRoleDto()
- .setRole(role)
- .setResourceId(Long.valueOf(resource.getId()));
- if (DefaultGroups.isAnyone(groupName)) {
- session.getMapper(RoleMapper.class).insertGroupRole(groupRole);
- session.commit();
- } else {
- GroupDto group = session.getMapper(UserMapper.class).selectGroupByName(groupName);
- if (group != null) {
- session.getMapper(RoleMapper.class).insertGroupRole(groupRole.setGroupId(group.getId()));
- session.commit();
- }
- }
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
- }
-
- public void grantDefaultRoles(Resource resource) {
- if (resource.getId() != null) {
- SqlSession session = myBatis.openSession();
- try {
- removeRoles(resource, session);
- grantDefaultRoles(resource, UserRole.ADMIN, session);
- grantDefaultRoles(resource, UserRole.USER, session);
- grantDefaultRoles(resource, UserRole.CODEVIEWER, session);
- session.commit();
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
- }
-
- private void removeRoles(Resource resource, SqlSession session) {
- Long resourceId = Long.valueOf(resource.getId());
- RoleMapper mapper = session.getMapper(RoleMapper.class);
- mapper.deleteGroupRolesByResourceId(resourceId);
- mapper.deleteUserRolesByResourceId(resourceId);
- }
-
- private void grantDefaultRoles(Resource resource, String role, SqlSession session) {
- UserMapper userMapper = session.getMapper(UserMapper.class);
- RoleMapper roleMapper = session.getMapper(RoleMapper.class);
-
- String[] groupNames = settings.getStringArrayBySeparator("sonar.role." + role + "." + resource.getQualifier() + ".defaultGroups", ",");
- for (String groupName : groupNames) {
- GroupRoleDto groupRole = new GroupRoleDto().setRole(role).setResourceId(Long.valueOf(resource.getId()));
- if (DefaultGroups.isAnyone(groupName)) {
- roleMapper.insertGroupRole(groupRole);
- } else {
- GroupDto group = userMapper.selectGroupByName(groupName);
- if (group != null) {
- roleMapper.insertGroupRole(groupRole.setGroupId(group.getId()));
- }
- }
- }
-
- String[] logins = settings.getStringArrayBySeparator("sonar.role." + role + "." + resource.getQualifier() + ".defaultUsers", ",");
- for (String login : logins) {
- UserDto user = userMapper.selectUserByLogin(login);
- if (user != null) {
- roleMapper.insertUserRole(new UserRoleDto().setRole(role).setUserId(user.getId()).setResourceId(Long.valueOf(resource.getId())));
- }
- }
- }
-}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.core.security;
+
+import org.apache.ibatis.session.SqlSession;
+import org.sonar.api.BatchExtension;
+import org.sonar.api.config.Settings;
+import org.sonar.api.resources.Resource;
+import org.sonar.api.security.DefaultGroups;
+import org.sonar.api.security.ResourcePermissions;
+import org.sonar.api.web.UserRole;
+import org.sonar.core.persistence.MyBatis;
+import org.sonar.core.user.*;
+
+/**
+ * @since 3.2
+ */
+public class DefaultResourcePermissions implements ResourcePermissions, BatchExtension {
+
+ private final Settings settings;
+ private final MyBatis myBatis;
+
+ public DefaultResourcePermissions(Settings settings, MyBatis myBatis) {
+ this.settings = settings;
+ this.myBatis = myBatis;
+ }
+
+ public boolean hasRoles(Resource resource) {
+ if (resource.getId() != null) {
+ SqlSession session = myBatis.openSession();
+ try {
+ RoleMapper roleMapper = session.getMapper(RoleMapper.class);
+ Long resourceId = Long.valueOf(resource.getId());
+ return roleMapper.countGroupRoles(resourceId) + roleMapper.countUserRoles(resourceId) > 0;
+
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+ return false;
+ }
+
+ public void grantUserRole(Resource resource, String login, String role) {
+ if (resource.getId() != null) {
+ SqlSession session = myBatis.openSession();
+ try {
+ UserDto user = session.getMapper(UserMapper.class).selectUserByLogin(login);
+ if (user != null) {
+ UserRoleDto userRole = new UserRoleDto()
+ .setRole(role)
+ .setUserId(user.getId())
+ .setResourceId(Long.valueOf(resource.getId()));
+ RoleMapper roleMapper = session.getMapper(RoleMapper.class);
+ roleMapper.deleteUserRole(userRole);
+ roleMapper.insertUserRole(userRole);
+ session.commit();
+ }
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+ }
+
+ public void grantGroupRole(Resource resource, String groupName, String role) {
+ if (resource.getId() != null) {
+ SqlSession session = myBatis.openSession();
+ try {
+ GroupRoleDto groupRole = new GroupRoleDto()
+ .setRole(role)
+ .setResourceId(Long.valueOf(resource.getId()));
+ RoleMapper roleMapper = session.getMapper(RoleMapper.class);
+ if (DefaultGroups.isAnyone(groupName)) {
+ roleMapper.deleteGroupRole(groupRole);
+ roleMapper.insertGroupRole(groupRole);
+ session.commit();
+ } else {
+ GroupDto group = session.getMapper(UserMapper.class).selectGroupByName(groupName);
+ if (group != null) {
+ groupRole.setGroupId(group.getId());
+ roleMapper.deleteGroupRole(groupRole);
+ roleMapper.insertGroupRole(groupRole);
+ session.commit();
+ }
+ }
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+ }
+
+ public void grantDefaultRoles(Resource resource) {
+ if (resource.getId() != null) {
+ SqlSession session = myBatis.openSession();
+ try {
+ removeRoles(resource, session);
+ grantDefaultRoles(resource, UserRole.ADMIN, session);
+ grantDefaultRoles(resource, UserRole.USER, session);
+ grantDefaultRoles(resource, UserRole.CODEVIEWER, session);
+ session.commit();
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+ }
+
+ private void removeRoles(Resource resource, SqlSession session) {
+ Long resourceId = Long.valueOf(resource.getId());
+ RoleMapper mapper = session.getMapper(RoleMapper.class);
+ mapper.deleteGroupRolesByResourceId(resourceId);
+ mapper.deleteUserRolesByResourceId(resourceId);
+ }
+
+ private void grantDefaultRoles(Resource resource, String role, SqlSession session) {
+ UserMapper userMapper = session.getMapper(UserMapper.class);
+ RoleMapper roleMapper = session.getMapper(RoleMapper.class);
+
+ String[] groupNames = settings.getStringArrayBySeparator("sonar.role." + role + "." + resource.getQualifier() + ".defaultGroups", ",");
+ for (String groupName : groupNames) {
+ GroupRoleDto groupRole = new GroupRoleDto().setRole(role).setResourceId(Long.valueOf(resource.getId()));
+ if (DefaultGroups.isAnyone(groupName)) {
+ roleMapper.insertGroupRole(groupRole);
+ } else {
+ GroupDto group = userMapper.selectGroupByName(groupName);
+ if (group != null) {
+ roleMapper.insertGroupRole(groupRole.setGroupId(group.getId()));
+ }
+ }
+ }
+
+ String[] logins = settings.getStringArrayBySeparator("sonar.role." + role + "." + resource.getQualifier() + ".defaultUsers", ",");
+ for (String login : logins) {
+ UserDto user = userMapper.selectUserByLogin(login);
+ if (user != null) {
+ roleMapper.insertUserRole(new UserRoleDto().setRole(role).setUserId(user.getId()).setResourceId(Long.valueOf(resource.getId())));
+ }
+ }
+ }
+}
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.resources.Project;
-import org.sonar.api.security.ResourcePermissioning;
+import org.sonar.api.security.ResourcePermissions;
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.*;
public class ApplyProjectRolesDecoratorTest {
- private ResourcePermissioning resourcePermissioning;
+ private ResourcePermissions resourcePermissions;
private ApplyProjectRolesDecorator decorator;
@Before
public void init() {
- resourcePermissioning = mock(ResourcePermissioning.class);
- decorator = new ApplyProjectRolesDecorator(resourcePermissioning);
+ resourcePermissions = mock(ResourcePermissions.class);
+ decorator = new ApplyProjectRolesDecorator(resourcePermissions);
}
@Test
public void doNotGrantDefaultRolesWhenExistingPermissions() {
Project project = new Project("project");
project.setId(10);
- when(resourcePermissioning.hasRoles(project)).thenReturn(true);
+ when(resourcePermissions.hasRoles(project)).thenReturn(true);
decorator.decorate(project, null);
- verify(resourcePermissioning, never()).grantDefaultRoles(project);
+ verify(resourcePermissions, never()).grantDefaultRoles(project);
}
@Test
Project project = new Project("project");
Project module = new Project("module").setParent(project);
module.setId(10);
- when(resourcePermissioning.hasRoles(project)).thenReturn(false);
+ when(resourcePermissions.hasRoles(project)).thenReturn(false);
decorator.decorate(module, null);
- verify(resourcePermissioning, never()).grantDefaultRoles(module);
+ verify(resourcePermissions, never()).grantDefaultRoles(module);
}
@Test
public void grantDefaultRolesWhenNoPermissions() {
Project project = new Project("project");
project.setId(10);
- when(resourcePermissioning.hasRoles(project)).thenReturn(false);
+ when(resourcePermissions.hasRoles(project)).thenReturn(false);
decorator.decorate(project, null);
- verify(resourcePermissioning).grantDefaultRoles(project);
+ verify(resourcePermissions).grantDefaultRoles(project);
}
}
+++ /dev/null
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar 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.
- *
- * Sonar 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 Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.plugins.core.security;
-
-import org.junit.Test;
-import org.sonar.api.config.Settings;
-import org.sonar.api.resources.Project;
-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;
-
-public class DefaultResourcePermissioningTest extends AbstractDaoTestCase {
-
- private Resource project = new Project("project").setId(123);
-
- @Test
- public void grantGroupRole() {
- setupData("grantGroupRole");
-
- DefaultResourcePermissioning permissioning = new DefaultResourcePermissioning(new Settings(), getMyBatis());
- permissioning.grantGroupRole(project, "sonar-administrators", "admin");
-
- checkTables("grantGroupRole", "group_roles");
- }
-
- @Test
- public void grantGroupRole_anyone() {
- setupData("grantGroupRole_anyone");
-
- DefaultResourcePermissioning permissioning = new DefaultResourcePermissioning(new Settings(), getMyBatis());
- permissioning.grantGroupRole(project, DefaultGroups.ANYONE, "admin");
-
- checkTables("grantGroupRole_anyone", "group_roles");
- }
-
- @Test
- public void grantGroupRole_ignore_if_group_not_found() {
- setupData("grantGroupRole_ignore_if_group_not_found");
-
- DefaultResourcePermissioning permissioning = new DefaultResourcePermissioning(new Settings(), getMyBatis());
- permissioning.grantGroupRole(project, "not_found", "admin");
-
- checkTables("grantGroupRole_ignore_if_group_not_found", "group_roles");
- }
-
- @Test
- public void grantGroupRole_ignore_if_not_persisted() {
- setupData("grantGroupRole_ignore_if_not_persisted");
-
- DefaultResourcePermissioning permissioning = new DefaultResourcePermissioning(new Settings(), getMyBatis());
- Project resourceWithoutId = new Project("");
- permissioning.grantGroupRole(resourceWithoutId, "sonar-users", "admin");
-
- checkTables("grantGroupRole_ignore_if_not_persisted", "group_roles");
- }
-
- @Test
- public void grantDefaultRoles() {
- setupData("grantDefaultRoles");
-
- 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");
- }
-
- @Test
- public void grantDefaultRoles_unknown_group() {
- setupData("grantDefaultRoles_unknown_group");
-
- Settings settings = new Settings();
- settings.setProperty("sonar.role.admin.TRK.defaultGroups", "sonar-administrators,unknown");
- DefaultResourcePermissioning permissioning = new DefaultResourcePermissioning(settings, getMyBatis());
- permissioning.grantDefaultRoles(project);
-
- checkTables("grantDefaultRoles_unknown_group", "group_roles");
- }
-
- @Test
- public void grantDefaultRoles_users() {
- setupData("grantDefaultRoles_users");
-
- Settings settings = new Settings();
- settings.setProperty("sonar.role.admin.TRK.defaultUsers", "marius,disabled,notfound");
- DefaultResourcePermissioning permissioning = new DefaultResourcePermissioning(settings, getMyBatis());
- permissioning.grantDefaultRoles(project);
-
- checkTables("grantDefaultRoles_users", "user_roles");
- }
-
- @Test
- public void hasRoles() {
- setupData("hasRoles");
- DefaultResourcePermissioning permissioning = new DefaultResourcePermissioning(new Settings(), getMyBatis());
-
- // no groups and at least one user
- assertThat(permissioning.hasRoles(new Project("only_users").setId(1))).isTrue();
-
- // no users and at least one group
- assertThat(permissioning.hasRoles(new Project("only_groups").setId(2))).isTrue();
-
- // groups and users
- assertThat(permissioning.hasRoles(new Project("groups_and_users").setId(3))).isTrue();
-
- // no groups, no users
- assertThat(permissioning.hasRoles(new Project("no_groups_no_users").setId(4))).isFalse();
-
- // does not exist
- assertThat(permissioning.hasRoles(new Project("not_found"))).isFalse();
- }
-}
\ No newline at end of file
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.core.security;
+
+import org.junit.Test;
+import org.sonar.api.config.Settings;
+import org.sonar.api.resources.Project;
+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;
+
+public class DefaultResourcePermissionsTest extends AbstractDaoTestCase {
+
+ private Resource project = new Project("project").setId(123);
+
+ @Test
+ public void grantGroupRole() {
+ setupData("grantGroupRole");
+
+ DefaultResourcePermissions permissions = new DefaultResourcePermissions(new Settings(), getMyBatis());
+ permissions.grantGroupRole(project, "sonar-administrators", "admin");
+
+ // do not insert duplicated rows
+ permissions.grantGroupRole(project, "sonar-administrators", "admin");
+
+ checkColumns("grantGroupRole", "group_roles", "group_id", "resource_id", "role");
+ }
+
+ @Test
+ public void grantGroupRole_anyone() {
+ setupData("grantGroupRole_anyone");
+
+ DefaultResourcePermissions permissions = new DefaultResourcePermissions(new Settings(), getMyBatis());
+ permissions.grantGroupRole(project, DefaultGroups.ANYONE, "admin");
+
+ checkTables("grantGroupRole_anyone", "group_roles");
+ }
+
+ @Test
+ public void grantGroupRole_ignore_if_group_not_found() {
+ setupData("grantGroupRole_ignore_if_group_not_found");
+
+ DefaultResourcePermissions permissions = new DefaultResourcePermissions(new Settings(), getMyBatis());
+ permissions.grantGroupRole(project, "not_found", "admin");
+
+ checkTables("grantGroupRole_ignore_if_group_not_found", "group_roles");
+ }
+
+ @Test
+ public void grantGroupRole_ignore_if_not_persisted() {
+ setupData("grantGroupRole_ignore_if_not_persisted");
+
+ DefaultResourcePermissions permissions = new DefaultResourcePermissions(new Settings(), getMyBatis());
+ Project resourceWithoutId = new Project("");
+ permissions.grantGroupRole(resourceWithoutId, "sonar-users", "admin");
+
+ checkTables("grantGroupRole_ignore_if_not_persisted", "group_roles");
+ }
+
+ @Test
+ public void grantUserRole() {
+ setupData("grantUserRole");
+
+ DefaultResourcePermissions permissions = new DefaultResourcePermissions(new Settings(), getMyBatis());
+ permissions.grantUserRole(project, "marius", "admin");
+
+ // do not insert duplicated rows
+ permissions.grantUserRole(project, "marius", "admin");
+
+ checkColumns("grantUserRole", "user_roles", "user_id", "resource_id", "role");
+ }
+
+ @Test
+ public void grantDefaultRoles() {
+ setupData("grantDefaultRoles");
+
+ 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", "");
+ DefaultResourcePermissions permissions = new DefaultResourcePermissions(settings, getMyBatis());
+
+ permissions.grantDefaultRoles(project);
+
+ checkTables("grantDefaultRoles", "user_roles", "group_roles");
+ }
+
+ @Test
+ public void grantDefaultRoles_unknown_group() {
+ setupData("grantDefaultRoles_unknown_group");
+
+ Settings settings = new Settings();
+ settings.setProperty("sonar.role.admin.TRK.defaultGroups", "sonar-administrators,unknown");
+ DefaultResourcePermissions permissions = new DefaultResourcePermissions(settings, getMyBatis());
+ permissions.grantDefaultRoles(project);
+
+ checkTables("grantDefaultRoles_unknown_group", "group_roles");
+ }
+
+ @Test
+ public void grantDefaultRoles_users() {
+ setupData("grantDefaultRoles_users");
+
+ Settings settings = new Settings();
+ settings.setProperty("sonar.role.admin.TRK.defaultUsers", "marius,disabled,notfound");
+ DefaultResourcePermissions permissions = new DefaultResourcePermissions(settings, getMyBatis());
+ permissions.grantDefaultRoles(project);
+
+ checkTables("grantDefaultRoles_users", "user_roles");
+ }
+
+ @Test
+ public void hasRoles() {
+ setupData("hasRoles");
+ DefaultResourcePermissions permissions = new DefaultResourcePermissions(new Settings(), getMyBatis());
+
+ // no groups and at least one user
+ assertThat(permissions.hasRoles(new Project("only_users").setId(1))).isTrue();
+
+ // no users and at least one group
+ assertThat(permissions.hasRoles(new Project("only_groups").setId(2))).isTrue();
+
+ // groups and users
+ assertThat(permissions.hasRoles(new Project("groups_and_users").setId(3))).isTrue();
+
+ // no groups, no users
+ assertThat(permissions.hasRoles(new Project("no_groups_no_users").setId(4))).isFalse();
+
+ // does not exist
+ assertThat(permissions.hasRoles(new Project("not_found"))).isFalse();
+ }
+}
\ No newline at end of file
+++ /dev/null
-<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 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="[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
+++ /dev/null
-<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"/>
-</dataset>
\ No newline at end of file
+++ /dev/null
-<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 rows : sonar-administrators (admin)
- -->
- <group_roles id="3" group_id="100" resource_id="123" role="admin"/>
-
-</dataset>
\ No newline at end of file
+++ /dev/null
-<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"/>
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <groups id="100" name="sonar-administrators"/>
- <groups id="101" name="sonar-users"/>
- <users id="200" login="marius" name="Marius" email="[null]" active="[true]"/>
- <users id="201" login="disabled" name="Disabled" email="[null]" active="[false]"/>
-
- <!-- 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 row : marius (admin)
- -->
- <user_roles id="2" user_id="200" resource_id="123" role="admin"/>
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <groups id="100" name="sonar-administrators" />
- <groups id="101" name="sonar-users" />
- <users id="200" login="marius" name="Marius" email="[null]" active="[true]" />
- <users id="201" login="disabled" name="Disabled" email="[null]" active="[false]" />
-
- <!-- 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"/>
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <groups id="100" name="sonar-administrators"/>
- <groups id="101" name="sonar-users"/>
-
- <group_roles id="1" group_id="100" resource_id="123" role="admin"/>
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <groups id="100" name="sonar-administrators" />
- <groups id="101" name="sonar-users" />
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <groups id="100" name="sonar-administrators" />
- <groups id="101" name="sonar-users" />
-
- <group_roles id="1" group_id="[null]" resource_id="123" role="admin"/>
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <groups id="100" name="sonar-administrators" />
- <groups id="101" name="sonar-users" />
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <groups id="100" name="sonar-administrators" />
- <groups id="101" name="sonar-users" />
-
- <!-- already existed -->
- <group_roles id="1" group_id="[null]" resource_id="123" role="admin"/>
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <groups id="100" name="sonar-administrators" />
- <groups id="101" name="sonar-users" />
-
- <!-- already existed -->
- <group_roles id="1" group_id="[null]" resource_id="123" role="admin"/>
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <groups id="100" name="sonar-administrators" />
- <groups id="101" name="sonar-users" />
-
- <!-- already existed -->
- <group_roles id="1" group_id="[null]" resource_id="123" role="admin"/>
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <groups id="100" name="sonar-administrators" />
- <groups id="101" name="sonar-users" />
-
- <!-- already existed -->
- <group_roles id="1" group_id="[null]" resource_id="123" role="admin"/>
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <groups id="100" name="sonar-administrators"/>
- <groups id="101" name="sonar-users"/>
- <users id="200" login="marius" name="Marius" email="[null]" active="[true]"/>
-
- <!-- only_users -->
- <user_roles id="1" user_id="200" resource_id="1" role="admin"/>
-
- <!-- only_groups -->
- <group_roles id="1" group_id="100" resource_id="2" role="admin"/>
-
- <!-- groups_and_users -->
- <group_roles id="2" group_id="101" resource_id="3" role="user"/>
- <user_roles id="2" user_id="200" resource_id="3" role="admin"/>
-
-</dataset>
\ No newline at end of file
--- /dev/null
+<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 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="[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
--- /dev/null
+<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"/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<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 rows : sonar-administrators (admin)
+ -->
+ <group_roles id="3" group_id="100" resource_id="123" role="admin"/>
+
+</dataset>
\ No newline at end of file
--- /dev/null
+<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"/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <groups id="100" name="sonar-administrators"/>
+ <groups id="101" name="sonar-users"/>
+ <users id="200" login="marius" name="Marius" email="[null]" active="[true]"/>
+ <users id="201" login="disabled" name="Disabled" email="[null]" active="[false]"/>
+
+ <!-- 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 row : marius (admin)
+ -->
+ <user_roles id="2" user_id="200" resource_id="123" role="admin"/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <groups id="100" name="sonar-administrators" />
+ <groups id="101" name="sonar-users" />
+ <users id="200" login="marius" name="Marius" email="[null]" active="[true]" />
+ <users id="201" login="disabled" name="Disabled" email="[null]" active="[false]" />
+
+ <!-- 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"/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <groups id="100" name="sonar-administrators"/>
+ <groups id="101" name="sonar-users"/>
+
+ <group_roles group_id="100" resource_id="123" role="admin"/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <groups id="100" name="sonar-administrators" />
+ <groups id="101" name="sonar-users" />
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <groups id="100" name="sonar-administrators" />
+ <groups id="101" name="sonar-users" />
+
+ <group_roles id="1" group_id="[null]" resource_id="123" role="admin"/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <groups id="100" name="sonar-administrators" />
+ <groups id="101" name="sonar-users" />
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <groups id="100" name="sonar-administrators" />
+ <groups id="101" name="sonar-users" />
+
+ <!-- already existed -->
+ <group_roles id="1" group_id="[null]" resource_id="123" role="admin"/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <groups id="100" name="sonar-administrators" />
+ <groups id="101" name="sonar-users" />
+
+ <!-- already existed -->
+ <group_roles id="1" group_id="[null]" resource_id="123" role="admin"/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <groups id="100" name="sonar-administrators" />
+ <groups id="101" name="sonar-users" />
+
+ <!-- already existed -->
+ <group_roles id="1" group_id="[null]" resource_id="123" role="admin"/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <groups id="100" name="sonar-administrators" />
+ <groups id="101" name="sonar-users" />
+
+ <!-- already existed -->
+ <group_roles id="1" group_id="[null]" resource_id="123" role="admin"/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <users id="200" login="marius" name="Marius" email="[null]" active="[true]"/>
+
+ <user_roles user_id="200" resource_id="123" role="admin"/>
+
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <users id="200" login="marius" name="Marius" email="[null]" active="[true]" />
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <groups id="100" name="sonar-administrators"/>
+ <groups id="101" name="sonar-users"/>
+ <users id="200" login="marius" name="Marius" email="[null]" active="[true]"/>
+
+ <!-- only_users -->
+ <user_roles id="1" user_id="200" resource_id="1" role="admin"/>
+
+ <!-- only_groups -->
+ <group_roles id="1" group_id="100" resource_id="2" role="admin"/>
+
+ <!-- groups_and_users -->
+ <group_roles id="2" group_id="101" resource_id="3" role="user"/>
+ <user_roles id="2" user_id="200" resource_id="3" role="admin"/>
+
+</dataset>
\ No newline at end of file
void insertUserRole(UserRoleDto userRole);
+ void deleteUserRole(UserRoleDto userRole);
+
+ void deleteGroupRole(GroupRoleDto groupRole);
+
void deleteGroupRolesByResourceId(Long resourceId);
void deleteUserRolesByResourceId(Long resourceId);
VALUES (#{id}, #{userId}, #{resourceId}, #{role})
</insert>
+ <delete id="deleteGroupRole" parameterType="map">
+ delete from group_roles where resource_id=#{resourceId} and role=#{role}
+ <choose>
+ <when test="groupId != null">
+ and group_id=#{groupId}
+ </when>
+ <otherwise>
+ and group_id is null
+ </otherwise>
+ </choose>
+ </delete>
+
+ <delete id="deleteUserRole" parameterType="map">
+ delete from user_roles where resource_id=#{resourceId} and user_id=#{userId} and role=#{role}
+ </delete>
+
<delete id="deleteGroupRolesByResourceId" parameterType="long">
delete from group_roles where resource_id=#{id}
</delete>
+++ /dev/null
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2012 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar 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.
- *
- * Sonar 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 Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
-package org.sonar.api.security;
-
-import org.sonar.api.BatchComponent;
-import org.sonar.api.resources.Resource;
-
-/**
- * Grant access to newly created projects.
- *
- * <p>This component is not supposed to be called by standard plugins.</p>
- *
- * @since 3.2
- */
-public interface ResourcePermissioning extends BatchComponent {
-
- boolean hasRoles(Resource resource);
-
- void grantDefaultRoles(Resource resource);
-
- void grantUserRole(Resource resource, String login, String role);
-
- void grantGroupRole(Resource resource, String groupName, String role);
-}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.api.security;
+
+import org.sonar.api.BatchComponent;
+import org.sonar.api.resources.Resource;
+
+/**
+ * Grant access to newly created projects.
+ *
+ * <p>This component is not supposed to be called by standard plugins.</p>
+ *
+ * @since 3.2
+ */
+public interface ResourcePermissions extends BatchComponent {
+
+ boolean hasRoles(Resource resource);
+
+ void grantDefaultRoles(Resource resource);
+
+ void grantUserRole(Resource resource, String login, String role);
+
+ void grantGroupRole(Resource resource, String groupName, String role);
+}