From 741fd9a540edee477fe7354417cb295404ff0865 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 22 Feb 2017 13:57:05 +0100 Subject: Add OrganizationPermission#all() --- .../db/permission/OrganizationPermission.java | 7 ++++ .../db/permission/OrganizationPermissionTest.java | 42 ++++++++++++++++++++++ .../organization/OrganizationCreationImpl.java | 16 +++++---- .../ws/SearchGlobalPermissionsAction.java | 27 +++++++------- .../org/sonar/server/user/ws/CurrentAction.java | 5 ++- .../ws/SearchMyOrganizationsActionTest.java | 11 +++--- 6 files changed, 79 insertions(+), 29 deletions(-) create mode 100644 server/sonar-db-dao/src/test/java/org/sonar/db/permission/OrganizationPermissionTest.java (limited to 'server') diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/OrganizationPermission.java b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/OrganizationPermission.java index b28a18ee2e3..738510f9728 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/permission/OrganizationPermission.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/permission/OrganizationPermission.java @@ -19,6 +19,9 @@ */ package org.sonar.db.permission; +import java.util.Arrays; +import java.util.stream.Stream; + public enum OrganizationPermission { ADMINISTER("admin"), @@ -50,4 +53,8 @@ public enum OrganizationPermission { } throw new IllegalArgumentException("Unsupported permission: " + key); } + + public static Stream all() { + return Arrays.stream(values()); + } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/permission/OrganizationPermissionTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/OrganizationPermissionTest.java new file mode 100644 index 00000000000..fafc5a1c950 --- /dev/null +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/permission/OrganizationPermissionTest.java @@ -0,0 +1,42 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.db.permission; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class OrganizationPermissionTest { + + @Test + public void fromKey_returns_enum_with_specified_key() { + for (OrganizationPermission p : OrganizationPermission.values()) { + assertThat(OrganizationPermission.fromKey(p.getKey())).isEqualTo(p); + } + } + + @Test + public void all_returns_stream_of_values() { + assertThat(OrganizationPermission.all()).hasSize(OrganizationPermission.values().length); + for (OrganizationPermission permission : OrganizationPermission.values()) { + assertThat(OrganizationPermission.all()).contains(permission); + } + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationCreationImpl.java b/server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationCreationImpl.java index 81de1e7cc40..64c5a850115 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationCreationImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/organization/OrganizationCreationImpl.java @@ -34,6 +34,7 @@ import org.sonar.db.DbSession; import org.sonar.db.organization.DefaultTemplates; import org.sonar.db.organization.OrganizationDto; import org.sonar.db.permission.GroupPermissionDto; +import org.sonar.db.permission.OrganizationPermission; import org.sonar.db.permission.UserPermissionDto; import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto; import org.sonar.db.permission.template.PermissionTemplateDto; @@ -101,7 +102,8 @@ public class OrganizationCreationImpl implements OrganizationCreation { OrganizationDto organization = insertOrganization(dbSession, newOrganization, dto -> dto.setGuarded(true).setUserId(newUser.getId())); - GlobalPermissions.ALL.forEach(permission -> insertUserPermissions(dbSession, newUser, organization, permission)); + OrganizationPermission.all() + .forEach(p -> insertUserPermissions(dbSession, newUser, organization, p)); insertPersonalOrgDefaultTemplate(dbSession, organization); dbSession.commit(); @@ -193,7 +195,7 @@ public class OrganizationCreationImpl implements OrganizationCreation { insertProjectCreatorPermission(dbSession, permissionTemplateDto, UserRole.ADMIN, now); insertProjectCreatorPermission(dbSession, permissionTemplateDto, UserRole.ISSUE_ADMIN, now); - insertProjectCreatorPermission(dbSession, permissionTemplateDto, GlobalPermissions.SCAN_EXECUTION, now); + insertProjectCreatorPermission(dbSession, permissionTemplateDto, OrganizationPermission.SCAN.getKey(), now); insertGroupPermission(dbSession, permissionTemplateDto, UserRole.USER, null); insertGroupPermission(dbSession, permissionTemplateDto, UserRole.CODEVIEWER, null); @@ -226,23 +228,23 @@ public class OrganizationCreationImpl implements OrganizationCreation { .setOrganizationUuid(organization.getUuid()) .setName(OWNERS_GROUP_NAME) .setDescription(format(OWNERS_GROUP_DESCRIPTION_PATTERN, organization.getName()))); - GlobalPermissions.ALL.forEach(permission -> addPermissionToGroup(dbSession, group, permission)); + OrganizationPermission.all().forEach(p -> addPermissionToGroup(dbSession, group, p)); return group; } - private void addPermissionToGroup(DbSession dbSession, GroupDto group, String permission) { + private void addPermissionToGroup(DbSession dbSession, GroupDto group, OrganizationPermission permission) { dbClient.groupPermissionDao().insert( dbSession, new GroupPermissionDto() .setOrganizationUuid(group.getOrganizationUuid()) .setGroupId(group.getId()) - .setRole(permission)); + .setRole(permission.getKey())); } - private void insertUserPermissions(DbSession dbSession, UserDto userDto, OrganizationDto organization, String permission) { + private void insertUserPermissions(DbSession dbSession, UserDto userDto, OrganizationDto organization, OrganizationPermission permission) { dbClient.userPermissionDao().insert( dbSession, - new UserPermissionDto(organization.getUuid(), permission, userDto.getId(), null)); + new UserPermissionDto(organization.getUuid(), permission.getKey(), userDto.getId(), null)); } private void addCurrentUserToGroup(DbSession dbSession, GroupDto group, int createUserId) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchGlobalPermissionsAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchGlobalPermissionsAction.java index aad2f968554..00abbf8fb23 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchGlobalPermissionsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/SearchGlobalPermissionsAction.java @@ -24,10 +24,10 @@ import org.sonar.api.i18n.I18n; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; -import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.organization.OrganizationDto; +import org.sonar.db.permission.OrganizationPermission; import org.sonar.db.permission.PermissionQuery; import org.sonar.server.user.UserSession; import org.sonarqube.ws.WsPermissions.Permission; @@ -84,18 +84,19 @@ public class SearchGlobalPermissionsAction implements PermissionsWsAction { WsSearchGlobalPermissionsResponse.Builder response = WsSearchGlobalPermissionsResponse.newBuilder(); Permission.Builder permission = newBuilder(); - for (String permissionKey : GlobalPermissions.ALL) { - PermissionQuery permissionQuery = permissionQuery(permissionKey); - - response.addPermissions( - permission - .clear() - .setKey(permissionKey) - .setName(i18nName(permissionKey)) - .setDescription(i18nDescriptionMessage(permissionKey)) - .setUsersCount(countUsers(dbSession, org, permissionQuery)) - .setGroupsCount(countGroups(dbSession, org, permissionKey))); - } + OrganizationPermission.all() + .map(OrganizationPermission::getKey) + .forEach(permissionKey -> { + PermissionQuery query = permissionQuery(permissionKey); + response.addPermissions( + permission + .clear() + .setKey(permissionKey) + .setName(i18nName(permissionKey)) + .setDescription(i18nDescriptionMessage(permissionKey)) + .setUsersCount(countUsers(dbSession, org, query)) + .setGroupsCount(countGroups(dbSession, org, permissionKey))); + }); return response.build(); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentAction.java b/server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentAction.java index b05fa1f423b..b8744cbb460 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentAction.java @@ -19,7 +19,6 @@ */ package org.sonar.server.user.ws; -import java.util.Arrays; import java.util.Collection; import java.util.Optional; import org.sonar.api.server.ws.Request; @@ -28,9 +27,9 @@ import org.sonar.api.server.ws.WebService.NewController; import org.sonar.api.utils.text.JsonWriter; import org.sonar.db.DbClient; import org.sonar.db.DbSession; +import org.sonar.db.permission.OrganizationPermission; import org.sonar.db.user.UserDto; import org.sonar.server.organization.DefaultOrganizationProvider; -import org.sonar.db.permission.OrganizationPermission; import org.sonar.server.user.UserSession; import static com.google.common.base.Strings.isNullOrEmpty; @@ -141,7 +140,7 @@ public class CurrentAction implements UsersWsAction { json.name("global").beginArray(); String defaultOrganizationUuid = defaultOrganizationProvider.get().getUuid(); - Arrays.stream(OrganizationPermission.values()) + OrganizationPermission.all() .filter(permission -> userSession.hasPermission(permission, defaultOrganizationUuid)) .forEach(permission -> json.value(permission.getKey())); diff --git a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/SearchMyOrganizationsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/SearchMyOrganizationsActionTest.java index 90a04317e14..213e9cf55ae 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/organization/ws/SearchMyOrganizationsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/organization/ws/SearchMyOrganizationsActionTest.java @@ -19,7 +19,6 @@ */ package org.sonar.server.organization.ws; -import java.util.Arrays; import org.junit.Rule; import org.junit.Test; import org.sonar.api.server.ws.WebService; @@ -161,11 +160,11 @@ public class SearchMyOrganizationsActionTest { OrganizationDto organization2 = dbTester.organizations().insert(); GroupDto group = dbTester.users().insertGroup(organization2); dbTester.users().insertMember(group, user); - Arrays.stream(OrganizationPermission.values()) - .filter(s -> s != ADMINISTER) - .forEach(s -> { - dbTester.users().insertPermissionOnUser(organization1, user, s); - dbTester.users().insertPermissionOnGroup(group, s); + OrganizationPermission.all() + .filter(p -> p != ADMINISTER) + .forEach(p -> { + dbTester.users().insertPermissionOnUser(organization1, user, p); + dbTester.users().insertPermissionOnGroup(group, p); }); userSessionRule.logIn(user); -- cgit v1.2.3