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"));
}
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();
}
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");
}
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));
}
}
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();
}
Aggregations