UserApi.java 47.12 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.ImpersonationToken;
import org.gitlab4j.api.models.ImpersonationToken.Scope;
import org.gitlab4j.api.models.SshKey;
import org.gitlab4j.api.models.User;
/**
 * 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;
    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;
    /**
     * Get a list of users.
     * <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 {
        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
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
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); Response response = get(Response.Status.OK, formData.asMap(), "users"); return (response.readEntity(new GenericType<List<User>>() {})); } /** * Get a Pager of active users. * * <pre><code>GitLab Endpoint: GET /users?active=true</code></pre> * * @param itemsPerPage the number of active User instances that will be fetched per page * @return a Pager of active User * @throws GitLabApiException if any exception occurs */