Search in sources :

Example 1 with ADMINISTER

use of org.sonar.db.permission.OrganizationPermission.ADMINISTER in project sonarqube by SonarSource.

the class UpdateAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    userSession.checkLoggedIn();
    try (DbSession dbSession = dbClient.openSession(false)) {
        organizationFlags.checkEnabled(dbSession);
        String key = request.mandatoryParam(PARAM_KEY);
        UpdateOrganizationRequest updateRequest = new UpdateOrganizationRequest(request.getParam(PARAM_NAME, (rqt, paramKey) -> wsSupport.getAndCheckName(rqt)), request.getParam(PARAM_DESCRIPTION, (rqt, paramKey) -> emptyAsNull(wsSupport.getAndCheckDescription(rqt))), request.getParam(PARAM_URL, (rqt, paramKey) -> emptyAsNull(wsSupport.getAndCheckUrl(rqt))), request.getParam(PARAM_AVATAR_URL, (rqt, paramKey) -> emptyAsNull(wsSupport.getAndCheckAvatar(rqt))));
        OrganizationDto dto = getDto(dbSession, key);
        userSession.checkPermission(ADMINISTER, dto);
        dto.setName(updateRequest.getName().or(dto::getName)).setDescription(updateRequest.getDescription().or(dto::getDescription)).setUrl(updateRequest.getUrl().or(dto::getUrl)).setAvatarUrl(updateRequest.getAvatar().or(dto::getAvatarUrl));
        dbClient.organizationDao().update(dbSession, dto);
        dbSession.commit();
        writeResponse(request, response, dto);
    }
}
Also used : OrganizationFlags(org.sonar.server.organization.OrganizationFlags) PARAM_AVATAR_URL(org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_AVATAR_URL) ADMINISTER(org.sonar.db.permission.OrganizationPermission.ADMINISTER) PARAM_KEY(org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_KEY) DbSession(org.sonar.db.DbSession) NotFoundException(org.sonar.server.exceptions.NotFoundException) String.format(java.lang.String.format) Organizations(org.sonarqube.ws.Organizations) Request(org.sonar.api.server.ws.Request) DbClient(org.sonar.db.DbClient) WebService(org.sonar.api.server.ws.WebService) PARAM_DESCRIPTION(org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_DESCRIPTION) PARAM_URL(org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_URL) Response(org.sonar.api.server.ws.Response) Optional(java.util.Optional) OrganizationDto(org.sonar.db.organization.OrganizationDto) UserSession(org.sonar.server.user.UserSession) PARAM_NAME(org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_NAME) CheckForNull(javax.annotation.CheckForNull) Nullable(javax.annotation.Nullable) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) DbSession(org.sonar.db.DbSession) OrganizationDto(org.sonar.db.organization.OrganizationDto)

Example 2 with ADMINISTER

use of org.sonar.db.permission.OrganizationPermission.ADMINISTER in project sonarqube by SonarSource.

the class SearchMyOrganizationsActionTest method returns_organization_of_authenticated_user_only_for_ADMIN_permission.

@Test
public void returns_organization_of_authenticated_user_only_for_ADMIN_permission() {
    UserDto user = dbTester.users().insertUser();
    OrganizationDto organization1 = dbTester.organizations().insert();
    OrganizationDto organization2 = dbTester.organizations().insert();
    GroupDto group = dbTester.users().insertGroup(organization2);
    dbTester.users().insertMember(group, user);
    OrganizationPermission.all().filter(p -> p != ADMINISTER).forEach(p -> {
        dbTester.users().insertPermissionOnUser(organization1, user, p);
        dbTester.users().insertPermissionOnGroup(group, p);
    });
    userSessionRule.logIn(user);
    assertJson(underTest.newRequest().execute().getInput()).isSimilarTo(NO_ORGANIZATIONS_RESPONSE);
}
Also used : GroupDto(org.sonar.db.user.GroupDto) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) System2(org.sonar.api.utils.System2) UserDto(org.sonar.db.user.UserDto) ADMINISTER(org.sonar.db.permission.OrganizationPermission.ADMINISTER) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) JsonAssert.assertJson(org.sonar.test.JsonAssert.assertJson) Test(org.junit.Test) OrganizationPermission(org.sonar.db.permission.OrganizationPermission) WsActionTester(org.sonar.server.ws.WsActionTester) DbClient(org.sonar.db.DbClient) Rule(org.junit.Rule) WebService(org.sonar.api.server.ws.WebService) SYSTEM_ADMIN(org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN) OrganizationDto(org.sonar.db.organization.OrganizationDto) TestResponse(org.sonar.server.ws.TestResponse) UserDto(org.sonar.db.user.UserDto) GroupDto(org.sonar.db.user.GroupDto) OrganizationDto(org.sonar.db.organization.OrganizationDto) Test(org.junit.Test)

Aggregations

WebService (org.sonar.api.server.ws.WebService)2 DbClient (org.sonar.db.DbClient)2 OrganizationDto (org.sonar.db.organization.OrganizationDto)2 ADMINISTER (org.sonar.db.permission.OrganizationPermission.ADMINISTER)2 String.format (java.lang.String.format)1 Optional (java.util.Optional)1 CheckForNull (javax.annotation.CheckForNull)1 Nullable (javax.annotation.Nullable)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Rule (org.junit.Rule)1 Test (org.junit.Test)1 Request (org.sonar.api.server.ws.Request)1 Response (org.sonar.api.server.ws.Response)1 System2 (org.sonar.api.utils.System2)1 SYSTEM_ADMIN (org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN)1 DbSession (org.sonar.db.DbSession)1 DbTester (org.sonar.db.DbTester)1 OrganizationPermission (org.sonar.db.permission.OrganizationPermission)1 GroupDto (org.sonar.db.user.GroupDto)1 UserDto (org.sonar.db.user.UserDto)1