package org.sonar.server.permission;
import java.util.Date;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import static java.lang.String.format;
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.server.permission.ws.template.DefaultPermissionTemplateFinder;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.client.permission.DeleteTemplateWsRequest;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.db.DbSession;
import org.sonar.db.permission.GroupWithPermissionDto;
import org.sonar.db.permission.OldPermissionQuery;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.WsPermissions.Group;
import org.sonarqube.ws.WsPermissions.WsGroupsResponse;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.permission.OldPermissionQuery;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.permission.UserWithPermissionDto;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.WsPermissions;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.permission.OldPermissionQuery;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.user.GroupDto;
import org.sonar.server.permission.ws.PermissionDependenciesFinder;
import org.sonar.server.permission.ws.PermissionsWsAction;
import org.sonar.api.utils.System2;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
import org.sonar.server.permission.ws.PermissionDependenciesFinder;
import org.sonar.server.permission.ws.PermissionsWsAction;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.permission.OldPermissionQuery;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.permission.UserWithPermissionDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.permission.ws.PermissionDependenciesFinder;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.server.permission.ApplyPermissionTemplateQuery;
import org.sonar.server.permission.PermissionService;
import org.sonar.server.permission.ws.PermissionDependenciesFinder;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentDtoFunctions;
import org.sonar.db.component.ComponentQuery;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.server.permission.ApplyPermissionTemplateQuery;
import org.sonar.server.permission.PermissionService;
import org.sonar.server.permission.ws.PermissionDependenciesFinder;
import org.sonar.api.utils.System2;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.server.permission.ws.PermissionsWsAction;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.WsPermissions.CreateTemplateWsResponse;
import java.util.Date;
import org.sonar.api.utils.System2;
import org.sonar.core.util.Uuids;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
public class PermissionTemplateDtoBuilder {
private final System2 system;
import com.google.common.base.Function;
import javax.annotation.Nonnull;
import org.sonar.api.utils.DateUtils;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonarqube.ws.WsPermissions.PermissionTemplate;
public class PermissionTemplateDtoToPermissionTemplateResponse {
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.user.GroupDto;
import org.sonar.server.permission.ws.PermissionDependenciesFinder;
import org.sonar.server.permission.ws.PermissionsWsAction;
import org.sonar.api.utils.System2;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.permission.template.PermissionTemplateCharacteristicDao;
import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
import org.sonar.server.permission.ws.PermissionDependenciesFinder;
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.permission.ws.PermissionDependenciesFinder;
import org.sonar.server.permission.ws.PermissionsWsAction;
import org.sonar.core.permission.ProjectPermissions;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.server.permission.ws.PermissionsWsAction;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.WsPermissions;
import com.google.common.collect.Table;
import java.util.List;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.server.permission.ws.template.DefaultPermissionTemplateFinder.TemplateUuidQualifier;
import static com.google.common.base.MoreObjects.firstNonNull;
import java.util.List;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.permission.CountByTemplateAndPermissionDto;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.CountByTemplateAndPermissionDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
import org.sonar.server.permission.ws.template.DefaultPermissionTemplateFinder.TemplateUuidQualifier;
import org.sonarqube.ws.client.permission.SearchTemplatesWsRequest;
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.server.permission.ws.PermissionDependenciesFinder;
import org.sonar.server.permission.ws.PermissionsWsAction;
import org.sonar.server.platform.PersistentSettings;
import org.sonar.api.utils.System2;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.server.permission.ws.PermissionDependenciesFinder;
import org.sonar.server.permission.ws.PermissionsWsAction;
import org.sonar.server.permission.ws.WsTemplateRef;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.loadedtemplate.LoadedTemplateDto;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.user.GroupDto;
import org.sonar.server.permission.DefaultPermissionTemplates;
import org.sonar.server.platform.PersistentSettings;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
import org.sonar.db.permission.PermissionRepository;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.user.GroupRoleDto;
import org.sonar.server.computation.batch.TreeRootHolderRule;
import org.sonar.server.computation.component.Component;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.component.ComponentTesting.newView;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
import static org.sonar.server.computation.component.Component.Type.PROJECT;
import static org.sonar.server.computation.component.Component.Type.VIEW;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.ResourceTypesRule;
-import org.sonar.db.permission.PermissionTemplateDto;
-import org.sonar.db.permission.PermissionTemplateGroupDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateGroupDto;
import org.sonar.db.user.GroupDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
import static org.sonar.api.server.ws.WebService.Param.SELECTED;
import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateGroupDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateGroupDto;
import static org.sonar.db.user.GroupTesting.newGroupDto;
import static org.sonar.test.JsonAssert.assertJson;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.ResourceTypesRule;
-import org.sonar.db.permission.PermissionTemplateDto;
-import org.sonar.db.permission.PermissionTemplateUserDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateUserDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.web.UserRole.ADMIN;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateUserDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateUserDto;
import static org.sonar.test.JsonAssert.assertJson;
import static org.sonarqube.ws.MediaTypes.PROTOBUF;
import static org.sonarqube.ws.WsPermissions.UsersWsResponse.parseFrom;
import org.sonar.db.component.ResourceTypesRule;
import org.sonar.db.permission.GroupWithPermissionDto;
import org.sonar.db.permission.OldPermissionQuery;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.user.GroupDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
import static org.sonar.api.web.UserRole.ADMIN;
import static org.sonar.api.web.UserRole.CODEVIEWER;
import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
import static org.sonar.db.user.GroupMembershipQuery.IN;
import static org.sonar.db.user.GroupTesting.newGroupDto;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_ID;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.ResourceTypesRule;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
import org.sonar.db.permission.template.PermissionTemplateCharacteristicMapper;
import org.sonar.server.component.ComponentFinder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
import org.sonar.db.DbTester;
import org.sonar.db.component.ResourceTypesRule;
import org.sonar.db.permission.OldPermissionQuery;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.permission.UserWithPermissionDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.component.ComponentFinder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.web.UserRole.CODEVIEWER;
import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
import static org.sonar.db.user.GroupMembershipQuery.IN;
import static org.sonar.db.user.UserTesting.newUserDto;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
import org.sonar.db.permission.GroupWithPermissionDto;
import org.sonar.db.permission.OldPermissionQuery;
import org.sonar.db.permission.PermissionRepository;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.permission.UserWithPermissionDto;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.GroupRoleDto;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.sonar.db.component.ComponentTesting.newProjectDto;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
import static org.sonar.db.user.GroupMembershipQuery.IN;
import static org.sonar.db.user.GroupTesting.newGroupDto;
import static org.sonar.db.user.UserTesting.newUserDto;
import static org.sonar.db.component.ComponentTesting.newDeveloper;
import static org.sonar.db.component.ComponentTesting.newProjectDto;
import static org.sonar.db.component.ComponentTesting.newView;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
import static org.sonar.db.user.GroupMembershipQuery.IN;
import static org.sonar.db.user.GroupTesting.newGroupDto;
import static org.sonar.db.user.UserTesting.newUserDto;
import org.sonar.db.permission.GroupWithPermissionDto;
import org.sonar.db.permission.OldPermissionQuery;
import org.sonar.db.permission.PermissionRepository;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.permission.UserWithPermissionDto;
import org.sonar.db.user.GroupDbTester;
import org.sonar.db.user.GroupDto;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.UnauthorizedException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_DESCRIPTION;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_NAME;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY_PATTERN;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.ResourceTypesRule;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.GroupTesting;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.internal.util.collections.Sets.newSet;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
import org.sonar.db.component.ResourceTypesRule;
import org.sonar.db.permission.GroupWithPermissionDto;
import org.sonar.db.permission.OldPermissionQuery;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.user.GroupDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.security.DefaultGroups.ANYONE;
import static org.sonar.api.web.UserRole.CODEVIEWER;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
import static org.sonar.db.user.GroupMembershipQuery.IN;
import static org.sonar.db.user.GroupTesting.newGroupDto;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_ID;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.ResourceTypesRule;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.permission.template.PermissionTemplateCharacteristicDao;
import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
import org.sonar.db.permission.template.PermissionTemplateCharacteristicMapper;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
import org.sonar.db.DbTester;
import org.sonar.db.component.ResourceTypesRule;
import org.sonar.db.permission.OldPermissionQuery;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.permission.UserWithPermissionDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.component.ComponentFinder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.web.UserRole.CODEVIEWER;
import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
import static org.sonar.db.user.GroupMembershipQuery.IN;
import static org.sonar.db.user.UserTesting.newUserDto;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.ResourceTypesRule;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
import static org.sonar.core.util.Uuids.UUID_EXAMPLE_02;
import static org.sonar.core.util.Uuids.UUID_EXAMPLE_03;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
import static org.sonar.db.user.GroupTesting.newGroupDto;
import static org.sonar.db.user.UserTesting.newUserDto;
import static org.sonar.server.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty;
import org.sonar.api.resources.Qualifiers;
import static java.util.Collections.singletonList;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
public class SearchTemplatesDataTest {
@Rule
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
-import org.sonar.db.permission.PermissionTemplateDto;
-import org.sonar.db.permission.PermissionTemplateTesting;
+import org.sonar.db.permission.template.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateTesting;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.ResourceTypesRule;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.user.GroupDto;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_DESCRIPTION;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ID;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_NAME;
import org.sonar.db.DbSession;
import org.sonar.db.loadedtemplate.LoadedTemplateDao;
import org.sonar.db.loadedtemplate.LoadedTemplateDto;
-import org.sonar.db.permission.PermissionTemplateDao;
-import org.sonar.db.permission.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateDao;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.user.GroupDao;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDao;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
-import org.sonar.db.permission.PermissionTemplateDao;
+import org.sonar.db.permission.template.PermissionTemplateDao;
import org.sonar.db.user.GroupDao;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.GroupRoleDto;
import org.sonar.db.metric.MetricDao;
import org.sonar.db.notification.NotificationQueueDao;
import org.sonar.db.permission.PermissionDao;
-import org.sonar.db.permission.PermissionTemplateDao;
+import org.sonar.db.permission.template.PermissionTemplateDao;
import org.sonar.db.permission.template.PermissionTemplateCharacteristicDao;
import org.sonar.db.property.PropertiesDao;
import org.sonar.db.purge.PurgeDao;
import org.sonar.db.metric.MetricDao;
import org.sonar.db.notification.NotificationQueueDao;
import org.sonar.db.permission.PermissionDao;
-import org.sonar.db.permission.PermissionTemplateDao;
+import org.sonar.db.permission.template.PermissionTemplateDao;
import org.sonar.db.permission.template.PermissionTemplateCharacteristicDao;
import org.sonar.db.property.PropertiesDao;
import org.sonar.db.purge.PurgeDao;
import org.sonar.db.notification.NotificationQueueDto;
import org.sonar.db.notification.NotificationQueueMapper;
import org.sonar.db.permission.GroupWithPermissionDto;
-import org.sonar.db.permission.PermissionTemplateDto;
-import org.sonar.db.permission.PermissionTemplateGroupDto;
-import org.sonar.db.permission.PermissionTemplateMapper;
-import org.sonar.db.permission.PermissionTemplateUserDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateGroupDto;
+import org.sonar.db.permission.template.PermissionTemplateMapper;
+import org.sonar.db.permission.template.PermissionTemplateUserDto;
import org.sonar.db.permission.UserWithPermissionDto;
import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
import org.sonar.db.permission.template.PermissionTemplateCharacteristicMapper;
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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;
-
-public class CountByTemplateAndPermissionDto {
- private long templateId;
- private String permission;
- private int count;
-
- public long getTemplateId() {
- return templateId;
- }
-
- public void setTemplateId(long templateId) {
- this.templateId = templateId;
- }
-
- public String getPermission() {
- return permission;
- }
-
- public void setPermission(String permission) {
- this.permission = permission;
- }
-
- public int getCount() {
- return count;
- }
-
- public void setCount(int count) {
- this.count = count;
- }
-}
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.permission.template.PermissionTemplate;
import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateGroupDto;
+import org.sonar.db.permission.template.PermissionTemplateUserDto;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.GroupRoleDto;
import org.sonar.db.user.UserRoleDto;
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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 java.util.List;
-import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
-
-public class PermissionTemplate {
- private final PermissionTemplateDto template;
- private final List<PermissionTemplateUserDto> userPermissions;
- private final List<PermissionTemplateGroupDto> groupPermissions;
- private final List<PermissionTemplateCharacteristicDto> characteristics;
-
- public PermissionTemplate(PermissionTemplateDto template,
- List<PermissionTemplateUserDto> userPermissions,
- List<PermissionTemplateGroupDto> groupPermissions,
- List<PermissionTemplateCharacteristicDto> characteristics) {
- this.template = template;
- this.userPermissions = userPermissions;
- this.groupPermissions = groupPermissions;
- this.characteristics = characteristics;
- }
-
- public PermissionTemplateDto getTemplate() {
- return template;
- }
-
- public List<PermissionTemplateUserDto> getUserPermissions() {
- return userPermissions;
- }
-
- public List<PermissionTemplateGroupDto> getGroupPermissions() {
- return groupPermissions;
- }
-
- public List<PermissionTemplateCharacteristicDto> getCharacteristics() {
- return characteristics;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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 com.google.common.annotations.VisibleForTesting;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
-import org.apache.ibatis.session.ResultHandler;
-import org.apache.ibatis.session.RowBounds;
-import org.apache.ibatis.session.SqlSession;
-import org.sonar.api.security.DefaultGroups;
-import org.sonar.api.utils.System2;
-import org.sonar.api.web.UserRole;
-import org.sonar.db.Dao;
-import org.sonar.db.DbSession;
-import org.sonar.db.MyBatis;
-import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
-import org.sonar.db.permission.template.PermissionTemplateCharacteristicMapper;
-
-import static com.google.common.collect.Maps.newHashMap;
-import static java.lang.String.format;
-import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput;
-
-public class PermissionTemplateDao implements Dao {
-
- public static final String QUERY_PARAMETER = "query";
- public static final String TEMPLATE_ID_PARAMETER = "templateId";
- private static final String ANYONE_GROUP_PARAMETER = "anyoneGroup";
-
- private final MyBatis myBatis;
- private final System2 system;
-
- public PermissionTemplateDao(MyBatis myBatis, System2 system) {
- this.myBatis = myBatis;
- this.system = system;
- }
-
- /**
- * @return a paginated list of users.
- */
- public List<UserWithPermissionDto> selectUsers(OldPermissionQuery query, Long templateId, int offset, int limit) {
- DbSession session = myBatis.openSession(false);
- try {
- return selectUsers(session, query, templateId, offset, limit);
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
- /**
- * @return a paginated list of users.
- */
- public List<UserWithPermissionDto> selectUsers(DbSession session, OldPermissionQuery query, Long templateId, int offset, int limit) {
- Map<String, Object> params = newHashMap();
- params.put(QUERY_PARAMETER, query);
- params.put(TEMPLATE_ID_PARAMETER, templateId);
- return mapper(session).selectUsers(params, new RowBounds(offset, limit));
- }
-
- public int countUsers(DbSession session, OldPermissionQuery query, Long templateId) {
- Map<String, Object> params = newHashMap();
- params.put(QUERY_PARAMETER, query);
- params.put(TEMPLATE_ID_PARAMETER, templateId);
- return mapper(session).countUsers(params);
- }
-
- @VisibleForTesting
- List<UserWithPermissionDto> selectUsers(OldPermissionQuery query, Long templateId) {
- return selectUsers(query, templateId, 0, Integer.MAX_VALUE);
- }
-
- /**
- * 'Anyone' group is not returned when it has not the asked permission.
- * Membership parameter from query is not taking into account in order to deal more easily with the 'Anyone' group.
- * @return a non paginated list of groups.
- */
- public List<GroupWithPermissionDto> selectGroups(DbSession session, OldPermissionQuery query, Long templateId) {
- return selectGroups(session, query, templateId, 0, Integer.MAX_VALUE);
- }
-
- public List<GroupWithPermissionDto> selectGroups(DbSession session, OldPermissionQuery query, Long templateId, int offset, int limit) {
- Map<String, Object> params = groupsParameters(query, templateId);
- return mapper(session).selectGroups(params, new RowBounds(offset, limit));
- }
-
- public List<GroupWithPermissionDto> selectGroups(OldPermissionQuery query, Long templateId) {
- DbSession session = myBatis.openSession(false);
- try {
- return selectGroups(session, query, templateId);
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
- public int countGroups(DbSession session, OldPermissionQuery query, long templateId) {
- return countGroups(session, query, templateId, null);
- }
-
- private static int countGroups(DbSession session, OldPermissionQuery query, long templateId, @Nullable String groupName) {
- Map<String, Object> parameters = groupsParameters(query, templateId);
- if (groupName != null) {
- parameters.put("groupName", groupName.toUpperCase(Locale.ENGLISH));
- }
- return mapper(session).countGroups(parameters);
- }
-
- public boolean hasGroup(DbSession session, OldPermissionQuery query, long templateId, String groupName) {
- return countGroups(session, query, templateId, groupName) > 0;
- }
-
- private static Map<String, Object> groupsParameters(OldPermissionQuery query, Long templateId) {
- Map<String, Object> params = newHashMap();
- params.put(QUERY_PARAMETER, query);
- params.put(TEMPLATE_ID_PARAMETER, templateId);
- params.put("anyoneGroup", DefaultGroups.ANYONE);
- params.put("projectAdminPermission", UserRole.ADMIN);
- return params;
- }
-
- @CheckForNull
- public PermissionTemplateDto selectByUuid(DbSession session, String templateUuid) {
- return mapper(session).selectByUuid(templateUuid);
- }
-
- @CheckForNull
- public PermissionTemplateDto selectByUuid(String templateUuid) {
- DbSession session = myBatis.openSession(false);
- try {
- return selectByUuid(session, templateUuid);
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
- @CheckForNull
- public PermissionTemplate selectByUuidWithUserAndGroupPermissions(DbSession session, String templateUuid) {
- PermissionTemplateMapper mapper = mapper(session);
-
- PermissionTemplateDto template = mapper.selectByUuid(templateUuid);
- if (template == null) {
- return null;
- }
-
- List<PermissionTemplateUserDto> userPermissions = mapper.selectUserPermissionsByTemplateId(template.getId());
- List<PermissionTemplateGroupDto> groupPermissions = mapper.selectGroupPermissionsByTemplateId(template.getId());
- PermissionTemplateCharacteristicMapper characteristicMapper = session.getMapper(PermissionTemplateCharacteristicMapper.class);
- List<PermissionTemplateCharacteristicDto> characteristics = characteristicMapper.selectByTemplateId(template.getId());
-
- return new PermissionTemplate(template, userPermissions, groupPermissions, characteristics);
- }
-
- @CheckForNull
- public PermissionTemplate selectByUuidWithUserAndGroupPermissions(String templateUuid) {
- DbSession session = myBatis.openSession(false);
- try {
- return selectByUuidWithUserAndGroupPermissions(session, templateUuid);
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
- public List<PermissionTemplateDto> selectAll(DbSession session, String nameMatch) {
- String uppercaseNameMatch = toUppercaseSqlQuery(nameMatch);
- return mapper(session).selectAll(uppercaseNameMatch);
- }
-
- public List<PermissionTemplateDto> selectAll(DbSession session) {
- return mapper(session).selectAll(null);
- }
-
- public List<PermissionTemplateDto> selectAll() {
- DbSession session = myBatis.openSession(false);
- try {
- return selectAll(session);
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
- public int countAll(DbSession dbSession, String nameQuery) {
- String upperCasedNameQuery = toUppercaseSqlQuery(nameQuery);
-
- return mapper(dbSession).countAll(upperCasedNameQuery);
- }
-
- private static String toUppercaseSqlQuery(String nameMatch) {
- String wildcard = "%";
- return format("%s%s%s", wildcard, nameMatch.toUpperCase(Locale.ENGLISH), wildcard);
-
- }
-
- public PermissionTemplateDto insert(DbSession session, PermissionTemplateDto permissionTemplate) {
- mapper(session).insert(permissionTemplate);
- session.commit();
-
- return permissionTemplate;
- }
-
- /**
- * Each row returns a #{@link CountByProjectAndPermissionDto}
- */
- public void usersCountByTemplateIdAndPermission(DbSession dbSession, List<Long> templateIds, ResultHandler resultHandler) {
- Map<String, Object> parameters = new HashMap<>(1);
-
- executeLargeInputsWithoutOutput(
- templateIds,
- partitionedTemplateIds -> {
- parameters.put("templateIds", partitionedTemplateIds);
- mapper(dbSession).usersCountByTemplateIdAndPermission(parameters, resultHandler);
- return null;
- });
- }
-
- /**
- * Each row returns a #{@link CountByProjectAndPermissionDto}
- */
- public void groupsCountByTemplateIdAndPermission(DbSession dbSession, List<Long> templateIds, ResultHandler resultHandler) {
- Map<String, Object> parameters = new HashMap<>(2);
- parameters.put(ANYONE_GROUP_PARAMETER, DefaultGroups.ANYONE);
-
- executeLargeInputsWithoutOutput(
- templateIds,
- partitionedTemplateIds -> {
- parameters.put("templateIds", partitionedTemplateIds);
- mapper(dbSession).groupsCountByTemplateIdAndPermission(parameters, resultHandler);
- return null;
- });
- }
-
- public void deleteById(DbSession session, long templateId) {
- PermissionTemplateMapper mapper = mapper(session);
- mapper.deleteUserPermissions(templateId);
- mapper.deleteGroupPermissions(templateId);
- session.getMapper(PermissionTemplateCharacteristicMapper.class).deleteByTemplateId(templateId);
- mapper.delete(templateId);
- }
-
- /**
- * @deprecated since 5.2 use {@link #update(DbSession, PermissionTemplateDto)}
- */
- @Deprecated
- public void update(Long templateId, String templateName, @Nullable String description, @Nullable String projectPattern) {
- PermissionTemplateDto permissionTemplate = new PermissionTemplateDto()
- .setId(templateId)
- .setName(templateName)
- .setDescription(description)
- .setKeyPattern(projectPattern)
- .setUpdatedAt(now());
-
- DbSession session = myBatis.openSession(false);
- try {
- update(session, permissionTemplate);
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
- public PermissionTemplateDto update(DbSession session, PermissionTemplateDto permissionTemplate) {
- mapper(session).update(permissionTemplate);
- session.commit();
-
- return permissionTemplate;
- }
-
- /**
- * @deprecated since 5.2 {@link #insertUserPermission(DbSession, Long, Long, String)}
- */
- @Deprecated
- public void insertUserPermission(Long templateId, Long userId, String permission) {
- DbSession session = myBatis.openSession(false);
- try {
- insertUserPermission(session, templateId, userId, permission);
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
- public void insertUserPermission(DbSession session, Long templateId, Long userId, String permission) {
- PermissionTemplateUserDto permissionTemplateUser = new PermissionTemplateUserDto()
- .setTemplateId(templateId)
- .setUserId(userId)
- .setPermission(permission)
- .setCreatedAt(now())
- .setUpdatedAt(now());
-
- mapper(session).insertUserPermission(permissionTemplateUser);
- session.commit();
- }
-
- public void insertUserPermission(DbSession session, PermissionTemplateUserDto permissionTemplateUserDto) {
- mapper(session).insertUserPermission(permissionTemplateUserDto);
- }
-
- /**
- * @deprecated since 5.2 {@link #deleteUserPermission(DbSession, Long, Long, String)}
- */
- @Deprecated
- public void deleteUserPermission(Long templateId, Long userId, String permission) {
- DbSession session = myBatis.openSession(false);
- try {
- deleteUserPermission(session, templateId, userId, permission);
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
- public void deleteUserPermission(DbSession session, Long templateId, Long userId, String permission) {
- PermissionTemplateUserDto permissionTemplateUser = new PermissionTemplateUserDto()
- .setTemplateId(templateId)
- .setPermission(permission)
- .setUserId(userId);
- mapper(session).deleteUserPermission(permissionTemplateUser);
- session.commit();
- }
-
- /**
- * @deprecated since 5.2 use {@link #insertGroupPermission(DbSession, Long, Long, String)}
- */
- @Deprecated
- public void insertGroupPermission(Long templateId, @Nullable Long groupId, String permission) {
- DbSession session = myBatis.openSession(false);
- try {
- insertGroupPermission(session, templateId, groupId, permission);
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
- public void insertGroupPermission(DbSession session, Long templateId, @Nullable Long groupId, String permission) {
- PermissionTemplateGroupDto permissionTemplateGroup = new PermissionTemplateGroupDto()
- .setTemplateId(templateId)
- .setPermission(permission)
- .setGroupId(groupId)
- .setCreatedAt(now())
- .setUpdatedAt(now());
- mapper(session).insertGroupPermission(permissionTemplateGroup);
- session.commit();
- }
-
- public void insertGroupPermission(DbSession session, PermissionTemplateGroupDto permissionTemplateGroup) {
- mapper(session).insertGroupPermission(permissionTemplateGroup);
- }
-
- /**
- * @deprecated since 5.2 use {@link #deleteGroupPermission(DbSession, Long, Long, String)}
- */
- @Deprecated
- public void deleteGroupPermission(Long templateId, @Nullable Long groupId, String permission) {
- DbSession session = myBatis.openSession(false);
- try {
- deleteGroupPermission(session, templateId, groupId, permission);
- } finally {
- MyBatis.closeQuietly(session);
- }
- }
-
- public void deleteGroupPermission(DbSession session, Long templateId, @Nullable Long groupId, String permission) {
- PermissionTemplateGroupDto permissionTemplateGroup = new PermissionTemplateGroupDto()
- .setTemplateId(templateId)
- .setPermission(permission)
- .setGroupId(groupId);
- mapper(session).deleteGroupPermission(permissionTemplateGroup);
- session.commit();
- }
-
- /**
- * Load permission template and load associated collections of users and groups permissions, and characteristics
- */
- PermissionTemplate selectPermissionTemplateWithPermissions(DbSession session, String templateUuid) {
- PermissionTemplateDto template = selectByUuid(session, templateUuid);
- if (template == null) {
- throw new IllegalArgumentException("Could not retrieve permission template with uuid " + templateUuid);
- }
- PermissionTemplate templateWithDependencies = selectByUuidWithUserAndGroupPermissions(session, template.getUuid());
- if (templateWithDependencies == null) {
- throw new IllegalArgumentException("Could not retrieve permissions for template with uuid " + templateUuid);
- }
- return templateWithDependencies;
- }
-
- public PermissionTemplateDto selectByName(DbSession dbSession, String name) {
- return mapper(dbSession).selectByName(name.toUpperCase(Locale.ENGLISH));
- }
-
- public List<String> selectPotentialPermissionsByUserIdAndTemplateId(DbSession dbSession, @Nullable Long currentUserId, long templateId) {
- return mapper(dbSession).selectPotentialPermissionsByUserIdAndTemplateId(currentUserId, templateId);
- }
-
- /**
- * Remove a group from all templates (used when removing a group)
- */
- public void deleteByGroup(SqlSession session, Long groupId) {
- session.getMapper(PermissionTemplateMapper.class).deleteByGroupId(groupId);
- }
-
- private Date now() {
- return new Date(system.now());
- }
-
- private static PermissionTemplateMapper mapper(SqlSession session) {
- return session.getMapper(PermissionTemplateMapper.class);
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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 java.util.Date;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
-
-public class PermissionTemplateDto {
-
- private Long id;
- private String name;
- private String uuid;
- private String description;
- private String keyPattern;
- private Date createdAt;
- private Date updatedAt;
-
- public Long getId() {
- return id;
- }
-
- public PermissionTemplateDto setId(Long id) {
- this.id = id;
- return this;
- }
-
- public String getName() {
- return name;
- }
-
- public PermissionTemplateDto setName(String name) {
- this.name = name;
- return this;
- }
-
- /**
- * @deprecated since 5.2 use {@link #getUuid()}
- */
- @Deprecated
- public String getKee() {
- return uuid;
- }
-
- /**
- * @deprecated since 5.2 use {@link #setUuid(String)}
- */
- @Deprecated
- public PermissionTemplateDto setKee(String kee) {
- this.uuid = kee;
- return this;
- }
-
- /**
- * @since 5.2 the kee column is a proper uuid. Before that it was build on the name + timestamp
- */
- public String getUuid() {
- return uuid;
- }
-
- /**
- * @since 5.2 the kee column is a proper uuid. Before it was build on the name + timestamp
- */
- public PermissionTemplateDto setUuid(String uuid) {
- this.uuid = uuid;
- return this;
- }
-
- @CheckForNull
- public String getDescription() {
- return description;
- }
-
- public PermissionTemplateDto setDescription(@Nullable String description) {
- this.description = description;
- return this;
- }
-
- @CheckForNull
- public String getKeyPattern() {
- return keyPattern;
- }
-
- public PermissionTemplateDto setKeyPattern(@Nullable String regexp) {
- this.keyPattern = regexp;
- return this;
- }
-
- public Date getCreatedAt() {
- return createdAt;
- }
-
- public PermissionTemplateDto setCreatedAt(Date createdAt) {
- this.createdAt = createdAt;
- return this;
- }
-
- public Date getUpdatedAt() {
- return updatedAt;
- }
-
- public PermissionTemplateDto setUpdatedAt(Date updatedAt) {
- this.updatedAt = updatedAt;
- return this;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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 java.util.Date;
-import javax.annotation.Nullable;
-
-public class PermissionTemplateGroupDto {
- private Long id;
- private Long templateId;
- private Long groupId;
- private String permission;
- private String groupName;
- private Date createdAt;
- private Date updatedAt;
-
- public Long getId() {
- return id;
- }
-
- public PermissionTemplateGroupDto setId(Long id) {
- this.id = id;
- return this;
- }
-
- public Long getTemplateId() {
- return templateId;
- }
-
- public PermissionTemplateGroupDto setTemplateId(Long templateId) {
- this.templateId = templateId;
- return this;
- }
-
- public Long getGroupId() {
- return groupId;
- }
-
- public PermissionTemplateGroupDto setGroupId(@Nullable Long groupId) {
- this.groupId = groupId;
- return this;
- }
-
- public String getPermission() {
- return permission;
- }
-
- public PermissionTemplateGroupDto setPermission(String permission) {
- this.permission = permission;
- return this;
- }
-
- public String getGroupName() {
- return groupName;
- }
-
- public PermissionTemplateGroupDto setGroupName(String groupName) {
- this.groupName = groupName;
- return this;
- }
-
- public Date getCreatedAt() {
- return createdAt;
- }
-
- public PermissionTemplateGroupDto setCreatedAt(Date createdAt) {
- this.createdAt = createdAt;
- return this;
- }
-
- public Date getUpdatedAt() {
- return updatedAt;
- }
-
- public PermissionTemplateGroupDto setUpdatedAt(Date updatedAt) {
- this.updatedAt = updatedAt;
- return this;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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 java.util.List;
-import java.util.Map;
-import javax.annotation.Nullable;
-import org.apache.ibatis.annotations.Param;
-import org.apache.ibatis.session.ResultHandler;
-import org.apache.ibatis.session.RowBounds;
-
-/**
- * @since 3.7
- */
-public interface PermissionTemplateMapper {
-
- void insert(PermissionTemplateDto permissionTemplate);
-
- void update(PermissionTemplateDto permissionTemplate);
-
- void delete(long templateId);
-
- void deleteUserPermissions(long templateId);
-
- void deleteUserPermission(PermissionTemplateUserDto permissionTemplateUser);
-
- void deleteGroupPermissions(long templateId);
-
- void deleteGroupPermission(PermissionTemplateGroupDto permissionTemplateGroup);
-
- PermissionTemplateDto selectByUuid(String templateUuid);
-
- List<PermissionTemplateUserDto> selectUserPermissionsByTemplateId(long templateId);
-
- List<PermissionTemplateGroupDto> selectGroupPermissionsByTemplateId(long templateId);
-
- void insertUserPermission(PermissionTemplateUserDto permissionTemplateUser);
-
- void insertGroupPermission(PermissionTemplateGroupDto permissionTemplateGroup);
-
- void deleteByGroupId(long groupId);
-
- List<GroupWithPermissionDto> selectGroups(Map<String, Object> params, RowBounds rowBounds);
-
- List<UserWithPermissionDto> selectUsers(Map<String, Object> params, RowBounds rowBounds);
-
- PermissionTemplateDto selectByName(String name);
-
- int countUsers(Map<String, Object> params);
-
- int countGroups(Map<String, Object> parameters);
-
- List<PermissionTemplateDto> selectAll(@Param("nameMatch") String nameMatch);
-
- int countAll(@Param("nameMatch") String nameMatch);
-
- void usersCountByTemplateIdAndPermission(Map<String, Object> parameters, ResultHandler resultHandler);
-
- void groupsCountByTemplateIdAndPermission(Map<String, Object> parameters, ResultHandler resultHandler);
-
- List<String> selectPotentialPermissionsByUserIdAndTemplateId(@Param("userId") @Nullable Long currentUserId, @Param("templateId") long templateId);
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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 java.util.Date;
-
-public class PermissionTemplateUserDto {
- private Long id;
- private Long templateId;
- private Long userId;
- private String permission;
- private String userName;
- private String userLogin;
- private Date createdAt;
- private Date updatedAt;
-
- public Long getId() {
- return id;
- }
-
- public PermissionTemplateUserDto setId(Long id) {
- this.id = id;
- return this;
- }
-
- public Long getTemplateId() {
- return templateId;
- }
-
- public PermissionTemplateUserDto setTemplateId(Long templateId) {
- this.templateId = templateId;
- return this;
- }
-
- public Long getUserId() {
- return userId;
- }
-
- public PermissionTemplateUserDto setUserId(Long userId) {
- this.userId = userId;
- return this;
- }
-
- public String getUserName() {
- return userName;
- }
-
- public PermissionTemplateUserDto setUserName(String userName) {
- this.userName = userName;
- return this;
- }
-
- public String getUserLogin() {
- return userLogin;
- }
-
- public PermissionTemplateUserDto setUserLogin(String userLogin) {
- this.userLogin = userLogin;
- return this;
- }
-
- public String getPermission() {
- return permission;
- }
-
- public PermissionTemplateUserDto setPermission(String permission) {
- this.permission = permission;
- return this;
- }
-
- public Date getCreatedAt() {
- return createdAt;
- }
-
- public PermissionTemplateUserDto setCreatedAt(Date createdAt) {
- this.createdAt = createdAt;
- return this;
- }
-
- public Date getUpdatedAt() {
- return updatedAt;
- }
-
- public PermissionTemplateUserDto setUpdatedAt(Date updatedAt) {
- this.updatedAt = updatedAt;
- return this;
- }
-}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.template;
+
+public class CountByTemplateAndPermissionDto {
+ private long templateId;
+ private String permission;
+ private int count;
+
+ public long getTemplateId() {
+ return templateId;
+ }
+
+ public void setTemplateId(long templateId) {
+ this.templateId = templateId;
+ }
+
+ public String getPermission() {
+ return permission;
+ }
+
+ public void setPermission(String permission) {
+ this.permission = permission;
+ }
+
+ public int getCount() {
+ return count;
+ }
+
+ public void setCount(int count) {
+ this.count = count;
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.template;
+
+import java.util.List;
+import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateGroupDto;
+import org.sonar.db.permission.template.PermissionTemplateUserDto;
+
+public class PermissionTemplate {
+ private final PermissionTemplateDto template;
+ private final List<PermissionTemplateUserDto> userPermissions;
+ private final List<PermissionTemplateGroupDto> groupPermissions;
+ private final List<PermissionTemplateCharacteristicDto> characteristics;
+
+ public PermissionTemplate(PermissionTemplateDto template,
+ List<PermissionTemplateUserDto> userPermissions,
+ List<PermissionTemplateGroupDto> groupPermissions,
+ List<PermissionTemplateCharacteristicDto> characteristics) {
+ this.template = template;
+ this.userPermissions = userPermissions;
+ this.groupPermissions = groupPermissions;
+ this.characteristics = characteristics;
+ }
+
+ public PermissionTemplateDto getTemplate() {
+ return template;
+ }
+
+ public List<PermissionTemplateUserDto> getUserPermissions() {
+ return userPermissions;
+ }
+
+ public List<PermissionTemplateGroupDto> getGroupPermissions() {
+ return groupPermissions;
+ }
+
+ public List<PermissionTemplateCharacteristicDto> getCharacteristics() {
+ return characteristics;
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.template;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+import org.apache.ibatis.session.ResultHandler;
+import org.apache.ibatis.session.RowBounds;
+import org.apache.ibatis.session.SqlSession;
+import org.sonar.api.security.DefaultGroups;
+import org.sonar.api.utils.System2;
+import org.sonar.api.web.UserRole;
+import org.sonar.db.Dao;
+import org.sonar.db.DbSession;
+import org.sonar.db.MyBatis;
+import org.sonar.db.permission.CountByProjectAndPermissionDto;
+import org.sonar.db.permission.GroupWithPermissionDto;
+import org.sonar.db.permission.OldPermissionQuery;
+import org.sonar.db.permission.UserWithPermissionDto;
+
+import static com.google.common.collect.Maps.newHashMap;
+import static java.lang.String.format;
+import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput;
+
+public class PermissionTemplateDao implements Dao {
+
+ public static final String QUERY_PARAMETER = "query";
+ public static final String TEMPLATE_ID_PARAMETER = "templateId";
+ private static final String ANYONE_GROUP_PARAMETER = "anyoneGroup";
+
+ private final MyBatis myBatis;
+ private final System2 system;
+
+ public PermissionTemplateDao(MyBatis myBatis, System2 system) {
+ this.myBatis = myBatis;
+ this.system = system;
+ }
+
+ /**
+ * @return a paginated list of users.
+ */
+ public List<UserWithPermissionDto> selectUsers(OldPermissionQuery query, Long templateId, int offset, int limit) {
+ DbSession session = myBatis.openSession(false);
+ try {
+ return selectUsers(session, query, templateId, offset, limit);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ /**
+ * @return a paginated list of users.
+ */
+ public List<UserWithPermissionDto> selectUsers(DbSession session, OldPermissionQuery query, Long templateId, int offset, int limit) {
+ Map<String, Object> params = newHashMap();
+ params.put(QUERY_PARAMETER, query);
+ params.put(TEMPLATE_ID_PARAMETER, templateId);
+ return mapper(session).selectUsers(params, new RowBounds(offset, limit));
+ }
+
+ public int countUsers(DbSession session, OldPermissionQuery query, Long templateId) {
+ Map<String, Object> params = newHashMap();
+ params.put(QUERY_PARAMETER, query);
+ params.put(TEMPLATE_ID_PARAMETER, templateId);
+ return mapper(session).countUsers(params);
+ }
+
+ @VisibleForTesting
+ List<UserWithPermissionDto> selectUsers(OldPermissionQuery query, Long templateId) {
+ return selectUsers(query, templateId, 0, Integer.MAX_VALUE);
+ }
+
+ /**
+ * 'Anyone' group is not returned when it has not the asked permission.
+ * Membership parameter from query is not taking into account in order to deal more easily with the 'Anyone' group.
+ * @return a non paginated list of groups.
+ */
+ public List<GroupWithPermissionDto> selectGroups(DbSession session, OldPermissionQuery query, Long templateId) {
+ return selectGroups(session, query, templateId, 0, Integer.MAX_VALUE);
+ }
+
+ public List<GroupWithPermissionDto> selectGroups(DbSession session, OldPermissionQuery query, Long templateId, int offset, int limit) {
+ Map<String, Object> params = groupsParameters(query, templateId);
+ return mapper(session).selectGroups(params, new RowBounds(offset, limit));
+ }
+
+ public List<GroupWithPermissionDto> selectGroups(OldPermissionQuery query, Long templateId) {
+ DbSession session = myBatis.openSession(false);
+ try {
+ return selectGroups(session, query, templateId);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ public int countGroups(DbSession session, OldPermissionQuery query, long templateId) {
+ return countGroups(session, query, templateId, null);
+ }
+
+ private static int countGroups(DbSession session, OldPermissionQuery query, long templateId, @Nullable String groupName) {
+ Map<String, Object> parameters = groupsParameters(query, templateId);
+ if (groupName != null) {
+ parameters.put("groupName", groupName.toUpperCase(Locale.ENGLISH));
+ }
+ return mapper(session).countGroups(parameters);
+ }
+
+ public boolean hasGroup(DbSession session, OldPermissionQuery query, long templateId, String groupName) {
+ return countGroups(session, query, templateId, groupName) > 0;
+ }
+
+ private static Map<String, Object> groupsParameters(OldPermissionQuery query, Long templateId) {
+ Map<String, Object> params = newHashMap();
+ params.put(QUERY_PARAMETER, query);
+ params.put(TEMPLATE_ID_PARAMETER, templateId);
+ params.put("anyoneGroup", DefaultGroups.ANYONE);
+ params.put("projectAdminPermission", UserRole.ADMIN);
+ return params;
+ }
+
+ @CheckForNull
+ public PermissionTemplateDto selectByUuid(DbSession session, String templateUuid) {
+ return mapper(session).selectByUuid(templateUuid);
+ }
+
+ @CheckForNull
+ public PermissionTemplateDto selectByUuid(String templateUuid) {
+ DbSession session = myBatis.openSession(false);
+ try {
+ return selectByUuid(session, templateUuid);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ @CheckForNull
+ public PermissionTemplate selectByUuidWithUserAndGroupPermissions(DbSession session, String templateUuid) {
+ PermissionTemplateMapper mapper = mapper(session);
+
+ PermissionTemplateDto template = mapper.selectByUuid(templateUuid);
+ if (template == null) {
+ return null;
+ }
+
+ List<PermissionTemplateUserDto> userPermissions = mapper.selectUserPermissionsByTemplateId(template.getId());
+ List<PermissionTemplateGroupDto> groupPermissions = mapper.selectGroupPermissionsByTemplateId(template.getId());
+ PermissionTemplateCharacteristicMapper characteristicMapper = session.getMapper(PermissionTemplateCharacteristicMapper.class);
+ List<PermissionTemplateCharacteristicDto> characteristics = characteristicMapper.selectByTemplateId(template.getId());
+
+ return new PermissionTemplate(template, userPermissions, groupPermissions, characteristics);
+ }
+
+ @CheckForNull
+ public PermissionTemplate selectByUuidWithUserAndGroupPermissions(String templateUuid) {
+ DbSession session = myBatis.openSession(false);
+ try {
+ return selectByUuidWithUserAndGroupPermissions(session, templateUuid);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ public List<PermissionTemplateDto> selectAll(DbSession session, String nameMatch) {
+ String uppercaseNameMatch = toUppercaseSqlQuery(nameMatch);
+ return mapper(session).selectAll(uppercaseNameMatch);
+ }
+
+ public List<PermissionTemplateDto> selectAll(DbSession session) {
+ return mapper(session).selectAll(null);
+ }
+
+ public List<PermissionTemplateDto> selectAll() {
+ DbSession session = myBatis.openSession(false);
+ try {
+ return selectAll(session);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ public int countAll(DbSession dbSession, String nameQuery) {
+ String upperCasedNameQuery = toUppercaseSqlQuery(nameQuery);
+
+ return mapper(dbSession).countAll(upperCasedNameQuery);
+ }
+
+ private static String toUppercaseSqlQuery(String nameMatch) {
+ String wildcard = "%";
+ return format("%s%s%s", wildcard, nameMatch.toUpperCase(Locale.ENGLISH), wildcard);
+
+ }
+
+ public PermissionTemplateDto insert(DbSession session, PermissionTemplateDto permissionTemplate) {
+ mapper(session).insert(permissionTemplate);
+ session.commit();
+
+ return permissionTemplate;
+ }
+
+ /**
+ * Each row returns a #{@link CountByProjectAndPermissionDto}
+ */
+ public void usersCountByTemplateIdAndPermission(DbSession dbSession, List<Long> templateIds, ResultHandler resultHandler) {
+ Map<String, Object> parameters = new HashMap<>(1);
+
+ executeLargeInputsWithoutOutput(
+ templateIds,
+ partitionedTemplateIds -> {
+ parameters.put("templateIds", partitionedTemplateIds);
+ mapper(dbSession).usersCountByTemplateIdAndPermission(parameters, resultHandler);
+ return null;
+ });
+ }
+
+ /**
+ * Each row returns a #{@link CountByProjectAndPermissionDto}
+ */
+ public void groupsCountByTemplateIdAndPermission(DbSession dbSession, List<Long> templateIds, ResultHandler resultHandler) {
+ Map<String, Object> parameters = new HashMap<>(2);
+ parameters.put(ANYONE_GROUP_PARAMETER, DefaultGroups.ANYONE);
+
+ executeLargeInputsWithoutOutput(
+ templateIds,
+ partitionedTemplateIds -> {
+ parameters.put("templateIds", partitionedTemplateIds);
+ mapper(dbSession).groupsCountByTemplateIdAndPermission(parameters, resultHandler);
+ return null;
+ });
+ }
+
+ public void deleteById(DbSession session, long templateId) {
+ PermissionTemplateMapper mapper = mapper(session);
+ mapper.deleteUserPermissions(templateId);
+ mapper.deleteGroupPermissions(templateId);
+ session.getMapper(PermissionTemplateCharacteristicMapper.class).deleteByTemplateId(templateId);
+ mapper.delete(templateId);
+ }
+
+ /**
+ * @deprecated since 5.2 use {@link #update(DbSession, PermissionTemplateDto)}
+ */
+ @Deprecated
+ public void update(Long templateId, String templateName, @Nullable String description, @Nullable String projectPattern) {
+ PermissionTemplateDto permissionTemplate = new PermissionTemplateDto()
+ .setId(templateId)
+ .setName(templateName)
+ .setDescription(description)
+ .setKeyPattern(projectPattern)
+ .setUpdatedAt(now());
+
+ DbSession session = myBatis.openSession(false);
+ try {
+ update(session, permissionTemplate);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ public PermissionTemplateDto update(DbSession session, PermissionTemplateDto permissionTemplate) {
+ mapper(session).update(permissionTemplate);
+ session.commit();
+
+ return permissionTemplate;
+ }
+
+ /**
+ * @deprecated since 5.2 {@link #insertUserPermission(DbSession, Long, Long, String)}
+ */
+ @Deprecated
+ public void insertUserPermission(Long templateId, Long userId, String permission) {
+ DbSession session = myBatis.openSession(false);
+ try {
+ insertUserPermission(session, templateId, userId, permission);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ public void insertUserPermission(DbSession session, Long templateId, Long userId, String permission) {
+ PermissionTemplateUserDto permissionTemplateUser = new PermissionTemplateUserDto()
+ .setTemplateId(templateId)
+ .setUserId(userId)
+ .setPermission(permission)
+ .setCreatedAt(now())
+ .setUpdatedAt(now());
+
+ mapper(session).insertUserPermission(permissionTemplateUser);
+ session.commit();
+ }
+
+ public void insertUserPermission(DbSession session, PermissionTemplateUserDto permissionTemplateUserDto) {
+ mapper(session).insertUserPermission(permissionTemplateUserDto);
+ }
+
+ /**
+ * @deprecated since 5.2 {@link #deleteUserPermission(DbSession, Long, Long, String)}
+ */
+ @Deprecated
+ public void deleteUserPermission(Long templateId, Long userId, String permission) {
+ DbSession session = myBatis.openSession(false);
+ try {
+ deleteUserPermission(session, templateId, userId, permission);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ public void deleteUserPermission(DbSession session, Long templateId, Long userId, String permission) {
+ PermissionTemplateUserDto permissionTemplateUser = new PermissionTemplateUserDto()
+ .setTemplateId(templateId)
+ .setPermission(permission)
+ .setUserId(userId);
+ mapper(session).deleteUserPermission(permissionTemplateUser);
+ session.commit();
+ }
+
+ /**
+ * @deprecated since 5.2 use {@link #insertGroupPermission(DbSession, Long, Long, String)}
+ */
+ @Deprecated
+ public void insertGroupPermission(Long templateId, @Nullable Long groupId, String permission) {
+ DbSession session = myBatis.openSession(false);
+ try {
+ insertGroupPermission(session, templateId, groupId, permission);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ public void insertGroupPermission(DbSession session, Long templateId, @Nullable Long groupId, String permission) {
+ PermissionTemplateGroupDto permissionTemplateGroup = new PermissionTemplateGroupDto()
+ .setTemplateId(templateId)
+ .setPermission(permission)
+ .setGroupId(groupId)
+ .setCreatedAt(now())
+ .setUpdatedAt(now());
+ mapper(session).insertGroupPermission(permissionTemplateGroup);
+ session.commit();
+ }
+
+ public void insertGroupPermission(DbSession session, PermissionTemplateGroupDto permissionTemplateGroup) {
+ mapper(session).insertGroupPermission(permissionTemplateGroup);
+ }
+
+ /**
+ * @deprecated since 5.2 use {@link #deleteGroupPermission(DbSession, Long, Long, String)}
+ */
+ @Deprecated
+ public void deleteGroupPermission(Long templateId, @Nullable Long groupId, String permission) {
+ DbSession session = myBatis.openSession(false);
+ try {
+ deleteGroupPermission(session, templateId, groupId, permission);
+ } finally {
+ MyBatis.closeQuietly(session);
+ }
+ }
+
+ public void deleteGroupPermission(DbSession session, Long templateId, @Nullable Long groupId, String permission) {
+ PermissionTemplateGroupDto permissionTemplateGroup = new PermissionTemplateGroupDto()
+ .setTemplateId(templateId)
+ .setPermission(permission)
+ .setGroupId(groupId);
+ mapper(session).deleteGroupPermission(permissionTemplateGroup);
+ session.commit();
+ }
+
+ /**
+ * Load permission template and load associated collections of users and groups permissions, and characteristics
+ */
+ public PermissionTemplate selectPermissionTemplateWithPermissions(DbSession session, String templateUuid) {
+ PermissionTemplateDto template = selectByUuid(session, templateUuid);
+ if (template == null) {
+ throw new IllegalArgumentException("Could not retrieve permission template with uuid " + templateUuid);
+ }
+ PermissionTemplate templateWithDependencies = selectByUuidWithUserAndGroupPermissions(session, template.getUuid());
+ if (templateWithDependencies == null) {
+ throw new IllegalArgumentException("Could not retrieve permissions for template with uuid " + templateUuid);
+ }
+ return templateWithDependencies;
+ }
+
+ public PermissionTemplateDto selectByName(DbSession dbSession, String name) {
+ return mapper(dbSession).selectByName(name.toUpperCase(Locale.ENGLISH));
+ }
+
+ public List<String> selectPotentialPermissionsByUserIdAndTemplateId(DbSession dbSession, @Nullable Long currentUserId, long templateId) {
+ return mapper(dbSession).selectPotentialPermissionsByUserIdAndTemplateId(currentUserId, templateId);
+ }
+
+ /**
+ * Remove a group from all templates (used when removing a group)
+ */
+ public void deleteByGroup(SqlSession session, Long groupId) {
+ session.getMapper(PermissionTemplateMapper.class).deleteByGroupId(groupId);
+ }
+
+ private Date now() {
+ return new Date(system.now());
+ }
+
+ private static PermissionTemplateMapper mapper(SqlSession session) {
+ return session.getMapper(PermissionTemplateMapper.class);
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.template;
+
+import java.util.Date;
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
+public class PermissionTemplateDto {
+
+ private Long id;
+ private String name;
+ private String uuid;
+ private String description;
+ private String keyPattern;
+ private Date createdAt;
+ private Date updatedAt;
+
+ public Long getId() {
+ return id;
+ }
+
+ public PermissionTemplateDto setId(Long id) {
+ this.id = id;
+ return this;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public PermissionTemplateDto setName(String name) {
+ this.name = name;
+ return this;
+ }
+
+ /**
+ * @deprecated since 5.2 use {@link #getUuid()}
+ */
+ @Deprecated
+ public String getKee() {
+ return uuid;
+ }
+
+ /**
+ * @deprecated since 5.2 use {@link #setUuid(String)}
+ */
+ @Deprecated
+ public PermissionTemplateDto setKee(String kee) {
+ this.uuid = kee;
+ return this;
+ }
+
+ /**
+ * @since 5.2 the kee column is a proper uuid. Before that it was build on the name + timestamp
+ */
+ public String getUuid() {
+ return uuid;
+ }
+
+ /**
+ * @since 5.2 the kee column is a proper uuid. Before it was build on the name + timestamp
+ */
+ public PermissionTemplateDto setUuid(String uuid) {
+ this.uuid = uuid;
+ return this;
+ }
+
+ @CheckForNull
+ public String getDescription() {
+ return description;
+ }
+
+ public PermissionTemplateDto setDescription(@Nullable String description) {
+ this.description = description;
+ return this;
+ }
+
+ @CheckForNull
+ public String getKeyPattern() {
+ return keyPattern;
+ }
+
+ public PermissionTemplateDto setKeyPattern(@Nullable String regexp) {
+ this.keyPattern = regexp;
+ return this;
+ }
+
+ public Date getCreatedAt() {
+ return createdAt;
+ }
+
+ public PermissionTemplateDto setCreatedAt(Date createdAt) {
+ this.createdAt = createdAt;
+ return this;
+ }
+
+ public Date getUpdatedAt() {
+ return updatedAt;
+ }
+
+ public PermissionTemplateDto setUpdatedAt(Date updatedAt) {
+ this.updatedAt = updatedAt;
+ return this;
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.template;
+
+import java.util.Date;
+import javax.annotation.Nullable;
+
+public class PermissionTemplateGroupDto {
+ private Long id;
+ private Long templateId;
+ private Long groupId;
+ private String permission;
+ private String groupName;
+ private Date createdAt;
+ private Date updatedAt;
+
+ public Long getId() {
+ return id;
+ }
+
+ public PermissionTemplateGroupDto setId(Long id) {
+ this.id = id;
+ return this;
+ }
+
+ public Long getTemplateId() {
+ return templateId;
+ }
+
+ public PermissionTemplateGroupDto setTemplateId(Long templateId) {
+ this.templateId = templateId;
+ return this;
+ }
+
+ public Long getGroupId() {
+ return groupId;
+ }
+
+ public PermissionTemplateGroupDto setGroupId(@Nullable Long groupId) {
+ this.groupId = groupId;
+ return this;
+ }
+
+ public String getPermission() {
+ return permission;
+ }
+
+ public PermissionTemplateGroupDto setPermission(String permission) {
+ this.permission = permission;
+ return this;
+ }
+
+ public String getGroupName() {
+ return groupName;
+ }
+
+ public PermissionTemplateGroupDto setGroupName(String groupName) {
+ this.groupName = groupName;
+ return this;
+ }
+
+ public Date getCreatedAt() {
+ return createdAt;
+ }
+
+ public PermissionTemplateGroupDto setCreatedAt(Date createdAt) {
+ this.createdAt = createdAt;
+ return this;
+ }
+
+ public Date getUpdatedAt() {
+ return updatedAt;
+ }
+
+ public PermissionTemplateGroupDto setUpdatedAt(Date updatedAt) {
+ this.updatedAt = updatedAt;
+ return this;
+ }
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.template;
+
+import java.util.List;
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.session.ResultHandler;
+import org.apache.ibatis.session.RowBounds;
+import org.sonar.db.permission.GroupWithPermissionDto;
+import org.sonar.db.permission.UserWithPermissionDto;
+
+/**
+ * @since 3.7
+ */
+public interface PermissionTemplateMapper {
+
+ void insert(PermissionTemplateDto permissionTemplate);
+
+ void update(PermissionTemplateDto permissionTemplate);
+
+ void delete(long templateId);
+
+ void deleteUserPermissions(long templateId);
+
+ void deleteUserPermission(PermissionTemplateUserDto permissionTemplateUser);
+
+ void deleteGroupPermissions(long templateId);
+
+ void deleteGroupPermission(PermissionTemplateGroupDto permissionTemplateGroup);
+
+ PermissionTemplateDto selectByUuid(String templateUuid);
+
+ List<PermissionTemplateUserDto> selectUserPermissionsByTemplateId(long templateId);
+
+ List<PermissionTemplateGroupDto> selectGroupPermissionsByTemplateId(long templateId);
+
+ void insertUserPermission(PermissionTemplateUserDto permissionTemplateUser);
+
+ void insertGroupPermission(PermissionTemplateGroupDto permissionTemplateGroup);
+
+ void deleteByGroupId(long groupId);
+
+ List<GroupWithPermissionDto> selectGroups(Map<String, Object> params, RowBounds rowBounds);
+
+ List<UserWithPermissionDto> selectUsers(Map<String, Object> params, RowBounds rowBounds);
+
+ PermissionTemplateDto selectByName(String name);
+
+ int countUsers(Map<String, Object> params);
+
+ int countGroups(Map<String, Object> parameters);
+
+ List<PermissionTemplateDto> selectAll(@Param("nameMatch") String nameMatch);
+
+ int countAll(@Param("nameMatch") String nameMatch);
+
+ void usersCountByTemplateIdAndPermission(Map<String, Object> parameters, ResultHandler resultHandler);
+
+ void groupsCountByTemplateIdAndPermission(Map<String, Object> parameters, ResultHandler resultHandler);
+
+ List<String> selectPotentialPermissionsByUserIdAndTemplateId(@Param("userId") @Nullable Long currentUserId, @Param("templateId") long templateId);
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.template;
+
+import java.util.Date;
+
+public class PermissionTemplateUserDto {
+ private Long id;
+ private Long templateId;
+ private Long userId;
+ private String permission;
+ private String userName;
+ private String userLogin;
+ private Date createdAt;
+ private Date updatedAt;
+
+ public Long getId() {
+ return id;
+ }
+
+ public PermissionTemplateUserDto setId(Long id) {
+ this.id = id;
+ return this;
+ }
+
+ public Long getTemplateId() {
+ return templateId;
+ }
+
+ public PermissionTemplateUserDto setTemplateId(Long templateId) {
+ this.templateId = templateId;
+ return this;
+ }
+
+ public Long getUserId() {
+ return userId;
+ }
+
+ public PermissionTemplateUserDto setUserId(Long userId) {
+ this.userId = userId;
+ return this;
+ }
+
+ public String getUserName() {
+ return userName;
+ }
+
+ public PermissionTemplateUserDto setUserName(String userName) {
+ this.userName = userName;
+ return this;
+ }
+
+ public String getUserLogin() {
+ return userLogin;
+ }
+
+ public PermissionTemplateUserDto setUserLogin(String userLogin) {
+ this.userLogin = userLogin;
+ return this;
+ }
+
+ public String getPermission() {
+ return permission;
+ }
+
+ public PermissionTemplateUserDto setPermission(String permission) {
+ this.permission = permission;
+ return this;
+ }
+
+ public Date getCreatedAt() {
+ return createdAt;
+ }
+
+ public PermissionTemplateUserDto setCreatedAt(Date createdAt) {
+ this.createdAt = createdAt;
+ return this;
+ }
+
+ public Date getUpdatedAt() {
+ return updatedAt;
+ }
+
+ public PermissionTemplateUserDto setUpdatedAt(Date updatedAt) {
+ this.updatedAt = updatedAt;
+ return this;
+ }
+}
+++ /dev/null
-<?xml version="1.0" encoding="UTF-8" ?>
-<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
-<mapper namespace="org.sonar.db.permission.PermissionTemplateMapper">
-
- <insert id="insert" parameterType="PermissionTemplate" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
- INSERT INTO permission_templates (name, kee, description, key_pattern, created_at, updated_at)
- VALUES (#{name}, #{kee}, #{description}, #{keyPattern}, #{createdAt}, #{updatedAt})
- </insert>
-
- <update id="update" parameterType="PermissionTemplate">
- UPDATE permission_templates
- SET name = #{name}, description = #{description}, key_pattern = #{keyPattern}, updated_at = #{updatedAt}
- WHERE id = #{id}
- </update>
-
- <delete id="delete" parameterType="long">
- DELETE FROM permission_templates
- WHERE id = #{templateId}
- </delete>
-
- <delete id="deleteUserPermissions" parameterType="long">
- DELETE FROM perm_templates_users
- WHERE template_id = #{templateId}
- </delete>
-
- <delete id="deleteGroupPermissions" parameterType="long">
- DELETE FROM perm_templates_groups
- WHERE template_id = #{templateId}
- </delete>
-
- <insert id="insertUserPermission" parameterType="PermissionTemplateUser">
- INSERT INTO perm_templates_users (template_id, user_id, permission_reference, created_at, updated_at)
- VALUES (#{templateId}, #{userId}, #{permission}, #{createdAt}, #{updatedAt})
- </insert>
-
- <delete id="deleteUserPermission" parameterType="PermissionTemplateUser">
- DELETE FROM perm_templates_users
- WHERE template_id = #{templateId}
- AND user_id = #{userId}
- AND permission_reference = #{permission}
- </delete>
-
- <insert id="insertGroupPermission" parameterType="PermissionTemplateGroup">
- INSERT INTO perm_templates_groups (template_id, group_id, permission_reference, created_at, updated_at)
- VALUES (#{templateId}, #{groupId}, #{permission}, #{createdAt}, #{updatedAt})
- </insert>
-
- <delete id="deleteGroupPermission" parameterType="PermissionTemplateGroup">
- DELETE FROM perm_templates_groups
- WHERE template_id = #{templateId}
- AND permission_reference = #{permission}
- AND
- <choose>
- <when test="groupId != null">
- group_id = #{groupId}
- </when>
- <otherwise>
- group_id IS NULL
- </otherwise>
- </choose>
- </delete>
-
- <delete id="deleteByGroupId" parameterType="long">
- DELETE FROM perm_templates_groups
- WHERE group_id = #{groupId}
- </delete>
-
- <select id="selectUsers" parameterType="map" resultType="UserWithPermission">
- SELECT u.login as login, u.name as name, u.email, ptu.permission_reference as permission
- <include refid="usersSelection"/>
- ORDER BY u.name
- </select>
-
- <select id="countUsers" parameterType="map" resultType="int">
- SELECT count(u.login)
- <include refid="usersSelection"/>
- </select>
-
- <sql id="usersSelection">
- FROM users u
- LEFT JOIN perm_templates_users ptu ON ptu.user_id=u.id
- AND ptu.permission_reference=#{query.permission}
- AND ptu.template_id=#{templateId}
- <where>
- u.active = ${_true}
- <choose>
- <when test="query.membership() == 'IN'">
- AND ptu.permission_reference IS NOT NULL
- </when>
- <when test="query.membership() == 'OUT'">
- AND ptu.permission_reference IS NULL
- </when>
- </choose>
- <if test="query.search() != null">
- AND (UPPER(u.name) LIKE #{query.searchSql} ESCAPE '/')
- </if>
- </where>
- </sql>
-
- <select id="selectGroups" parameterType="map" resultType="GroupWithPermission">
- SELECT name, description, permission FROM
- (SELECT g.name as name, g.description as description, ptg.permission_reference as permission
- FROM groups g
- LEFT JOIN perm_templates_groups ptg ON ptg.group_id=g.id
- AND ptg.permission_reference=#{query.permission}
- AND ptg.template_id=#{templateId}
-
- <if test="!query.permission().equals(projectAdminPermission)">
- UNION
- -- Add Anyone group permission
- SELECT
- #{anyoneGroup} as name,
- NULL as description,
- (select ptg.permission_reference
- FROM perm_templates_groups ptg
- <where>
- AND ptg.permission_reference=#{query.permission}
- AND ptg.template_id=#{templateId}
- AND ptg.group_id IS NULL
- </where>) as permission
- FROM groups g
- </if>
-
- ) groups
- <where>
- <if test="query.search() != null">
- AND (UPPER(groups.name) LIKE #{query.searchSql} ESCAPE '/')
- </if>
- <choose>
- <when test="query.membership() == 'IN'">
- AND groups.permission IS NOT NULL
- </when>
- <when test="query.membership() == 'OUT'">
- AND groups.permission IS NULL
- </when>
- </choose>
- </where>
- ORDER BY groups.name
- </select>
-
- <select id="countGroups" parameterType="map" resultType="int">
- SELECT count(1) FROM
- (SELECT g.name as name, g.description as description, ptg.permission_reference as permission
- FROM groups g
- LEFT JOIN perm_templates_groups ptg ON ptg.group_id=g.id
- AND ptg.permission_reference=#{query.permission}
- AND ptg.template_id=#{templateId}
- UNION
- -- Add Anyone group permission
- SELECT
- #{anyoneGroup} as name,
- NULL as description,
- (select ptg.permission_reference
- FROM perm_templates_groups ptg
- <where>
- AND ptg.permission_reference=#{query.permission}
- AND ptg.template_id=#{templateId}
- AND ptg.group_id IS NULL
- </where>) as permission
- FROM groups g
- ) groups
- <where>
- <if test="groupName!=null">
- AND (UPPER(groups.name) LIKE #{groupName} ESCAPE '/')
- </if>
- <if test="query.search() != null">
- AND (UPPER(groups.name) LIKE #{query.searchSql} ESCAPE '/')
- </if>
- <choose>
- <when test="query.membership() == 'IN'">
- AND groups.permission IS NOT NULL
- </when>
- <when test="query.membership() == 'OUT'">
- AND groups.permission IS NULL
- </when>
- </choose>
- </where>
- </select>
-
- <sql id="templateColumns">
- id, name, kee, description, key_pattern AS keyPattern, created_at AS createdAt, updated_at AS updatedAt
- </sql>
-
- <select id="selectByUuid" parameterType="String" resultType="PermissionTemplate">
- SELECT
- <include refid="templateColumns"/>
- FROM permission_templates
- WHERE kee=#{uuid}
- </select>
-
- <select id="selectAll" parameterType="map" resultType="PermissionTemplate">
- SELECT
- <include refid="templateColumns"/>
- FROM permission_templates
- <where>
- <if test="nameMatch!=null">
- AND (UPPER(name) LIKE #{nameMatch} ESCAPE '/')
- </if>
- </where>
- ORDER BY UPPER(name), name
- </select>
-
- <select id="countAll" parameterType="String" resultType="int">
- SELECT count(1)
- FROM permission_templates
- <where>
- <if test="nameMatch!=null">
- AND (UPPER(name) LIKE #{nameMatch} ESCAPE '/')
- </if>
- </where>
- </select>
-
- <select id="selectByName" parameterType="String" resultType="PermissionTemplate">
- SELECT
- <include refid="templateColumns"/>
- FROM permission_templates
- WHERE UPPER(name)=#{templateName}
- </select>
-
- <select id="selectUserPermissionsByTemplateId" parameterType="Long" resultType="PermissionTemplateUser">
- SELECT
- ptu.id,
- ptu.template_id as templateId,
- ptu.permission_reference AS permission,
- ptu.user_id AS userId,
- u.name AS userName,
- u.login AS userLogin,
- ptu.created_at AS createdAt,
- ptu.updated_at AS updatedAt
- FROM perm_templates_users ptu
- INNER JOIN users u ON u.id = ptu.user_id AND u.active = ${_true}
- WHERE ptu.template_id = #{templateId}
- </select>
-
- <select id="selectGroupPermissionsByTemplateId" parameterType="Long" resultType="PermissionTemplateGroup">
- SELECT
- ptg.id,
- ptg.template_id as templateId,
- ptg.permission_reference AS permission,
- ptg.group_id AS groupId,
- g.name AS groupName,
- ptg.created_at as createdAt,
- ptg.updated_at as updatedAt
- FROM perm_templates_groups ptg
- LEFT OUTER JOIN groups g ON g.id=ptg.group_id
- WHERE ptg.template_id=#{templateId}
- AND (g.name IS NOT NULL OR ptg.group_id IS NULL)
- </select>
-
- <select id="selectPotentialPermissionsByUserIdAndTemplateId" parameterType="map" resultType="String">
- <if test="userId!=null">
- -- from template users
- select ptu.permission_reference as permission_key
- from perm_templates_users ptu
- <where>
- and ptu.user_id=#{userId}
- and ptu.template_id=#{templateId}
- </where>
- UNION
- -- from template groups except anyone group
- select ptg.permission_reference as permission_key
- from perm_templates_groups ptg
- inner join groups_users gu on ptg.group_id = gu.group_id
- <where>
- and gu.user_id=#{userId}
- and ptg.template_id=#{templateId}
- </where>
- UNION
- -- from template characteristics
- select ptc.permission_key as permission_key
- from perm_tpl_characteristics ptc
- <where>
- and with_project_creator = ${_true}
- and ptc.template_id = #{templateId}
- </where>
- UNION
- </if>
- -- from anyone group
- select ptg.permission_reference as permission_key
- from perm_templates_groups ptg
- where ptg.template_id=#{templateId}
- and ptg.group_id IS NULL
- </select>
-
- <select id="usersCountByTemplateIdAndPermission" parameterType="map"
- resultType="org.sonar.db.permission.CountByTemplateAndPermissionDto">
- SELECT ptu.template_id as templateId, ptu.permission_reference as permission, count(u.login) as count
- FROM users u
- INNER JOIN perm_templates_users ptu ON ptu.user_id=u.id
- AND ptu.template_id in
- <foreach collection="templateIds" open="(" close=")" item="id" separator=",">
- #{id}
- </foreach>
- <where>
- AND u.active = ${_true}
- </where>
- GROUP BY ptu.template_id, ptu.permission_reference
- </select>
-
- <select id="groupsCountByTemplateIdAndPermission" parameterType="map"
- resultType="org.sonar.db.permission.CountByTemplateAndPermissionDto">
- SELECT count(1) as count, permission, templateId
- FROM
- (SELECT g.name as name, ptg.permission_reference as permission, ptg.template_id as templateId
- FROM groups g
- INNER JOIN perm_templates_groups ptg ON ptg.group_id=g.id
- UNION
- -- Add Anyone group permission
- SELECT #{anyoneGroup} as name, ptg.permission_reference as permission, ptg.template_id as templateId
- FROM perm_templates_groups ptg
- <where>
- AND ptg.group_id IS NULL
- </where>
- ) groups
- <where>
- AND groups.templateId in
- <foreach collection="templateIds" open="(" close=")" item="id" separator=",">
- #{id}
- </foreach>
- </where>
- GROUP BY groups.permission, groups.templateId
- </select>
-</mapper>
--- /dev/null
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="org.sonar.db.permission.template.PermissionTemplateMapper">
+
+ <insert id="insert" parameterType="PermissionTemplate" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
+ INSERT INTO permission_templates (name, kee, description, key_pattern, created_at, updated_at)
+ VALUES (#{name}, #{kee}, #{description}, #{keyPattern}, #{createdAt}, #{updatedAt})
+ </insert>
+
+ <update id="update" parameterType="PermissionTemplate">
+ UPDATE permission_templates
+ SET name = #{name}, description = #{description}, key_pattern = #{keyPattern}, updated_at = #{updatedAt}
+ WHERE id = #{id}
+ </update>
+
+ <delete id="delete" parameterType="long">
+ DELETE FROM permission_templates
+ WHERE id = #{templateId}
+ </delete>
+
+ <delete id="deleteUserPermissions" parameterType="long">
+ DELETE FROM perm_templates_users
+ WHERE template_id = #{templateId}
+ </delete>
+
+ <delete id="deleteGroupPermissions" parameterType="long">
+ DELETE FROM perm_templates_groups
+ WHERE template_id = #{templateId}
+ </delete>
+
+ <insert id="insertUserPermission" parameterType="PermissionTemplateUser">
+ INSERT INTO perm_templates_users (template_id, user_id, permission_reference, created_at, updated_at)
+ VALUES (#{templateId}, #{userId}, #{permission}, #{createdAt}, #{updatedAt})
+ </insert>
+
+ <delete id="deleteUserPermission" parameterType="PermissionTemplateUser">
+ DELETE FROM perm_templates_users
+ WHERE template_id = #{templateId}
+ AND user_id = #{userId}
+ AND permission_reference = #{permission}
+ </delete>
+
+ <insert id="insertGroupPermission" parameterType="PermissionTemplateGroup">
+ INSERT INTO perm_templates_groups (template_id, group_id, permission_reference, created_at, updated_at)
+ VALUES (#{templateId}, #{groupId}, #{permission}, #{createdAt}, #{updatedAt})
+ </insert>
+
+ <delete id="deleteGroupPermission" parameterType="PermissionTemplateGroup">
+ DELETE FROM perm_templates_groups
+ WHERE template_id = #{templateId}
+ AND permission_reference = #{permission}
+ AND
+ <choose>
+ <when test="groupId != null">
+ group_id = #{groupId}
+ </when>
+ <otherwise>
+ group_id IS NULL
+ </otherwise>
+ </choose>
+ </delete>
+
+ <delete id="deleteByGroupId" parameterType="long">
+ DELETE FROM perm_templates_groups
+ WHERE group_id = #{groupId}
+ </delete>
+
+ <select id="selectUsers" parameterType="map" resultType="UserWithPermission">
+ SELECT u.login as login, u.name as name, u.email, ptu.permission_reference as permission
+ <include refid="usersSelection"/>
+ ORDER BY u.name
+ </select>
+
+ <select id="countUsers" parameterType="map" resultType="int">
+ SELECT count(u.login)
+ <include refid="usersSelection"/>
+ </select>
+
+ <sql id="usersSelection">
+ FROM users u
+ LEFT JOIN perm_templates_users ptu ON ptu.user_id=u.id
+ AND ptu.permission_reference=#{query.permission}
+ AND ptu.template_id=#{templateId}
+ <where>
+ u.active = ${_true}
+ <choose>
+ <when test="query.membership() == 'IN'">
+ AND ptu.permission_reference IS NOT NULL
+ </when>
+ <when test="query.membership() == 'OUT'">
+ AND ptu.permission_reference IS NULL
+ </when>
+ </choose>
+ <if test="query.search() != null">
+ AND (UPPER(u.name) LIKE #{query.searchSql} ESCAPE '/')
+ </if>
+ </where>
+ </sql>
+
+ <select id="selectGroups" parameterType="map" resultType="GroupWithPermission">
+ SELECT name, description, permission FROM
+ (SELECT g.name as name, g.description as description, ptg.permission_reference as permission
+ FROM groups g
+ LEFT JOIN perm_templates_groups ptg ON ptg.group_id=g.id
+ AND ptg.permission_reference=#{query.permission}
+ AND ptg.template_id=#{templateId}
+
+ <if test="!query.permission().equals(projectAdminPermission)">
+ UNION
+ -- Add Anyone group permission
+ SELECT
+ #{anyoneGroup} as name,
+ NULL as description,
+ (select ptg.permission_reference
+ FROM perm_templates_groups ptg
+ <where>
+ AND ptg.permission_reference=#{query.permission}
+ AND ptg.template_id=#{templateId}
+ AND ptg.group_id IS NULL
+ </where>) as permission
+ FROM groups g
+ </if>
+
+ ) groups
+ <where>
+ <if test="query.search() != null">
+ AND (UPPER(groups.name) LIKE #{query.searchSql} ESCAPE '/')
+ </if>
+ <choose>
+ <when test="query.membership() == 'IN'">
+ AND groups.permission IS NOT NULL
+ </when>
+ <when test="query.membership() == 'OUT'">
+ AND groups.permission IS NULL
+ </when>
+ </choose>
+ </where>
+ ORDER BY groups.name
+ </select>
+
+ <select id="countGroups" parameterType="map" resultType="int">
+ SELECT count(1) FROM
+ (SELECT g.name as name, g.description as description, ptg.permission_reference as permission
+ FROM groups g
+ LEFT JOIN perm_templates_groups ptg ON ptg.group_id=g.id
+ AND ptg.permission_reference=#{query.permission}
+ AND ptg.template_id=#{templateId}
+ UNION
+ -- Add Anyone group permission
+ SELECT
+ #{anyoneGroup} as name,
+ NULL as description,
+ (select ptg.permission_reference
+ FROM perm_templates_groups ptg
+ <where>
+ AND ptg.permission_reference=#{query.permission}
+ AND ptg.template_id=#{templateId}
+ AND ptg.group_id IS NULL
+ </where>) as permission
+ FROM groups g
+ ) groups
+ <where>
+ <if test="groupName!=null">
+ AND (UPPER(groups.name) LIKE #{groupName} ESCAPE '/')
+ </if>
+ <if test="query.search() != null">
+ AND (UPPER(groups.name) LIKE #{query.searchSql} ESCAPE '/')
+ </if>
+ <choose>
+ <when test="query.membership() == 'IN'">
+ AND groups.permission IS NOT NULL
+ </when>
+ <when test="query.membership() == 'OUT'">
+ AND groups.permission IS NULL
+ </when>
+ </choose>
+ </where>
+ </select>
+
+ <sql id="templateColumns">
+ id, name, kee, description, key_pattern AS keyPattern, created_at AS createdAt, updated_at AS updatedAt
+ </sql>
+
+ <select id="selectByUuid" parameterType="String" resultType="PermissionTemplate">
+ SELECT
+ <include refid="templateColumns"/>
+ FROM permission_templates
+ WHERE kee=#{uuid}
+ </select>
+
+ <select id="selectAll" parameterType="map" resultType="PermissionTemplate">
+ SELECT
+ <include refid="templateColumns"/>
+ FROM permission_templates
+ <where>
+ <if test="nameMatch!=null">
+ AND (UPPER(name) LIKE #{nameMatch} ESCAPE '/')
+ </if>
+ </where>
+ ORDER BY UPPER(name), name
+ </select>
+
+ <select id="countAll" parameterType="String" resultType="int">
+ SELECT count(1)
+ FROM permission_templates
+ <where>
+ <if test="nameMatch!=null">
+ AND (UPPER(name) LIKE #{nameMatch} ESCAPE '/')
+ </if>
+ </where>
+ </select>
+
+ <select id="selectByName" parameterType="String" resultType="PermissionTemplate">
+ SELECT
+ <include refid="templateColumns"/>
+ FROM permission_templates
+ WHERE UPPER(name)=#{templateName}
+ </select>
+
+ <select id="selectUserPermissionsByTemplateId" parameterType="Long" resultType="PermissionTemplateUser">
+ SELECT
+ ptu.id,
+ ptu.template_id as templateId,
+ ptu.permission_reference AS permission,
+ ptu.user_id AS userId,
+ u.name AS userName,
+ u.login AS userLogin,
+ ptu.created_at AS createdAt,
+ ptu.updated_at AS updatedAt
+ FROM perm_templates_users ptu
+ INNER JOIN users u ON u.id = ptu.user_id AND u.active = ${_true}
+ WHERE ptu.template_id = #{templateId}
+ </select>
+
+ <select id="selectGroupPermissionsByTemplateId" parameterType="Long" resultType="PermissionTemplateGroup">
+ SELECT
+ ptg.id,
+ ptg.template_id as templateId,
+ ptg.permission_reference AS permission,
+ ptg.group_id AS groupId,
+ g.name AS groupName,
+ ptg.created_at as createdAt,
+ ptg.updated_at as updatedAt
+ FROM perm_templates_groups ptg
+ LEFT OUTER JOIN groups g ON g.id=ptg.group_id
+ WHERE ptg.template_id=#{templateId}
+ AND (g.name IS NOT NULL OR ptg.group_id IS NULL)
+ </select>
+
+ <select id="selectPotentialPermissionsByUserIdAndTemplateId" parameterType="map" resultType="String">
+ <if test="userId!=null">
+ -- from template users
+ select ptu.permission_reference as permission_key
+ from perm_templates_users ptu
+ <where>
+ and ptu.user_id=#{userId}
+ and ptu.template_id=#{templateId}
+ </where>
+ UNION
+ -- from template groups except anyone group
+ select ptg.permission_reference as permission_key
+ from perm_templates_groups ptg
+ inner join groups_users gu on ptg.group_id = gu.group_id
+ <where>
+ and gu.user_id=#{userId}
+ and ptg.template_id=#{templateId}
+ </where>
+ UNION
+ -- from template characteristics
+ select ptc.permission_key as permission_key
+ from perm_tpl_characteristics ptc
+ <where>
+ and with_project_creator = ${_true}
+ and ptc.template_id = #{templateId}
+ </where>
+ UNION
+ </if>
+ -- from anyone group
+ select ptg.permission_reference as permission_key
+ from perm_templates_groups ptg
+ where ptg.template_id=#{templateId}
+ and ptg.group_id IS NULL
+ </select>
+
+ <select id="usersCountByTemplateIdAndPermission" parameterType="map"
+ resultType="org.sonar.db.permission.template.CountByTemplateAndPermissionDto">
+ SELECT ptu.template_id as templateId, ptu.permission_reference as permission, count(u.login) as count
+ FROM users u
+ INNER JOIN perm_templates_users ptu ON ptu.user_id=u.id
+ AND ptu.template_id in
+ <foreach collection="templateIds" open="(" close=")" item="id" separator=",">
+ #{id}
+ </foreach>
+ <where>
+ AND u.active = ${_true}
+ </where>
+ GROUP BY ptu.template_id, ptu.permission_reference
+ </select>
+
+ <select id="groupsCountByTemplateIdAndPermission" parameterType="map"
+ resultType="org.sonar.db.permission.template.CountByTemplateAndPermissionDto">
+ SELECT count(1) as count, permission, templateId
+ FROM
+ (SELECT g.name as name, ptg.permission_reference as permission, ptg.template_id as templateId
+ FROM groups g
+ INNER JOIN perm_templates_groups ptg ON ptg.group_id=g.id
+ UNION
+ -- Add Anyone group permission
+ SELECT #{anyoneGroup} as name, ptg.permission_reference as permission, ptg.template_id as templateId
+ FROM perm_templates_groups ptg
+ <where>
+ AND ptg.group_id IS NULL
+ </where>
+ ) groups
+ <where>
+ AND groups.templateId in
+ <foreach collection="templateIds" open="(" close=")" item="id" separator=",">
+ #{id}
+ </foreach>
+ </where>
+ GROUP BY groups.permission, groups.templateId
+ </select>
+</mapper>
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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 java.util.List;
-import org.junit.Rule;
-import org.junit.Test;
-import org.sonar.api.utils.System2;
-import org.sonar.api.web.UserRole;
-import org.sonar.db.DbSession;
-import org.sonar.db.DbTester;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-
-public class GroupWithPermissionTemplateDaoTest {
-
- private static final long TEMPLATE_ID = 50L;
-
- @Rule
- public DbTester dbTester = DbTester.create(System2.INSTANCE);
- DbSession session = dbTester.getSession();
-
- PermissionTemplateDao underTest = dbTester.getDbClient().permissionTemplateDao();
-
- @Test
- public void select_groups() {
- dbTester.prepareDbUnit(getClass(), "groups_with_permissions.xml");
-
- OldPermissionQuery query = OldPermissionQuery.builder().permission("user").build();
- List<GroupWithPermissionDto> result = underTest.selectGroups(session, query, TEMPLATE_ID);
- int count = underTest.countGroups(session, query, TEMPLATE_ID);
-
- assertThat(result).hasSize(4);
- assertThat(count).isEqualTo(4);
-
- GroupWithPermissionDto anyone = result.get(0);
- assertThat(anyone.getName()).isEqualTo("Anyone");
- assertThat(anyone.getDescription()).isNull();
- assertThat(anyone.getPermission()).isNotNull();
-
- GroupWithPermissionDto group1 = result.get(1);
- assertThat(group1.getName()).isEqualTo("sonar-administrators");
- assertThat(group1.getDescription()).isEqualTo("System administrators");
- assertThat(group1.getPermission()).isNotNull();
-
- GroupWithPermissionDto group2 = result.get(2);
- assertThat(group2.getName()).isEqualTo("sonar-reviewers");
- assertThat(group2.getDescription()).isEqualTo("Reviewers");
- assertThat(group2.getPermission()).isNull();
-
- GroupWithPermissionDto group3 = result.get(3);
- assertThat(group3.getName()).isEqualTo("sonar-users");
- assertThat(group3.getDescription()).isEqualTo("Any new users created will automatically join this group");
- assertThat(group3.getPermission()).isNotNull();
- }
-
- @Test
- public void anyone_group_is_returned_when_it_has_no_permission() {
- dbTester.prepareDbUnit(getClass(), "groups_with_permissions.xml");
-
- // Anyone group is returned even if it doesn't have the permission
- OldPermissionQuery query = OldPermissionQuery.builder().permission(UserRole.USER).build();
- List<GroupWithPermissionDto> result = underTest.selectGroups(session, query, TEMPLATE_ID);
- assertThat(result).hasSize(4);
-
- GroupWithPermissionDto group1 = result.get(1);
- assertThat(group1.getName()).isEqualTo("sonar-administrators");
- assertThat(group1.getPermission()).isNotNull();
-
- GroupWithPermissionDto group2 = result.get(2);
- assertThat(group2.getName()).isEqualTo("sonar-reviewers");
- assertThat(group2.getPermission()).isNull();
-
- GroupWithPermissionDto group3 = result.get(3);
- assertThat(group3.getName()).isEqualTo("sonar-users");
- assertThat(group3.getPermission()).isNotNull();
- }
-
- @Test
- public void search_by_groups_name() {
- dbTester.prepareDbUnit(getClass(), "groups_with_permissions.xml");
-
- List<GroupWithPermissionDto> result = underTest.selectGroups(session, OldPermissionQuery.builder().permission("user").search("aDMini").build(), TEMPLATE_ID);
- assertThat(result).hasSize(1);
- assertThat(result.get(0).getName()).isEqualTo("sonar-administrators");
-
- result = underTest.selectGroups(session, OldPermissionQuery.builder().permission("user").search("sonar").build(), TEMPLATE_ID);
- assertThat(result).hasSize(3);
- }
-
- @Test
- public void search_groups_should_be_sorted_by_group_name() {
- dbTester.prepareDbUnit(getClass(), "groups_with_permissions_should_be_sorted_by_group_name.xml");
-
- List<GroupWithPermissionDto> result = underTest.selectGroups(session, OldPermissionQuery.builder().permission("user").build(), TEMPLATE_ID);
- assertThat(result).hasSize(4);
- assertThat(result.get(0).getName()).isEqualTo("Anyone");
- assertThat(result.get(1).getName()).isEqualTo("sonar-administrators");
- assertThat(result.get(2).getName()).isEqualTo("sonar-reviewers");
- assertThat(result.get(3).getName()).isEqualTo("sonar-users");
- }
-
-}
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.permission.template.PermissionTemplateDbTester;
+import org.sonar.db.permission.template.PermissionTemplateDto;
import org.sonar.db.user.GroupDbTester;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.RoleDao;
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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 java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Date;
-import java.util.List;
-import org.apache.ibatis.session.ResultContext;
-import org.apache.ibatis.session.ResultHandler;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.api.utils.System2;
-import org.sonar.api.web.UserRole;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.DbTester;
-import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
-import org.sonar.db.permission.template.PermissionTemplateDbTester;
-import org.sonar.db.user.GroupDbTester;
-import org.sonar.db.user.GroupDto;
-import org.sonar.db.user.UserDbTester;
-import org.sonar.db.user.UserDto;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.tuple;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-import static org.sonar.api.web.UserRole.ADMIN;
-import static org.sonar.api.web.UserRole.CODEVIEWER;
-import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
-import static org.sonar.api.web.UserRole.USER;
-import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
-import static org.sonar.db.user.GroupTesting.newGroupDto;
-import static org.sonar.db.user.UserTesting.newUserDto;
-
-public class PermissionTemplateDaoTest {
-
- System2 system = mock(System2.class);
-
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
- @Rule
- public DbTester db = DbTester.create(system);
- DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
- GroupDbTester groupDb = new GroupDbTester(db);
- UserDbTester userDb = new UserDbTester(db);
- PermissionTemplateDbTester templateDb = new PermissionTemplateDbTester(db);
-
- PermissionTemplateDao underTest = new PermissionTemplateDao(db.myBatis(), system);
-
- @Test
- public void should_create_permission_template() throws ParseException {
- db.prepareDbUnit(getClass(), "createPermissionTemplate.xml");
-
- Date now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2013-01-02 01:04:05");
- when(system.now()).thenReturn(now.getTime());
-
- PermissionTemplateDto permissionTemplate = underTest.insert(db.getSession(), newPermissionTemplateDto()
- .setName("my template")
- .setDescription("my description")
- .setKeyPattern("myregexp"));
- assertThat(permissionTemplate).isNotNull();
- assertThat(permissionTemplate.getId()).isEqualTo(1L);
-
- db.assertDbUnitTable(getClass(), "createPermissionTemplate-result.xml", "permission_templates", "id", "name", "description");
- }
-
- @Test
- public void should_select_permission_template() {
- db.prepareDbUnit(getClass(), "selectPermissionTemplate.xml");
-
- PermissionTemplate result = underTest.selectByUuidWithUserAndGroupPermissions("my_template_20130102_030405");
-
- assertThat(result).isNotNull();
- PermissionTemplateDto template = result.getTemplate();
- assertThat(template.getName()).isEqualTo("my template");
- assertThat(template.getUuid()).isEqualTo("my_template_20130102_030405");
- assertThat(template.getDescription()).isEqualTo("my description");
- List<PermissionTemplateUserDto> usersPermissions = result.getUserPermissions();
- assertThat(usersPermissions).hasSize(3);
- assertThat(usersPermissions).extracting("userId").containsOnly(1L, 2L, 1L);
- assertThat(usersPermissions).extracting("userLogin").containsOnly("login1", "login2", "login2");
- assertThat(usersPermissions).extracting("userName").containsOnly("user1", "user2", "user2");
- assertThat(usersPermissions).extracting("permission").containsOnly("user_permission1", "user_permission1", "user_permission2");
- List<PermissionTemplateGroupDto> groupsPermissions = result.getGroupPermissions();
- assertThat(groupsPermissions).hasSize(3);
- assertThat(groupsPermissions).extracting("groupId").containsOnly(1L, 2L, null);
- assertThat(groupsPermissions).extracting("groupName").containsOnly("group1", "group2", null);
- assertThat(groupsPermissions).extracting("permission").containsOnly("group_permission1", "group_permission1", "group_permission2");
- }
-
- @Test
- public void should_select_permission_template_by_key() {
- db.prepareDbUnit(getClass(), "selectPermissionTemplate.xml");
-
- PermissionTemplateDto permissionTemplate = underTest.selectByUuid("my_template_20130102_030405");
-
- assertThat(permissionTemplate).isNotNull();
- assertThat(permissionTemplate.getId()).isEqualTo(1L);
- assertThat(permissionTemplate.getName()).isEqualTo("my template");
- assertThat(permissionTemplate.getUuid()).isEqualTo("my_template_20130102_030405");
- assertThat(permissionTemplate.getDescription()).isEqualTo("my description");
- }
-
- @Test
- public void should_select_all_permission_templates() {
- db.prepareDbUnit(getClass(), "selectAllPermissionTemplates.xml");
- commit();
-
- List<PermissionTemplateDto> permissionTemplates = underTest.selectAll();
-
- assertThat(permissionTemplates).hasSize(3);
- assertThat(permissionTemplates).extracting("id").containsOnly(1L, 2L, 3L);
- assertThat(permissionTemplates).extracting("name").containsOnly("template1", "template2", "template3");
- assertThat(permissionTemplates).extracting("kee").containsOnly("template1_20130102_030405", "template2_20130102_030405", "template3_20130102_030405");
- assertThat(permissionTemplates).extracting("description").containsOnly("description1", "description2", "description3");
- }
-
- @Test
- public void should_update_permission_template() {
- db.prepareDbUnit(getClass(), "updatePermissionTemplate.xml");
-
- underTest.update(1L, "new_name", "new_description", "new_regexp");
-
- db.assertDbUnitTable(getClass(), "updatePermissionTemplate-result.xml", "permission_templates", "id", "name", "kee", "description");
- }
-
- @Test
- public void should_delete_permission_template() {
- db.prepareDbUnit(getClass(), "deletePermissionTemplate.xml");
-
- underTest.deleteById(dbSession, 1L);
- dbSession.commit();
-
- checkTemplateTables("deletePermissionTemplate-result.xml");
- db.assertDbUnitTable(getClass(), "deletePermissionTemplate-result.xml", "perm_tpl_characteristics");
- }
-
- @Test
- public void should_add_user_permission_to_template() {
- db.prepareDbUnit(getClass(), "addUserPermissionToTemplate.xml");
-
- underTest.insertUserPermission(1L, 1L, "new_permission");
-
- checkTemplateTables("addUserPermissionToTemplate-result.xml");
- }
-
- @Test
- public void should_remove_user_permission_from_template() {
- db.prepareDbUnit(getClass(), "removeUserPermissionFromTemplate.xml");
-
- underTest.deleteUserPermission(1L, 2L, "permission_to_remove");
-
- checkTemplateTables("removeUserPermissionFromTemplate-result.xml");
- }
-
- @Test
- public void should_add_group_permission_to_template() {
- db.prepareDbUnit(getClass(), "addGroupPermissionToTemplate.xml");
-
- underTest.insertGroupPermission(1L, 1L, "new_permission");
-
- checkTemplateTables("addGroupPermissionToTemplate-result.xml");
- }
-
- @Test
- public void should_remove_group_permission_from_template() {
- db.prepareDbUnit(getClass(), "removeGroupPermissionFromTemplate.xml");
-
- underTest.deleteGroupPermission(1L, 2L, "permission_to_remove");
-
- checkTemplateTables("removeGroupPermissionFromTemplate-result.xml");
- }
-
- @Test
- public void remove_by_group() {
- db.prepareDbUnit(getClass(), "remove_by_group.xml");
-
- underTest.deleteByGroup(db.getSession(), 2L);
- db.getSession().commit();
-
- db.assertDbUnitTable(getClass(), "remove_by_group-result.xml", "permission_templates", "id", "name", "kee", "description");
- }
-
- @Test
- public void should_add_group_permission_with_null_name() {
- db.prepareDbUnit(getClass(), "addNullGroupPermissionToTemplate.xml");
-
- underTest.insertGroupPermission(1L, null, "new_permission");
-
- checkTemplateTables("addNullGroupPermissionToTemplate-result.xml");
- }
-
- @Test
- public void should_remove_group_permission_with_null_name() {
- db.prepareDbUnit(getClass(), "removeNullGroupPermissionFromTemplate.xml");
-
- underTest.deleteGroupPermission(1L, null, "permission_to_remove");
-
- checkTemplateTables("removeNullGroupPermissionFromTemplate-result.xml");
- }
-
- @Test
- public void new_permission_template_with_empty_user_group_characteristics() {
- PermissionTemplateDto template = underTest.insert(dbSession, newPermissionTemplateDto().setUuid("TEMPLATE_UUID"));
-
- PermissionTemplate result = underTest.selectByUuidWithUserAndGroupPermissions(dbSession, "TEMPLATE_UUID");
-
- assertThat(result.getTemplate())
- .extracting(PermissionTemplateDto::getId, PermissionTemplateDto::getUuid, PermissionTemplateDto::getName, PermissionTemplateDto::getDescription)
- .containsExactly(template.getId(), template.getUuid(), template.getName(), template.getDescription());
-
- assertThat(result.getUserPermissions()).isEmpty();
- assertThat(result.getGroupPermissions()).isEmpty();
- assertThat(result.getCharacteristics()).isEmpty();
- }
-
- @Test
- public void unknown_permission_template() {
- PermissionTemplate result = underTest.selectByUuidWithUserAndGroupPermissions(dbSession, "UNKNOWN_TEMPLATE_UUID");
-
- assertThat(result).isNull();
- }
-
- @Test
- public void permission_template_with_user_group_and_characteristics() {
- PermissionTemplateDto template = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("TEMPLATE_UUID"));
- GroupDto group = groupDb.insertGroup(newGroupDto());
- UserDto user = userDb.insertUser(newUserDto());
- templateDb.addGroupToTemplate(template.getId(), group.getId(), UserRole.ADMIN);
- templateDb.addUserToTemplate(template.getId(), user.getId(), UserRole.CODEVIEWER);
- templateDb.addProjectCreatorToTemplate(template.getId(), UserRole.USER);
-
- PermissionTemplate result = underTest.selectByUuidWithUserAndGroupPermissions(dbSession, "TEMPLATE_UUID");
- assertThat(result.getTemplate())
- .extracting(PermissionTemplateDto::getId, PermissionTemplateDto::getUuid, PermissionTemplateDto::getName, PermissionTemplateDto::getDescription)
- .containsExactly(template.getId(), template.getUuid(), template.getName(), template.getDescription());
- assertThat(result.getCharacteristics()).hasSize(1)
- .extracting(PermissionTemplateCharacteristicDto::getPermission, PermissionTemplateCharacteristicDto::getWithProjectCreator)
- .containsExactly(tuple(UserRole.USER, true));
- assertThat(result.getGroupPermissions()).hasSize(1)
- .extracting(PermissionTemplateGroupDto::getGroupId, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
- .containsExactly(tuple(group.getId(), group.getName(), UserRole.ADMIN));
- assertThat(result.getUserPermissions()).hasSize(1)
- .extracting(PermissionTemplateUserDto::getUserId, PermissionTemplateUserDto::getUserLogin, PermissionTemplateUserDto::getPermission)
- .containsExactly(tuple(user.getId(), user.getLogin(), UserRole.CODEVIEWER));
- }
-
- @Test
- public void should_fail_on_unmatched_template() {
- expectedException.expect(IllegalArgumentException.class);
-
- underTest.selectPermissionTemplateWithPermissions(db.getSession(), "unmatched");
- }
-
- @Test
- public void group_count_by_template_and_permission() {
- PermissionTemplateDto template1 = templateDb.insertTemplate();
- PermissionTemplateDto template2 = templateDb.insertTemplate();
- PermissionTemplateDto template3 = templateDb.insertTemplate();
-
- GroupDto group1 = groupDb.insertGroup();
- GroupDto group2 = groupDb.insertGroup();
- GroupDto group3 = groupDb.insertGroup();
-
- templateDb.addGroupToTemplate(42L, group1.getId(), ISSUE_ADMIN);
- templateDb.addGroupToTemplate(template1.getId(), group1.getId(), CODEVIEWER);
- templateDb.addGroupToTemplate(template1.getId(), group2.getId(), CODEVIEWER);
- templateDb.addGroupToTemplate(template1.getId(), group3.getId(), CODEVIEWER);
- templateDb.addGroupToTemplate(template1.getId(), null, CODEVIEWER);
- templateDb.addGroupToTemplate(template1.getId(), group1.getId(), ADMIN);
- templateDb.addGroupToTemplate(template2.getId(), group1.getId(), ADMIN);
-
- final List<CountByTemplateAndPermissionDto> result = new ArrayList<>();
- underTest.groupsCountByTemplateIdAndPermission(dbSession, Arrays.asList(template1.getId(), template2.getId(), template3.getId()), new ResultHandler() {
- @Override
- public void handleResult(ResultContext context) {
- result.add((CountByTemplateAndPermissionDto) context.getResultObject());
- }
- });
-
- assertThat(result).hasSize(3);
- assertThat(result).extracting("permission").containsOnly(ADMIN, CODEVIEWER);
- assertThat(result).extracting("templateId").containsOnly(template1.getId(), template2.getId());
- assertThat(result).extracting("count").containsOnly(4, 1);
- }
-
- @Test
- public void user_count_by_template_and_permission() {
- PermissionTemplateDto template1 = templateDb.insertTemplate();
- PermissionTemplateDto template2 = templateDb.insertTemplate();
- PermissionTemplateDto template3 = templateDb.insertTemplate();
-
- UserDto user1 = userDb.insertUser();
- UserDto user2 = userDb.insertUser();
- UserDto user3 = userDb.insertUser();
-
- templateDb.addUserToTemplate(42L, user1.getId(), ISSUE_ADMIN);
- templateDb.addUserToTemplate(template1.getId(), user1.getId(), ADMIN);
- templateDb.addUserToTemplate(template1.getId(), user2.getId(), ADMIN);
- templateDb.addUserToTemplate(template1.getId(), user3.getId(), ADMIN);
- templateDb.addUserToTemplate(template1.getId(), user1.getId(), USER);
- templateDb.addUserToTemplate(template2.getId(), user1.getId(), USER);
-
- final List<CountByTemplateAndPermissionDto> result = new ArrayList<>();
- underTest.usersCountByTemplateIdAndPermission(dbSession, Arrays.asList(template1.getId(), template2.getId(), template3.getId()),
- context -> result.add((CountByTemplateAndPermissionDto) context.getResultObject()));
- assertThat(result).hasSize(3);
- assertThat(result).extracting("permission").containsOnly(ADMIN, USER);
- assertThat(result).extracting("templateId").containsOnly(template1.getId(), template2.getId());
- assertThat(result).extracting("count").containsOnly(3, 1);
- }
-
- @Test
- public void select_by_name_query_and_pagination() {
- templateDb.insertTemplate(newPermissionTemplateDto().setName("aaabbb"));
- templateDb.insertTemplate(newPermissionTemplateDto().setName("aaaccc"));
-
- List<PermissionTemplateDto> templates = underTest.selectAll(dbSession, "aaa");
- int count = underTest.countAll(dbSession, "aaa");
-
- assertThat(templates.get(0).getName()).isEqualTo("aaabbb");
- assertThat(count).isEqualTo(2);
- }
-
- @Test
- public void selectPotentialPermissions_with_unknown_template_and_no_user() {
- List<String> result = underTest.selectPotentialPermissionsByUserIdAndTemplateId(dbSession, null, 42L);
-
- assertThat(result).isEmpty();
- }
-
- @Test
- public void selectPotentialPermissions_with_empty_template_and_new_user() {
- UserDto user = userDb.insertUser();
- PermissionTemplateDto template = templateDb.insertTemplate();
-
- List<String> result = underTest.selectPotentialPermissionsByUserIdAndTemplateId(dbSession, user.getId(), template.getId());
-
- assertThat(result).isEmpty();
- }
-
- @Test
- public void selectPotentialPermission_with_template_users_groups_and_project_creator() {
- UserDto user = userDb.insertUser();
- GroupDto group = groupDb.insertGroup();
- groupDb.addUserToGroup(user.getId(), group.getId());
- PermissionTemplateDto template = templateDb.insertTemplate();
- templateDb.addProjectCreatorToTemplate(template.getId(), SCAN_EXECUTION);
- templateDb.addProjectCreatorToTemplate(template.getId(), UserRole.ADMIN);
- templateDb.addUserToTemplate(template.getId(), user.getId(), UserRole.USER);
- templateDb.addUserToTemplate(template.getId(), user.getId(), UserRole.ADMIN);
- templateDb.addGroupToTemplate(template.getId(), group.getId(), UserRole.CODEVIEWER);
- templateDb.addGroupToTemplate(template.getId(), group.getId(), UserRole.ADMIN);
- templateDb.addGroupToTemplate(template.getId(), null, UserRole.ISSUE_ADMIN);
-
- List<String> resultWithUser = underTest.selectPotentialPermissionsByUserIdAndTemplateId(dbSession, user.getId(), template.getId());
- List<String> resultWithoutUser = underTest.selectPotentialPermissionsByUserIdAndTemplateId(dbSession, null, template.getId());
-
- assertThat(resultWithUser).containsOnlyOnce(SCAN_EXECUTION, UserRole.ADMIN, UserRole.USER, UserRole.CODEVIEWER, UserRole.ISSUE_ADMIN);
- // only permission from anyone group
- assertThat(resultWithoutUser).containsOnly(UserRole.ISSUE_ADMIN);
- }
-
- private void commit() {
- dbSession.commit();
- }
-
- private void checkTemplateTables(String fileName) {
- db.assertDbUnitTable(getClass(), fileName, "permission_templates", "id", "name", "description");
- db.assertDbUnitTable(getClass(), fileName, "perm_templates_users", "id", "template_id", "user_id", "permission_reference");
- db.assertDbUnitTable(getClass(), fileName, "perm_templates_groups", "id", "template_id", "group_id", "permission_reference");
- }
-
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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 java.util.Date;
-import org.apache.commons.lang.math.RandomUtils;
-import org.sonar.core.permission.ProjectPermissions;
-import org.sonar.core.util.Uuids;
-import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
-
-import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
-import static org.apache.commons.lang.RandomStringUtils.randomAscii;
-
-public class PermissionTemplateTesting {
- public static PermissionTemplateDto newPermissionTemplateDto() {
- return new PermissionTemplateDto()
- .setName(randomAlphanumeric(60))
- .setDescription(randomAscii(500))
- .setUuid(Uuids.create())
- .setCreatedAt(new Date())
- .setUpdatedAt(new Date());
- }
-
- public static PermissionTemplateUserDto newPermissionTemplateUserDto() {
- return new PermissionTemplateUserDto()
- .setPermission(ProjectPermissions.ALL.get(RandomUtils.nextInt(ProjectPermissions.ALL.size())))
- .setCreatedAt(new Date())
- .setUpdatedAt(new Date());
- }
-
- public static PermissionTemplateGroupDto newPermissionTemplateGroupDto() {
- return new PermissionTemplateGroupDto()
- .setPermission(ProjectPermissions.ALL.get(RandomUtils.nextInt(ProjectPermissions.ALL.size())))
- .setCreatedAt(new Date())
- .setUpdatedAt(new Date());
- }
-
- public static PermissionTemplateCharacteristicDto newPermissionTemplateCharacteristicDto() {
- return new PermissionTemplateCharacteristicDto()
- .setPermission(ProjectPermissions.ALL.get(RandomUtils.nextInt(ProjectPermissions.ALL.size())))
- .setWithProjectCreator(RandomUtils.nextBoolean())
- .setCreatedAt(System.currentTimeMillis())
- .setUpdatedAt(System.currentTimeMillis());
- }
-
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact 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 com.google.common.base.Predicate;
-import com.google.common.collect.Iterables;
-import java.util.List;
-import javax.annotation.Nullable;
-import org.junit.Rule;
-import org.junit.Test;
-import org.sonar.api.utils.System2;
-import org.sonar.db.DbTester;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-
-public class UserWithPermissionTemplateDaoTest {
-
- private static final Long TEMPLATE_ID = 50L;
-
- @Rule
- public DbTester dbTester = DbTester.create(System2.INSTANCE);
-
- PermissionTemplateDao dao = dbTester.getDbClient().permissionTemplateDao();
-
- @Test
- public void select_all_users() {
- dbTester.prepareDbUnit(getClass(), "users_with_permissions.xml");
-
- OldPermissionQuery query = OldPermissionQuery.builder().permission("user").build();
- List<UserWithPermissionDto> result = dao.selectUsers(query, TEMPLATE_ID);
- assertThat(result).hasSize(3);
-
- UserWithPermissionDto user1 = result.get(0);
- assertThat(user1.getLogin()).isEqualTo("user1");
- assertThat(user1.getName()).isEqualTo("User1");
- assertThat(user1.getPermission()).isNotNull();
-
- UserWithPermissionDto user2 = result.get(1);
- assertThat(user2.getLogin()).isEqualTo("user2");
- assertThat(user2.getName()).isEqualTo("User2");
- assertThat(user2.getPermission()).isNotNull();
-
- UserWithPermissionDto user3 = result.get(2);
- assertThat(user3.getLogin()).isEqualTo("user3");
- assertThat(user3.getName()).isEqualTo("User3");
- assertThat(user3.getPermission()).isNull();
- }
-
- @Test
- public void return_nothing_on_unknown_template_key() {
- dbTester.prepareDbUnit(getClass(), "users_with_permissions.xml");
-
- OldPermissionQuery query = OldPermissionQuery.builder().permission("user").build();
- List<UserWithPermissionDto> result = dao.selectUsers(query, 999L);
- assertThat(result).hasSize(3);
-
- UserWithPermissionDto user1 = result.get(0);
- assertThat(user1.getPermission()).isNull();
-
- UserWithPermissionDto user2 = result.get(1);
- assertThat(user2.getPermission()).isNull();
-
- UserWithPermissionDto user3 = result.get(2);
- assertThat(user3.getPermission()).isNull();
- }
-
- @Test
- public void select_only_user_with_permission() {
- dbTester.prepareDbUnit(getClass(), "users_with_permissions.xml");
-
- // user1 and user2 have permission user
- assertThat(dao.selectUsers(OldPermissionQuery.builder().permission("user").membership(OldPermissionQuery.IN).build(), TEMPLATE_ID)).hasSize(2);
- }
-
- @Test
- public void select_only_user_without_permission() {
- dbTester.prepareDbUnit(getClass(), "users_with_permissions.xml");
-
- // Only user3 has not the user permission
- assertThat(dao.selectUsers(OldPermissionQuery.builder().permission("user").membership(OldPermissionQuery.OUT).build(), TEMPLATE_ID)).hasSize(1);
- }
-
- @Test
- public void select_only_enable_users() {
- dbTester.prepareDbUnit(getClass(), "select_only_enable_users.xml");
-
- OldPermissionQuery query = OldPermissionQuery.builder().permission("user").build();
- List<UserWithPermissionDto> result = dao.selectUsers(query, 999L);
- assertThat(result).hasSize(3);
-
- // Disabled user should not be returned
- assertThat(Iterables.find(result, new Predicate<UserWithPermissionDto>() {
- @Override
- public boolean apply(@Nullable UserWithPermissionDto input) {
- return input.getLogin().equals("disabledUser");
- }
- }, null)).isNull();
- }
-
- @Test
- public void search_by_user_name() {
- dbTester.prepareDbUnit(getClass(), "users_with_permissions.xml");
-
- List<UserWithPermissionDto> result = dao.selectUsers(OldPermissionQuery.builder().permission("user").search("SEr1").build(), TEMPLATE_ID);
- assertThat(result).hasSize(1);
- assertThat(result.get(0).getName()).isEqualTo("User1");
-
- result = dao.selectUsers(OldPermissionQuery.builder().permission("user").search("user").build(), TEMPLATE_ID);
- assertThat(result).hasSize(3);
- }
-
- @Test
- public void should_be_sorted_by_user_name() {
- dbTester.prepareDbUnit(getClass(), "users_with_permissions_should_be_sorted_by_user_name.xml");
-
- List<UserWithPermissionDto> result = dao.selectUsers(OldPermissionQuery.builder().permission("user").build(), TEMPLATE_ID);
- assertThat(result).hasSize(3);
- assertThat(result.get(0).getName()).isEqualTo("User1");
- assertThat(result.get(1).getName()).isEqualTo("User2");
- assertThat(result.get(2).getName()).isEqualTo("User3");
- }
-
- @Test
- public void should_be_paginated() {
- dbTester.prepareDbUnit(getClass(), "users_with_permissions.xml");
-
- List<UserWithPermissionDto> result = dao.selectUsers(OldPermissionQuery.builder().permission("user").build(), TEMPLATE_ID, 0, 2);
- assertThat(result).hasSize(2);
- assertThat(result.get(0).getName()).isEqualTo("User1");
- assertThat(result.get(1).getName()).isEqualTo("User2");
-
- result = dao.selectUsers(OldPermissionQuery.builder().permission("user").build(), TEMPLATE_ID, 1, 2);
- assertThat(result).hasSize(2);
- assertThat(result.get(0).getName()).isEqualTo("User2");
- assertThat(result.get(1).getName()).isEqualTo("User3");
-
- result = dao.selectUsers(OldPermissionQuery.builder().permission("user").build(), TEMPLATE_ID, 2, 1);
- assertThat(result).hasSize(1);
- assertThat(result.get(0).getName()).isEqualTo("User3");
- }
-
-}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.template;
+
+import java.util.List;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.api.web.UserRole;
+import org.sonar.db.DbSession;
+import org.sonar.db.DbTester;
+import org.sonar.db.permission.GroupWithPermissionDto;
+import org.sonar.db.permission.OldPermissionQuery;
+import org.sonar.db.permission.template.PermissionTemplateDao;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+
+public class GroupWithPermissionTemplateDaoTest {
+
+ private static final long TEMPLATE_ID = 50L;
+
+ @Rule
+ public DbTester dbTester = DbTester.create(System2.INSTANCE);
+ DbSession session = dbTester.getSession();
+
+ PermissionTemplateDao underTest = dbTester.getDbClient().permissionTemplateDao();
+
+ @Test
+ public void select_groups() {
+ dbTester.prepareDbUnit(getClass(), "groups_with_permissions.xml");
+
+ OldPermissionQuery query = OldPermissionQuery.builder().permission("user").build();
+ List<GroupWithPermissionDto> result = underTest.selectGroups(session, query, TEMPLATE_ID);
+ int count = underTest.countGroups(session, query, TEMPLATE_ID);
+
+ assertThat(result).hasSize(4);
+ assertThat(count).isEqualTo(4);
+
+ GroupWithPermissionDto anyone = result.get(0);
+ assertThat(anyone.getName()).isEqualTo("Anyone");
+ assertThat(anyone.getDescription()).isNull();
+ assertThat(anyone.getPermission()).isNotNull();
+
+ GroupWithPermissionDto group1 = result.get(1);
+ assertThat(group1.getName()).isEqualTo("sonar-administrators");
+ assertThat(group1.getDescription()).isEqualTo("System administrators");
+ assertThat(group1.getPermission()).isNotNull();
+
+ GroupWithPermissionDto group2 = result.get(2);
+ assertThat(group2.getName()).isEqualTo("sonar-reviewers");
+ assertThat(group2.getDescription()).isEqualTo("Reviewers");
+ assertThat(group2.getPermission()).isNull();
+
+ GroupWithPermissionDto group3 = result.get(3);
+ assertThat(group3.getName()).isEqualTo("sonar-users");
+ assertThat(group3.getDescription()).isEqualTo("Any new users created will automatically join this group");
+ assertThat(group3.getPermission()).isNotNull();
+ }
+
+ @Test
+ public void anyone_group_is_returned_when_it_has_no_permission() {
+ dbTester.prepareDbUnit(getClass(), "groups_with_permissions.xml");
+
+ // Anyone group is returned even if it doesn't have the permission
+ OldPermissionQuery query = OldPermissionQuery.builder().permission(UserRole.USER).build();
+ List<GroupWithPermissionDto> result = underTest.selectGroups(session, query, TEMPLATE_ID);
+ assertThat(result).hasSize(4);
+
+ GroupWithPermissionDto group1 = result.get(1);
+ assertThat(group1.getName()).isEqualTo("sonar-administrators");
+ assertThat(group1.getPermission()).isNotNull();
+
+ GroupWithPermissionDto group2 = result.get(2);
+ assertThat(group2.getName()).isEqualTo("sonar-reviewers");
+ assertThat(group2.getPermission()).isNull();
+
+ GroupWithPermissionDto group3 = result.get(3);
+ assertThat(group3.getName()).isEqualTo("sonar-users");
+ assertThat(group3.getPermission()).isNotNull();
+ }
+
+ @Test
+ public void search_by_groups_name() {
+ dbTester.prepareDbUnit(getClass(), "groups_with_permissions.xml");
+
+ List<GroupWithPermissionDto> result = underTest.selectGroups(session, OldPermissionQuery.builder().permission("user").search("aDMini").build(), TEMPLATE_ID);
+ assertThat(result).hasSize(1);
+ assertThat(result.get(0).getName()).isEqualTo("sonar-administrators");
+
+ result = underTest.selectGroups(session, OldPermissionQuery.builder().permission("user").search("sonar").build(), TEMPLATE_ID);
+ assertThat(result).hasSize(3);
+ }
+
+ @Test
+ public void search_groups_should_be_sorted_by_group_name() {
+ dbTester.prepareDbUnit(getClass(), "groups_with_permissions_should_be_sorted_by_group_name.xml");
+
+ List<GroupWithPermissionDto> result = underTest.selectGroups(session, OldPermissionQuery.builder().permission("user").build(), TEMPLATE_ID);
+ assertThat(result).hasSize(4);
+ assertThat(result.get(0).getName()).isEqualTo("Anyone");
+ assertThat(result.get(1).getName()).isEqualTo("sonar-administrators");
+ assertThat(result.get(2).getName()).isEqualTo("sonar-reviewers");
+ assertThat(result.get(3).getName()).isEqualTo("sonar-users");
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.template;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+import org.apache.ibatis.session.ResultContext;
+import org.apache.ibatis.session.ResultHandler;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.utils.System2;
+import org.sonar.api.web.UserRole;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.DbTester;
+import org.sonar.db.user.GroupDbTester;
+import org.sonar.db.user.GroupDto;
+import org.sonar.db.user.UserDbTester;
+import org.sonar.db.user.UserDto;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.sonar.api.web.UserRole.ADMIN;
+import static org.sonar.api.web.UserRole.CODEVIEWER;
+import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
+import static org.sonar.api.web.UserRole.USER;
+import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.user.GroupTesting.newGroupDto;
+import static org.sonar.db.user.UserTesting.newUserDto;
+
+public class PermissionTemplateDaoTest {
+
+ System2 system = mock(System2.class);
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+ @Rule
+ public DbTester db = DbTester.create(system);
+ DbClient dbClient = db.getDbClient();
+ DbSession dbSession = db.getSession();
+ GroupDbTester groupDb = new GroupDbTester(db);
+ UserDbTester userDb = new UserDbTester(db);
+ PermissionTemplateDbTester templateDb = new PermissionTemplateDbTester(db);
+
+ PermissionTemplateDao underTest = new PermissionTemplateDao(db.myBatis(), system);
+
+ @Test
+ public void should_create_permission_template() throws ParseException {
+ db.prepareDbUnit(getClass(), "createPermissionTemplate.xml");
+
+ Date now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2013-01-02 01:04:05");
+ when(system.now()).thenReturn(now.getTime());
+
+ PermissionTemplateDto permissionTemplate = underTest.insert(db.getSession(), newPermissionTemplateDto()
+ .setName("my template")
+ .setDescription("my description")
+ .setKeyPattern("myregexp"));
+ assertThat(permissionTemplate).isNotNull();
+ assertThat(permissionTemplate.getId()).isEqualTo(1L);
+
+ db.assertDbUnitTable(getClass(), "createPermissionTemplate-result.xml", "permission_templates", "id", "name", "description");
+ }
+
+ @Test
+ public void should_select_permission_template() {
+ db.prepareDbUnit(getClass(), "selectPermissionTemplate.xml");
+
+ PermissionTemplate result = underTest.selectByUuidWithUserAndGroupPermissions("my_template_20130102_030405");
+
+ assertThat(result).isNotNull();
+ PermissionTemplateDto template = result.getTemplate();
+ assertThat(template.getName()).isEqualTo("my template");
+ assertThat(template.getUuid()).isEqualTo("my_template_20130102_030405");
+ assertThat(template.getDescription()).isEqualTo("my description");
+ List<PermissionTemplateUserDto> usersPermissions = result.getUserPermissions();
+ assertThat(usersPermissions).hasSize(3);
+ assertThat(usersPermissions).extracting("userId").containsOnly(1L, 2L, 1L);
+ assertThat(usersPermissions).extracting("userLogin").containsOnly("login1", "login2", "login2");
+ assertThat(usersPermissions).extracting("userName").containsOnly("user1", "user2", "user2");
+ assertThat(usersPermissions).extracting("permission").containsOnly("user_permission1", "user_permission1", "user_permission2");
+ List<PermissionTemplateGroupDto> groupsPermissions = result.getGroupPermissions();
+ assertThat(groupsPermissions).hasSize(3);
+ assertThat(groupsPermissions).extracting("groupId").containsOnly(1L, 2L, null);
+ assertThat(groupsPermissions).extracting("groupName").containsOnly("group1", "group2", null);
+ assertThat(groupsPermissions).extracting("permission").containsOnly("group_permission1", "group_permission1", "group_permission2");
+ }
+
+ @Test
+ public void should_select_permission_template_by_key() {
+ db.prepareDbUnit(getClass(), "selectPermissionTemplate.xml");
+
+ PermissionTemplateDto permissionTemplate = underTest.selectByUuid("my_template_20130102_030405");
+
+ assertThat(permissionTemplate).isNotNull();
+ assertThat(permissionTemplate.getId()).isEqualTo(1L);
+ assertThat(permissionTemplate.getName()).isEqualTo("my template");
+ assertThat(permissionTemplate.getUuid()).isEqualTo("my_template_20130102_030405");
+ assertThat(permissionTemplate.getDescription()).isEqualTo("my description");
+ }
+
+ @Test
+ public void should_select_all_permission_templates() {
+ db.prepareDbUnit(getClass(), "selectAllPermissionTemplates.xml");
+ commit();
+
+ List<PermissionTemplateDto> permissionTemplates = underTest.selectAll();
+
+ assertThat(permissionTemplates).hasSize(3);
+ assertThat(permissionTemplates).extracting("id").containsOnly(1L, 2L, 3L);
+ assertThat(permissionTemplates).extracting("name").containsOnly("template1", "template2", "template3");
+ assertThat(permissionTemplates).extracting("kee").containsOnly("template1_20130102_030405", "template2_20130102_030405", "template3_20130102_030405");
+ assertThat(permissionTemplates).extracting("description").containsOnly("description1", "description2", "description3");
+ }
+
+ @Test
+ public void should_update_permission_template() {
+ db.prepareDbUnit(getClass(), "updatePermissionTemplate.xml");
+
+ underTest.update(1L, "new_name", "new_description", "new_regexp");
+
+ db.assertDbUnitTable(getClass(), "updatePermissionTemplate-result.xml", "permission_templates", "id", "name", "kee", "description");
+ }
+
+ @Test
+ public void should_delete_permission_template() {
+ db.prepareDbUnit(getClass(), "deletePermissionTemplate.xml");
+
+ underTest.deleteById(dbSession, 1L);
+ dbSession.commit();
+
+ checkTemplateTables("deletePermissionTemplate-result.xml");
+ db.assertDbUnitTable(getClass(), "deletePermissionTemplate-result.xml", "perm_tpl_characteristics");
+ }
+
+ @Test
+ public void should_add_user_permission_to_template() {
+ db.prepareDbUnit(getClass(), "addUserPermissionToTemplate.xml");
+
+ underTest.insertUserPermission(1L, 1L, "new_permission");
+
+ checkTemplateTables("addUserPermissionToTemplate-result.xml");
+ }
+
+ @Test
+ public void should_remove_user_permission_from_template() {
+ db.prepareDbUnit(getClass(), "removeUserPermissionFromTemplate.xml");
+
+ underTest.deleteUserPermission(1L, 2L, "permission_to_remove");
+
+ checkTemplateTables("removeUserPermissionFromTemplate-result.xml");
+ }
+
+ @Test
+ public void should_add_group_permission_to_template() {
+ db.prepareDbUnit(getClass(), "addGroupPermissionToTemplate.xml");
+
+ underTest.insertGroupPermission(1L, 1L, "new_permission");
+
+ checkTemplateTables("addGroupPermissionToTemplate-result.xml");
+ }
+
+ @Test
+ public void should_remove_group_permission_from_template() {
+ db.prepareDbUnit(getClass(), "removeGroupPermissionFromTemplate.xml");
+
+ underTest.deleteGroupPermission(1L, 2L, "permission_to_remove");
+
+ checkTemplateTables("removeGroupPermissionFromTemplate-result.xml");
+ }
+
+ @Test
+ public void remove_by_group() {
+ db.prepareDbUnit(getClass(), "remove_by_group.xml");
+
+ underTest.deleteByGroup(db.getSession(), 2L);
+ db.getSession().commit();
+
+ db.assertDbUnitTable(getClass(), "remove_by_group-result.xml", "permission_templates", "id", "name", "kee", "description");
+ }
+
+ @Test
+ public void should_add_group_permission_with_null_name() {
+ db.prepareDbUnit(getClass(), "addNullGroupPermissionToTemplate.xml");
+
+ underTest.insertGroupPermission(1L, null, "new_permission");
+
+ checkTemplateTables("addNullGroupPermissionToTemplate-result.xml");
+ }
+
+ @Test
+ public void should_remove_group_permission_with_null_name() {
+ db.prepareDbUnit(getClass(), "removeNullGroupPermissionFromTemplate.xml");
+
+ underTest.deleteGroupPermission(1L, null, "permission_to_remove");
+
+ checkTemplateTables("removeNullGroupPermissionFromTemplate-result.xml");
+ }
+
+ @Test
+ public void new_permission_template_with_empty_user_group_characteristics() {
+ PermissionTemplateDto template = underTest.insert(dbSession, newPermissionTemplateDto().setUuid("TEMPLATE_UUID"));
+
+ PermissionTemplate result = underTest.selectByUuidWithUserAndGroupPermissions(dbSession, "TEMPLATE_UUID");
+
+ assertThat(result.getTemplate())
+ .extracting(PermissionTemplateDto::getId, PermissionTemplateDto::getUuid, PermissionTemplateDto::getName, PermissionTemplateDto::getDescription)
+ .containsExactly(template.getId(), template.getUuid(), template.getName(), template.getDescription());
+
+ assertThat(result.getUserPermissions()).isEmpty();
+ assertThat(result.getGroupPermissions()).isEmpty();
+ assertThat(result.getCharacteristics()).isEmpty();
+ }
+
+ @Test
+ public void unknown_permission_template() {
+ PermissionTemplate result = underTest.selectByUuidWithUserAndGroupPermissions(dbSession, "UNKNOWN_TEMPLATE_UUID");
+
+ assertThat(result).isNull();
+ }
+
+ @Test
+ public void permission_template_with_user_group_and_characteristics() {
+ PermissionTemplateDto template = dbClient.permissionTemplateDao().insert(dbSession, newPermissionTemplateDto().setUuid("TEMPLATE_UUID"));
+ GroupDto group = groupDb.insertGroup(newGroupDto());
+ UserDto user = userDb.insertUser(newUserDto());
+ templateDb.addGroupToTemplate(template.getId(), group.getId(), UserRole.ADMIN);
+ templateDb.addUserToTemplate(template.getId(), user.getId(), UserRole.CODEVIEWER);
+ templateDb.addProjectCreatorToTemplate(template.getId(), UserRole.USER);
+
+ PermissionTemplate result = underTest.selectByUuidWithUserAndGroupPermissions(dbSession, "TEMPLATE_UUID");
+ assertThat(result.getTemplate())
+ .extracting(PermissionTemplateDto::getId, PermissionTemplateDto::getUuid, PermissionTemplateDto::getName, PermissionTemplateDto::getDescription)
+ .containsExactly(template.getId(), template.getUuid(), template.getName(), template.getDescription());
+ assertThat(result.getCharacteristics()).hasSize(1)
+ .extracting(PermissionTemplateCharacteristicDto::getPermission, PermissionTemplateCharacteristicDto::getWithProjectCreator)
+ .containsExactly(tuple(UserRole.USER, true));
+ assertThat(result.getGroupPermissions()).hasSize(1)
+ .extracting(PermissionTemplateGroupDto::getGroupId, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
+ .containsExactly(tuple(group.getId(), group.getName(), UserRole.ADMIN));
+ assertThat(result.getUserPermissions()).hasSize(1)
+ .extracting(PermissionTemplateUserDto::getUserId, PermissionTemplateUserDto::getUserLogin, PermissionTemplateUserDto::getPermission)
+ .containsExactly(tuple(user.getId(), user.getLogin(), UserRole.CODEVIEWER));
+ }
+
+ @Test
+ public void should_fail_on_unmatched_template() {
+ expectedException.expect(IllegalArgumentException.class);
+
+ underTest.selectPermissionTemplateWithPermissions(db.getSession(), "unmatched");
+ }
+
+ @Test
+ public void group_count_by_template_and_permission() {
+ PermissionTemplateDto template1 = templateDb.insertTemplate();
+ PermissionTemplateDto template2 = templateDb.insertTemplate();
+ PermissionTemplateDto template3 = templateDb.insertTemplate();
+
+ GroupDto group1 = groupDb.insertGroup();
+ GroupDto group2 = groupDb.insertGroup();
+ GroupDto group3 = groupDb.insertGroup();
+
+ templateDb.addGroupToTemplate(42L, group1.getId(), ISSUE_ADMIN);
+ templateDb.addGroupToTemplate(template1.getId(), group1.getId(), CODEVIEWER);
+ templateDb.addGroupToTemplate(template1.getId(), group2.getId(), CODEVIEWER);
+ templateDb.addGroupToTemplate(template1.getId(), group3.getId(), CODEVIEWER);
+ templateDb.addGroupToTemplate(template1.getId(), null, CODEVIEWER);
+ templateDb.addGroupToTemplate(template1.getId(), group1.getId(), ADMIN);
+ templateDb.addGroupToTemplate(template2.getId(), group1.getId(), ADMIN);
+
+ final List<CountByTemplateAndPermissionDto> result = new ArrayList<>();
+ underTest.groupsCountByTemplateIdAndPermission(dbSession, Arrays.asList(template1.getId(), template2.getId(), template3.getId()), new ResultHandler() {
+ @Override
+ public void handleResult(ResultContext context) {
+ result.add((CountByTemplateAndPermissionDto) context.getResultObject());
+ }
+ });
+
+ assertThat(result).hasSize(3);
+ assertThat(result).extracting("permission").containsOnly(ADMIN, CODEVIEWER);
+ assertThat(result).extracting("templateId").containsOnly(template1.getId(), template2.getId());
+ assertThat(result).extracting("count").containsOnly(4, 1);
+ }
+
+ @Test
+ public void user_count_by_template_and_permission() {
+ PermissionTemplateDto template1 = templateDb.insertTemplate();
+ PermissionTemplateDto template2 = templateDb.insertTemplate();
+ PermissionTemplateDto template3 = templateDb.insertTemplate();
+
+ UserDto user1 = userDb.insertUser();
+ UserDto user2 = userDb.insertUser();
+ UserDto user3 = userDb.insertUser();
+
+ templateDb.addUserToTemplate(42L, user1.getId(), ISSUE_ADMIN);
+ templateDb.addUserToTemplate(template1.getId(), user1.getId(), ADMIN);
+ templateDb.addUserToTemplate(template1.getId(), user2.getId(), ADMIN);
+ templateDb.addUserToTemplate(template1.getId(), user3.getId(), ADMIN);
+ templateDb.addUserToTemplate(template1.getId(), user1.getId(), USER);
+ templateDb.addUserToTemplate(template2.getId(), user1.getId(), USER);
+
+ final List<CountByTemplateAndPermissionDto> result = new ArrayList<>();
+ underTest.usersCountByTemplateIdAndPermission(dbSession, Arrays.asList(template1.getId(), template2.getId(), template3.getId()),
+ context -> result.add((CountByTemplateAndPermissionDto) context.getResultObject()));
+ assertThat(result).hasSize(3);
+ assertThat(result).extracting("permission").containsOnly(ADMIN, USER);
+ assertThat(result).extracting("templateId").containsOnly(template1.getId(), template2.getId());
+ assertThat(result).extracting("count").containsOnly(3, 1);
+ }
+
+ @Test
+ public void select_by_name_query_and_pagination() {
+ templateDb.insertTemplate(newPermissionTemplateDto().setName("aaabbb"));
+ templateDb.insertTemplate(newPermissionTemplateDto().setName("aaaccc"));
+
+ List<PermissionTemplateDto> templates = underTest.selectAll(dbSession, "aaa");
+ int count = underTest.countAll(dbSession, "aaa");
+
+ assertThat(templates.get(0).getName()).isEqualTo("aaabbb");
+ assertThat(count).isEqualTo(2);
+ }
+
+ @Test
+ public void selectPotentialPermissions_with_unknown_template_and_no_user() {
+ List<String> result = underTest.selectPotentialPermissionsByUserIdAndTemplateId(dbSession, null, 42L);
+
+ assertThat(result).isEmpty();
+ }
+
+ @Test
+ public void selectPotentialPermissions_with_empty_template_and_new_user() {
+ UserDto user = userDb.insertUser();
+ PermissionTemplateDto template = templateDb.insertTemplate();
+
+ List<String> result = underTest.selectPotentialPermissionsByUserIdAndTemplateId(dbSession, user.getId(), template.getId());
+
+ assertThat(result).isEmpty();
+ }
+
+ @Test
+ public void selectPotentialPermission_with_template_users_groups_and_project_creator() {
+ UserDto user = userDb.insertUser();
+ GroupDto group = groupDb.insertGroup();
+ groupDb.addUserToGroup(user.getId(), group.getId());
+ PermissionTemplateDto template = templateDb.insertTemplate();
+ templateDb.addProjectCreatorToTemplate(template.getId(), SCAN_EXECUTION);
+ templateDb.addProjectCreatorToTemplate(template.getId(), UserRole.ADMIN);
+ templateDb.addUserToTemplate(template.getId(), user.getId(), UserRole.USER);
+ templateDb.addUserToTemplate(template.getId(), user.getId(), UserRole.ADMIN);
+ templateDb.addGroupToTemplate(template.getId(), group.getId(), UserRole.CODEVIEWER);
+ templateDb.addGroupToTemplate(template.getId(), group.getId(), UserRole.ADMIN);
+ templateDb.addGroupToTemplate(template.getId(), null, UserRole.ISSUE_ADMIN);
+
+ List<String> resultWithUser = underTest.selectPotentialPermissionsByUserIdAndTemplateId(dbSession, user.getId(), template.getId());
+ List<String> resultWithoutUser = underTest.selectPotentialPermissionsByUserIdAndTemplateId(dbSession, null, template.getId());
+
+ assertThat(resultWithUser).containsOnlyOnce(SCAN_EXECUTION, UserRole.ADMIN, UserRole.USER, UserRole.CODEVIEWER, UserRole.ISSUE_ADMIN);
+ // only permission from anyone group
+ assertThat(resultWithoutUser).containsOnly(UserRole.ISSUE_ADMIN);
+ }
+
+ private void commit() {
+ dbSession.commit();
+ }
+
+ private void checkTemplateTables(String fileName) {
+ db.assertDbUnitTable(getClass(), fileName, "permission_templates", "id", "name", "description");
+ db.assertDbUnitTable(getClass(), fileName, "perm_templates_users", "id", "template_id", "user_id", "permission_reference");
+ db.assertDbUnitTable(getClass(), fileName, "perm_templates_groups", "id", "template_id", "group_id", "permission_reference");
+ }
+
+}
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
-import org.sonar.db.permission.PermissionTemplateDto;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateCharacteristicDto;
-import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateCharacteristicDto;
+import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
public class PermissionTemplateDbTester {
private final DbTester db;
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.template;
+
+import java.util.Date;
+import org.apache.commons.lang.math.RandomUtils;
+import org.sonar.core.permission.ProjectPermissions;
+import org.sonar.core.util.Uuids;
+import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
+import org.sonar.db.permission.template.PermissionTemplateDto;
+import org.sonar.db.permission.template.PermissionTemplateGroupDto;
+import org.sonar.db.permission.template.PermissionTemplateUserDto;
+
+import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
+import static org.apache.commons.lang.RandomStringUtils.randomAscii;
+
+public class PermissionTemplateTesting {
+ public static PermissionTemplateDto newPermissionTemplateDto() {
+ return new PermissionTemplateDto()
+ .setName(randomAlphanumeric(60))
+ .setDescription(randomAscii(500))
+ .setUuid(Uuids.create())
+ .setCreatedAt(new Date())
+ .setUpdatedAt(new Date());
+ }
+
+ public static PermissionTemplateUserDto newPermissionTemplateUserDto() {
+ return new PermissionTemplateUserDto()
+ .setPermission(ProjectPermissions.ALL.get(RandomUtils.nextInt(ProjectPermissions.ALL.size())))
+ .setCreatedAt(new Date())
+ .setUpdatedAt(new Date());
+ }
+
+ public static PermissionTemplateGroupDto newPermissionTemplateGroupDto() {
+ return new PermissionTemplateGroupDto()
+ .setPermission(ProjectPermissions.ALL.get(RandomUtils.nextInt(ProjectPermissions.ALL.size())))
+ .setCreatedAt(new Date())
+ .setUpdatedAt(new Date());
+ }
+
+ public static PermissionTemplateCharacteristicDto newPermissionTemplateCharacteristicDto() {
+ return new PermissionTemplateCharacteristicDto()
+ .setPermission(ProjectPermissions.ALL.get(RandomUtils.nextInt(ProjectPermissions.ALL.size())))
+ .setWithProjectCreator(RandomUtils.nextBoolean())
+ .setCreatedAt(System.currentTimeMillis())
+ .setUpdatedAt(System.currentTimeMillis());
+ }
+
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.template;
+
+import com.google.common.base.Predicate;
+import com.google.common.collect.Iterables;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+import org.sonar.db.permission.OldPermissionQuery;
+import org.sonar.db.permission.UserWithPermissionDto;
+import org.sonar.db.permission.template.PermissionTemplateDao;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+
+public class UserWithPermissionTemplateDaoTest {
+
+ private static final Long TEMPLATE_ID = 50L;
+
+ @Rule
+ public DbTester dbTester = DbTester.create(System2.INSTANCE);
+
+ PermissionTemplateDao dao = dbTester.getDbClient().permissionTemplateDao();
+
+ @Test
+ public void select_all_users() {
+ dbTester.prepareDbUnit(getClass(), "users_with_permissions.xml");
+
+ OldPermissionQuery query = OldPermissionQuery.builder().permission("user").build();
+ List<UserWithPermissionDto> result = dao.selectUsers(query, TEMPLATE_ID);
+ assertThat(result).hasSize(3);
+
+ UserWithPermissionDto user1 = result.get(0);
+ assertThat(user1.getLogin()).isEqualTo("user1");
+ assertThat(user1.getName()).isEqualTo("User1");
+ assertThat(user1.getPermission()).isNotNull();
+
+ UserWithPermissionDto user2 = result.get(1);
+ assertThat(user2.getLogin()).isEqualTo("user2");
+ assertThat(user2.getName()).isEqualTo("User2");
+ assertThat(user2.getPermission()).isNotNull();
+
+ UserWithPermissionDto user3 = result.get(2);
+ assertThat(user3.getLogin()).isEqualTo("user3");
+ assertThat(user3.getName()).isEqualTo("User3");
+ assertThat(user3.getPermission()).isNull();
+ }
+
+ @Test
+ public void return_nothing_on_unknown_template_key() {
+ dbTester.prepareDbUnit(getClass(), "users_with_permissions.xml");
+
+ OldPermissionQuery query = OldPermissionQuery.builder().permission("user").build();
+ List<UserWithPermissionDto> result = dao.selectUsers(query, 999L);
+ assertThat(result).hasSize(3);
+
+ UserWithPermissionDto user1 = result.get(0);
+ assertThat(user1.getPermission()).isNull();
+
+ UserWithPermissionDto user2 = result.get(1);
+ assertThat(user2.getPermission()).isNull();
+
+ UserWithPermissionDto user3 = result.get(2);
+ assertThat(user3.getPermission()).isNull();
+ }
+
+ @Test
+ public void select_only_user_with_permission() {
+ dbTester.prepareDbUnit(getClass(), "users_with_permissions.xml");
+
+ // user1 and user2 have permission user
+ assertThat(dao.selectUsers(OldPermissionQuery.builder().permission("user").membership(OldPermissionQuery.IN).build(), TEMPLATE_ID)).hasSize(2);
+ }
+
+ @Test
+ public void select_only_user_without_permission() {
+ dbTester.prepareDbUnit(getClass(), "users_with_permissions.xml");
+
+ // Only user3 has not the user permission
+ assertThat(dao.selectUsers(OldPermissionQuery.builder().permission("user").membership(OldPermissionQuery.OUT).build(), TEMPLATE_ID)).hasSize(1);
+ }
+
+ @Test
+ public void select_only_enable_users() {
+ dbTester.prepareDbUnit(getClass(), "select_only_enable_users.xml");
+
+ OldPermissionQuery query = OldPermissionQuery.builder().permission("user").build();
+ List<UserWithPermissionDto> result = dao.selectUsers(query, 999L);
+ assertThat(result).hasSize(3);
+
+ // Disabled user should not be returned
+ assertThat(Iterables.find(result, new Predicate<UserWithPermissionDto>() {
+ @Override
+ public boolean apply(@Nullable UserWithPermissionDto input) {
+ return input.getLogin().equals("disabledUser");
+ }
+ }, null)).isNull();
+ }
+
+ @Test
+ public void search_by_user_name() {
+ dbTester.prepareDbUnit(getClass(), "users_with_permissions.xml");
+
+ List<UserWithPermissionDto> result = dao.selectUsers(OldPermissionQuery.builder().permission("user").search("SEr1").build(), TEMPLATE_ID);
+ assertThat(result).hasSize(1);
+ assertThat(result.get(0).getName()).isEqualTo("User1");
+
+ result = dao.selectUsers(OldPermissionQuery.builder().permission("user").search("user").build(), TEMPLATE_ID);
+ assertThat(result).hasSize(3);
+ }
+
+ @Test
+ public void should_be_sorted_by_user_name() {
+ dbTester.prepareDbUnit(getClass(), "users_with_permissions_should_be_sorted_by_user_name.xml");
+
+ List<UserWithPermissionDto> result = dao.selectUsers(OldPermissionQuery.builder().permission("user").build(), TEMPLATE_ID);
+ assertThat(result).hasSize(3);
+ assertThat(result.get(0).getName()).isEqualTo("User1");
+ assertThat(result.get(1).getName()).isEqualTo("User2");
+ assertThat(result.get(2).getName()).isEqualTo("User3");
+ }
+
+ @Test
+ public void should_be_paginated() {
+ dbTester.prepareDbUnit(getClass(), "users_with_permissions.xml");
+
+ List<UserWithPermissionDto> result = dao.selectUsers(OldPermissionQuery.builder().permission("user").build(), TEMPLATE_ID, 0, 2);
+ assertThat(result).hasSize(2);
+ assertThat(result.get(0).getName()).isEqualTo("User1");
+ assertThat(result.get(1).getName()).isEqualTo("User2");
+
+ result = dao.selectUsers(OldPermissionQuery.builder().permission("user").build(), TEMPLATE_ID, 1, 2);
+ assertThat(result).hasSize(2);
+ assertThat(result.get(0).getName()).isEqualTo("User2");
+ assertThat(result.get(1).getName()).isEqualTo("User3");
+
+ result = dao.selectUsers(OldPermissionQuery.builder().permission("user").build(), TEMPLATE_ID, 2, 1);
+ assertThat(result).hasSize(1);
+ assertThat(result.get(0).getName()).isEqualTo("User3");
+ }
+
+}
+++ /dev/null
-<dataset>
-
- <groups id="200" name="sonar-administrators" description="System administrators"/>
- <groups id="201" name="sonar-users" description="Any new users created will automatically join this group"/>
- <groups id="202" name="sonar-reviewers" description="Reviewers"/>
-
- <perm_templates_groups id="1" group_id="200" permission_reference="user" template_id="50"/>
- <perm_templates_groups id="2" group_id="200" permission_reference="admin" template_id="50"/>
- <perm_templates_groups id="3" group_id="200" permission_reference="codeviewer" template_id="50"/>
-
- <perm_templates_groups id="4" group_id="201" permission_reference="user" template_id="50"/>
-
- <!-- Permission 'user' for Anyone group -->
- <perm_templates_groups id="5" group_id="[null]" permission_reference="user" template_id="50"/>
-
-</dataset>
+++ /dev/null
-<dataset>
-
- <groups id="200" name="sonar-reviewers"/>
- <groups id="201" name="sonar-users"/>
- <groups id="202" name="sonar-administrators"/>
-
- <perm_templates_groups id="1" group_id="200" permission_reference="user" template_id="50"/>
- <perm_templates_groups id="2" group_id="200" permission_reference="admin" template_id="50"/>
- <perm_templates_groups id="3" group_id="200" permission_reference="codeviewer" template_id="50"/>
-
- <perm_templates_groups id="4" group_id="201" permission_reference="user" template_id="50"/>
-
- <!-- Permission 'user' for Anyone group -->
- <perm_templates_groups id="5" group_id="[null]" permission_reference="user" template_id="50"/>
-
-</dataset>
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
- <perm_templates_users/>
- <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="new_permission"/>
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
- <perm_templates_users/>
- <perm_templates_groups/>
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
- <perm_templates_users/>
- <perm_templates_groups id="1" template_id="1" group_id="[null]" permission_reference="new_permission"/>
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
- <perm_templates_users/>
- <perm_templates_groups/>
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
- <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="new_permission"/>
- <perm_templates_groups/>
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
- <perm_templates_users/>
- <perm_templates_groups/>
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="Môü Gnô Gnèçà ß" kee="mou_gno_gneca_20130102_010405" description="my description"
- key_pattern="[null]"/>
-</dataset>
+++ /dev/null
-<dataset></dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="my template" kee="my_template_20130102_010405" description="my description"
- key_pattern="myregexp"/>
-</dataset>
+++ /dev/null
-<dataset></dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <permission_templates id="2" name="other template" kee="my_template_20130102_030405" description="other description"
- created_at="[null]" updated_at="[null]"/>
-
- <perm_templates_users id="4" template_id="2" user_id="2" permission_reference="user_permission2"/>
-
- <perm_templates_groups id="4" template_id="2" group_id="[null]" permission_reference="group_permission2"/>
-
- <perm_tpl_characteristics id="2" template_id="2" permission_key="user" with_project_creator="[true]" created_at="1000000" updated_at="1000000"/>
-</dataset>
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"
- created_at="[null]" updated_at="[null]"/>
- <permission_templates id="2" name="other template" kee="my_template_20130102_030405" description="other description"
- created_at="[null]" updated_at="[null]"/>
-
- <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="user_permission1"/>
- <perm_templates_users id="2" template_id="1" user_id="2" permission_reference="user_permission1"/>
- <perm_templates_users id="3" template_id="1" user_id="2" permission_reference="user_permission2"/>
- <perm_templates_users id="4" template_id="2" user_id="2" permission_reference="user_permission2"/>
-
- <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="group_permission1"/>
- <perm_templates_groups id="2" template_id="1" group_id="2" permission_reference="group_permission1"/>
- <perm_templates_groups id="3" template_id="1" group_id="[null]" permission_reference="group_permission2"/>
- <perm_templates_groups id="4" template_id="2" group_id="[null]" permission_reference="group_permission2"/>
-
- <perm_tpl_characteristics id="1" template_id="1" permission_key="user" with_project_creator="[true]" created_at="1000000" updated_at="1000000"/>
- <perm_tpl_characteristics id="2" template_id="2" permission_key="user" with_project_creator="[true]" created_at="1000000" updated_at="1000000"/>
-
-</dataset>
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
- <perm_templates_users/>
- <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
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
- <perm_templates_users/>
- <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
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
- <perm_templates_users/>
- <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>
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
- <perm_templates_users/>
- <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>
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
-
- <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
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
-
- <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
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
- <perm_templates_users/>
- <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission_other_group"/>
-</dataset>
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
- <perm_templates_users/>
- <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="permission_to_remove2"/>
-</dataset>
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="template1" kee="template1_20130102_030405" description="description1"
- created_at="[null]" updated_at="[null]"/>
- <permission_templates id="2" name="template2" kee="template2_20130102_030405" description="description2"
- created_at="[null]" updated_at="[null]"/>
- <permission_templates id="3" name="template3" kee="template3_20130102_030405" description="description3"
- created_at="[null]" updated_at="[null]"/>
-</dataset>
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"
- created_at="[null]" updated_at="[null]"/>
-
- <users id="1" login="user1" name="user1"/>
- <users id="2" login="user2" name="user2"/>
-
- <groups id="1" name="group1"/>
- <groups id="2" name="group2"/>
-</dataset>
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"
- created_at="[null]" updated_at="[null]"/>
-
- <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="user_permission1"/>
- <perm_templates_users id="2" template_id="1" user_id="2" permission_reference="user_permission1"/>
- <perm_templates_users id="3" template_id="1" user_id="2" permission_reference="user_permission2"/>
-
- <users id="1" login="login1" name="user1"/>
- <users id="2" login="login2" name="user2"/>
-
- <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="group_permission1"/>
- <perm_templates_groups id="2" template_id="1" group_id="2" permission_reference="group_permission1"/>
- <!-- Anyone group -->
- <perm_templates_groups id="3" template_id="1" group_id="[null]" permission_reference="group_permission2"/>
- <!-- Unmatched group -->
- <perm_templates_groups id="4" template_id="1" group_id="999999" permission_reference="group_permission2"/>
-
- <groups id="1" name="group1"/>
- <groups id="2" name="group2"/>
-</dataset>
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="new_name" kee="template1_20130102_030405" description="new_description"
- key_pattern="new_regexp" created_at="[null]" updated_at="[null]"/>
- <permission_templates id="2" name="template2" kee="template2_20130102_030405" description="description2"
- key_pattern="barregexp" created_at="[null]" updated_at="[null]"/>
-</dataset>
+++ /dev/null
-<dataset>
- <permission_templates id="1" name="template1" kee="template1_20130102_030405" description="description1"
- key_pattern="fooregexp" created_at="[null]" updated_at="[null]"/>
- <permission_templates id="2" name="template2" kee="template2_20130102_030405" description="description2"
- key_pattern="barregexp" created_at="[null]" updated_at="[null]"/>
-</dataset>
+++ /dev/null
-<dataset>
-
- <users id="200" login="user1" name="User1" active="[true]"/>
- <users id="201" login="user2" name="User2" active="[true]"/>
- <users id="202" login="user3" name="User3" active="[true]"/>
- <users id="999" login="disabledUser" name="disabledUser" active="[false]"/>
-
- <perm_templates_users id="1" user_id="200" permission_reference="user" template_id="50"/>
- <perm_templates_users id="2" user_id="200" permission_reference="admin" template_id="50"/>
- <perm_templates_users id="3" user_id="200" permission_reference="codeviewer" template_id="50"/>
-
- <perm_templates_users id="4" user_id="201" permission_reference="user" template_id="50"/>
-
-</dataset>
+++ /dev/null
-<dataset>
-
- <users id="200" login="user1" name="User1" active="[true]"/>
- <users id="201" login="user2" name="User2" active="[true]"/>
- <users id="202" login="user3" name="User3" active="[true]"/>
-
- <perm_templates_users id="1" user_id="200" permission_reference="user" template_id="50"/>
- <perm_templates_users id="2" user_id="200" permission_reference="admin" template_id="50"/>
- <perm_templates_users id="3" user_id="200" permission_reference="codeviewer" template_id="50"/>
-
- <perm_templates_users id="4" user_id="201" permission_reference="user" template_id="50"/>
-
-</dataset>
+++ /dev/null
-<dataset>
-
- <users id="200" login="user3" name="User3" active="[true]"/>
- <users id="201" login="user1" name="User1" active="[true]"/>
- <users id="202" login="user2" name="User2" active="[true]"/>
-
- <perm_templates_users id="1" user_id="200" permission_reference="user" template_id="50"/>
- <perm_templates_users id="2" user_id="200" permission_reference="admin" template_id="50"/>
- <perm_templates_users id="3" user_id="200" permission_reference="codeviewer" template_id="50"/>
-
- <perm_templates_users id="4" user_id="201" permission_reference="user" template_id="50"/>
-
-</dataset>
--- /dev/null
+<dataset>
+
+ <groups id="200" name="sonar-administrators" description="System administrators"/>
+ <groups id="201" name="sonar-users" description="Any new users created will automatically join this group"/>
+ <groups id="202" name="sonar-reviewers" description="Reviewers"/>
+
+ <perm_templates_groups id="1" group_id="200" permission_reference="user" template_id="50"/>
+ <perm_templates_groups id="2" group_id="200" permission_reference="admin" template_id="50"/>
+ <perm_templates_groups id="3" group_id="200" permission_reference="codeviewer" template_id="50"/>
+
+ <perm_templates_groups id="4" group_id="201" permission_reference="user" template_id="50"/>
+
+ <!-- Permission 'user' for Anyone group -->
+ <perm_templates_groups id="5" group_id="[null]" permission_reference="user" template_id="50"/>
+
+</dataset>
--- /dev/null
+<dataset>
+
+ <groups id="200" name="sonar-reviewers"/>
+ <groups id="201" name="sonar-users"/>
+ <groups id="202" name="sonar-administrators"/>
+
+ <perm_templates_groups id="1" group_id="200" permission_reference="user" template_id="50"/>
+ <perm_templates_groups id="2" group_id="200" permission_reference="admin" template_id="50"/>
+ <perm_templates_groups id="3" group_id="200" permission_reference="codeviewer" template_id="50"/>
+
+ <perm_templates_groups id="4" group_id="201" permission_reference="user" template_id="50"/>
+
+ <!-- Permission 'user' for Anyone group -->
+ <perm_templates_groups id="5" group_id="[null]" permission_reference="user" template_id="50"/>
+
+</dataset>
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
+ <perm_templates_users/>
+ <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="new_permission"/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
+ <perm_templates_users/>
+ <perm_templates_groups/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
+ <perm_templates_users/>
+ <perm_templates_groups id="1" template_id="1" group_id="[null]" permission_reference="new_permission"/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
+ <perm_templates_users/>
+ <perm_templates_groups/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
+ <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="new_permission"/>
+ <perm_templates_groups/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
+ <perm_templates_users/>
+ <perm_templates_groups/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="Môü Gnô Gnèçà ß" kee="mou_gno_gneca_20130102_010405" description="my description"
+ key_pattern="[null]"/>
+</dataset>
--- /dev/null
+<dataset></dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="my template" kee="my_template_20130102_010405" description="my description"
+ key_pattern="myregexp"/>
+</dataset>
--- /dev/null
+<dataset></dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <permission_templates id="2" name="other template" kee="my_template_20130102_030405" description="other description"
+ created_at="[null]" updated_at="[null]"/>
+
+ <perm_templates_users id="4" template_id="2" user_id="2" permission_reference="user_permission2"/>
+
+ <perm_templates_groups id="4" template_id="2" group_id="[null]" permission_reference="group_permission2"/>
+
+ <perm_tpl_characteristics id="2" template_id="2" permission_key="user" with_project_creator="[true]" created_at="1000000" updated_at="1000000"/>
+</dataset>
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"
+ created_at="[null]" updated_at="[null]"/>
+ <permission_templates id="2" name="other template" kee="my_template_20130102_030405" description="other description"
+ created_at="[null]" updated_at="[null]"/>
+
+ <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="user_permission1"/>
+ <perm_templates_users id="2" template_id="1" user_id="2" permission_reference="user_permission1"/>
+ <perm_templates_users id="3" template_id="1" user_id="2" permission_reference="user_permission2"/>
+ <perm_templates_users id="4" template_id="2" user_id="2" permission_reference="user_permission2"/>
+
+ <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="group_permission1"/>
+ <perm_templates_groups id="2" template_id="1" group_id="2" permission_reference="group_permission1"/>
+ <perm_templates_groups id="3" template_id="1" group_id="[null]" permission_reference="group_permission2"/>
+ <perm_templates_groups id="4" template_id="2" group_id="[null]" permission_reference="group_permission2"/>
+
+ <perm_tpl_characteristics id="1" template_id="1" permission_key="user" with_project_creator="[true]" created_at="1000000" updated_at="1000000"/>
+ <perm_tpl_characteristics id="2" template_id="2" permission_key="user" with_project_creator="[true]" created_at="1000000" updated_at="1000000"/>
+
+</dataset>
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
+ <perm_templates_users/>
+ <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
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
+ <perm_templates_users/>
+ <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
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
+ <perm_templates_users/>
+ <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>
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
+ <perm_templates_users/>
+ <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>
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
+
+ <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
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
+
+ <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
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
+ <perm_templates_users/>
+ <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission_other_group"/>
+</dataset>
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/>
+ <perm_templates_users/>
+ <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="permission_to_remove2"/>
+</dataset>
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="template1" kee="template1_20130102_030405" description="description1"
+ created_at="[null]" updated_at="[null]"/>
+ <permission_templates id="2" name="template2" kee="template2_20130102_030405" description="description2"
+ created_at="[null]" updated_at="[null]"/>
+ <permission_templates id="3" name="template3" kee="template3_20130102_030405" description="description3"
+ created_at="[null]" updated_at="[null]"/>
+</dataset>
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"
+ created_at="[null]" updated_at="[null]"/>
+
+ <users id="1" login="user1" name="user1"/>
+ <users id="2" login="user2" name="user2"/>
+
+ <groups id="1" name="group1"/>
+ <groups id="2" name="group2"/>
+</dataset>
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"
+ created_at="[null]" updated_at="[null]"/>
+
+ <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="user_permission1"/>
+ <perm_templates_users id="2" template_id="1" user_id="2" permission_reference="user_permission1"/>
+ <perm_templates_users id="3" template_id="1" user_id="2" permission_reference="user_permission2"/>
+
+ <users id="1" login="login1" name="user1"/>
+ <users id="2" login="login2" name="user2"/>
+
+ <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="group_permission1"/>
+ <perm_templates_groups id="2" template_id="1" group_id="2" permission_reference="group_permission1"/>
+ <!-- Anyone group -->
+ <perm_templates_groups id="3" template_id="1" group_id="[null]" permission_reference="group_permission2"/>
+ <!-- Unmatched group -->
+ <perm_templates_groups id="4" template_id="1" group_id="999999" permission_reference="group_permission2"/>
+
+ <groups id="1" name="group1"/>
+ <groups id="2" name="group2"/>
+</dataset>
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="new_name" kee="template1_20130102_030405" description="new_description"
+ key_pattern="new_regexp" created_at="[null]" updated_at="[null]"/>
+ <permission_templates id="2" name="template2" kee="template2_20130102_030405" description="description2"
+ key_pattern="barregexp" created_at="[null]" updated_at="[null]"/>
+</dataset>
--- /dev/null
+<dataset>
+ <permission_templates id="1" name="template1" kee="template1_20130102_030405" description="description1"
+ key_pattern="fooregexp" created_at="[null]" updated_at="[null]"/>
+ <permission_templates id="2" name="template2" kee="template2_20130102_030405" description="description2"
+ key_pattern="barregexp" created_at="[null]" updated_at="[null]"/>
+</dataset>
--- /dev/null
+<dataset>
+
+ <users id="200" login="user1" name="User1" active="[true]"/>
+ <users id="201" login="user2" name="User2" active="[true]"/>
+ <users id="202" login="user3" name="User3" active="[true]"/>
+ <users id="999" login="disabledUser" name="disabledUser" active="[false]"/>
+
+ <perm_templates_users id="1" user_id="200" permission_reference="user" template_id="50"/>
+ <perm_templates_users id="2" user_id="200" permission_reference="admin" template_id="50"/>
+ <perm_templates_users id="3" user_id="200" permission_reference="codeviewer" template_id="50"/>
+
+ <perm_templates_users id="4" user_id="201" permission_reference="user" template_id="50"/>
+
+</dataset>
--- /dev/null
+<dataset>
+
+ <users id="200" login="user1" name="User1" active="[true]"/>
+ <users id="201" login="user2" name="User2" active="[true]"/>
+ <users id="202" login="user3" name="User3" active="[true]"/>
+
+ <perm_templates_users id="1" user_id="200" permission_reference="user" template_id="50"/>
+ <perm_templates_users id="2" user_id="200" permission_reference="admin" template_id="50"/>
+ <perm_templates_users id="3" user_id="200" permission_reference="codeviewer" template_id="50"/>
+
+ <perm_templates_users id="4" user_id="201" permission_reference="user" template_id="50"/>
+
+</dataset>
--- /dev/null
+<dataset>
+
+ <users id="200" login="user3" name="User3" active="[true]"/>
+ <users id="201" login="user1" name="User1" active="[true]"/>
+ <users id="202" login="user2" name="User2" active="[true]"/>
+
+ <perm_templates_users id="1" user_id="200" permission_reference="user" template_id="50"/>
+ <perm_templates_users id="2" user_id="200" permission_reference="admin" template_id="50"/>
+ <perm_templates_users id="3" user_id="200" permission_reference="codeviewer" template_id="50"/>
+
+ <perm_templates_users id="4" user_id="201" permission_reference="user" template_id="50"/>
+
+</dataset>