Search in sources :

Example 21 with User

use of org.gitlab4j.api.models.User in project catma by forTEXT.

the class GitlabManagerPrivileged method createUser.

// it's more convenient to work with the User class internally, which is why this method exists
private User createUser(String email, String username, String password, String publicname, String provider) throws IOException {
    UserApi userApi = privilegedGitLabApi.getUserApi();
    if (password == null) {
        // generate a random password
        password = RandomStringUtils.random(12, 0, GitlabUtils.PWD_CHARS.length - 1, false, false, GitlabUtils.PWD_CHARS, new SecureRandom());
    }
    User user = new User();
    user.setEmail(email);
    user.setUsername(username);
    user.setName(publicname);
    user.setIsAdmin(false);
    user.setSkipConfirmation(true);
    if (provider != null) {
        Identity identity = new Identity();
        identity.setExternUid(username);
        identity.setProvider(provider);
        user.setIdentities(Collections.singletonList(identity));
    }
    try {
        // do not send a pwd reset link
        user = userApi.createUser(user, password, false);
        return user;
    } catch (GitLabApiException e) {
        throw new IOException("Failed to create user", e);
    }
}
Also used : User(org.gitlab4j.api.models.User) GitUser(de.catma.repository.git.GitUser) SecureRandom(java.security.SecureRandom) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException) UserApi(org.gitlab4j.api.UserApi) Identity(org.gitlab4j.api.models.Identity)

Example 22 with User

use of org.gitlab4j.api.models.User in project catma by forTEXT.

the class GitlabManagerPrivileged method updateLastLoginAndGetTermsOfUseConsent.

public boolean updateLastLoginAndGetTermsOfUseConsent(de.catma.user.User catmaUser) {
    try {
        UserApi userApi = privilegedGitLabApi.getUserApi();
        User user = userApi.getUser(catmaUser.getUserId());
        Optional<CustomAttribute> optionalLastLoginAtt = Optional.empty();
        if (user.getCustomAttributes() != null) {
            optionalLastLoginAtt = user.getCustomAttributes().stream().filter(attr -> attr.getKey().equals(CustomAttributeName.last_login.name())).findFirst();
        }
        if (!optionalLastLoginAtt.isPresent()) {
            logger.info(String.format("First Login of %1$s", user.getUsername()));
            userApi.createCustomAttribute(user.getId(), CustomAttributeName.last_login.name(), LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));
        } else {
            CustomAttribute lastLogin = optionalLastLoginAtt.get();
            logger.info(String.format("Last Login of %1$s was %2$s.", user.getUsername(), lastLogin.getValue()));
            lastLogin.setValue(LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));
            userApi.changeCustomAttribute(user.getId(), optionalLastLoginAtt.get());
        }
        if (user.getCustomAttributes() != null) {
            return Boolean.valueOf(user.getCustomAttributes().stream().filter(attr -> attr.getKey().equals(CustomAttributeName.terms_of_use_consent_given.name())).findFirst().orElse(new CustomAttribute().withValue(Boolean.FALSE.toString())).getValue());
        } else {
            return false;
        }
    } catch (GitLabApiException e) {
        logger.log(Level.SEVERE, "Could access custom attributes", e);
        return false;
    }
}
Also used : User(org.gitlab4j.api.models.User) GitUser(de.catma.repository.git.GitUser) CustomAttribute(org.gitlab4j.api.models.CustomAttribute) GitLabApiException(org.gitlab4j.api.GitLabApiException) UserApi(org.gitlab4j.api.UserApi)

Example 23 with User

use of org.gitlab4j.api.models.User in project catma by forTEXT.

the class GitlabManagerPrivileged method modifyUserAttributes.

@Override
public void modifyUserAttributes(int userId, String name, String password) throws IOException {
    try {
        User user = privilegedGitLabApi.getUserApi().getUser(userId);
        BiConsumer<String, Consumer<String>> fExecIfNotNull = (attr, func) -> {
            if (attr != null)
                func.accept(attr);
        };
        fExecIfNotNull.accept(name, user::setName);
        this.privilegedGitLabApi.getUserApi().updateUser(user, password);
    } catch (GitLabApiException e) {
        throw new IOException("failed to check for username", e);
    }
}
Also used : CustomAttribute(org.gitlab4j.api.models.CustomAttribute) Date(java.util.Date) LocalDateTime(java.time.LocalDateTime) Level(java.util.logging.Level) SecureRandom(java.security.SecureRandom) Scope(org.gitlab4j.api.models.PersonalAccessToken.Scope) BiConsumer(java.util.function.BiConsumer) Pair(de.catma.util.Pair) NotificationSettingsApi(org.gitlab4j.api.NotificationSettingsApi) PersonalAccessToken(org.gitlab4j.api.models.PersonalAccessToken) CATMAPropertyKey(de.catma.properties.CATMAPropertyKey) NotificationSettings(org.gitlab4j.api.models.NotificationSettings) GitlabUtils(de.catma.repository.git.GitlabUtils) IOException(java.io.IOException) Logger(java.util.logging.Logger) ZoneId(java.time.ZoneId) Consumer(java.util.function.Consumer) User(org.gitlab4j.api.models.User) List(java.util.List) ImpersonationState(org.gitlab4j.api.Constants.ImpersonationState) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) Identity(org.gitlab4j.api.models.Identity) Optional(java.util.Optional) UserApi(org.gitlab4j.api.UserApi) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) GitLabApiException(org.gitlab4j.api.GitLabApiException) Collections(java.util.Collections) GitLabApi(org.gitlab4j.api.GitLabApi) GitUser(de.catma.repository.git.GitUser) IRemoteGitManagerPrivileged(de.catma.repository.git.interfaces.IRemoteGitManagerPrivileged) User(org.gitlab4j.api.models.User) GitUser(de.catma.repository.git.GitUser) BiConsumer(java.util.function.BiConsumer) Consumer(java.util.function.Consumer) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException)

Example 24 with User

use of org.gitlab4j.api.models.User in project choerodon-starters by open-hand.

the class UserApi method getblockedUsers.

/**
 * Get a list of blocked users using the specified page and per page settings.
 * <p>
 * GET /users?blocked=true
 *
 * @param page    the page to get
 * @param perPage the number of users per page
 * @return the list of blocked Users in the specified range
 * @throws GitLabApiException if any exception occurs
 */
public List<User> getblockedUsers(int page, int perPage) throws GitLabApiException {
    GitLabApiForm formData = new GitLabApiForm().withParam("blocked", true).withParam(PAGE_PARAM, page).withParam(PER_PAGE_PARAM, perPage);
    Response response = get(Response.Status.OK, formData.asMap(), "users");
    return (response.readEntity(new GenericType<List<User>>() {
    }));
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) User(org.gitlab4j.api.models.User)

Example 25 with User

use of org.gitlab4j.api.models.User in project choerodon-starters by open-hand.

the class UserApi method findUsers.

/**
 * Search users by Email or username
 * <p>
 * GET /users?search=:email_or_username
 *
 * @param emailOrUsername the email or username to search for
 * @return the User List with the email or username like emailOrUsername
 * @throws GitLabApiException if any exception occurs
 */
public List<User> findUsers(String emailOrUsername) throws GitLabApiException {
    GitLabApiForm formData = new GitLabApiForm().withParam("search", emailOrUsername, true).withParam(PER_PAGE_PARAM, getDefaultPerPage());
    Response response = get(Response.Status.OK, formData.asMap(), "users");
    return (response.readEntity(new GenericType<List<User>>() {
    }));
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) User(org.gitlab4j.api.models.User)

Aggregations

User (org.gitlab4j.api.models.User)29 Response (javax.ws.rs.core.Response)9 GitLabApiException (org.gitlab4j.api.GitLabApiException)8 GenericType (javax.ws.rs.core.GenericType)6 UserApi (org.gitlab4j.api.UserApi)6 Test (org.junit.Test)6 GitUser (de.catma.repository.git.GitUser)5 File (java.io.File)5 Project (org.gitlab4j.api.models.Project)4 IOException (java.io.IOException)3 CustomAttribute (org.gitlab4j.api.models.CustomAttribute)3 PersonalAccessToken (org.gitlab4j.api.models.PersonalAccessToken)3 ILocalGitRepositoryManager (de.catma.repository.git.interfaces.ILocalGitRepositoryManager)2 IRemoteGitServerManager (de.catma.repository.git.interfaces.IRemoteGitServerManager)2 GitLabServerManagerTest (de.catma.repository.git.managers.GitLabServerManagerTest)2 PersonalAccessToken (de.catma.repository.git.managers.gitlab4j_api_custom.models.PersonalAccessToken)2 Pair (de.catma.util.Pair)2 SecureRandom (java.security.SecureRandom)2 List (java.util.List)2 Form (javax.ws.rs.core.Form)2