...

Source file src/github.com/xanzy/go-gitlab/users.go

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, Sander van Harmelen
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package gitlab
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"net"
    23  	"net/http"
    24  	"time"
    25  )
    26  
    27  // List a couple of standard errors.
    28  var (
    29  	ErrUserActivatePrevented         = errors.New("Cannot activate a user that is blocked by admin or by LDAP synchronization")
    30  	ErrUserApprovePrevented          = errors.New("Cannot approve a user that is blocked by admin or by LDAP synchronization")
    31  	ErrUserBlockPrevented            = errors.New("Cannot block a user that is already blocked by LDAP synchronization")
    32  	ErrUserConflict                  = errors.New("User does not have a pending request")
    33  	ErrUserDeactivatePrevented       = errors.New("Cannot deactivate a user that is blocked by admin or by LDAP synchronization")
    34  	ErrUserDisableTwoFactorPrevented = errors.New("Cannot disable two factor authentication if not authenticated as administrator")
    35  	ErrUserNotFound                  = errors.New("User does not exist")
    36  	ErrUserRejectPrevented           = errors.New("Cannot reject a user if not authenticated as administrator")
    37  	ErrUserTwoFactorNotEnabled       = errors.New("Cannot disable two factor authentication if not enabled")
    38  	ErrUserUnblockPrevented          = errors.New("Cannot unblock a user that is blocked by LDAP synchronization")
    39  )
    40  
    41  // UsersService handles communication with the user related methods of
    42  // the GitLab API.
    43  //
    44  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html
    45  type UsersService struct {
    46  	client *Client
    47  }
    48  
    49  // BasicUser included in other service responses (such as merge requests, pipelines, etc).
    50  type BasicUser struct {
    51  	ID        int        `json:"id"`
    52  	Username  string     `json:"username"`
    53  	Name      string     `json:"name"`
    54  	State     string     `json:"state"`
    55  	CreatedAt *time.Time `json:"created_at"`
    56  	AvatarURL string     `json:"avatar_url"`
    57  	WebURL    string     `json:"web_url"`
    58  }
    59  
    60  // User represents a GitLab user.
    61  //
    62  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html
    63  type User struct {
    64  	ID                             int                `json:"id"`
    65  	Username                       string             `json:"username"`
    66  	Email                          string             `json:"email"`
    67  	Name                           string             `json:"name"`
    68  	State                          string             `json:"state"`
    69  	WebURL                         string             `json:"web_url"`
    70  	CreatedAt                      *time.Time         `json:"created_at"`
    71  	Bio                            string             `json:"bio"`
    72  	Bot                            bool               `json:"bot"`
    73  	Location                       string             `json:"location"`
    74  	PublicEmail                    string             `json:"public_email"`
    75  	Skype                          string             `json:"skype"`
    76  	Linkedin                       string             `json:"linkedin"`
    77  	Twitter                        string             `json:"twitter"`
    78  	WebsiteURL                     string             `json:"website_url"`
    79  	Organization                   string             `json:"organization"`
    80  	JobTitle                       string             `json:"job_title"`
    81  	ExternUID                      string             `json:"extern_uid"`
    82  	Provider                       string             `json:"provider"`
    83  	ThemeID                        int                `json:"theme_id"`
    84  	LastActivityOn                 *ISOTime           `json:"last_activity_on"`
    85  	ColorSchemeID                  int                `json:"color_scheme_id"`
    86  	IsAdmin                        bool               `json:"is_admin"`
    87  	AvatarURL                      string             `json:"avatar_url"`
    88  	CanCreateGroup                 bool               `json:"can_create_group"`
    89  	CanCreateProject               bool               `json:"can_create_project"`
    90  	ProjectsLimit                  int                `json:"projects_limit"`
    91  	CurrentSignInAt                *time.Time         `json:"current_sign_in_at"`
    92  	CurrentSignInIP                *net.IP            `json:"current_sign_in_ip"`
    93  	LastSignInAt                   *time.Time         `json:"last_sign_in_at"`
    94  	LastSignInIP                   *net.IP            `json:"last_sign_in_ip"`
    95  	ConfirmedAt                    *time.Time         `json:"confirmed_at"`
    96  	TwoFactorEnabled               bool               `json:"two_factor_enabled"`
    97  	Note                           string             `json:"note"`
    98  	Identities                     []*UserIdentity    `json:"identities"`
    99  	External                       bool               `json:"external"`
   100  	PrivateProfile                 bool               `json:"private_profile"`
   101  	SharedRunnersMinutesLimit      int                `json:"shared_runners_minutes_limit"`
   102  	ExtraSharedRunnersMinutesLimit int                `json:"extra_shared_runners_minutes_limit"`
   103  	UsingLicenseSeat               bool               `json:"using_license_seat"`
   104  	CustomAttributes               []*CustomAttribute `json:"custom_attributes"`
   105  	NamespaceID                    int                `json:"namespace_id"`
   106  	Locked                         bool               `json:"locked"`
   107  }
   108  
   109  // UserIdentity represents a user identity.
   110  type UserIdentity struct {
   111  	Provider  string `json:"provider"`
   112  	ExternUID string `json:"extern_uid"`
   113  }
   114  
   115  // ListUsersOptions represents the available ListUsers() options.
   116  //
   117  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#list-users
   118  type ListUsersOptions struct {
   119  	ListOptions
   120  	Active          *bool `url:"active,omitempty" json:"active,omitempty"`
   121  	Blocked         *bool `url:"blocked,omitempty" json:"blocked,omitempty"`
   122  	ExcludeInternal *bool `url:"exclude_internal,omitempty" json:"exclude_internal,omitempty"`
   123  	ExcludeExternal *bool `url:"exclude_external,omitempty" json:"exclude_external,omitempty"`
   124  
   125  	// The options below are only available for admins.
   126  	Search               *string    `url:"search,omitempty" json:"search,omitempty"`
   127  	Username             *string    `url:"username,omitempty" json:"username,omitempty"`
   128  	ExternalUID          *string    `url:"extern_uid,omitempty" json:"extern_uid,omitempty"`
   129  	Provider             *string    `url:"provider,omitempty" json:"provider,omitempty"`
   130  	CreatedBefore        *time.Time `url:"created_before,omitempty" json:"created_before,omitempty"`
   131  	CreatedAfter         *time.Time `url:"created_after,omitempty" json:"created_after,omitempty"`
   132  	OrderBy              *string    `url:"order_by,omitempty" json:"order_by,omitempty"`
   133  	Sort                 *string    `url:"sort,omitempty" json:"sort,omitempty"`
   134  	TwoFactor            *string    `url:"two_factor,omitempty" json:"two_factor,omitempty"`
   135  	Admins               *bool      `url:"admins,omitempty" json:"admins,omitempty"`
   136  	External             *bool      `url:"external,omitempty" json:"external,omitempty"`
   137  	WithoutProjects      *bool      `url:"without_projects,omitempty" json:"without_projects,omitempty"`
   138  	WithCustomAttributes *bool      `url:"with_custom_attributes,omitempty" json:"with_custom_attributes,omitempty"`
   139  	WithoutProjectBots   *bool      `url:"without_project_bots,omitempty" json:"without_project_bots,omitempty"`
   140  }
   141  
   142  // ListUsers gets a list of users.
   143  //
   144  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#list-users
   145  func (s *UsersService) ListUsers(opt *ListUsersOptions, options ...RequestOptionFunc) ([]*User, *Response, error) {
   146  	req, err := s.client.NewRequest(http.MethodGet, "users", opt, options)
   147  	if err != nil {
   148  		return nil, nil, err
   149  	}
   150  
   151  	var usr []*User
   152  	resp, err := s.client.Do(req, &usr)
   153  	if err != nil {
   154  		return nil, resp, err
   155  	}
   156  
   157  	return usr, resp, nil
   158  }
   159  
   160  // GetUsersOptions represents the available GetUser() options.
   161  //
   162  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#single-user
   163  type GetUsersOptions struct {
   164  	WithCustomAttributes *bool `url:"with_custom_attributes,omitempty" json:"with_custom_attributes,omitempty"`
   165  }
   166  
   167  // GetUser gets a single user.
   168  //
   169  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#single-user
   170  func (s *UsersService) GetUser(user int, opt GetUsersOptions, options ...RequestOptionFunc) (*User, *Response, error) {
   171  	u := fmt.Sprintf("users/%d", user)
   172  
   173  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   174  	if err != nil {
   175  		return nil, nil, err
   176  	}
   177  
   178  	usr := new(User)
   179  	resp, err := s.client.Do(req, usr)
   180  	if err != nil {
   181  		return nil, resp, err
   182  	}
   183  
   184  	return usr, resp, nil
   185  }
   186  
   187  // CreateUserOptions represents the available CreateUser() options.
   188  //
   189  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#user-creation
   190  type CreateUserOptions struct {
   191  	Email               *string `url:"email,omitempty" json:"email,omitempty"`
   192  	Password            *string `url:"password,omitempty" json:"password,omitempty"`
   193  	ResetPassword       *bool   `url:"reset_password,omitempty" json:"reset_password,omitempty"`
   194  	ForceRandomPassword *bool   `url:"force_random_password,omitempty" json:"force_random_password,omitempty"`
   195  	Username            *string `url:"username,omitempty" json:"username,omitempty"`
   196  	Name                *string `url:"name,omitempty" json:"name,omitempty"`
   197  	Skype               *string `url:"skype,omitempty" json:"skype,omitempty"`
   198  	Linkedin            *string `url:"linkedin,omitempty" json:"linkedin,omitempty"`
   199  	Twitter             *string `url:"twitter,omitempty" json:"twitter,omitempty"`
   200  	WebsiteURL          *string `url:"website_url,omitempty" json:"website_url,omitempty"`
   201  	Organization        *string `url:"organization,omitempty" json:"organization,omitempty"`
   202  	JobTitle            *string `url:"job_title,omitempty" json:"job_title,omitempty"`
   203  	ProjectsLimit       *int    `url:"projects_limit,omitempty" json:"projects_limit,omitempty"`
   204  	ExternUID           *string `url:"extern_uid,omitempty" json:"extern_uid,omitempty"`
   205  	Provider            *string `url:"provider,omitempty" json:"provider,omitempty"`
   206  	Bio                 *string `url:"bio,omitempty" json:"bio,omitempty"`
   207  	Location            *string `url:"location,omitempty" json:"location,omitempty"`
   208  	Admin               *bool   `url:"admin,omitempty" json:"admin,omitempty"`
   209  	CanCreateGroup      *bool   `url:"can_create_group,omitempty" json:"can_create_group,omitempty"`
   210  	SkipConfirmation    *bool   `url:"skip_confirmation,omitempty" json:"skip_confirmation,omitempty"`
   211  	External            *bool   `url:"external,omitempty" json:"external,omitempty"`
   212  	PrivateProfile      *bool   `url:"private_profile,omitempty" json:"private_profile,omitempty"`
   213  	Note                *string `url:"note,omitempty" json:"note,omitempty"`
   214  	ThemeID             *int    `url:"theme_id,omitempty" json:"theme_id,omitempty"`
   215  }
   216  
   217  // CreateUser creates a new user. Note only administrators can create new users.
   218  //
   219  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#user-creation
   220  func (s *UsersService) CreateUser(opt *CreateUserOptions, options ...RequestOptionFunc) (*User, *Response, error) {
   221  	req, err := s.client.NewRequest(http.MethodPost, "users", opt, options)
   222  	if err != nil {
   223  		return nil, nil, err
   224  	}
   225  
   226  	usr := new(User)
   227  	resp, err := s.client.Do(req, usr)
   228  	if err != nil {
   229  		return nil, resp, err
   230  	}
   231  
   232  	return usr, resp, nil
   233  }
   234  
   235  // ModifyUserOptions represents the available ModifyUser() options.
   236  //
   237  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#user-modification
   238  type ModifyUserOptions struct {
   239  	Email              *string `url:"email,omitempty" json:"email,omitempty"`
   240  	Password           *string `url:"password,omitempty" json:"password,omitempty"`
   241  	Username           *string `url:"username,omitempty" json:"username,omitempty"`
   242  	Name               *string `url:"name,omitempty" json:"name,omitempty"`
   243  	Skype              *string `url:"skype,omitempty" json:"skype,omitempty"`
   244  	Linkedin           *string `url:"linkedin,omitempty" json:"linkedin,omitempty"`
   245  	Twitter            *string `url:"twitter,omitempty" json:"twitter,omitempty"`
   246  	WebsiteURL         *string `url:"website_url,omitempty" json:"website_url,omitempty"`
   247  	Organization       *string `url:"organization,omitempty" json:"organization,omitempty"`
   248  	JobTitle           *string `url:"job_title,omitempty" json:"job_title,omitempty"`
   249  	ProjectsLimit      *int    `url:"projects_limit,omitempty" json:"projects_limit,omitempty"`
   250  	ExternUID          *string `url:"extern_uid,omitempty" json:"extern_uid,omitempty"`
   251  	Provider           *string `url:"provider,omitempty" json:"provider,omitempty"`
   252  	Bio                *string `url:"bio,omitempty" json:"bio,omitempty"`
   253  	Location           *string `url:"location,omitempty" json:"location,omitempty"`
   254  	Admin              *bool   `url:"admin,omitempty" json:"admin,omitempty"`
   255  	CanCreateGroup     *bool   `url:"can_create_group,omitempty" json:"can_create_group,omitempty"`
   256  	SkipReconfirmation *bool   `url:"skip_reconfirmation,omitempty" json:"skip_reconfirmation,omitempty"`
   257  	External           *bool   `url:"external,omitempty" json:"external,omitempty"`
   258  	PrivateProfile     *bool   `url:"private_profile,omitempty" json:"private_profile,omitempty"`
   259  	Note               *string `url:"note,omitempty" json:"note,omitempty"`
   260  	ThemeID            *int    `url:"theme_id,omitempty" json:"theme_id,omitempty"`
   261  	PublicEmail        *string `url:"public_email,omitempty" json:"public_email,omitempty"`
   262  	CommitEmail        *string `url:"commit_email,omitempty" json:"commit_email,omitempty"`
   263  }
   264  
   265  // ModifyUser modifies an existing user. Only administrators can change attributes
   266  // of a user.
   267  //
   268  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#user-modification
   269  func (s *UsersService) ModifyUser(user int, opt *ModifyUserOptions, options ...RequestOptionFunc) (*User, *Response, error) {
   270  	u := fmt.Sprintf("users/%d", user)
   271  
   272  	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
   273  	if err != nil {
   274  		return nil, nil, err
   275  	}
   276  
   277  	usr := new(User)
   278  	resp, err := s.client.Do(req, usr)
   279  	if err != nil {
   280  		return nil, resp, err
   281  	}
   282  
   283  	return usr, resp, nil
   284  }
   285  
   286  // DeleteUser deletes a user. Available only for administrators. This is an
   287  // idempotent function, calling this function for a non-existent user id still
   288  // returns a status code 200 OK. The JSON response differs if the user was
   289  // actually deleted or not. In the former the user is returned and in the
   290  // latter not.
   291  //
   292  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#user-deletion
   293  func (s *UsersService) DeleteUser(user int, options ...RequestOptionFunc) (*Response, error) {
   294  	u := fmt.Sprintf("users/%d", user)
   295  
   296  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   297  	if err != nil {
   298  		return nil, err
   299  	}
   300  
   301  	return s.client.Do(req, nil)
   302  }
   303  
   304  // CurrentUser gets currently authenticated user.
   305  //
   306  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#list-current-user
   307  func (s *UsersService) CurrentUser(options ...RequestOptionFunc) (*User, *Response, error) {
   308  	req, err := s.client.NewRequest(http.MethodGet, "user", nil, options)
   309  	if err != nil {
   310  		return nil, nil, err
   311  	}
   312  
   313  	usr := new(User)
   314  	resp, err := s.client.Do(req, usr)
   315  	if err != nil {
   316  		return nil, resp, err
   317  	}
   318  
   319  	return usr, resp, nil
   320  }
   321  
   322  // UserStatus represents the current status of a user
   323  //
   324  // GitLab API docs:
   325  // https://docs.gitlab.com/ee/api/users.html#user-status
   326  type UserStatus struct {
   327  	Emoji        string            `json:"emoji"`
   328  	Availability AvailabilityValue `json:"availability"`
   329  	Message      string            `json:"message"`
   330  	MessageHTML  string            `json:"message_html"`
   331  }
   332  
   333  // CurrentUserStatus retrieves the user status
   334  //
   335  // GitLab API docs:
   336  // https://docs.gitlab.com/ee/api/users.html#user-status
   337  func (s *UsersService) CurrentUserStatus(options ...RequestOptionFunc) (*UserStatus, *Response, error) {
   338  	req, err := s.client.NewRequest(http.MethodGet, "user/status", nil, options)
   339  	if err != nil {
   340  		return nil, nil, err
   341  	}
   342  
   343  	status := new(UserStatus)
   344  	resp, err := s.client.Do(req, status)
   345  	if err != nil {
   346  		return nil, resp, err
   347  	}
   348  
   349  	return status, resp, nil
   350  }
   351  
   352  // GetUserStatus retrieves a user's status
   353  //
   354  // GitLab API docs:
   355  // https://docs.gitlab.com/ee/api/users.html#get-the-status-of-a-user
   356  func (s *UsersService) GetUserStatus(user int, options ...RequestOptionFunc) (*UserStatus, *Response, error) {
   357  	u := fmt.Sprintf("users/%d/status", user)
   358  
   359  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   360  	if err != nil {
   361  		return nil, nil, err
   362  	}
   363  
   364  	status := new(UserStatus)
   365  	resp, err := s.client.Do(req, status)
   366  	if err != nil {
   367  		return nil, resp, err
   368  	}
   369  
   370  	return status, resp, nil
   371  }
   372  
   373  // UserStatusOptions represents the options required to set the status
   374  //
   375  // GitLab API docs:
   376  // https://docs.gitlab.com/ee/api/users.html#set-user-status
   377  type UserStatusOptions struct {
   378  	Emoji        *string            `url:"emoji,omitempty" json:"emoji,omitempty"`
   379  	Availability *AvailabilityValue `url:"availability,omitempty" json:"availability,omitempty"`
   380  	Message      *string            `url:"message,omitempty" json:"message,omitempty"`
   381  }
   382  
   383  // SetUserStatus sets the user's status
   384  //
   385  // GitLab API docs:
   386  // https://docs.gitlab.com/ee/api/users.html#set-user-status
   387  func (s *UsersService) SetUserStatus(opt *UserStatusOptions, options ...RequestOptionFunc) (*UserStatus, *Response, error) {
   388  	req, err := s.client.NewRequest(http.MethodPut, "user/status", opt, options)
   389  	if err != nil {
   390  		return nil, nil, err
   391  	}
   392  
   393  	status := new(UserStatus)
   394  	resp, err := s.client.Do(req, status)
   395  	if err != nil {
   396  		return nil, resp, err
   397  	}
   398  
   399  	return status, resp, nil
   400  }
   401  
   402  // UserAssociationsCount represents the user associations count.
   403  //
   404  // Gitlab API docs: https://docs.gitlab.com/ee/api/users.html#list-associations-count-for-user
   405  type UserAssociationsCount struct {
   406  	GroupsCount        int `json:"groups_count"`
   407  	ProjectsCount      int `json:"projects_count"`
   408  	IssuesCount        int `json:"issues_count"`
   409  	MergeRequestsCount int `json:"merge_requests_count"`
   410  }
   411  
   412  // GetUserAssociationsCount gets a list of a specified user associations.
   413  //
   414  // Gitlab API docs: https://docs.gitlab.com/ee/api/users.html#list-associations-count-for-user
   415  func (s *UsersService) GetUserAssociationsCount(user int, options ...RequestOptionFunc) (*UserAssociationsCount, *Response, error) {
   416  	u := fmt.Sprintf("users/%d/associations_count", user)
   417  
   418  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   419  	if err != nil {
   420  		return nil, nil, err
   421  	}
   422  
   423  	uac := new(UserAssociationsCount)
   424  	resp, err := s.client.Do(req, uac)
   425  	if err != nil {
   426  		return nil, resp, err
   427  	}
   428  
   429  	return uac, resp, nil
   430  }
   431  
   432  // SSHKey represents a SSH key.
   433  //
   434  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#list-ssh-keys
   435  type SSHKey struct {
   436  	ID        int        `json:"id"`
   437  	Title     string     `json:"title"`
   438  	Key       string     `json:"key"`
   439  	CreatedAt *time.Time `json:"created_at"`
   440  	ExpiresAt *time.Time `json:"expires_at"`
   441  }
   442  
   443  // ListSSHKeysOptions represents the available ListSSHKeys options.
   444  //
   445  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#list-ssh-keys
   446  type ListSSHKeysOptions ListOptions
   447  
   448  // ListSSHKeys gets a list of currently authenticated user's SSH keys.
   449  //
   450  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#list-ssh-keys
   451  func (s *UsersService) ListSSHKeys(opt *ListSSHKeysOptions, options ...RequestOptionFunc) ([]*SSHKey, *Response, error) {
   452  	req, err := s.client.NewRequest(http.MethodGet, "user/keys", opt, options)
   453  	if err != nil {
   454  		return nil, nil, err
   455  	}
   456  
   457  	var k []*SSHKey
   458  	resp, err := s.client.Do(req, &k)
   459  	if err != nil {
   460  		return nil, resp, err
   461  	}
   462  
   463  	return k, resp, nil
   464  }
   465  
   466  // ListSSHKeysForUserOptions represents the available ListSSHKeysForUser() options.
   467  //
   468  // GitLab API docs:
   469  // https://docs.gitlab.com/ee/api/users.html#list-ssh-keys-for-user
   470  type ListSSHKeysForUserOptions ListOptions
   471  
   472  // ListSSHKeysForUser gets a list of a specified user's SSH keys.
   473  //
   474  // GitLab API docs:
   475  // https://docs.gitlab.com/ee/api/users.html#list-ssh-keys-for-user
   476  func (s *UsersService) ListSSHKeysForUser(uid interface{}, opt *ListSSHKeysForUserOptions, options ...RequestOptionFunc) ([]*SSHKey, *Response, error) {
   477  	user, err := parseID(uid)
   478  	if err != nil {
   479  		return nil, nil, err
   480  	}
   481  	u := fmt.Sprintf("users/%s/keys", user)
   482  
   483  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   484  	if err != nil {
   485  		return nil, nil, err
   486  	}
   487  
   488  	var k []*SSHKey
   489  	resp, err := s.client.Do(req, &k)
   490  	if err != nil {
   491  		return nil, resp, err
   492  	}
   493  
   494  	return k, resp, nil
   495  }
   496  
   497  // GetSSHKey gets a single key.
   498  //
   499  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#single-ssh-key
   500  func (s *UsersService) GetSSHKey(key int, options ...RequestOptionFunc) (*SSHKey, *Response, error) {
   501  	u := fmt.Sprintf("user/keys/%d", key)
   502  
   503  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   504  	if err != nil {
   505  		return nil, nil, err
   506  	}
   507  
   508  	k := new(SSHKey)
   509  	resp, err := s.client.Do(req, k)
   510  	if err != nil {
   511  		return nil, resp, err
   512  	}
   513  
   514  	return k, resp, nil
   515  }
   516  
   517  // GetSSHKeyForUser gets a single key for a given user.
   518  //
   519  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#single-ssh-key-for-given-user
   520  func (s *UsersService) GetSSHKeyForUser(user int, key int, options ...RequestOptionFunc) (*SSHKey, *Response, error) {
   521  	u := fmt.Sprintf("users/%d/keys/%d", user, key)
   522  
   523  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   524  	if err != nil {
   525  		return nil, nil, err
   526  	}
   527  
   528  	k := new(SSHKey)
   529  	resp, err := s.client.Do(req, k)
   530  	if err != nil {
   531  		return nil, resp, err
   532  	}
   533  
   534  	return k, resp, nil
   535  }
   536  
   537  // AddSSHKeyOptions represents the available AddSSHKey() options.
   538  //
   539  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#add-ssh-key
   540  type AddSSHKeyOptions struct {
   541  	Title     *string  `url:"title,omitempty" json:"title,omitempty"`
   542  	Key       *string  `url:"key,omitempty" json:"key,omitempty"`
   543  	ExpiresAt *ISOTime `url:"expires_at,omitempty" json:"expires_at,omitempty"`
   544  }
   545  
   546  // AddSSHKey creates a new key owned by the currently authenticated user.
   547  //
   548  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#add-ssh-key
   549  func (s *UsersService) AddSSHKey(opt *AddSSHKeyOptions, options ...RequestOptionFunc) (*SSHKey, *Response, error) {
   550  	req, err := s.client.NewRequest(http.MethodPost, "user/keys", opt, options)
   551  	if err != nil {
   552  		return nil, nil, err
   553  	}
   554  
   555  	k := new(SSHKey)
   556  	resp, err := s.client.Do(req, k)
   557  	if err != nil {
   558  		return nil, resp, err
   559  	}
   560  
   561  	return k, resp, nil
   562  }
   563  
   564  // AddSSHKeyForUser creates new key owned by specified user. Available only for
   565  // admin.
   566  //
   567  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#add-ssh-key-for-user
   568  func (s *UsersService) AddSSHKeyForUser(user int, opt *AddSSHKeyOptions, options ...RequestOptionFunc) (*SSHKey, *Response, error) {
   569  	u := fmt.Sprintf("users/%d/keys", user)
   570  
   571  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   572  	if err != nil {
   573  		return nil, nil, err
   574  	}
   575  
   576  	k := new(SSHKey)
   577  	resp, err := s.client.Do(req, k)
   578  	if err != nil {
   579  		return nil, resp, err
   580  	}
   581  
   582  	return k, resp, nil
   583  }
   584  
   585  // DeleteSSHKey deletes key owned by currently authenticated user. This is an
   586  // idempotent function and calling it on a key that is already deleted or not
   587  // available results in 200 OK.
   588  //
   589  // GitLab API docs:
   590  // https://docs.gitlab.com/ee/api/users.html#delete-ssh-key-for-current-user
   591  func (s *UsersService) DeleteSSHKey(key int, options ...RequestOptionFunc) (*Response, error) {
   592  	u := fmt.Sprintf("user/keys/%d", key)
   593  
   594  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   595  	if err != nil {
   596  		return nil, err
   597  	}
   598  
   599  	return s.client.Do(req, nil)
   600  }
   601  
   602  // DeleteSSHKeyForUser deletes key owned by a specified user. Available only
   603  // for admin.
   604  //
   605  // GitLab API docs:
   606  // https://docs.gitlab.com/ee/api/users.html#delete-ssh-key-for-given-user
   607  func (s *UsersService) DeleteSSHKeyForUser(user, key int, options ...RequestOptionFunc) (*Response, error) {
   608  	u := fmt.Sprintf("users/%d/keys/%d", user, key)
   609  
   610  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   611  	if err != nil {
   612  		return nil, err
   613  	}
   614  
   615  	return s.client.Do(req, nil)
   616  }
   617  
   618  // GPGKey represents a GPG key.
   619  //
   620  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#list-all-gpg-keys
   621  type GPGKey struct {
   622  	ID        int        `json:"id"`
   623  	Key       string     `json:"key"`
   624  	CreatedAt *time.Time `json:"created_at"`
   625  }
   626  
   627  // ListGPGKeys gets a list of currently authenticated user’s GPG keys.
   628  //
   629  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#list-all-gpg-keys
   630  func (s *UsersService) ListGPGKeys(options ...RequestOptionFunc) ([]*GPGKey, *Response, error) {
   631  	req, err := s.client.NewRequest(http.MethodGet, "user/gpg_keys", nil, options)
   632  	if err != nil {
   633  		return nil, nil, err
   634  	}
   635  
   636  	var ks []*GPGKey
   637  	resp, err := s.client.Do(req, &ks)
   638  	if err != nil {
   639  		return nil, resp, err
   640  	}
   641  
   642  	return ks, resp, nil
   643  }
   644  
   645  // GetGPGKey gets a specific GPG key of currently authenticated user.
   646  //
   647  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#get-a-specific-gpg-key
   648  func (s *UsersService) GetGPGKey(key int, options ...RequestOptionFunc) (*GPGKey, *Response, error) {
   649  	u := fmt.Sprintf("user/gpg_keys/%d", key)
   650  
   651  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   652  	if err != nil {
   653  		return nil, nil, err
   654  	}
   655  
   656  	k := new(GPGKey)
   657  	resp, err := s.client.Do(req, k)
   658  	if err != nil {
   659  		return nil, resp, err
   660  	}
   661  
   662  	return k, resp, nil
   663  }
   664  
   665  // AddGPGKeyOptions represents the available AddGPGKey() options.
   666  //
   667  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#add-a-gpg-key
   668  type AddGPGKeyOptions struct {
   669  	Key *string `url:"key,omitempty" json:"key,omitempty"`
   670  }
   671  
   672  // AddGPGKey creates a new GPG key owned by the currently authenticated user.
   673  //
   674  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#add-a-gpg-key
   675  func (s *UsersService) AddGPGKey(opt *AddGPGKeyOptions, options ...RequestOptionFunc) (*GPGKey, *Response, error) {
   676  	req, err := s.client.NewRequest(http.MethodPost, "user/gpg_keys", opt, options)
   677  	if err != nil {
   678  		return nil, nil, err
   679  	}
   680  
   681  	k := new(GPGKey)
   682  	resp, err := s.client.Do(req, k)
   683  	if err != nil {
   684  		return nil, resp, err
   685  	}
   686  
   687  	return k, resp, nil
   688  }
   689  
   690  // DeleteGPGKey deletes a GPG key owned by currently authenticated user.
   691  //
   692  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#delete-a-gpg-key
   693  func (s *UsersService) DeleteGPGKey(key int, options ...RequestOptionFunc) (*Response, error) {
   694  	u := fmt.Sprintf("user/gpg_keys/%d", key)
   695  
   696  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   697  	if err != nil {
   698  		return nil, err
   699  	}
   700  
   701  	return s.client.Do(req, nil)
   702  }
   703  
   704  // ListGPGKeysForUser gets a list of a specified user’s GPG keys.
   705  //
   706  // GitLab API docs:
   707  // https://docs.gitlab.com/ee/api/users.html#list-all-gpg-keys-for-given-user
   708  func (s *UsersService) ListGPGKeysForUser(user int, options ...RequestOptionFunc) ([]*GPGKey, *Response, error) {
   709  	u := fmt.Sprintf("users/%d/gpg_keys", user)
   710  
   711  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   712  	if err != nil {
   713  		return nil, nil, err
   714  	}
   715  
   716  	var ks []*GPGKey
   717  	resp, err := s.client.Do(req, &ks)
   718  	if err != nil {
   719  		return nil, resp, err
   720  	}
   721  
   722  	return ks, resp, nil
   723  }
   724  
   725  // GetGPGKeyForUser gets a specific GPG key for a given user.
   726  //
   727  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#get-a-specific-gpg-key-for-a-given-user
   728  func (s *UsersService) GetGPGKeyForUser(user, key int, options ...RequestOptionFunc) (*GPGKey, *Response, error) {
   729  	u := fmt.Sprintf("users/%d/gpg_keys/%d", user, key)
   730  
   731  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   732  	if err != nil {
   733  		return nil, nil, err
   734  	}
   735  
   736  	k := new(GPGKey)
   737  	resp, err := s.client.Do(req, k)
   738  	if err != nil {
   739  		return nil, resp, err
   740  	}
   741  
   742  	return k, resp, nil
   743  }
   744  
   745  // AddGPGKeyForUser creates new GPG key owned by the specified user.
   746  //
   747  // GitLab API docs:
   748  // https://docs.gitlab.com/ee/api/users.html#add-a-gpg-key-for-a-given-user
   749  func (s *UsersService) AddGPGKeyForUser(user int, opt *AddGPGKeyOptions, options ...RequestOptionFunc) (*GPGKey, *Response, error) {
   750  	u := fmt.Sprintf("users/%d/gpg_keys", user)
   751  
   752  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   753  	if err != nil {
   754  		return nil, nil, err
   755  	}
   756  
   757  	k := new(GPGKey)
   758  	resp, err := s.client.Do(req, k)
   759  	if err != nil {
   760  		return nil, resp, err
   761  	}
   762  
   763  	return k, resp, nil
   764  }
   765  
   766  // DeleteGPGKeyForUser deletes a GPG key owned by a specified user.
   767  //
   768  // GitLab API docs:
   769  // https://docs.gitlab.com/ee/api/users.html#delete-a-gpg-key-for-a-given-user
   770  func (s *UsersService) DeleteGPGKeyForUser(user, key int, options ...RequestOptionFunc) (*Response, error) {
   771  	u := fmt.Sprintf("users/%d/gpg_keys/%d", user, key)
   772  
   773  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   774  	if err != nil {
   775  		return nil, err
   776  	}
   777  
   778  	return s.client.Do(req, nil)
   779  }
   780  
   781  // Email represents an Email.
   782  //
   783  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#list-emails
   784  type Email struct {
   785  	ID          int        `json:"id"`
   786  	Email       string     `json:"email"`
   787  	ConfirmedAt *time.Time `json:"confirmed_at"`
   788  }
   789  
   790  // ListEmails gets a list of currently authenticated user's Emails.
   791  //
   792  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#list-emails
   793  func (s *UsersService) ListEmails(options ...RequestOptionFunc) ([]*Email, *Response, error) {
   794  	req, err := s.client.NewRequest(http.MethodGet, "user/emails", nil, options)
   795  	if err != nil {
   796  		return nil, nil, err
   797  	}
   798  
   799  	var e []*Email
   800  	resp, err := s.client.Do(req, &e)
   801  	if err != nil {
   802  		return nil, resp, err
   803  	}
   804  
   805  	return e, resp, nil
   806  }
   807  
   808  // ListEmailsForUserOptions represents the available ListEmailsForUser() options.
   809  //
   810  // GitLab API docs:
   811  // https://docs.gitlab.com/ee/api/users.html#list-emails-for-user
   812  type ListEmailsForUserOptions ListOptions
   813  
   814  // ListEmailsForUser gets a list of a specified user's Emails. Available
   815  // only for admin
   816  //
   817  // GitLab API docs:
   818  // https://docs.gitlab.com/ee/api/users.html#list-emails-for-user
   819  func (s *UsersService) ListEmailsForUser(user int, opt *ListEmailsForUserOptions, options ...RequestOptionFunc) ([]*Email, *Response, error) {
   820  	u := fmt.Sprintf("users/%d/emails", user)
   821  
   822  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
   823  	if err != nil {
   824  		return nil, nil, err
   825  	}
   826  
   827  	var e []*Email
   828  	resp, err := s.client.Do(req, &e)
   829  	if err != nil {
   830  		return nil, resp, err
   831  	}
   832  
   833  	return e, resp, nil
   834  }
   835  
   836  // GetEmail gets a single email.
   837  //
   838  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#single-email
   839  func (s *UsersService) GetEmail(email int, options ...RequestOptionFunc) (*Email, *Response, error) {
   840  	u := fmt.Sprintf("user/emails/%d", email)
   841  
   842  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   843  	if err != nil {
   844  		return nil, nil, err
   845  	}
   846  
   847  	e := new(Email)
   848  	resp, err := s.client.Do(req, e)
   849  	if err != nil {
   850  		return nil, resp, err
   851  	}
   852  
   853  	return e, resp, nil
   854  }
   855  
   856  // AddEmailOptions represents the available AddEmail() options.
   857  //
   858  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#add-email
   859  type AddEmailOptions struct {
   860  	Email            *string `url:"email,omitempty" json:"email,omitempty"`
   861  	SkipConfirmation *bool   `url:"skip_confirmation,omitempty" json:"skip_confirmation,omitempty"`
   862  }
   863  
   864  // AddEmail creates a new email owned by the currently authenticated user.
   865  //
   866  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#add-email
   867  func (s *UsersService) AddEmail(opt *AddEmailOptions, options ...RequestOptionFunc) (*Email, *Response, error) {
   868  	req, err := s.client.NewRequest(http.MethodPost, "user/emails", opt, options)
   869  	if err != nil {
   870  		return nil, nil, err
   871  	}
   872  
   873  	e := new(Email)
   874  	resp, err := s.client.Do(req, e)
   875  	if err != nil {
   876  		return nil, resp, err
   877  	}
   878  
   879  	return e, resp, nil
   880  }
   881  
   882  // AddEmailForUser creates new email owned by specified user. Available only for
   883  // admin.
   884  //
   885  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#add-email-for-user
   886  func (s *UsersService) AddEmailForUser(user int, opt *AddEmailOptions, options ...RequestOptionFunc) (*Email, *Response, error) {
   887  	u := fmt.Sprintf("users/%d/emails", user)
   888  
   889  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   890  	if err != nil {
   891  		return nil, nil, err
   892  	}
   893  
   894  	e := new(Email)
   895  	resp, err := s.client.Do(req, e)
   896  	if err != nil {
   897  		return nil, resp, err
   898  	}
   899  
   900  	return e, resp, nil
   901  }
   902  
   903  // DeleteEmail deletes email owned by currently authenticated user. This is an
   904  // idempotent function and calling it on a key that is already deleted or not
   905  // available results in 200 OK.
   906  //
   907  // GitLab API docs:
   908  // https://docs.gitlab.com/ee/api/users.html#delete-email-for-current-user
   909  func (s *UsersService) DeleteEmail(email int, options ...RequestOptionFunc) (*Response, error) {
   910  	u := fmt.Sprintf("user/emails/%d", email)
   911  
   912  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   913  	if err != nil {
   914  		return nil, err
   915  	}
   916  
   917  	return s.client.Do(req, nil)
   918  }
   919  
   920  // DeleteEmailForUser deletes email owned by a specified user. Available only
   921  // for admin.
   922  //
   923  // GitLab API docs:
   924  // https://docs.gitlab.com/ee/api/users.html#delete-email-for-given-user
   925  func (s *UsersService) DeleteEmailForUser(user, email int, options ...RequestOptionFunc) (*Response, error) {
   926  	u := fmt.Sprintf("users/%d/emails/%d", user, email)
   927  
   928  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   929  	if err != nil {
   930  		return nil, err
   931  	}
   932  
   933  	return s.client.Do(req, nil)
   934  }
   935  
   936  // BlockUser blocks the specified user. Available only for admin.
   937  //
   938  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#block-user
   939  func (s *UsersService) BlockUser(user int, options ...RequestOptionFunc) error {
   940  	u := fmt.Sprintf("users/%d/block", user)
   941  
   942  	req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
   943  	if err != nil {
   944  		return err
   945  	}
   946  
   947  	resp, err := s.client.Do(req, nil)
   948  	if err != nil && resp == nil {
   949  		return err
   950  	}
   951  
   952  	switch resp.StatusCode {
   953  	case 201:
   954  		return nil
   955  	case 403:
   956  		return ErrUserBlockPrevented
   957  	case 404:
   958  		return ErrUserNotFound
   959  	default:
   960  		return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
   961  	}
   962  }
   963  
   964  // UnblockUser unblocks the specified user. Available only for admin.
   965  //
   966  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#unblock-user
   967  func (s *UsersService) UnblockUser(user int, options ...RequestOptionFunc) error {
   968  	u := fmt.Sprintf("users/%d/unblock", user)
   969  
   970  	req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
   971  	if err != nil {
   972  		return err
   973  	}
   974  
   975  	resp, err := s.client.Do(req, nil)
   976  	if err != nil && resp == nil {
   977  		return err
   978  	}
   979  
   980  	switch resp.StatusCode {
   981  	case 201:
   982  		return nil
   983  	case 403:
   984  		return ErrUserUnblockPrevented
   985  	case 404:
   986  		return ErrUserNotFound
   987  	default:
   988  		return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
   989  	}
   990  }
   991  
   992  // BanUser bans the specified user. Available only for admin.
   993  //
   994  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#ban-user
   995  func (s *UsersService) BanUser(user int, options ...RequestOptionFunc) error {
   996  	u := fmt.Sprintf("users/%d/ban", user)
   997  
   998  	req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
   999  	if err != nil {
  1000  		return err
  1001  	}
  1002  
  1003  	resp, err := s.client.Do(req, nil)
  1004  	if err != nil && resp == nil {
  1005  		return err
  1006  	}
  1007  
  1008  	switch resp.StatusCode {
  1009  	case 201:
  1010  		return nil
  1011  	case 404:
  1012  		return ErrUserNotFound
  1013  	default:
  1014  		return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
  1015  	}
  1016  }
  1017  
  1018  // UnbanUser unbans the specified user. Available only for admin.
  1019  //
  1020  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#unban-user
  1021  func (s *UsersService) UnbanUser(user int, options ...RequestOptionFunc) error {
  1022  	u := fmt.Sprintf("users/%d/unban", user)
  1023  
  1024  	req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
  1025  	if err != nil {
  1026  		return err
  1027  	}
  1028  
  1029  	resp, err := s.client.Do(req, nil)
  1030  	if err != nil && resp == nil {
  1031  		return err
  1032  	}
  1033  
  1034  	switch resp.StatusCode {
  1035  	case 201:
  1036  		return nil
  1037  	case 404:
  1038  		return ErrUserNotFound
  1039  	default:
  1040  		return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
  1041  	}
  1042  }
  1043  
  1044  // DeactivateUser deactivate the specified user. Available only for admin.
  1045  //
  1046  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#deactivate-user
  1047  func (s *UsersService) DeactivateUser(user int, options ...RequestOptionFunc) error {
  1048  	u := fmt.Sprintf("users/%d/deactivate", user)
  1049  
  1050  	req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
  1051  	if err != nil {
  1052  		return err
  1053  	}
  1054  
  1055  	resp, err := s.client.Do(req, nil)
  1056  	if err != nil && resp == nil {
  1057  		return err
  1058  	}
  1059  
  1060  	switch resp.StatusCode {
  1061  	case 201:
  1062  		return nil
  1063  	case 403:
  1064  		return ErrUserDeactivatePrevented
  1065  	case 404:
  1066  		return ErrUserNotFound
  1067  	default:
  1068  		return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
  1069  	}
  1070  }
  1071  
  1072  // ActivateUser activate the specified user. Available only for admin.
  1073  //
  1074  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#activate-user
  1075  func (s *UsersService) ActivateUser(user int, options ...RequestOptionFunc) error {
  1076  	u := fmt.Sprintf("users/%d/activate", user)
  1077  
  1078  	req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
  1079  	if err != nil {
  1080  		return err
  1081  	}
  1082  
  1083  	resp, err := s.client.Do(req, nil)
  1084  	if err != nil && resp == nil {
  1085  		return err
  1086  	}
  1087  
  1088  	switch resp.StatusCode {
  1089  	case 201:
  1090  		return nil
  1091  	case 403:
  1092  		return ErrUserActivatePrevented
  1093  	case 404:
  1094  		return ErrUserNotFound
  1095  	default:
  1096  		return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
  1097  	}
  1098  }
  1099  
  1100  // ApproveUser approve the specified user. Available only for admin.
  1101  //
  1102  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#approve-user
  1103  func (s *UsersService) ApproveUser(user int, options ...RequestOptionFunc) error {
  1104  	u := fmt.Sprintf("users/%d/approve", user)
  1105  
  1106  	req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
  1107  	if err != nil {
  1108  		return err
  1109  	}
  1110  
  1111  	resp, err := s.client.Do(req, nil)
  1112  	if err != nil && resp == nil {
  1113  		return err
  1114  	}
  1115  
  1116  	switch resp.StatusCode {
  1117  	case 201:
  1118  		return nil
  1119  	case 403:
  1120  		return ErrUserApprovePrevented
  1121  	case 404:
  1122  		return ErrUserNotFound
  1123  	default:
  1124  		return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
  1125  	}
  1126  }
  1127  
  1128  // RejectUser reject the specified user. Available only for admin.
  1129  //
  1130  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#reject-user
  1131  func (s *UsersService) RejectUser(user int, options ...RequestOptionFunc) error {
  1132  	u := fmt.Sprintf("users/%d/reject", user)
  1133  
  1134  	req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
  1135  	if err != nil {
  1136  		return err
  1137  	}
  1138  
  1139  	resp, err := s.client.Do(req, nil)
  1140  	if err != nil && resp == nil {
  1141  		return err
  1142  	}
  1143  
  1144  	switch resp.StatusCode {
  1145  	case 200:
  1146  		return nil
  1147  	case 403:
  1148  		return ErrUserRejectPrevented
  1149  	case 404:
  1150  		return ErrUserNotFound
  1151  	case 409:
  1152  		return ErrUserConflict
  1153  	default:
  1154  		return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
  1155  	}
  1156  }
  1157  
  1158  // ImpersonationToken represents an impersonation token.
  1159  //
  1160  // GitLab API docs:
  1161  // https://docs.gitlab.com/ee/api/users.html#get-all-impersonation-tokens-of-a-user
  1162  type ImpersonationToken struct {
  1163  	ID         int        `json:"id"`
  1164  	Name       string     `json:"name"`
  1165  	Active     bool       `json:"active"`
  1166  	Token      string     `json:"token"`
  1167  	Scopes     []string   `json:"scopes"`
  1168  	Revoked    bool       `json:"revoked"`
  1169  	CreatedAt  *time.Time `json:"created_at"`
  1170  	ExpiresAt  *ISOTime   `json:"expires_at"`
  1171  	LastUsedAt *time.Time `json:"last_used_at"`
  1172  }
  1173  
  1174  // GetAllImpersonationTokensOptions represents the available
  1175  // GetAllImpersonationTokens() options.
  1176  //
  1177  // GitLab API docs:
  1178  // https://docs.gitlab.com/ee/api/users.html#get-all-impersonation-tokens-of-a-user
  1179  type GetAllImpersonationTokensOptions struct {
  1180  	ListOptions
  1181  	State *string `url:"state,omitempty" json:"state,omitempty"`
  1182  }
  1183  
  1184  // GetAllImpersonationTokens retrieves all impersonation tokens of a user.
  1185  //
  1186  // GitLab API docs:
  1187  // https://docs.gitlab.com/ee/api/users.html#get-all-impersonation-tokens-of-a-user
  1188  func (s *UsersService) GetAllImpersonationTokens(user int, opt *GetAllImpersonationTokensOptions, options ...RequestOptionFunc) ([]*ImpersonationToken, *Response, error) {
  1189  	u := fmt.Sprintf("users/%d/impersonation_tokens", user)
  1190  
  1191  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
  1192  	if err != nil {
  1193  		return nil, nil, err
  1194  	}
  1195  
  1196  	var ts []*ImpersonationToken
  1197  	resp, err := s.client.Do(req, &ts)
  1198  	if err != nil {
  1199  		return nil, resp, err
  1200  	}
  1201  
  1202  	return ts, resp, nil
  1203  }
  1204  
  1205  // GetImpersonationToken retrieves an impersonation token of a user.
  1206  //
  1207  // GitLab API docs:
  1208  // https://docs.gitlab.com/ee/api/users.html#get-an-impersonation-token-of-a-user
  1209  func (s *UsersService) GetImpersonationToken(user, token int, options ...RequestOptionFunc) (*ImpersonationToken, *Response, error) {
  1210  	u := fmt.Sprintf("users/%d/impersonation_tokens/%d", user, token)
  1211  
  1212  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
  1213  	if err != nil {
  1214  		return nil, nil, err
  1215  	}
  1216  
  1217  	t := new(ImpersonationToken)
  1218  	resp, err := s.client.Do(req, &t)
  1219  	if err != nil {
  1220  		return nil, resp, err
  1221  	}
  1222  
  1223  	return t, resp, nil
  1224  }
  1225  
  1226  // CreateImpersonationTokenOptions represents the available
  1227  // CreateImpersonationToken() options.
  1228  //
  1229  // GitLab API docs:
  1230  // https://docs.gitlab.com/ee/api/users.html#create-an-impersonation-token
  1231  type CreateImpersonationTokenOptions struct {
  1232  	Name      *string    `url:"name,omitempty" json:"name,omitempty"`
  1233  	Scopes    *[]string  `url:"scopes,omitempty" json:"scopes,omitempty"`
  1234  	ExpiresAt *time.Time `url:"expires_at,omitempty" json:"expires_at,omitempty"`
  1235  }
  1236  
  1237  // CreateImpersonationToken creates an impersonation token.
  1238  //
  1239  // GitLab API docs:
  1240  // https://docs.gitlab.com/ee/api/users.html#create-an-impersonation-token
  1241  func (s *UsersService) CreateImpersonationToken(user int, opt *CreateImpersonationTokenOptions, options ...RequestOptionFunc) (*ImpersonationToken, *Response, error) {
  1242  	u := fmt.Sprintf("users/%d/impersonation_tokens", user)
  1243  
  1244  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
  1245  	if err != nil {
  1246  		return nil, nil, err
  1247  	}
  1248  
  1249  	t := new(ImpersonationToken)
  1250  	resp, err := s.client.Do(req, &t)
  1251  	if err != nil {
  1252  		return nil, resp, err
  1253  	}
  1254  
  1255  	return t, resp, nil
  1256  }
  1257  
  1258  // RevokeImpersonationToken revokes an impersonation token.
  1259  //
  1260  // GitLab API docs:
  1261  // https://docs.gitlab.com/ee/api/users.html#revoke-an-impersonation-token
  1262  func (s *UsersService) RevokeImpersonationToken(user, token int, options ...RequestOptionFunc) (*Response, error) {
  1263  	u := fmt.Sprintf("users/%d/impersonation_tokens/%d", user, token)
  1264  
  1265  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
  1266  	if err != nil {
  1267  		return nil, err
  1268  	}
  1269  
  1270  	return s.client.Do(req, nil)
  1271  }
  1272  
  1273  // CreatePersonalAccessTokenOptions represents the available
  1274  // CreatePersonalAccessToken() options.
  1275  //
  1276  // GitLab API docs:
  1277  // https://docs.gitlab.com/ee/api/users.html#create-a-personal-access-token
  1278  type CreatePersonalAccessTokenOptions struct {
  1279  	Name      *string   `url:"name,omitempty" json:"name,omitempty"`
  1280  	ExpiresAt *ISOTime  `url:"expires_at,omitempty" json:"expires_at,omitempty"`
  1281  	Scopes    *[]string `url:"scopes,omitempty" json:"scopes,omitempty"`
  1282  }
  1283  
  1284  // CreatePersonalAccessToken creates a personal access token.
  1285  //
  1286  // GitLab API docs:
  1287  // https://docs.gitlab.com/ee/api/users.html#create-a-personal-access-token
  1288  func (s *UsersService) CreatePersonalAccessToken(user int, opt *CreatePersonalAccessTokenOptions, options ...RequestOptionFunc) (*PersonalAccessToken, *Response, error) {
  1289  	u := fmt.Sprintf("users/%d/personal_access_tokens", user)
  1290  
  1291  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
  1292  	if err != nil {
  1293  		return nil, nil, err
  1294  	}
  1295  
  1296  	t := new(PersonalAccessToken)
  1297  	resp, err := s.client.Do(req, &t)
  1298  	if err != nil {
  1299  		return nil, resp, err
  1300  	}
  1301  
  1302  	return t, resp, nil
  1303  }
  1304  
  1305  // CreatePersonalAccessTokenForCurrentUserOptions represents the available
  1306  // CreatePersonalAccessTokenForCurrentUser() options.
  1307  //
  1308  // GitLab API docs:
  1309  // https://docs.gitlab.com/ee/api/users.html#create-a-personal-access-token-with-limited-scopes-for-the-currently-authenticated-user
  1310  type CreatePersonalAccessTokenForCurrentUserOptions struct {
  1311  	Name      *string   `url:"name,omitempty" json:"name,omitempty"`
  1312  	Scopes    *[]string `url:"scopes,omitempty" json:"scopes,omitempty"`
  1313  	ExpiresAt *ISOTime  `url:"expires_at,omitempty" json:"expires_at,omitempty"`
  1314  }
  1315  
  1316  // CreatePersonalAccessTokenForCurrentUser creates a personal access token with limited scopes for the currently authenticated user.
  1317  //
  1318  // GitLab API docs:
  1319  // https://docs.gitlab.com/ee/api/users.html#create-a-personal-access-token-with-limited-scopes-for-the-currently-authenticated-user
  1320  func (s *UsersService) CreatePersonalAccessTokenForCurrentUser(opt *CreatePersonalAccessTokenForCurrentUserOptions, options ...RequestOptionFunc) (*PersonalAccessToken, *Response, error) {
  1321  	u := "user/personal_access_tokens"
  1322  
  1323  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
  1324  	if err != nil {
  1325  		return nil, nil, err
  1326  	}
  1327  
  1328  	t := new(PersonalAccessToken)
  1329  	resp, err := s.client.Do(req, &t)
  1330  	if err != nil {
  1331  		return nil, resp, err
  1332  	}
  1333  
  1334  	return t, resp, nil
  1335  }
  1336  
  1337  // UserActivity represents an entry in the user/activities response
  1338  //
  1339  // GitLab API docs:
  1340  // https://docs.gitlab.com/ee/api/users.html#get-user-activities
  1341  type UserActivity struct {
  1342  	Username       string   `json:"username"`
  1343  	LastActivityOn *ISOTime `json:"last_activity_on"`
  1344  }
  1345  
  1346  // GetUserActivitiesOptions represents the options for GetUserActivities
  1347  //
  1348  // GitLap API docs:
  1349  // https://docs.gitlab.com/ee/api/users.html#get-user-activities
  1350  type GetUserActivitiesOptions struct {
  1351  	ListOptions
  1352  	From *ISOTime `url:"from,omitempty" json:"from,omitempty"`
  1353  }
  1354  
  1355  // GetUserActivities retrieves user activities (admin only)
  1356  //
  1357  // GitLab API docs:
  1358  // https://docs.gitlab.com/ee/api/users.html#get-user-activities
  1359  func (s *UsersService) GetUserActivities(opt *GetUserActivitiesOptions, options ...RequestOptionFunc) ([]*UserActivity, *Response, error) {
  1360  	req, err := s.client.NewRequest(http.MethodGet, "user/activities", opt, options)
  1361  	if err != nil {
  1362  		return nil, nil, err
  1363  	}
  1364  
  1365  	var t []*UserActivity
  1366  	resp, err := s.client.Do(req, &t)
  1367  	if err != nil {
  1368  		return nil, resp, err
  1369  	}
  1370  
  1371  	return t, resp, nil
  1372  }
  1373  
  1374  // UserMembership represents a membership of the user in a namespace or project.
  1375  //
  1376  // GitLab API docs:
  1377  // https://docs.gitlab.com/ee/api/users.html#user-memberships
  1378  type UserMembership struct {
  1379  	SourceID    int              `json:"source_id"`
  1380  	SourceName  string           `json:"source_name"`
  1381  	SourceType  string           `json:"source_type"`
  1382  	AccessLevel AccessLevelValue `json:"access_level"`
  1383  }
  1384  
  1385  // GetUserMembershipOptions represents the options available to query user memberships.
  1386  //
  1387  // GitLab API docs:
  1388  // ohttps://docs.gitlab.com/ee/api/users.html#user-memberships
  1389  type GetUserMembershipOptions struct {
  1390  	ListOptions
  1391  	Type *string `url:"type,omitempty" json:"type,omitempty"`
  1392  }
  1393  
  1394  // GetUserMemberships retrieves a list of the user's memberships.
  1395  //
  1396  // GitLab API docs:
  1397  // https://docs.gitlab.com/ee/api/users.html#user-memberships
  1398  func (s *UsersService) GetUserMemberships(user int, opt *GetUserMembershipOptions, options ...RequestOptionFunc) ([]*UserMembership, *Response, error) {
  1399  	u := fmt.Sprintf("users/%d/memberships", user)
  1400  
  1401  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
  1402  	if err != nil {
  1403  		return nil, nil, err
  1404  	}
  1405  
  1406  	var m []*UserMembership
  1407  	resp, err := s.client.Do(req, &m)
  1408  	if err != nil {
  1409  		return nil, resp, err
  1410  	}
  1411  
  1412  	return m, resp, nil
  1413  }
  1414  
  1415  // DisableTwoFactor disables two factor authentication for the specified user.
  1416  //
  1417  // GitLab API docs:
  1418  // https://docs.gitlab.com/ee/api/users.html#disable-two-factor-authentication
  1419  func (s *UsersService) DisableTwoFactor(user int, options ...RequestOptionFunc) error {
  1420  	u := fmt.Sprintf("users/%d/disable_two_factor", user)
  1421  
  1422  	req, err := s.client.NewRequest(http.MethodPatch, u, nil, options)
  1423  	if err != nil {
  1424  		return err
  1425  	}
  1426  
  1427  	resp, err := s.client.Do(req, nil)
  1428  	if err != nil && resp == nil {
  1429  		return err
  1430  	}
  1431  
  1432  	switch resp.StatusCode {
  1433  	case 204:
  1434  		return nil
  1435  	case 400:
  1436  		return ErrUserTwoFactorNotEnabled
  1437  	case 403:
  1438  		return ErrUserDisableTwoFactorPrevented
  1439  	case 404:
  1440  		return ErrUserNotFound
  1441  	default:
  1442  		return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
  1443  	}
  1444  }
  1445  
  1446  // UserRunner represents a GitLab runner linked to the current user.
  1447  //
  1448  // GitLab API docs:
  1449  // https://docs.gitlab.com/ee/api/users.html#create-a-runner
  1450  type UserRunner struct {
  1451  	ID             int        `json:"id"`
  1452  	Token          string     `json:"token"`
  1453  	TokenExpiresAt *time.Time `json:"token_expires_at"`
  1454  }
  1455  
  1456  // CreateUserRunnerOptions represents the available CreateUserRunner() options.
  1457  //
  1458  // GitLab API docs:
  1459  // https://docs.gitlab.com/ee/api/users.html#create-a-runner
  1460  type CreateUserRunnerOptions struct {
  1461  	RunnerType      *string   `url:"runner_type,omitempty" json:"runner_type,omitempty"`
  1462  	GroupID         *int      `url:"group_id,omitempty" json:"group_id,omitempty"`
  1463  	ProjectID       *int      `url:"project_id,omitempty" json:"project_id,omitempty"`
  1464  	Description     *string   `url:"description,omitempty" json:"description,omitempty"`
  1465  	Paused          *bool     `url:"paused,omitempty" json:"paused,omitempty"`
  1466  	Locked          *bool     `url:"locked,omitempty" json:"locked,omitempty"`
  1467  	RunUntagged     *bool     `url:"run_untagged,omitempty" json:"run_untagged,omitempty"`
  1468  	TagList         *[]string `url:"tag_list,omitempty" json:"tag_list,omitempty"`
  1469  	AccessLevel     *string   `url:"access_level,omitempty" json:"access_level,omitempty"`
  1470  	MaximumTimeout  *int      `url:"maximum_timeout,omitempty" json:"maximum_timeout,omitempty"`
  1471  	MaintenanceNote *string   `url:"maintenance_note,omitempty" json:"maintenance_note,omitempty"`
  1472  }
  1473  
  1474  // CreateUserRunner creates a runner linked to the current user.
  1475  //
  1476  // GitLab API docs:
  1477  // https://docs.gitlab.com/ee/api/users.html#create-a-runner
  1478  func (s *UsersService) CreateUserRunner(opts *CreateUserRunnerOptions, options ...RequestOptionFunc) (*UserRunner, *Response, error) {
  1479  	req, err := s.client.NewRequest(http.MethodPost, "user/runners", opts, options)
  1480  	if err != nil {
  1481  		return nil, nil, err
  1482  	}
  1483  
  1484  	r := new(UserRunner)
  1485  	resp, err := s.client.Do(req, r)
  1486  	if err != nil {
  1487  		return nil, resp, err
  1488  	}
  1489  
  1490  	return r, resp, nil
  1491  }
  1492  
  1493  // CreateServiceAccountUser creates a new service account user. Note only administrators can create new service account users.
  1494  //
  1495  // GitLab API docs: https://docs.gitlab.com/ee/api/users.html#create-service-account-user
  1496  func (s *UsersService) CreateServiceAccountUser(options ...RequestOptionFunc) (*User, *Response, error) {
  1497  	req, err := s.client.NewRequest(http.MethodPost, "service_accounts", nil, options)
  1498  	if err != nil {
  1499  		return nil, nil, err
  1500  	}
  1501  
  1502  	usr := new(User)
  1503  	resp, err := s.client.Do(req, usr)
  1504  	if err != nil {
  1505  		return nil, resp, err
  1506  	}
  1507  
  1508  	return usr, resp, nil
  1509  }
  1510  

View as plain text