Search in sources :

Example 16 with UserApi

use of org.gitlab4j.api.UserApi 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 17 with UserApi

use of org.gitlab4j.api.UserApi 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)

Aggregations

UserApi (org.gitlab4j.api.UserApi)17 GitLabApiException (org.gitlab4j.api.GitLabApiException)7 User (org.gitlab4j.api.models.User)5 GitUser (de.catma.repository.git.GitUser)4 IOException (java.io.IOException)3 PersonalAccessToken (org.gitlab4j.api.models.PersonalAccessToken)3 AfterEach (org.junit.jupiter.api.AfterEach)3 EventBus (com.google.common.eventbus.EventBus)2 BackgroundService (de.catma.backgroundservice.BackgroundService)2 File (java.io.File)2 GitLabApi (org.gitlab4j.api.GitLabApi)2 GroupApi (org.gitlab4j.api.GroupApi)2 CustomAttribute (org.gitlab4j.api.models.CustomAttribute)2 Pair (de.catma.util.Pair)1 SecureRandom (java.security.SecureRandom)1 ProjectApi (org.gitlab4j.api.ProjectApi)1 Identity (org.gitlab4j.api.models.Identity)1 Project (org.gitlab4j.api.models.Project)1 After (org.junit.After)1 Test (org.junit.jupiter.api.Test)1