Search in sources :

Example 1 with CreateWsResponse

use of org.sonarqube.ws.Users.CreateWsResponse in project sonarqube by SonarSource.

the class CreateAction method doHandle.

private CreateWsResponse doHandle(CreateRequest request) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        String login = request.getLogin();
        NewUser.Builder newUser = NewUser.builder().setLogin(login).setName(request.getName()).setEmail(request.getEmail()).setScmAccounts(request.getScmAccounts()).setPassword(request.getPassword());
        if (!request.isLocal()) {
            newUser.setExternalIdentity(new ExternalIdentity(SQ_AUTHORITY, login, login));
        }
        UserDto existingUser = dbClient.userDao().selectByLogin(dbSession, login);
        if (existingUser == null) {
            return buildResponse(userUpdater.createAndCommit(dbSession, newUser.build(), u -> {
            }));
        }
        checkArgument(!existingUser.isActive(), "An active user with login '%s' already exists", login);
        return buildResponse(userUpdater.reactivateAndCommit(dbSession, existingUser, newUser.build(), u -> {
        }));
    }
}
Also used : UserDto(org.sonar.db.user.UserDto) EmailValidator.isValidIfPresent(org.sonar.server.user.ws.EmailValidator.isValidIfPresent) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) NewUser(org.sonar.server.user.NewUser) DbSession(org.sonar.db.DbSession) UserUpdater(org.sonar.server.user.UserUpdater) NAME_MAX_LENGTH(org.sonar.server.user.UserUpdater.NAME_MAX_LENGTH) PARAM_PASSWORD(org.sonarqube.ws.client.user.UsersWsParameters.PARAM_PASSWORD) Request(org.sonar.api.server.ws.Request) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) PARAM_EMAIL(org.sonarqube.ws.client.user.UsersWsParameters.PARAM_EMAIL) WebService(org.sonar.api.server.ws.WebService) Response(org.sonar.api.server.ws.Response) Nullable(javax.annotation.Nullable) ACTION_CREATE(org.sonarqube.ws.client.user.UsersWsParameters.ACTION_CREATE) EMAIL_MAX_LENGTH(org.sonar.server.user.UserUpdater.EMAIL_MAX_LENGTH) LOGIN_MIN_LENGTH(org.sonar.server.user.UserUpdater.LOGIN_MIN_LENGTH) Collections.emptyList(java.util.Collections.emptyList) Optional.ofNullable(java.util.Optional.ofNullable) CreateWsResponse(org.sonarqube.ws.Users.CreateWsResponse) PARAM_LOGIN(org.sonarqube.ws.client.user.UsersWsParameters.PARAM_LOGIN) PARAM_LOCAL(org.sonarqube.ws.client.user.UsersWsParameters.PARAM_LOCAL) ExternalIdentity(org.sonar.server.user.ExternalIdentity) PARAM_NAME(org.sonarqube.ws.client.user.UsersWsParameters.PARAM_NAME) DbClient(org.sonar.db.DbClient) List(java.util.List) Strings.emptyToNull(com.google.common.base.Strings.emptyToNull) SQ_AUTHORITY(org.sonar.server.user.ExternalIdentity.SQ_AUTHORITY) Change(org.sonar.api.server.ws.Change) PARAM_SCM_ACCOUNT(org.sonarqube.ws.client.user.UsersWsParameters.PARAM_SCM_ACCOUNT) UserSession(org.sonar.server.user.UserSession) Collections(java.util.Collections) CheckForNull(javax.annotation.CheckForNull) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) LOGIN_MAX_LENGTH(org.sonar.server.user.UserUpdater.LOGIN_MAX_LENGTH) DbSession(org.sonar.db.DbSession) UserDto(org.sonar.db.user.UserDto) ExternalIdentity(org.sonar.server.user.ExternalIdentity) NewUser(org.sonar.server.user.NewUser)

Example 2 with CreateWsResponse

use of org.sonarqube.ws.Users.CreateWsResponse in project sonarqube by SonarSource.

the class CreateActionTest method create_user_with_comma_in_scm_account.

@Test
public void create_user_with_comma_in_scm_account() {
    logInAsSystemAdministrator();
    CreateWsResponse response = call(CreateRequest.builder().setLogin("john").setName("John").setEmail("john@email.com").setScmAccounts(singletonList("j,n")).setPassword("1234").build());
    assertThat(response.getUser().getScmAccountsList()).containsOnly("j,n");
}
Also used : CreateWsResponse(org.sonarqube.ws.Users.CreateWsResponse) Test(org.junit.Test)

Example 3 with CreateWsResponse

use of org.sonarqube.ws.Users.CreateWsResponse in project sonarqube by SonarSource.

the class CreateActionTest method create_user.

@Test
public void create_user() {
    logInAsSystemAdministrator();
    CreateWsResponse response = call(CreateRequest.builder().setLogin("john").setName("John").setEmail("john@email.com").setScmAccounts(singletonList("jn")).setPassword("1234").build());
    assertThat(response.getUser()).extracting(User::getLogin, User::getName, User::getEmail, User::getScmAccountsList, User::getLocal).containsOnly("john", "John", "john@email.com", singletonList("jn"), true);
    // exists in index
    assertThat(es.client().search(EsClient.prepareSearch(UserIndexDefinition.TYPE_USER).source(new SearchSourceBuilder().query(boolQuery().must(termQuery(FIELD_LOGIN, "john")).must(termQuery(FIELD_NAME, "John")).must(termQuery(FIELD_EMAIL, "john@email.com")).must(termQuery(FIELD_SCM_ACCOUNTS, "jn"))))).getHits().getHits()).hasSize(1);
    // exists in db
    Optional<UserDto> dbUser = db.users().selectUserByLogin("john");
    assertThat(dbUser).isPresent();
    assertThat(dbUser.get().isRoot()).isFalse();
    // member of default group
    assertThat(db.users().selectGroupUuidsOfUser(dbUser.get())).containsOnly(defaultGroup.getUuid());
}
Also used : UserDto(org.sonar.db.user.UserDto) UserTesting.newUserDto(org.sonar.db.user.UserTesting.newUserDto) CreateWsResponse(org.sonarqube.ws.Users.CreateWsResponse) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) Test(org.junit.Test)

Aggregations

CreateWsResponse (org.sonarqube.ws.Users.CreateWsResponse)3 Test (org.junit.Test)2 UserDto (org.sonar.db.user.UserDto)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Strings.emptyToNull (com.google.common.base.Strings.emptyToNull)1 Strings.isNullOrEmpty (com.google.common.base.Strings.isNullOrEmpty)1 Collections (java.util.Collections)1 Collections.emptyList (java.util.Collections.emptyList)1 List (java.util.List)1 Optional.ofNullable (java.util.Optional.ofNullable)1 CheckForNull (javax.annotation.CheckForNull)1 Nullable (javax.annotation.Nullable)1 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)1 Change (org.sonar.api.server.ws.Change)1 Request (org.sonar.api.server.ws.Request)1 Response (org.sonar.api.server.ws.Response)1 WebService (org.sonar.api.server.ws.WebService)1 DbClient (org.sonar.db.DbClient)1 DbSession (org.sonar.db.DbSession)1 UserTesting.newUserDto (org.sonar.db.user.UserTesting.newUserDto)1