Search in sources :

Example 91 with PermissionTemplateDto

use of org.sonar.db.permission.template.PermissionTemplateDto in project sonarqube by SonarSource.

the class TemplateUsersActionTest method search_for_users_with_response_example.

@Test
public void search_for_users_with_response_example() throws Exception {
    UserDto user1 = insertUser(newUserDto().setLogin("admin").setName("Administrator").setEmail("admin@admin.com"));
    UserDto user2 = insertUser(newUserDto().setLogin("george.orwell").setName("George Orwell").setEmail("george.orwell@1984.net"));
    PermissionTemplateDto template1 = addTemplateToDefaultOrganization();
    addUserToTemplate(newPermissionTemplateUser(CODEVIEWER, template1, user1));
    addUserToTemplate(newPermissionTemplateUser(CODEVIEWER, template1, user2));
    addUserToTemplate(newPermissionTemplateUser(ADMIN, template1, user2));
    loginAsAdmin(db.getDefaultOrganization());
    String result = newRequest(null, template1.getUuid()).execute().getInput();
    assertJson(result).isSimilarTo(getClass().getResource("template_users-example.json"));
}
Also used : PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) UserDto(org.sonar.db.user.UserDto) PermissionTemplateUserDto(org.sonar.db.permission.template.PermissionTemplateUserDto) UserTesting.newUserDto(org.sonar.db.user.UserTesting.newUserDto) PermissionTemplateTesting.newPermissionTemplateUserDto(org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateUserDto) BasePermissionWsTest(org.sonar.server.permission.ws.BasePermissionWsTest) Test(org.junit.Test)

Example 92 with PermissionTemplateDto

use of org.sonar.db.permission.template.PermissionTemplateDto in project sonarqube by SonarSource.

the class TemplateUsersActionTest method fail_if_template_uuid_and_name_provided.

@Test
public void fail_if_template_uuid_and_name_provided() throws Exception {
    PermissionTemplateDto template = addTemplateToDefaultOrganization();
    loginAsAdmin(db.getDefaultOrganization());
    expectedException.expect(BadRequestException.class);
    newRequest(null, template.getUuid()).setParam(PARAM_TEMPLATE_NAME, template.getName()).execute();
}
Also used : PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) BasePermissionWsTest(org.sonar.server.permission.ws.BasePermissionWsTest) Test(org.junit.Test)

Example 93 with PermissionTemplateDto

use of org.sonar.db.permission.template.PermissionTemplateDto in project sonarqube by SonarSource.

the class TemplateUsersActionTest method search_using_text_query.

@Test
public void search_using_text_query() throws Exception {
    loginAsAdmin(db.getDefaultOrganization());
    UserDto user1 = insertUser(newUserDto().setLogin("login-1").setName("name-1").setEmail("email-1"));
    UserDto user2 = insertUser(newUserDto().setLogin("login-2").setName("name-2").setEmail("email-2"));
    UserDto user3 = insertUser(newUserDto().setLogin("login-3").setName("name-3").setEmail("email-3"));
    PermissionTemplateDto template = addTemplateToDefaultOrganization();
    addUserToTemplate(newPermissionTemplateUser(USER, template, user1));
    addUserToTemplate(newPermissionTemplateUser(USER, template, user2));
    addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user1));
    addUserToTemplate(newPermissionTemplateUser(ISSUE_ADMIN, template, user3));
    PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
    addUserToTemplate(newPermissionTemplateUser(USER, anotherTemplate, user1));
    InputStream bytes = newRequest(null, null).setParam(PARAM_TEMPLATE_NAME, template.getName()).setParam(WebService.Param.TEXT_QUERY, "ame-1").setMediaType(PROTOBUF).execute().getInputStream();
    WsPermissions.UsersWsResponse response = WsPermissions.UsersWsResponse.parseFrom(bytes);
    assertThat(response.getUsersList()).extracting("login").containsOnly("login-1");
}
Also used : PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) InputStream(java.io.InputStream) UserDto(org.sonar.db.user.UserDto) PermissionTemplateUserDto(org.sonar.db.permission.template.PermissionTemplateUserDto) UserTesting.newUserDto(org.sonar.db.user.UserTesting.newUserDto) PermissionTemplateTesting.newPermissionTemplateUserDto(org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateUserDto) WsPermissions(org.sonarqube.ws.WsPermissions) BasePermissionWsTest(org.sonar.server.permission.ws.BasePermissionWsTest) Test(org.junit.Test)

Example 94 with PermissionTemplateDto

use of org.sonar.db.permission.template.PermissionTemplateDto in project sonarqube by SonarSource.

the class PermissionTemplateService method findTemplate.

/**
   * Return the permission template for the given component. If no template key pattern match then consider default
   * template for the component qualifier.
   */
@CheckForNull
private PermissionTemplateDto findTemplate(DbSession dbSession, String organizationUuid, ComponentDto component) {
    List<PermissionTemplateDto> allPermissionTemplates = dbClient.permissionTemplateDao().selectAll(dbSession, organizationUuid, null);
    List<PermissionTemplateDto> matchingTemplates = new ArrayList<>();
    for (PermissionTemplateDto permissionTemplateDto : allPermissionTemplates) {
        String keyPattern = permissionTemplateDto.getKeyPattern();
        if (StringUtils.isNotBlank(keyPattern) && component.getKey().matches(keyPattern)) {
            matchingTemplates.add(permissionTemplateDto);
        }
    }
    checkAtMostOneMatchForComponentKey(component.getKey(), matchingTemplates);
    if (matchingTemplates.size() == 1) {
        return matchingTemplates.get(0);
    }
    DefaultTemplates defaultTemplates = dbClient.organizationDao().getDefaultTemplates(dbSession, organizationUuid).orElseThrow(() -> new IllegalStateException(format("No Default templates defined for organization with uuid '%s'", organizationUuid)));
    String qualifier = component.qualifier();
    DefaultTemplatesResolverImpl.ResolvedDefaultTemplates resolvedDefaultTemplates = defaultTemplatesResolver.resolve(defaultTemplates);
    if (Qualifiers.PROJECT.equals(qualifier)) {
        return dbClient.permissionTemplateDao().selectByUuid(dbSession, resolvedDefaultTemplates.getProject());
    } else if (Qualifiers.VIEW.equals(qualifier)) {
        String viewDefaultTemplateUuid = resolvedDefaultTemplates.getView().orElseThrow(() -> new IllegalStateException("Attempt to create a view when Governance plugin is not installed"));
        return dbClient.permissionTemplateDao().selectByUuid(dbSession, viewDefaultTemplateUuid);
    } else {
        throw new IllegalArgumentException(format("Qualifier '%s' is not supported", qualifier));
    }
}
Also used : PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) DefaultTemplatesResolverImpl(org.sonar.server.permission.ws.template.DefaultTemplatesResolverImpl) ArrayList(java.util.ArrayList) DefaultTemplates(org.sonar.db.organization.DefaultTemplates) CheckForNull(javax.annotation.CheckForNull)

Example 95 with PermissionTemplateDto

use of org.sonar.db.permission.template.PermissionTemplateDto in project sonarqube by SonarSource.

the class RegisterPermissionTemplatesTest method insert_default_permission_template_if_fresh_install.

@Test
public void insert_default_permission_template_if_fresh_install() {
    db.users().insertGroup(db.getDefaultOrganization(), DefaultGroups.ADMINISTRATORS);
    underTest.start();
    PermissionTemplateDto defaultTemplate = selectTemplate();
    assertThat(defaultTemplate.getName()).isEqualTo("Default template");
    List<PermissionTemplateGroupDto> groupPermissions = selectGroupPermissions(defaultTemplate);
    assertThat(groupPermissions).hasSize(4);
    expectGroupPermission(groupPermissions, UserRole.ADMIN, DefaultGroups.ADMINISTRATORS);
    expectGroupPermission(groupPermissions, UserRole.ISSUE_ADMIN, DefaultGroups.ADMINISTRATORS);
    expectGroupPermission(groupPermissions, UserRole.CODEVIEWER, DefaultGroups.ANYONE);
    expectGroupPermission(groupPermissions, UserRole.USER, DefaultGroups.ANYONE);
    verifyDefaultTemplates();
    assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty();
}
Also used : PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) PermissionTemplateTesting.newPermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto) PermissionTemplateGroupDto(org.sonar.db.permission.template.PermissionTemplateGroupDto) Test(org.junit.Test)

Aggregations

PermissionTemplateDto (org.sonar.db.permission.template.PermissionTemplateDto)97 Test (org.junit.Test)63 BasePermissionWsTest (org.sonar.server.permission.ws.BasePermissionWsTest)42 OrganizationDto (org.sonar.db.organization.OrganizationDto)33 GroupDto (org.sonar.db.user.GroupDto)17 DbSession (org.sonar.db.DbSession)16 UserDto (org.sonar.db.user.UserDto)15 PermissionTemplateGroupDto (org.sonar.db.permission.template.PermissionTemplateGroupDto)14 InputStream (java.io.InputStream)13 PermissionTemplateTesting.newPermissionTemplateDto (org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto)12 DefaultTemplates (org.sonar.db.organization.DefaultTemplates)10 PermissionTemplateUserDto (org.sonar.db.permission.template.PermissionTemplateUserDto)9 WsPermissions (org.sonarqube.ws.WsPermissions)9 PermissionTemplateTesting.newPermissionTemplateGroupDto (org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateGroupDto)8 GroupTesting.newGroupDto (org.sonar.db.user.GroupTesting.newGroupDto)8 PermissionTemplateTesting.newPermissionTemplateUserDto (org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateUserDto)7 UserTesting.newUserDto (org.sonar.db.user.UserTesting.newUserDto)7 WsGroupsResponse (org.sonarqube.ws.WsPermissions.WsGroupsResponse)7 ComponentDto (org.sonar.db.component.ComponentDto)6 Date (java.util.Date)5