users.go 24 KB

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