users.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. //
  2. // Copyright 2017, 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. package gitlab
  17. import (
  18. "errors"
  19. "fmt"
  20. "time"
  21. )
  22. // UsersService handles communication with the user related methods of
  23. // the GitLab API.
  24. //
  25. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html
  26. type UsersService struct {
  27. client *Client
  28. }
  29. // User represents a GitLab user.
  30. //
  31. // GitLab API docs: https://docs.gitlab.com/ee/api/users.html
  32. type User struct {
  33. ID int `json:"id"`
  34. Username string `json:"username"`
  35. Email string `json:"email"`
  36. Name string `json:"name"`
  37. State string `json:"state"`
  38. CreatedAt *time.Time `json:"created_at"`
  39. Bio string `json:"bio"`
  40. Location string `json:"location"`
  41. PublicEmail string `json:"public_email"`
  42. Skype string `json:"skype"`
  43. Linkedin string `json:"linkedin"`
  44. Twitter string `json:"twitter"`
  45. WebsiteURL string `json:"website_url"`
  46. Organization string `json:"organization"`
  47. ExternUID string `json:"extern_uid"`
  48. Provider string `json:"provider"`
  49. ThemeID int `json:"theme_id"`
  50. LastActivityOn *ISOTime `json:"last_activity_on"`
  51. ColorSchemeID int `json:"color_scheme_id"`
  52. IsAdmin bool `json:"is_admin"`
  53. AvatarURL string `json:"avatar_url"`
  54. CanCreateGroup bool `json:"can_create_group"`
  55. CanCreateProject bool `json:"can_create_project"`
  56. ProjectsLimit int `json:"projects_limit"`
  57. CurrentSignInAt *time.Time `json:"current_sign_in_at"`
  58. LastSignInAt *time.Time `json:"last_sign_in_at"`
  59. ConfirmedAt *time.Time `json:"confirmed_at"`
  60. TwoFactorEnabled bool `json:"two_factor_enabled"`
  61. Identities []*UserIdentity `json:"identities"`
  62. External bool `json:"external"`
  63. PrivateProfile bool `json:"private_profile"`
  64. SharedRunnersMinutesLimit int `json:"shared_runners_minutes_limit"`
  65. }
  66. // UserIdentity represents a user identity.
  67. type UserIdentity struct {
  68. Provider string `json:"provider"`
  69. ExternUID string `json:"extern_uid"`
  70. }
  71. // ListUsersOptions represents the available ListUsers() options.
  72. //
  73. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#list-users
  74. type ListUsersOptions struct {
  75. ListOptions
  76. Active *bool `url:"active,omitempty" json:"active,omitempty"`
  77. Blocked *bool `url:"blocked,omitempty" json:"blocked,omitempty"`
  78. // The options below are only available for admins.
  79. Search *string `url:"search,omitempty" json:"search,omitempty"`
  80. Username *string `url:"username,omitempty" json:"username,omitempty"`
  81. ExternalUID *string `url:"extern_uid,omitempty" json:"extern_uid,omitempty"`
  82. Provider *string `url:"provider,omitempty" json:"provider,omitempty"`
  83. CreatedBefore *time.Time `url:"created_before,omitempty" json:"created_before,omitempty"`
  84. CreatedAfter *time.Time `url:"created_after,omitempty" json:"created_after,omitempty"`
  85. OrderBy *string `url:"order_by,omitempty" json:"order_by,omitempty"`
  86. Sort *string `url:"sort,omitempty" json:"sort,omitempty"`
  87. }
  88. // ListUsers gets a list of users.
  89. //
  90. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#list-users
  91. func (s *UsersService) ListUsers(opt *ListUsersOptions, options ...OptionFunc) ([]*User, *Response, error) {
  92. req, err := s.client.NewRequest("GET", "users", opt, options)
  93. if err != nil {
  94. return nil, nil, err
  95. }
  96. var usr []*User
  97. resp, err := s.client.Do(req, &usr)
  98. if err != nil {
  99. return nil, resp, err
  100. }
  101. return usr, resp, err
  102. }
  103. // GetUser gets a single user.
  104. //
  105. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#single-user
  106. func (s *UsersService) GetUser(user int, options ...OptionFunc) (*User, *Response, error) {
  107. u := fmt.Sprintf("users/%d", user)
  108. req, err := s.client.NewRequest("GET", u, nil, options)
  109. if err != nil {
  110. return nil, nil, err
  111. }
  112. usr := new(User)
  113. resp, err := s.client.Do(req, usr)
  114. if err != nil {
  115. return nil, resp, err
  116. }
  117. return usr, resp, err
  118. }
  119. // CreateUserOptions represents the available CreateUser() options.
  120. //
  121. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#user-creation
  122. type CreateUserOptions struct {
  123. Email *string `url:"email,omitempty" json:"email,omitempty"`
  124. Password *string `url:"password,omitempty" json:"password,omitempty"`
  125. ResetPassword *bool `url:"reset_password,omitempty" json:"reset_password,omitempty"`
  126. Username *string `url:"username,omitempty" json:"username,omitempty"`
  127. Name *string `url:"name,omitempty" json:"name,omitempty"`
  128. Skype *string `url:"skype,omitempty" json:"skype,omitempty"`
  129. Linkedin *string `url:"linkedin,omitempty" json:"linkedin,omitempty"`
  130. Twitter *string `url:"twitter,omitempty" json:"twitter,omitempty"`
  131. WebsiteURL *string `url:"website_url,omitempty" json:"website_url,omitempty"`
  132. Organization *string `url:"organization,omitempty" json:"organization,omitempty"`
  133. ProjectsLimit *int `url:"projects_limit,omitempty" json:"projects_limit,omitempty"`
  134. ExternUID *string `url:"extern_uid,omitempty" json:"extern_uid,omitempty"`
  135. Provider *string `url:"provider,omitempty" json:"provider,omitempty"`
  136. Bio *string `url:"bio,omitempty" json:"bio,omitempty"`
  137. Location *string `url:"location,omitempty" json:"location,omitempty"`
  138. Admin *bool `url:"admin,omitempty" json:"admin,omitempty"`
  139. CanCreateGroup *bool `url:"can_create_group,omitempty" json:"can_create_group,omitempty"`
  140. SkipConfirmation *bool `url:"skip_confirmation,omitempty" json:"skip_confirmation,omitempty"`
  141. External *bool `url:"external,omitempty" json:"external,omitempty"`
  142. }
  143. // CreateUser creates a new user. Note only administrators can create new users.
  144. //
  145. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#user-creation
  146. func (s *UsersService) CreateUser(opt *CreateUserOptions, options ...OptionFunc) (*User, *Response, error) {
  147. req, err := s.client.NewRequest("POST", "users", opt, options)
  148. if err != nil {
  149. return nil, nil, err
  150. }
  151. usr := new(User)
  152. resp, err := s.client.Do(req, usr)
  153. if err != nil {
  154. return nil, resp, err
  155. }
  156. return usr, resp, err
  157. }
  158. // ModifyUserOptions represents the available ModifyUser() options.
  159. //
  160. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#user-modification
  161. type ModifyUserOptions struct {
  162. Email *string `url:"email,omitempty" json:"email,omitempty"`
  163. Password *string `url:"password,omitempty" json:"password,omitempty"`
  164. Username *string `url:"username,omitempty" json:"username,omitempty"`
  165. Name *string `url:"name,omitempty" json:"name,omitempty"`
  166. Skype *string `url:"skype,omitempty" json:"skype,omitempty"`
  167. Linkedin *string `url:"linkedin,omitempty" json:"linkedin,omitempty"`
  168. Twitter *string `url:"twitter,omitempty" json:"twitter,omitempty"`
  169. WebsiteURL *string `url:"website_url,omitempty" json:"website_url,omitempty"`
  170. Organization *string `url:"organization,omitempty" json:"organization,omitempty"`
  171. ProjectsLimit *int `url:"projects_limit,omitempty" json:"projects_limit,omitempty"`
  172. ExternUID *string `url:"extern_uid,omitempty" json:"extern_uid,omitempty"`
  173. Provider *string `url:"provider,omitempty" json:"provider,omitempty"`
  174. Bio *string `url:"bio,omitempty" json:"bio,omitempty"`
  175. Location *string `url:"location,omitempty" json:"location,omitempty"`
  176. Admin *bool `url:"admin,omitempty" json:"admin,omitempty"`
  177. CanCreateGroup *bool `url:"can_create_group,omitempty" json:"can_create_group,omitempty"`
  178. SkipReconfirmation *bool `url:"skip_reconfirmation,omitempty" json:"skip_reconfirmation,omitempty"`
  179. External *bool `url:"external,omitempty" json:"external,omitempty"`
  180. }
  181. // ModifyUser modifies an existing user. Only administrators can change attributes
  182. // of a user.
  183. //
  184. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#user-modification
  185. func (s *UsersService) ModifyUser(user int, opt *ModifyUserOptions, options ...OptionFunc) (*User, *Response, error) {
  186. u := fmt.Sprintf("users/%d", user)
  187. req, err := s.client.NewRequest("PUT", u, opt, options)
  188. if err != nil {
  189. return nil, nil, err
  190. }
  191. usr := new(User)
  192. resp, err := s.client.Do(req, usr)
  193. if err != nil {
  194. return nil, resp, err
  195. }
  196. return usr, resp, err
  197. }
  198. // DeleteUser deletes a user. Available only for administrators. This is an
  199. // idempotent function, calling this function for a non-existent user id still
  200. // returns a status code 200 OK. The JSON response differs if the user was
  201. // actually deleted or not. In the former the user is returned and in the
  202. // latter not.
  203. //
  204. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#user-deletion
  205. func (s *UsersService) DeleteUser(user int, options ...OptionFunc) (*Response, error) {
  206. u := fmt.Sprintf("users/%d", user)
  207. req, err := s.client.NewRequest("DELETE", u, nil, options)
  208. if err != nil {
  209. return nil, err
  210. }
  211. return s.client.Do(req, nil)
  212. }
  213. // CurrentUser gets currently authenticated user.
  214. //
  215. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#current-user
  216. func (s *UsersService) CurrentUser(options ...OptionFunc) (*User, *Response, error) {
  217. req, err := s.client.NewRequest("GET", "user", nil, options)
  218. if err != nil {
  219. return nil, nil, err
  220. }
  221. usr := new(User)
  222. resp, err := s.client.Do(req, usr)
  223. if err != nil {
  224. return nil, resp, err
  225. }
  226. return usr, resp, err
  227. }
  228. // SSHKey represents a SSH key.
  229. //
  230. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#list-ssh-keys
  231. type SSHKey struct {
  232. ID int `json:"id"`
  233. Title string `json:"title"`
  234. Key string `json:"key"`
  235. CreatedAt *time.Time `json:"created_at"`
  236. }
  237. // ListSSHKeys gets a list of currently authenticated user's SSH keys.
  238. //
  239. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#list-ssh-keys
  240. func (s *UsersService) ListSSHKeys(options ...OptionFunc) ([]*SSHKey, *Response, error) {
  241. req, err := s.client.NewRequest("GET", "user/keys", nil, options)
  242. if err != nil {
  243. return nil, nil, err
  244. }
  245. var k []*SSHKey
  246. resp, err := s.client.Do(req, &k)
  247. if err != nil {
  248. return nil, resp, err
  249. }
  250. return k, resp, err
  251. }
  252. // ListSSHKeysForUserOptions represents the available ListSSHKeysForUser() options.
  253. //
  254. // GitLab API docs:
  255. // https://docs.gitlab.com/ce/api/users.html#list-ssh-keys-for-user
  256. type ListSSHKeysForUserOptions ListOptions
  257. // ListSSHKeysForUser gets a list of a specified user's SSH keys. Available
  258. // only for admin
  259. //
  260. // GitLab API docs:
  261. // https://docs.gitlab.com/ce/api/users.html#list-ssh-keys-for-user
  262. func (s *UsersService) ListSSHKeysForUser(user int, opt *ListSSHKeysForUserOptions, options ...OptionFunc) ([]*SSHKey, *Response, error) {
  263. u := fmt.Sprintf("users/%d/keys", user)
  264. req, err := s.client.NewRequest("GET", u, opt, options)
  265. if err != nil {
  266. return nil, nil, err
  267. }
  268. var k []*SSHKey
  269. resp, err := s.client.Do(req, &k)
  270. if err != nil {
  271. return nil, resp, err
  272. }
  273. return k, resp, err
  274. }
  275. // GetSSHKey gets a single key.
  276. //
  277. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#single-ssh-key
  278. func (s *UsersService) GetSSHKey(key int, options ...OptionFunc) (*SSHKey, *Response, error) {
  279. u := fmt.Sprintf("user/keys/%d", key)
  280. req, err := s.client.NewRequest("GET", u, nil, options)
  281. if err != nil {
  282. return nil, nil, err
  283. }
  284. k := new(SSHKey)
  285. resp, err := s.client.Do(req, k)
  286. if err != nil {
  287. return nil, resp, err
  288. }
  289. return k, resp, err
  290. }
  291. // AddSSHKeyOptions represents the available AddSSHKey() options.
  292. //
  293. // GitLab API docs: https://docs.gitlab.com/ce/api/projects.html#add-ssh-key
  294. type AddSSHKeyOptions struct {
  295. Title *string `url:"title,omitempty" json:"title,omitempty"`
  296. Key *string `url:"key,omitempty" json:"key,omitempty"`
  297. }
  298. // AddSSHKey creates a new key owned by the currently authenticated user.
  299. //
  300. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#add-ssh-key
  301. func (s *UsersService) AddSSHKey(opt *AddSSHKeyOptions, options ...OptionFunc) (*SSHKey, *Response, error) {
  302. req, err := s.client.NewRequest("POST", "user/keys", opt, options)
  303. if err != nil {
  304. return nil, nil, err
  305. }
  306. k := new(SSHKey)
  307. resp, err := s.client.Do(req, k)
  308. if err != nil {
  309. return nil, resp, err
  310. }
  311. return k, resp, err
  312. }
  313. // AddSSHKeyForUser creates new key owned by specified user. Available only for
  314. // admin.
  315. //
  316. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#add-ssh-key-for-user
  317. func (s *UsersService) AddSSHKeyForUser(user int, opt *AddSSHKeyOptions, options ...OptionFunc) (*SSHKey, *Response, error) {
  318. u := fmt.Sprintf("users/%d/keys", user)
  319. req, err := s.client.NewRequest("POST", u, opt, options)
  320. if err != nil {
  321. return nil, nil, err
  322. }
  323. k := new(SSHKey)
  324. resp, err := s.client.Do(req, k)
  325. if err != nil {
  326. return nil, resp, err
  327. }
  328. return k, resp, err
  329. }
  330. // DeleteSSHKey deletes key owned by currently authenticated user. This is an
  331. // idempotent function and calling it on a key that is already deleted or not
  332. // available results in 200 OK.
  333. //
  334. // GitLab API docs:
  335. // https://docs.gitlab.com/ce/api/users.html#delete-ssh-key-for-current-owner
  336. func (s *UsersService) DeleteSSHKey(key int, options ...OptionFunc) (*Response, error) {
  337. u := fmt.Sprintf("user/keys/%d", key)
  338. req, err := s.client.NewRequest("DELETE", u, nil, options)
  339. if err != nil {
  340. return nil, err
  341. }
  342. return s.client.Do(req, nil)
  343. }
  344. // DeleteSSHKeyForUser deletes key owned by a specified user. Available only
  345. // for admin.
  346. //
  347. // GitLab API docs:
  348. // https://docs.gitlab.com/ce/api/users.html#delete-ssh-key-for-given-user
  349. func (s *UsersService) DeleteSSHKeyForUser(user, key int, options ...OptionFunc) (*Response, error) {
  350. u := fmt.Sprintf("users/%d/keys/%d", user, key)
  351. req, err := s.client.NewRequest("DELETE", u, nil, options)
  352. if err != nil {
  353. return nil, err
  354. }
  355. return s.client.Do(req, nil)
  356. }
  357. // BlockUser blocks the specified user. Available only for admin.
  358. //
  359. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#block-user
  360. func (s *UsersService) BlockUser(user int, options ...OptionFunc) error {
  361. u := fmt.Sprintf("users/%d/block", user)
  362. req, err := s.client.NewRequest("POST", u, nil, options)
  363. if err != nil {
  364. return err
  365. }
  366. resp, err := s.client.Do(req, nil)
  367. if err != nil {
  368. return err
  369. }
  370. switch resp.StatusCode {
  371. case 201:
  372. return nil
  373. case 403:
  374. return errors.New("Cannot block a user that is already blocked by LDAP synchronization")
  375. case 404:
  376. return errors.New("User does not exist")
  377. default:
  378. return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
  379. }
  380. }
  381. // UnblockUser unblocks the specified user. Available only for admin.
  382. //
  383. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#unblock-user
  384. func (s *UsersService) UnblockUser(user int, options ...OptionFunc) error {
  385. u := fmt.Sprintf("users/%d/unblock", user)
  386. req, err := s.client.NewRequest("POST", u, nil, options)
  387. if err != nil {
  388. return err
  389. }
  390. resp, err := s.client.Do(req, nil)
  391. if err != nil {
  392. return err
  393. }
  394. switch resp.StatusCode {
  395. case 201:
  396. return nil
  397. case 403:
  398. return errors.New("Cannot unblock a user that is blocked by LDAP synchronization")
  399. case 404:
  400. return errors.New("User does not exist")
  401. default:
  402. return fmt.Errorf("Received unexpected result code: %d", resp.StatusCode)
  403. }
  404. }
  405. // Email represents an Email.
  406. //
  407. // GitLab API docs: https://doc.gitlab.com/ce/api/users.html#list-emails
  408. type Email struct {
  409. ID int `json:"id"`
  410. Email string `json:"email"`
  411. }
  412. // ListEmails gets a list of currently authenticated user's Emails.
  413. //
  414. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#list-emails
  415. func (s *UsersService) ListEmails(options ...OptionFunc) ([]*Email, *Response, error) {
  416. req, err := s.client.NewRequest("GET", "user/emails", nil, options)
  417. if err != nil {
  418. return nil, nil, err
  419. }
  420. var e []*Email
  421. resp, err := s.client.Do(req, &e)
  422. if err != nil {
  423. return nil, resp, err
  424. }
  425. return e, resp, err
  426. }
  427. // ListEmailsForUserOptions represents the available ListEmailsForUser() options.
  428. //
  429. // GitLab API docs:
  430. // https://docs.gitlab.com/ce/api/users.html#list-emails-for-user
  431. type ListEmailsForUserOptions ListOptions
  432. // ListEmailsForUser gets a list of a specified user's Emails. Available
  433. // only for admin
  434. //
  435. // GitLab API docs:
  436. // https://docs.gitlab.com/ce/api/users.html#list-emails-for-user
  437. func (s *UsersService) ListEmailsForUser(user int, opt *ListEmailsForUserOptions, options ...OptionFunc) ([]*Email, *Response, error) {
  438. u := fmt.Sprintf("users/%d/emails", user)
  439. req, err := s.client.NewRequest("GET", u, opt, options)
  440. if err != nil {
  441. return nil, nil, err
  442. }
  443. var e []*Email
  444. resp, err := s.client.Do(req, &e)
  445. if err != nil {
  446. return nil, resp, err
  447. }
  448. return e, resp, err
  449. }
  450. // GetEmail gets a single email.
  451. //
  452. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#single-email
  453. func (s *UsersService) GetEmail(email int, options ...OptionFunc) (*Email, *Response, error) {
  454. u := fmt.Sprintf("user/emails/%d", email)
  455. req, err := s.client.NewRequest("GET", u, nil, options)
  456. if err != nil {
  457. return nil, nil, err
  458. }
  459. e := new(Email)
  460. resp, err := s.client.Do(req, e)
  461. if err != nil {
  462. return nil, resp, err
  463. }
  464. return e, resp, err
  465. }
  466. // AddEmailOptions represents the available AddEmail() options.
  467. //
  468. // GitLab API docs: https://docs.gitlab.com/ce/api/projects.html#add-email
  469. type AddEmailOptions struct {
  470. Email *string `url:"email,omitempty" json:"email,omitempty"`
  471. }
  472. // AddEmail creates a new email owned by the currently authenticated user.
  473. //
  474. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#add-email
  475. func (s *UsersService) AddEmail(opt *AddEmailOptions, options ...OptionFunc) (*Email, *Response, error) {
  476. req, err := s.client.NewRequest("POST", "user/emails", opt, options)
  477. if err != nil {
  478. return nil, nil, err
  479. }
  480. e := new(Email)
  481. resp, err := s.client.Do(req, e)
  482. if err != nil {
  483. return nil, resp, err
  484. }
  485. return e, resp, err
  486. }
  487. // AddEmailForUser creates new email owned by specified user. Available only for
  488. // admin.
  489. //
  490. // GitLab API docs: https://docs.gitlab.com/ce/api/users.html#add-email-for-user
  491. func (s *UsersService) AddEmailForUser(user int, opt *AddEmailOptions, options ...OptionFunc) (*Email, *Response, error) {
  492. u := fmt.Sprintf("users/%d/emails", user)
  493. req, err := s.client.NewRequest("POST", u, opt, options)
  494. if err != nil {
  495. return nil, nil, err
  496. }
  497. e := new(Email)
  498. resp, err := s.client.Do(req, e)
  499. if err != nil {
  500. return nil, resp, err
  501. }
  502. return e, resp, err
  503. }
  504. // DeleteEmail deletes email owned by currently authenticated user. This is an
  505. // idempotent function and calling it on a key that is already deleted or not
  506. // available results in 200 OK.
  507. //
  508. // GitLab API docs:
  509. // https://docs.gitlab.com/ce/api/users.html#delete-email-for-current-owner
  510. func (s *UsersService) DeleteEmail(email int, options ...OptionFunc) (*Response, error) {
  511. u := fmt.Sprintf("user/emails/%d", email)
  512. req, err := s.client.NewRequest("DELETE", u, nil, options)
  513. if err != nil {
  514. return nil, err
  515. }
  516. return s.client.Do(req, nil)
  517. }
  518. // DeleteEmailForUser deletes email owned by a specified user. Available only
  519. // for admin.
  520. //
  521. // GitLab API docs:
  522. // https://docs.gitlab.com/ce/api/users.html#delete-email-for-given-user
  523. func (s *UsersService) DeleteEmailForUser(user, email int, options ...OptionFunc) (*Response, error) {
  524. u := fmt.Sprintf("users/%d/emails/%d", user, email)
  525. req, err := s.client.NewRequest("DELETE", u, nil, options)
  526. if err != nil {
  527. return nil, err
  528. }
  529. return s.client.Do(req, nil)
  530. }
  531. // ImpersonationToken represents an impersonation token.
  532. //
  533. // GitLab API docs:
  534. // https://docs.gitlab.com/ce/api/users.html#get-all-impersonation-tokens-of-a-user
  535. type ImpersonationToken struct {
  536. ID int `json:"id"`
  537. Name string `json:"name"`
  538. Active bool `json:"active"`
  539. Token string `json:"token"`
  540. Scopes []string `json:"scopes"`
  541. Revoked bool `json:"revoked"`
  542. CreatedAt *time.Time `json:"created_at"`
  543. ExpiresAt *ISOTime `json:"expires_at"`
  544. }
  545. // GetAllImpersonationTokensOptions represents the available
  546. // GetAllImpersonationTokens() options.
  547. //
  548. // GitLab API docs:
  549. // https://docs.gitlab.com/ce/api/users.html#get-all-impersonation-tokens-of-a-user
  550. type GetAllImpersonationTokensOptions struct {
  551. ListOptions
  552. State *string `url:"state,omitempty" json:"state,omitempty"`
  553. }
  554. // GetAllImpersonationTokens retrieves all impersonation tokens of a user.
  555. //
  556. // GitLab API docs:
  557. // https://docs.gitlab.com/ce/api/users.html#get-all-impersonation-tokens-of-a-user
  558. func (s *UsersService) GetAllImpersonationTokens(user int, opt *GetAllImpersonationTokensOptions, options ...OptionFunc) ([]*ImpersonationToken, *Response, error) {
  559. u := fmt.Sprintf("users/%d/impersonation_tokens", user)
  560. req, err := s.client.NewRequest("GET", u, opt, options)
  561. if err != nil {
  562. return nil, nil, err
  563. }
  564. var ts []*ImpersonationToken
  565. resp, err := s.client.Do(req, &ts)
  566. if err != nil {
  567. return nil, resp, err
  568. }
  569. return ts, resp, err
  570. }
  571. // GetImpersonationToken retrieves an impersonation token of a user.
  572. //
  573. // GitLab API docs:
  574. // https://docs.gitlab.com/ce/api/users.html#get-an-impersonation-token-of-a-user
  575. func (s *UsersService) GetImpersonationToken(user, token int, options ...OptionFunc) (*ImpersonationToken, *Response, error) {
  576. u := fmt.Sprintf("users/%d/impersonation_tokens/%d", user, token)
  577. req, err := s.client.NewRequest("GET", u, nil, options)
  578. if err != nil {
  579. return nil, nil, err
  580. }
  581. t := new(ImpersonationToken)
  582. resp, err := s.client.Do(req, &t)
  583. if err != nil {
  584. return nil, resp, err
  585. }
  586. return t, resp, err
  587. }
  588. // CreateImpersonationTokenOptions represents the available
  589. // CreateImpersonationToken() options.
  590. //
  591. // GitLab API docs:
  592. // https://docs.gitlab.com/ce/api/users.html#create-an-impersonation-token
  593. type CreateImpersonationTokenOptions struct {
  594. Name *string `url:"name,omitempty" json:"name,omitempty"`
  595. Scopes *[]string `url:"scopes,omitempty" json:"scopes,omitempty"`
  596. ExpiresAt *time.Time `url:"expires_at,omitempty" json:"expires_at,omitempty"`
  597. }
  598. // CreateImpersonationToken creates an impersonation token.
  599. //
  600. // GitLab API docs:
  601. // https://docs.gitlab.com/ce/api/users.html#create-an-impersonation-token
  602. func (s *UsersService) CreateImpersonationToken(user int, opt *CreateImpersonationTokenOptions, options ...OptionFunc) (*ImpersonationToken, *Response, error) {
  603. u := fmt.Sprintf("users/%d/impersonation_tokens", user)
  604. req, err := s.client.NewRequest("POST", u, opt, options)
  605. if err != nil {
  606. return nil, nil, err
  607. }
  608. t := new(ImpersonationToken)
  609. resp, err := s.client.Do(req, &t)
  610. if err != nil {
  611. return nil, resp, err
  612. }
  613. return t, resp, err
  614. }
  615. // RevokeImpersonationToken revokes an impersonation token.
  616. //
  617. // GitLab API docs:
  618. // https://docs.gitlab.com/ce/api/users.html#revoke-an-impersonation-token
  619. func (s *UsersService) RevokeImpersonationToken(user, token int, options ...OptionFunc) (*Response, error) {
  620. u := fmt.Sprintf("users/%d/impersonation_tokens/%d", user, token)
  621. req, err := s.client.NewRequest("DELETE", u, nil, options)
  622. if err != nil {
  623. return nil, err
  624. }
  625. return s.client.Do(req, nil)
  626. }
  627. // UserActivity represents an entry in the user/activities response
  628. //
  629. // GitLab API docs:
  630. // https://docs.gitlab.com/ce/api/users.html#get-user-activities-admin-only
  631. type UserActivity struct {
  632. Username string `json:"username"`
  633. LastActivityOn *ISOTime `json:"last_activity_on"`
  634. }
  635. // GetUserActivitiesOptions represents the options for GetUserActivities
  636. //
  637. // GitLap API docs:
  638. // https://docs.gitlab.com/ce/api/users.html#get-user-activities-admin-only
  639. type GetUserActivitiesOptions struct {
  640. From *ISOTime `url:"from,omitempty" json:"from,omitempty"`
  641. }
  642. // GetUserActivities retrieves user activities (admin only)
  643. //
  644. // GitLab API docs:
  645. // https://docs.gitlab.com/ce/api/users.html#get-user-activities-admin-only
  646. func (s *UsersService) GetUserActivities(opt *GetUserActivitiesOptions, options ...OptionFunc) ([]*UserActivity, *Response, error) {
  647. req, err := s.client.NewRequest("GET", "user/activities", opt, options)
  648. if err != nil {
  649. return nil, nil, err
  650. }
  651. var t []*UserActivity
  652. resp, err := s.client.Do(req, &t)
  653. if err != nil {
  654. return nil, resp, err
  655. }
  656. return t, resp, err
  657. }
  658. // UserStatus represents the current status of a user
  659. //
  660. // GitLab API docs:
  661. // https://docs.gitlab.com/ce/api/users.html#user-status
  662. type UserStatus struct {
  663. Emoji string `json:"emoji"`
  664. Message string `json:"message"`
  665. MessageHTML string `json:"message_html"`
  666. }
  667. // CurrentUserStatus retrieves the user status
  668. //
  669. // GitLab API docs:
  670. // https://docs.gitlab.com/ce/api/users.html#user-status
  671. func (s *UsersService) CurrentUserStatus(options ...OptionFunc) (*UserStatus, *Response, error) {
  672. req, err := s.client.NewRequest("GET", "user/status", nil, options)
  673. if err != nil {
  674. return nil, nil, err
  675. }
  676. status := new(UserStatus)
  677. resp, err := s.client.Do(req, status)
  678. if err != nil {
  679. return nil, resp, err
  680. }
  681. return status, resp, err
  682. }
  683. // GetUserStatus retrieves a user's status
  684. //
  685. // GitLab API docs:
  686. // https://docs.gitlab.com/ce/api/users.html#get-the-status-of-a-user
  687. func (s *UsersService) GetUserStatus(user int, options ...OptionFunc) (*UserStatus, *Response, error) {
  688. u := fmt.Sprintf("users/%d/status", user)
  689. req, err := s.client.NewRequest("GET", u, nil, options)
  690. if err != nil {
  691. return nil, nil, err
  692. }
  693. status := new(UserStatus)
  694. resp, err := s.client.Do(req, status)
  695. if err != nil {
  696. return nil, resp, err
  697. }
  698. return status, resp, err
  699. }
  700. // UserStatusOptions represents the options required to set the status
  701. //
  702. // GitLab API docs:
  703. // https://docs.gitlab.com/ce/api/users.html#set-user-status
  704. type UserStatusOptions struct {
  705. Emoji *string `url:"emoji,omitempty" json:"emoji,omitempty"`
  706. Message *string `url:"message,omitempty" json:"message,omitempty"`
  707. }
  708. // SetUserStatus sets the user's status
  709. //
  710. // GitLab API docs:
  711. // https://docs.gitlab.com/ce/api/users.html#set-user-status
  712. func (s *UsersService) SetUserStatus(opt *UserStatusOptions, options ...OptionFunc) (*UserStatus, *Response, error) {
  713. req, err := s.client.NewRequest("PUT", "user/status", opt, options)
  714. if err != nil {
  715. return nil, nil, err
  716. }
  717. status := new(UserStatus)
  718. resp, err := s.client.Do(req, status)
  719. if err != nil {
  720. return nil, resp, err
  721. }
  722. return status, resp, err
  723. }