UserApi.java 54.31 KiB
package org.gitlab4j.api;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.CustomAttribute;
import org.gitlab4j.api.models.Email;
import org.gitlab4j.api.models.GpgKey;
import org.gitlab4j.api.models.ImpersonationToken;
import org.gitlab4j.api.models.ImpersonationToken.Scope;
import org.gitlab4j.api.models.SshKey;
import org.gitlab4j.api.models.User;
import org.gitlab4j.api.utils.EmailChecker;
/**
 * This class provides an entry point to all the GitLab API users calls.
 * @see <a href="https://docs.gitlab.com/ce/api/users.html">Users API at GitLab</a>
public class UserApi extends AbstractApi {
    private boolean customAttributesEnabled = false;
    public UserApi(GitLabApi gitLabApi) {
        super(gitLabApi);
    /**
     * Enables custom attributes to be returned when fetching User instances.
    public void enableCustomAttributes() {
        customAttributesEnabled = true;
    /**
     * Disables custom attributes to be returned when fetching User instances.
    public void disableCustomAttributes() {
        customAttributesEnabled = false;
    /**
     * <p>Get a list of users.</p>
     * <strong>WARNING:</strong> Do not use this method to fetch users from https://gitlab.com,
     * gitlab.com has many 1,000,000's of users and it will a long time to fetch all of them.
     * Instead use {@link #getUsers(int itemsPerPage)} which will return a Pager of Group instances.
     * <pre><code>GitLab Endpoint: GET /users</code></pre>
     * @return a list of Users
     * @throws GitLabApiException if any exception occurs
    public List<User> getUsers() throws GitLabApiException {
        String url = this.gitLabApi.getGitLabServerUrl();
        if (url.startsWith("https://gitlab.com")) {
            GitLabApi.getLogger().warning("Fetching all users from " + url +
                    " may take many minutes to complete, use Pager<User> getUsers(int) instead.");
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
return (getUsers(getDefaultPerPage()).all()); } /** * Get a list of users using the specified page and per page settings. * * <pre><code>GitLab Endpoint: GET /users</code></pre> * * @param page the page to get * @param perPage the number of users per page * @return the list of Users in the specified range * @throws GitLabApiException if any exception occurs */ public List<User> getUsers(int page, int perPage) throws GitLabApiException { Response response = get(Response.Status.OK, getPageQueryParams(page, perPage, customAttributesEnabled), "users"); return (response.readEntity(new GenericType<List<User>>() {})); } /** * Get a Pager of users. * * <pre><code>GitLab Endpoint: GET /users</code></pre> * * @param itemsPerPage the number of User instances that will be fetched per page * @return a Pager of User * @throws GitLabApiException if any exception occurs */ public Pager<User> getUsers(int itemsPerPage) throws GitLabApiException { return (new Pager<User>(this, User.class, itemsPerPage, createGitLabApiForm().asMap(), "users")); } /** * Get a Stream of users. * * <pre><code>GitLab Endpoint: GET /users</code></pre> * * @return a Stream of Users. * @throws GitLabApiException if any exception occurs */ public Stream<User> getUsersStream() throws GitLabApiException { return (getUsers(getDefaultPerPage()).stream()); } /** * Get a list of active users * * <pre><code>GitLab Endpoint: GET /users?active=true</code></pre> * * @return a list of active Users * @throws GitLabApiException if any exception occurs */ public List<User> getActiveUsers() throws GitLabApiException { return (getActiveUsers(getDefaultPerPage()).all()); } /** * Get a list of active users using the specified page and per page settings. * * <pre><code>GitLab Endpoint: GET /users?active=true</code></pre> * * @param page the page to get * @param perPage the number of users per page * @return the list of active Users in the specified range * @throws GitLabApiException if any exception occurs */ public List<User> getActiveUsers(int page, int perPage) throws GitLabApiException { GitLabApiForm formData = createGitLabApiForm() .withParam("active", true) .withParam(PAGE_PARAM, page) .withParam(PER_PAGE_PARAM, perPage);