diff options
author | Jean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com> | 2013-06-26 16:36:11 +0200 |
---|---|---|
committer | Jean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com> | 2013-06-26 16:36:11 +0200 |
commit | 64c168a91d69a0fcfb69cafba29d3f718437c764 (patch) | |
tree | 07c6e7d7efcce0f7c8097b29ccc7b19e03d2d052 /sonar-core/src | |
parent | 31fba86ddcf83f57c5040cef8843341b79a02253 (diff) | |
download | sonarqube-64c168a91d69a0fcfb69cafba29d3f718437c764.tar.gz sonarqube-64c168a91d69a0fcfb69cafba29d3f718437c764.zip |
SONAR-4412 Extracted Role DAO to make it available to the upcoming ruby role service
Diffstat (limited to 'sonar-core/src')
5 files changed, 169 insertions, 50 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/resource/DefaultResourcePermissions.java b/sonar-core/src/main/java/org/sonar/core/resource/DefaultResourcePermissions.java index e90547aeddc..7a7e17b14e3 100644 --- a/sonar-core/src/main/java/org/sonar/core/resource/DefaultResourcePermissions.java +++ b/sonar-core/src/main/java/org/sonar/core/resource/DefaultResourcePermissions.java @@ -28,12 +28,7 @@ import org.sonar.api.security.ResourcePermissions; import org.sonar.api.task.TaskExtension; import org.sonar.api.web.UserRole; import org.sonar.core.persistence.MyBatis; -import org.sonar.core.user.GroupDto; -import org.sonar.core.user.GroupRoleDto; -import org.sonar.core.user.RoleMapper; -import org.sonar.core.user.UserDto; -import org.sonar.core.user.UserMapper; -import org.sonar.core.user.UserRoleDto; +import org.sonar.core.user.*; /** * @since 3.2 @@ -42,23 +37,20 @@ public class DefaultResourcePermissions implements ResourcePermissions, TaskExte private final Settings settings; private final MyBatis myBatis; + private final RoleDao roleDao; + private final UserDao userDao; - public DefaultResourcePermissions(Settings settings, MyBatis myBatis) { + public DefaultResourcePermissions(Settings settings, MyBatis myBatis, RoleDao roleDao, UserDao userDao) { this.settings = settings; this.myBatis = myBatis; + this.roleDao = roleDao; + this.userDao = userDao; } 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); - } + Long resourceId = Long.valueOf(resource.getId()); + return roleDao.countGroupRoles(resourceId) + roleDao.countUserRoles(resourceId) > 0; } return false; } @@ -73,9 +65,8 @@ public class DefaultResourcePermissions implements ResourcePermissions, TaskExte .setRole(role) .setUserId(user.getId()) .setResourceId(Long.valueOf(resource.getId())); - RoleMapper roleMapper = session.getMapper(RoleMapper.class); - roleMapper.deleteUserRole(userRole); - roleMapper.insertUserRole(userRole); + roleDao.deleteUserRole(userRole, session); + roleDao.insertUserRole(userRole, session); session.commit(); } } finally { @@ -91,17 +82,16 @@ public class DefaultResourcePermissions implements ResourcePermissions, TaskExte 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); + roleDao.deleteGroupRole(groupRole, session); + roleDao.insertGroupRole(groupRole, session); session.commit(); } else { - GroupDto group = session.getMapper(UserMapper.class).selectGroupByName(groupName); + GroupDto group = userDao.selectGroupByName(groupName, session); if (group != null) { groupRole.setGroupId(group.getId()); - roleMapper.deleteGroupRole(groupRole); - roleMapper.insertGroupRole(groupRole); + roleDao.deleteGroupRole(groupRole, session); + roleDao.insertGroupRole(groupRole, session); session.commit(); } } @@ -128,33 +118,30 @@ public class DefaultResourcePermissions implements ResourcePermissions, TaskExte 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); + roleDao.deleteGroupRolesByResourceId(resourceId, session); + roleDao.deleteUserRolesByResourceId(resourceId, session); } 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); + roleDao.insertGroupRole(groupRole, session); } else { - GroupDto group = userMapper.selectGroupByName(groupName); + GroupDto group = userDao.selectGroupByName(groupName, session); if (group != null) { - roleMapper.insertGroupRole(groupRole.setGroupId(group.getId())); + roleDao.insertGroupRole(groupRole.setGroupId(group.getId()), session); } } } String[] logins = settings.getStringArrayBySeparator("sonar.role." + role + "." + resource.getQualifier() + ".defaultUsers", ","); for (String login : logins) { - UserDto user = userMapper.selectUserByLogin(login); + UserDto user = userDao.selectActiveUserByLogin(login, session); if (user != null) { - roleMapper.insertUserRole(new UserRoleDto().setRole(role).setUserId(user.getId()).setResourceId(Long.valueOf(resource.getId()))); + UserRoleDto userRoleDto = new UserRoleDto().setRole(role).setUserId(user.getId()).setResourceId(Long.valueOf(resource.getId())); + roleDao.insertUserRole(userRoleDto, session); } } } diff --git a/sonar-core/src/main/java/org/sonar/core/user/RoleDao.java b/sonar-core/src/main/java/org/sonar/core/user/RoleDao.java new file mode 100644 index 00000000000..6a3a08eba8f --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/user/RoleDao.java @@ -0,0 +1,85 @@ +/* + * SonarQube, open source software quality management 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. + */ + +package org.sonar.core.user; + +import org.apache.ibatis.session.SqlSession; +import org.sonar.api.ServerExtension; +import org.sonar.api.task.TaskExtension; +import org.sonar.core.persistence.MyBatis; + +public class RoleDao implements TaskExtension, ServerExtension { + + private final MyBatis mybatis; + + public RoleDao(MyBatis mybatis) { + this.mybatis = mybatis; + } + + public void insertGroupRole(GroupRoleDto groupRole, SqlSession session) { + RoleMapper mapper = session.getMapper(RoleMapper.class); + mapper.insertGroupRole(groupRole); + } + + public void insertUserRole(UserRoleDto userRole, SqlSession session) { + RoleMapper mapper = session.getMapper(RoleMapper.class); + mapper.insertUserRole(userRole); + } + + public void deleteUserRole(UserRoleDto userRole, SqlSession session) { + RoleMapper mapper = session.getMapper(RoleMapper.class); + mapper.deleteUserRole(userRole); + } + + public void deleteGroupRole(GroupRoleDto groupRole, SqlSession session) { + RoleMapper mapper = session.getMapper(RoleMapper.class); + mapper.deleteGroupRole(groupRole); + } + + public void deleteGroupRolesByResourceId(Long resourceId, SqlSession session) { + RoleMapper mapper = session.getMapper(RoleMapper.class); + mapper.deleteGroupRolesByResourceId(resourceId); + } + + public void deleteUserRolesByResourceId(Long resourceId, SqlSession session) { + RoleMapper mapper = session.getMapper(RoleMapper.class); + mapper.deleteUserRolesByResourceId(resourceId); + } + + public int countGroupRoles(Long resourceId) { + SqlSession session = mybatis.openSession(); + try { + RoleMapper mapper = session.getMapper(RoleMapper.class); + return mapper.countGroupRoles(resourceId); + } finally { + MyBatis.closeQuietly(session); + } + } + + public int countUserRoles(Long resourceId) { + SqlSession session = mybatis.openSession(); + try { + RoleMapper mapper = session.getMapper(RoleMapper.class); + return mapper.countUserRoles(resourceId); + } finally { + MyBatis.closeQuietly(session); + } + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/user/UserDao.java b/sonar-core/src/main/java/org/sonar/core/user/UserDao.java index 6a2ce6a499b..c5d482ff139 100644 --- a/sonar-core/src/main/java/org/sonar/core/user/UserDao.java +++ b/sonar-core/src/main/java/org/sonar/core/user/UserDao.java @@ -47,13 +47,17 @@ public class UserDao { public UserDto selectActiveUserByLogin(String login) { SqlSession session = mybatis.openSession(); try { - UserMapper mapper = session.getMapper(UserMapper.class); - return mapper.selectUserByLogin(login); + return selectActiveUserByLogin(login, session); } finally { MyBatis.closeQuietly(session); } } + public UserDto selectActiveUserByLogin(String login, SqlSession session) { + UserMapper mapper = session.getMapper(UserMapper.class); + return mapper.selectUserByLogin(login); + } + public List<UserDto> selectUsersByLogins(List<String> logins) { List<UserDto> users = Lists.newArrayList(); if (!logins.isEmpty()) { @@ -118,11 +122,16 @@ public class UserDao { * @return the group, null if group not found */ + public GroupDto selectGroupByName(String name, SqlSession session) { + UserMapper mapper = session.getMapper(UserMapper.class); + return mapper.selectGroupByName(name); + } + + public GroupDto selectGroupByName(String name) { SqlSession session = mybatis.openSession(); try { - UserMapper mapper = session.getMapper(UserMapper.class); - return mapper.selectGroupByName(name); + return selectGroupByName(name, session); } finally { MyBatis.closeQuietly(session); } diff --git a/sonar-core/src/test/java/org/sonar/core/resource/DefaultResourcePermissionsTest.java b/sonar-core/src/test/java/org/sonar/core/resource/DefaultResourcePermissionsTest.java index 7f8182234df..df51c2356b9 100644 --- a/sonar-core/src/test/java/org/sonar/core/resource/DefaultResourcePermissionsTest.java +++ b/sonar-core/src/test/java/org/sonar/core/resource/DefaultResourcePermissionsTest.java @@ -25,7 +25,8 @@ 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 org.sonar.core.resource.DefaultResourcePermissions; +import org.sonar.core.user.RoleDao; +import org.sonar.core.user.UserDao; import static org.fest.assertions.Assertions.assertThat; @@ -37,7 +38,8 @@ public class DefaultResourcePermissionsTest extends AbstractDaoTestCase { public void grantGroupRole() { setupData("grantGroupRole"); - DefaultResourcePermissions permissions = new DefaultResourcePermissions(new Settings(), getMyBatis()); + DefaultResourcePermissions permissions = new DefaultResourcePermissions(new Settings(), getMyBatis(), + new RoleDao(getMyBatis()), new UserDao(getMyBatis())); permissions.grantGroupRole(project, "sonar-administrators", "admin"); // do not insert duplicated rows @@ -50,7 +52,8 @@ public class DefaultResourcePermissionsTest extends AbstractDaoTestCase { public void grantGroupRole_anyone() { setupData("grantGroupRole_anyone"); - DefaultResourcePermissions permissions = new DefaultResourcePermissions(new Settings(), getMyBatis()); + DefaultResourcePermissions permissions = new DefaultResourcePermissions(new Settings(), getMyBatis(), + new RoleDao(getMyBatis()), new UserDao(getMyBatis())); permissions.grantGroupRole(project, DefaultGroups.ANYONE, "admin"); checkTables("grantGroupRole_anyone", "group_roles"); @@ -60,7 +63,8 @@ public class DefaultResourcePermissionsTest extends AbstractDaoTestCase { public void grantGroupRole_ignore_if_group_not_found() { setupData("grantGroupRole_ignore_if_group_not_found"); - DefaultResourcePermissions permissions = new DefaultResourcePermissions(new Settings(), getMyBatis()); + DefaultResourcePermissions permissions = new DefaultResourcePermissions(new Settings(), getMyBatis(), + new RoleDao(getMyBatis()), new UserDao(getMyBatis())); permissions.grantGroupRole(project, "not_found", "admin"); checkTables("grantGroupRole_ignore_if_group_not_found", "group_roles"); @@ -70,7 +74,8 @@ public class DefaultResourcePermissionsTest extends AbstractDaoTestCase { public void grantGroupRole_ignore_if_not_persisted() { setupData("grantGroupRole_ignore_if_not_persisted"); - DefaultResourcePermissions permissions = new DefaultResourcePermissions(new Settings(), getMyBatis()); + DefaultResourcePermissions permissions = new DefaultResourcePermissions(new Settings(), getMyBatis(), + new RoleDao(getMyBatis()), new UserDao(getMyBatis())); Project resourceWithoutId = new Project(""); permissions.grantGroupRole(resourceWithoutId, "sonar-users", "admin"); @@ -81,7 +86,8 @@ public class DefaultResourcePermissionsTest extends AbstractDaoTestCase { public void grantUserRole() { setupData("grantUserRole"); - DefaultResourcePermissions permissions = new DefaultResourcePermissions(new Settings(), getMyBatis()); + DefaultResourcePermissions permissions = new DefaultResourcePermissions(new Settings(), getMyBatis(), + new RoleDao(getMyBatis()), new UserDao(getMyBatis())); permissions.grantUserRole(project, "marius", "admin"); // do not insert duplicated rows @@ -101,7 +107,8 @@ public class DefaultResourcePermissionsTest extends AbstractDaoTestCase { 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()); + DefaultResourcePermissions permissions = new DefaultResourcePermissions(settings, getMyBatis(), + new RoleDao(getMyBatis()), new UserDao(getMyBatis())); permissions.grantDefaultRoles(project); @@ -114,7 +121,8 @@ public class DefaultResourcePermissionsTest extends AbstractDaoTestCase { Settings settings = new Settings(); settings.setProperty("sonar.role.admin.TRK.defaultGroups", "sonar-administrators,unknown"); - DefaultResourcePermissions permissions = new DefaultResourcePermissions(settings, getMyBatis()); + DefaultResourcePermissions permissions = new DefaultResourcePermissions(settings, getMyBatis(), + new RoleDao(getMyBatis()), new UserDao(getMyBatis())); permissions.grantDefaultRoles(project); checkTables("grantDefaultRoles_unknown_group", "group_roles"); @@ -126,7 +134,8 @@ public class DefaultResourcePermissionsTest extends AbstractDaoTestCase { Settings settings = new Settings(); settings.setProperty("sonar.role.admin.TRK.defaultUsers", "marius,disabled,notfound"); - DefaultResourcePermissions permissions = new DefaultResourcePermissions(settings, getMyBatis()); + DefaultResourcePermissions permissions = new DefaultResourcePermissions(settings, getMyBatis(), + new RoleDao(getMyBatis()), new UserDao(getMyBatis())); permissions.grantDefaultRoles(project); checkTables("grantDefaultRoles_users", "user_roles"); @@ -135,7 +144,8 @@ public class DefaultResourcePermissionsTest extends AbstractDaoTestCase { @Test public void hasRoles() { setupData("hasRoles"); - DefaultResourcePermissions permissions = new DefaultResourcePermissions(new Settings(), getMyBatis()); + DefaultResourcePermissions permissions = new DefaultResourcePermissions(new Settings(), getMyBatis(), + new RoleDao(getMyBatis()), new UserDao(getMyBatis())); // no groups and at least one user assertThat(permissions.hasRoles(new Project("only_users").setId(1))).isTrue(); diff --git a/sonar-core/src/test/java/org/sonar/core/user/RoleDaoTest.java b/sonar-core/src/test/java/org/sonar/core/user/RoleDaoTest.java new file mode 100644 index 00000000000..1f803346a3d --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/user/RoleDaoTest.java @@ -0,0 +1,28 @@ +/* + * SonarQube, open source software quality management 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. + */ + +package org.sonar.core.user; + +import org.sonar.core.persistence.AbstractDaoTestCase; + +public class RoleDaoTest extends AbstractDaoTestCase { + + +} |