forked from gitlab4j/gitlab4j-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserApi.java
1557 lines (1429 loc) · 64.1 KB
/
UserApi.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.gitlab4j.api;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
import jakarta.ws.rs.core.Form;
import jakarta.ws.rs.core.GenericType;
import jakarta.ws.rs.core.Response;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.CustomAttribute;
import org.gitlab4j.api.models.Email;
import org.gitlab4j.api.models.Exists;
import org.gitlab4j.api.models.GpgKey;
import org.gitlab4j.api.models.ImpersonationToken;
import org.gitlab4j.api.models.ImpersonationToken.Scope;
import org.gitlab4j.api.models.Membership;
import org.gitlab4j.api.models.SshKey;
import org.gitlab4j.api.models.User;
import org.gitlab4j.api.utils.EmailChecker;
/**
* This class provides an entry point to all the GitLab API users calls.
*
* @see <a href="https://docs.gitlab.com/ce/api/users.html">Users API at GitLab</a>
*/
public class UserApi extends AbstractApi {
private boolean customAttributesEnabled = false;
public UserApi(GitLabApi gitLabApi) {
super(gitLabApi);
}
/**
* Enables custom attributes to be returned when fetching User instances.
*/
public void enableCustomAttributes() {
customAttributesEnabled = true;
}
/**
* Disables custom attributes to be returned when fetching User instances.
*/
public void disableCustomAttributes() {
customAttributesEnabled = false;
}
/**
* <p>Get a list of users.</p>
*
* <strong>WARNING:</strong> Do not use this method to fetch users from https://gitlab.com,
* gitlab.com has many 1,000,000's of users and it will a long time to fetch all of them.
* Instead use {@link #getUsers(int itemsPerPage)} which will return a Pager of Group instances.
*
* <pre><code>GitLab Endpoint: GET /users</code></pre>
*
* @return a list of Users
* @throws GitLabApiException if any exception occurs
*/
public List<User> getUsers() throws GitLabApiException {
String url = this.gitLabApi.getGitLabServerUrl();
if (url.startsWith("https://gitlab.com")) {
GitLabApi.getLogger()
.warning("Fetching all users from " + url
+ " may take many minutes to complete, use Pager<User> getUsers(int) instead.");
}
return (getUsers(getDefaultPerPage()).all());
}
/**
* Get a list of users using the specified page and per page settings.
*
* <pre><code>GitLab Endpoint: GET /users</code></pre>
*
* @param page the page to get
* @param perPage the number of users per page
* @return the list of Users in the specified range
* @throws GitLabApiException if any exception occurs
*/
public List<User> getUsers(int page, int perPage) throws GitLabApiException {
Response response =
get(Response.Status.OK, getPageQueryParams(page, perPage, customAttributesEnabled), "users");
return (response.readEntity(new GenericType<List<User>>() {}));
}
/**
* Get a Pager of users.
*
* <pre><code>GitLab Endpoint: GET /users</code></pre>
*
* @param itemsPerPage the number of User instances that will be fetched per page
* @return a Pager of User
* @throws GitLabApiException if any exception occurs
*/
public Pager<User> getUsers(int itemsPerPage) throws GitLabApiException {
return (new Pager<User>(
this, User.class, itemsPerPage, createGitLabApiForm().asMap(), "users"));
}
/**
* Get a Stream of users.
*
* <pre><code>GitLab Endpoint: GET /users</code></pre>
*
* @return a Stream of Users.
* @throws GitLabApiException if any exception occurs
*/
public Stream<User> getUsersStream() throws GitLabApiException {
return (getUsers(getDefaultPerPage()).stream());
}
/**
* Get a list of active users
*
* <pre><code>GitLab Endpoint: GET /users?active=true</code></pre>
*
* @return a list of active Users
* @throws GitLabApiException if any exception occurs
*/
public List<User> getActiveUsers() throws GitLabApiException {
return (getActiveUsers(getDefaultPerPage()).all());
}
/**
* Get a list of active users using the specified page and per page settings.
*
* <pre><code>GitLab Endpoint: GET /users?active=true</code></pre>
*
* @param page the page to get
* @param perPage the number of users per page
* @return the list of active Users in the specified range
* @throws GitLabApiException if any exception occurs
*/
public List<User> getActiveUsers(int page, int perPage) throws GitLabApiException {
GitLabApiForm formData = createGitLabApiForm()
.withParam("active", true)
.withParam(PAGE_PARAM, page)
.withParam(PER_PAGE_PARAM, perPage);
Response response = get(Response.Status.OK, formData.asMap(), "users");
return (response.readEntity(new GenericType<List<User>>() {}));
}
/**
* Get a Pager of active users.
*
* <pre><code>GitLab Endpoint: GET /users?active=true</code></pre>
*
* @param itemsPerPage the number of active User instances that will be fetched per page
* @return a Pager of active User
* @throws GitLabApiException if any exception occurs
*/
public Pager<User> getActiveUsers(int itemsPerPage) throws GitLabApiException {
GitLabApiForm formData = createGitLabApiForm().withParam("active", true);
return (new Pager<User>(this, User.class, itemsPerPage, formData.asMap(), "users"));
}
/**
* Get a Stream of active users
*
* <pre><code>GitLab Endpoint: GET /users?active=true</code></pre>
*
* @return a Stream of active Users
* @throws GitLabApiException if any exception occurs
*/
public Stream<User> getActiveUsersStream() throws GitLabApiException {
return (getActiveUsers(getDefaultPerPage()).stream());
}
/**
* Approves the specified user. Available only for admin.
*
* <pre><code>GitLab Endpoint: POST /users/:id/approve</code></pre>
*
* @param userId the ID of the user to approve
* @throws GitLabApiException if any exception occurs
*/
public void approveUser(Long userId) throws GitLabApiException {
if (userId == null) {
throw new RuntimeException("userId cannot be null");
}
post(Response.Status.CREATED, (Form) null, "users", userId, "approve");
}
/**
* Rejects specified user that is pending approval. Available only for administrators.
*
* <pre><code>GitLab Endpoint: POST /users/:id/reject</code></pre>
*
* @param userId the ID of the user to reject
* @throws GitLabApiException if any exception occurs
*/
public void rejectUser(Long userId) throws GitLabApiException {
if (userId == null) {
throw new RuntimeException("userId cannot be null");
}
post(Response.Status.OK, (Form) null, "users", userId, "reject");
}
/**
* Blocks the specified user. Available only for admin.
*
* <pre><code>GitLab Endpoint: POST /users/:id/block</code></pre>
*
* @param userId the ID of the user to block
* @throws GitLabApiException if any exception occurs
*/
public void blockUser(Long userId) throws GitLabApiException {
if (userId == null) {
throw new RuntimeException("userId cannot be null");
}
if (isApiVersion(ApiVersion.V3)) {
put(Response.Status.CREATED, null, "users", userId, "block");
} else {
post(Response.Status.CREATED, (Form) null, "users", userId, "block");
}
}
/**
* Unblocks the specified user. Available only for admin.
*
* <pre><code>GitLab Endpoint: POST /users/:id/unblock</code></pre>
*
* @param userId the ID of the user to unblock
* @throws GitLabApiException if any exception occurs
*/
public void unblockUser(Long userId) throws GitLabApiException {
if (userId == null) {
throw new RuntimeException("userId cannot be null");
}
if (isApiVersion(ApiVersion.V3)) {
put(Response.Status.CREATED, null, "users", userId, "unblock");
} else {
post(Response.Status.CREATED, (Form) null, "users", userId, "unblock");
}
}
/**
* Get a list of blocked users.
*
* <pre><code>GitLab Endpoint: GET /users?blocked=true</code></pre>
*
* @return a list of blocked Users
* @throws GitLabApiException if any exception occurs
*/
public List<User> getBlockedUsers() throws GitLabApiException {
return (getBlockedUsers(getDefaultPerPage()).all());
}
/**
* Get a list of blocked users using the specified page and per page settings.
*
* <pre><code>GitLab Endpoint: GET /users?blocked=true</code></pre>
*
* @param page the page to get
* @param perPage the number of users per page
* @return the list of blocked Users in the specified range
* @throws GitLabApiException if any exception occurs
*/
public List<User> getblockedUsers(int page, int perPage) throws GitLabApiException {
GitLabApiForm formData = createGitLabApiForm()
.withParam("blocked", true)
.withParam(PAGE_PARAM, page)
.withParam(PER_PAGE_PARAM, perPage);
Response response = get(Response.Status.OK, formData.asMap(), "users");
return (response.readEntity(new GenericType<List<User>>() {}));
}
/**
* Get a Pager of blocked users.
*
* <pre><code>GitLab Endpoint: GET /users?blocked=true</code></pre>
*
* @param itemsPerPage the number of blocked User instances that will be fetched per page
* @return a Pager of blocked User
* @throws GitLabApiException if any exception occurs
*/
public Pager<User> getBlockedUsers(int itemsPerPage) throws GitLabApiException {
GitLabApiForm formData = createGitLabApiForm().withParam("blocked", true);
return (new Pager<User>(this, User.class, itemsPerPage, formData.asMap(), "users"));
}
/**
* Get a Stream of blocked users.
*
* <pre><code>GitLab Endpoint: GET /users?blocked=true</code></pre>
*
* @return a Stream of blocked Users
* @throws GitLabApiException if any exception occurs
*/
public Stream<User> getBlockedUsersStream() throws GitLabApiException {
return (getBlockedUsers(getDefaultPerPage()).stream());
}
/**
* Get a single user.
*
* <pre><code>GitLab Endpoint: GET /users/:id</code></pre>
*
* @param userId the ID of the user to get
* @return the User instance for the specified user ID
* @throws GitLabApiException if any exception occurs
*/
public User getUser(Long userId) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm().withParam("with_custom_attributes", customAttributesEnabled);
Response response = get(Response.Status.OK, formData.asMap(), "users", userId);
return (response.readEntity(User.class));
}
/**
* Get a single user as an Optional instance.
*
* <pre><code>GitLab Endpoint: GET /users/:id</code></pre>
*
* @param userId the ID of the user to get
* @return the User for the specified user ID as an Optional instance
*/
public Optional<User> getOptionalUser(Long userId) {
try {
return (Optional.ofNullable(getUser(userId)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}
/**
* Lookup a user by username. Returns null if not found.
*
* <p>NOTE: This is for admin users only.</p>
*
* <pre><code>GitLab Endpoint: GET /users?username=:username</code></pre>
*
* @param username the username of the user to get
* @return the User instance for the specified username, or null if not found
* @throws GitLabApiException if any exception occurs
*/
public User getUser(String username) throws GitLabApiException {
GitLabApiForm formData = createGitLabApiForm()
.withParam("username", username, true)
.withParam(PAGE_PARAM, 1)
.withParam(PER_PAGE_PARAM, 1);
Response response = get(Response.Status.OK, formData.asMap(), "users");
List<User> users = response.readEntity(new GenericType<List<User>>() {});
return (users.isEmpty() ? null : users.get(0));
}
/**
* Lookup a user by username and return an Optional instance.
*
* <p>NOTE: This is for admin users only.</p>
*
* <pre><code>GitLab Endpoint: GET /users?username=:username</code></pre>
*
* @param username the username of the user to get
* @return the User for the specified username as an Optional instance
*/
public Optional<User> getOptionalUser(String username) {
try {
return (Optional.ofNullable(getUser(username)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}
/**
* Lookup a user by email address. Returns null if not found.
*
* <pre><code>GitLab Endpoint: GET /users?search=:email_or_username</code></pre>
*
* @param email the email of the user to get
* @return the User instance for the specified email, or null if not found
* @throws GitLabApiException if any exception occurs
* @throws IllegalArgumentException if email is not valid
*/
public User getUserByEmail(String email) throws GitLabApiException {
if (!EmailChecker.isValidEmail(email)) {
throw new IllegalArgumentException("email is not valid");
}
List<User> users = findUsers(email, 1, 1);
return (users.isEmpty() ? null : users.get(0));
}
/**
* Lookup a user by email address and returns an Optional with the User instance as the value.
*
* <pre><code>GitLab Endpoint: GET /users?search=:email_or_username</code></pre>
*
* @param email the email of the user to get
* @return the User for the specified email as an Optional instance
*/
public Optional<User> getOptionalUserByEmail(String email) {
try {
return (Optional.ofNullable(getUserByEmail(email)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}
/**
* Lookup a user by external UID. Returns null if not found.
*
* <p>NOTE: This is for admin users only.</p>
*
* <pre><code>GitLab Endpoint: GET /users?extern_uid=:externalUid&provider=:provider</code></pre>
*
* @param provider the provider of the external uid
* @param externalUid the external UID of the user
* @return the User instance for the specified external UID, or null if not found
* @throws GitLabApiException if any exception occurs
*/
public User getUserByExternalUid(String provider, String externalUid) throws GitLabApiException {
GitLabApiForm formData = createGitLabApiForm()
.withParam("provider", provider, true)
.withParam("extern_uid", externalUid, true)
.withParam(PAGE_PARAM, 1)
.withParam(PER_PAGE_PARAM, 1);
Response response = get(Response.Status.OK, formData.asMap(), "users");
List<User> users = response.readEntity(new GenericType<List<User>>() {});
return (users.isEmpty() ? null : users.get(0));
}
/**
* Lookup a user by external UID and return an Optional instance.
*
* <p>NOTE: This is for admin users only.</p>
*
* <pre><code>GitLab Endpoint: GET /users?extern_uid=:externUid&provider=:provider</code></pre>
*
* @param provider the provider of the external uid
* @param externalUid the external UID of the user
* @return the User for the specified external UID as an Optional instance
*/
public Optional<User> getOptionalUserByExternalUid(String provider, String externalUid) {
try {
return (Optional.ofNullable(getUserByExternalUid(provider, externalUid)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}
/**
* Search users by Email or username
*
* <pre><code>GitLab Endpoint: GET /users?search=:email_or_username</code></pre>
*
* @param emailOrUsername the email or username to search for
* @return the User List with the email or username like emailOrUsername
* @throws GitLabApiException if any exception occurs
*/
public List<User> findUsers(String emailOrUsername) throws GitLabApiException {
return (findUsers(emailOrUsername, getDefaultPerPage()).all());
}
/**
* Search users by Email or username in the specified page range.
*
* <pre><code>GitLab Endpoint: GET /users?search=:email_or_username</code></pre>
*
* @param emailOrUsername the email or username to search for
* @param page the page to get
* @param perPage the number of users per page
* @return the User List with the email or username like emailOrUsername in the specified page range
* @throws GitLabApiException if any exception occurs
*/
public List<User> findUsers(String emailOrUsername, int page, int perPage) throws GitLabApiException {
GitLabApiForm formData = createGitLabApiForm()
.withParam("search", emailOrUsername, true)
.withParam(PAGE_PARAM, page)
.withParam(PER_PAGE_PARAM, perPage);
Response response = get(Response.Status.OK, formData.asMap(), "users");
return (response.readEntity(new GenericType<List<User>>() {}));
}
/**
* Search users by Email or username and return a Pager
*
* <pre><code>GitLab Endpoint: GET /users?search=:email_or_username</code></pre>
*
* @param emailOrUsername the email or username to search for
* @param itemsPerPage the number of Project instances that will be fetched per page
* @return the User Pager with the email or username like emailOrUsername
* @throws GitLabApiException if any exception occurs
*/
public Pager<User> findUsers(String emailOrUsername, int itemsPerPage) throws GitLabApiException {
GitLabApiForm formData = createGitLabApiForm().withParam("search", emailOrUsername, true);
return (new Pager<User>(this, User.class, itemsPerPage, formData.asMap(), "users"));
}
/**
* Search users by Email or username.
*
* <pre><code>GitLab Endpoint: GET /users?search=:email_or_username</code></pre>
*
* @param emailOrUsername the email or username to search for
* @return a Stream of User instances with the email or username like emailOrUsername
* @throws GitLabApiException if any exception occurs
*/
public Stream<User> findUsersStream(String emailOrUsername) throws GitLabApiException {
return (findUsers(emailOrUsername, getDefaultPerPage()).stream());
}
/**
* <p>Creates a new user. Note only administrators can create new users.
* Either password or reset_password should be specified (reset_password takes priority).</p>
*
* <p>If both the User object's projectsLimit and the parameter projectsLimit is specified
* the parameter will take precedence.</p>
*
* <pre><code>GitLab Endpoint: POST /users</code></pre>
*
* <p>The following properties of the provided User instance can be set during creation:<pre><code> email (required) - Email
* username (required) - Username
* name (required) - Name
* skype (optional) - Skype ID
* linkedin (optional) - LinkedIn
* twitter (optional) - Twitter account
* websiteUrl (optional) - Website URL
* organization (optional) - Organization name
* projectsLimit (optional) - Number of projects user can create
* externUid (optional) - External UID
* provider (optional) - External provider name
* bio (optional) - User's biography
* location (optional) - User's location
* admin (optional) - User is admin - true or false (default)
* canCreateGroup (optional) - User can create groups - true or false
* skipConfirmation (optional) - Skip confirmation - true or false (default)
* external (optional) - Flags the user as external - true or false(default)
* sharedRunnersMinutesLimit (optional) - Pipeline minutes quota for this user
* </code></pre>
*
* @param user the User instance with the user info to create
* @param password the password for the new user
* @param projectsLimit the maximum number of project
* @return created User instance
* @throws GitLabApiException if any exception occurs
* @deprecated Will be removed in version 6.0, replaced by {@link #createUser(User, CharSequence, Boolean)}
*/
@Deprecated
public User createUser(User user, CharSequence password, Integer projectsLimit) throws GitLabApiException {
Form formData = userToForm(user, projectsLimit, password, null, true);
Response response = post(Response.Status.CREATED, formData, "users");
return (response.readEntity(User.class));
}
/**
* <p>Creates a new user. Note only administrators can create new users.
* Either password or resetPassword should be specified (resetPassword takes priority).</p>
*
* <pre><code>GitLab Endpoint: POST /users</code></pre>
*
* <p>The following properties of the provided User instance can be set during creation:<pre><code> email (required) - Email
* username (required) - Username
* name (required) - Name
* skype (optional) - Skype ID
* linkedin (optional) - LinkedIn
* twitter (optional) - Twitter account
* websiteUrl (optional) - Website URL
* organization (optional) - Organization name
* projectsLimit (optional) - Number of projects user can create
* externUid (optional) - External UID
* provider (optional) - External provider name
* bio (optional) - User's biography
* location (optional) - User's location
* admin (optional) - User is admin - true or false (default)
* canCreateGroup (optional) - User can create groups - true or false
* skipConfirmation (optional) - Skip confirmation - true or false (default)
* external (optional) - Flags the user as external - true or false(default)
* sharedRunnersMinutesLimit (optional) - Pipeline minutes quota for this user
* </code></pre>
*
* @param user the User instance with the user info to create
* @param password the password for the new user
* @param resetPassword whether to send a password reset link
* @return created User instance
* @throws GitLabApiException if any exception occurs
*/
public User createUser(User user, CharSequence password, Boolean resetPassword) throws GitLabApiException {
Form formData = userToForm(user, null, password, resetPassword, true);
Response response = post(Response.Status.CREATED, formData, "users");
return (response.readEntity(User.class));
}
/**
* <p>Modifies an existing user. Only administrators can change attributes of a user.</p>
*
* <pre><code>GitLab Endpoint: PUT /users</code></pre>
*
* <p>The following properties of the provided User instance can be set during update:<pre><code> email (required) - Email
* username (required) - Username
* name (required) - Name
* skype (optional) - Skype ID
* linkedin (optional) - LinkedIn
* twitter (optional) - Twitter account
* websiteUrl (optional) - Website URL
* organization (optional) - Organization name
* projectsLimit (optional) - Number of projects user can create
* externUid (optional) - External UID
* provider (optional) - External provider name
* bio (optional) - User's biography
* location (optional) - User's location
* admin (optional) - User is admin - true or false (default)
* canCreateGroup (optional) - User can create groups - true or false
* skipConfirmation (optional) - Skip confirmation - true or false (default)
* external (optional) - Flags the user as external - true or false(default)
* sharedRunnersMinutesLimit (optional) - Pipeline minutes quota for this user
* </code></pre>
*
* @param user the User instance with the user info to modify
* @param password the new password for the user
* @return the modified User instance
* @throws GitLabApiException if any exception occurs
*/
public User updateUser(User user, CharSequence password) throws GitLabApiException {
Form form = userToForm(user, null, password, false, false);
Response response = put(Response.Status.OK, form.asMap(), "users", user.getId());
return (response.readEntity(User.class));
}
/**
* Modifies an existing user. Only administrators can change attributes of a user.
*
* <pre><code>GitLab Endpoint: PUT /users/:id</code></pre>
*
* <p>The following properties of the provided User instance can be set during update:<pre><code> email (required) - Email
* username (required) - Username
* name (required) - Name
* skype (optional) - Skype ID
* linkedin (optional) - LinkedIn
* twitter (optional) - Twitter account
* websiteUrl (optional) - Website URL
* organization (optional) - Organization name
* projectsLimit (optional) - Number of projects user can create
* externUid (optional) - External UID
* provider (optional) - External provider name
* bio (optional) - User's biography
* location (optional) - User's location
* admin (optional) - User is admin - true or false (default)
* canCreateGroup (optional) - User can create groups - true or false
* skipConfirmation (optional) - Skip confirmation - true or false (default)
* external (optional) - Flags the user as external - true or false(default)
* sharedRunnersMinutesLimit (optional) - Pipeline minutes quota for this user
* </code></pre>
*
* @param user the User instance with the user info to modify
* @param password the new password for the user
* @param projectsLimit the maximum number of project
* @return the modified User instance
* @throws GitLabApiException if any exception occurs
* @deprecated Will be removed in version 6.0, replaced by {@link #updateUser(User, CharSequence)}
*/
@Deprecated
public User modifyUser(User user, CharSequence password, Integer projectsLimit) throws GitLabApiException {
Form form = userToForm(user, projectsLimit, password, false, false);
Response response = put(Response.Status.OK, form.asMap(), "users", user.getId());
return (response.readEntity(User.class));
}
/**
* Deletes a user. Available only for administrators.
*
* <pre><code>GitLab Endpoint: DELETE /users/:id</code></pre>
*
* @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance
* @throws GitLabApiException if any exception occurs
*/
public void deleteUser(Object userIdOrUsername) throws GitLabApiException {
deleteUser(userIdOrUsername, null);
}
/**
* Deletes a user. Available only for administrators.
*
* <pre><code>GitLab Endpoint: DELETE /users/:id</code></pre>
*
* @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance
* @param hardDelete If true, contributions that would usually be moved to the
* ghost user will be deleted instead, as well as groups owned solely by this user
* @throws GitLabApiException if any exception occurs
*/
public void deleteUser(Object userIdOrUsername, Boolean hardDelete) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm().withParam("hard_delete ", hardDelete);
Response.Status expectedStatus =
(isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
delete(expectedStatus, formData.asMap(), "users", getUserIdOrUsername(userIdOrUsername));
}
/**
* Get currently authenticated user.
*
* <pre><code>GitLab Endpoint: GET /user</code></pre>
*
* @return the User instance for the currently authenticated user
* @throws GitLabApiException if any exception occurs
*/
public User getCurrentUser() throws GitLabApiException {
Response response = get(Response.Status.OK, null, "user");
return (response.readEntity(User.class));
}
/**
* Get a list of currently authenticated user's SSH keys.
*
* <pre><code>GitLab Endpoint: GET /user/keys</code></pre>
*
* @return a list of currently authenticated user's SSH keys
* @throws GitLabApiException if any exception occurs
*/
public List<SshKey> getSshKeys() throws GitLabApiException {
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "user", "keys");
return (response.readEntity(new GenericType<List<SshKey>>() {}));
}
/**
* Get a list of a specified user's SSH keys. Available only for admin users.
*
* <pre><code>GitLab Endpoint: GET /users/:id/keys</code></pre>
*
* @param userId the user ID to get the SSH keys for
* @return a list of a specified user's SSH keys
* @throws GitLabApiException if any exception occurs
*/
public List<SshKey> getSshKeys(Long userId) throws GitLabApiException {
if (userId == null) {
throw new RuntimeException("userId cannot be null");
}
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "users", userId, "keys");
List<SshKey> keys = response.readEntity(new GenericType<List<SshKey>>() {});
if (keys != null) {
keys.forEach(key -> key.setUserId(userId));
}
return (keys);
}
/**
* Get a single SSH Key.
*
* <pre><code>GitLab Endpoint: GET /user/keys/:key_id</code></pre>
*
* @param keyId the ID of the SSH key.
* @return an SshKey instance holding the info on the SSH key specified by keyId
* @throws GitLabApiException if any exception occurs
*/
public SshKey getSshKey(Long keyId) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "user", "keys", keyId);
return (response.readEntity(SshKey.class));
}
/**
* Get a single SSH Key as an Optional instance.
*
* <pre><code>GitLab Endpoint: GET /user/keys/:key_id</code></pre>
*
* @param keyId the ID of the SSH key
* @return an SshKey as an Optional instance holding the info on the SSH key specified by keyId
*/
public Optional<SshKey> getOptionalSshKey(Long keyId) {
try {
return (Optional.ofNullable(getSshKey(keyId)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}
/**
* Creates a new key owned by the currently authenticated user.
*
* <pre><code>GitLab Endpoint: POST /user/keys</code></pre>
*
* @param title the new SSH Key's title
* @param key the new SSH key
* @return an SshKey instance with info on the added SSH key
* @throws GitLabApiException if any exception occurs
* @deprecated use {@link #addSshKey(String, String, Date)} instead
*/
@Deprecated
public SshKey addSshKey(String title, String key) throws GitLabApiException {
return addSshKey(title, key, null);
}
/**
* Creates a new key owned by the currently authenticated user.
*
* <pre><code>GitLab Endpoint: POST /user/keys</code></pre>
*
* @param title the new SSH Key's title
* @param key the new SSH key
* @param expiresAt the expiration date of the ssh key, optional
* @return an SshKey instance with info on the added SSH key
* @throws GitLabApiException if any exception occurs
*/
public SshKey addSshKey(String title, String key, Date expiresAt) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("title", title)
.withParam("key", key)
.withParam("expires_at", expiresAt);
Response response = post(Response.Status.CREATED, formData, "user", "keys");
return (response.readEntity(SshKey.class));
}
/**
* Create new key owned by specified user. Available only for admin users.
*
* <pre><code>GitLab Endpoint: POST /users/:id/keys</code></pre>
*
* @param userId the ID of the user to add the SSH key for
* @param title the new SSH Key's title
* @param key the new SSH key
* @return an SshKey instance with info on the added SSH key
* @throws GitLabApiException if any exception occurs
* @deprecated use {@link #addSshKey(Long, String, String, Date)} instead
*/
@Deprecated
public SshKey addSshKey(Long userId, String title, String key) throws GitLabApiException {
return addSshKey(userId, title, key, null);
}
/**
* Create new key owned by specified user. Available only for admin users.
*
* <pre><code>GitLab Endpoint: POST /users/:id/keys</code></pre>
*
* @param userId the ID of the user to add the SSH key for
* @param title the new SSH Key's title
* @param key the new SSH key
* @param expiresAt the expiration date of the ssh key, optional
* @return an SshKey instance with info on the added SSH key
* @throws GitLabApiException if any exception occurs
*/
public SshKey addSshKey(Long userId, String title, String key, Date expiresAt) throws GitLabApiException {
if (userId == null) {
throw new RuntimeException("userId cannot be null");
}
GitLabApiForm formData = new GitLabApiForm()
.withParam("title", title)
.withParam("key", key)
.withParam("expires_at", expiresAt);
Response response = post(Response.Status.CREATED, formData, "users", userId, "keys");
SshKey sshKey = response.readEntity(SshKey.class);
if (sshKey != null) {
sshKey.setUserId(userId);
}
return (sshKey);
}
/**
* Deletes key owned by currently authenticated user. This is an idempotent function and calling it
* on a key that is already deleted or not available results in success.
*
* <pre><code>GitLab Endpoint: DELETE /user/keys/:key_id</code></pre>
*
* @param keyId the key ID to delete
* @throws GitLabApiException if any exception occurs
*/
public void deleteSshKey(Long keyId) throws GitLabApiException {
if (keyId == null) {
throw new RuntimeException("keyId cannot be null");
}
Response.Status expectedStatus =
(isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
delete(expectedStatus, null, "user", "keys", keyId);
}
/**
* Deletes key owned by a specified user. Available only for admin users.
*
* <pre><code>GitLab Endpoint: DELETE /users/:id/keys/:key_id</code></pre>
*
* @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance
* @param keyId the key ID to delete
* @throws GitLabApiException if any exception occurs
*/
public void deleteSshKey(Object userIdOrUsername, Long keyId) throws GitLabApiException {
if (keyId == null) {
throw new RuntimeException("keyId cannot be null");
}
Response.Status expectedStatus =
(isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
delete(expectedStatus, null, "users", getUserIdOrUsername(userIdOrUsername), "keys", keyId);
}
/**
* Get a list of a specified user's impersonation tokens. Available only for admin users.
*
* <pre><code>GitLab Endpoint: GET /users/:id/impersonation_tokens</code></pre>
*
* @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance
* @return a list of a specified user's impersonation tokens
* @throws GitLabApiException if any exception occurs
*/
public List<ImpersonationToken> getImpersonationTokens(Object userIdOrUsername) throws GitLabApiException {
return (getImpersonationTokens(userIdOrUsername, null));
}
/**
* Get a list of a specified user's impersonation tokens. Available only for admin users.
*
* <pre><code>GitLab Endpoint: GET /users/:id/impersonation_tokens</code></pre>
*
* @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance
* @param state the state of impersonation tokens to list (ALL, ACTIVE, INACTIVE)
* @return a list of a specified user's impersonation tokens
* @throws GitLabApiException if any exception occurs
*/
public List<ImpersonationToken> getImpersonationTokens(Object userIdOrUsername, ImpersonationState state)
throws GitLabApiException {
GitLabApiForm formData =
new GitLabApiForm().withParam("state", state).withParam(PER_PAGE_PARAM, getDefaultPerPage());
Response response = get(
Response.Status.OK,
formData.asMap(),
"users",
getUserIdOrUsername(userIdOrUsername),
"impersonation_tokens");
return (response.readEntity(new GenericType<List<ImpersonationToken>>() {}));
}
/**
* Get an impersonation token of a user. Available only for admin users.
*
* <pre><code>GitLab Endpoint: GET /users/:user_id/impersonation_tokens/:impersonation_token_id</code></pre>
*
* @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance
* @param tokenId the impersonation token ID to get
* @return the specified impersonation token
* @throws GitLabApiException if any exception occurs
*/
public ImpersonationToken getImpersonationToken(Object userIdOrUsername, Long tokenId) throws GitLabApiException {
if (tokenId == null) {
throw new RuntimeException("tokenId cannot be null");
}
Response response = get(
Response.Status.OK,
null,
"users",
getUserIdOrUsername(userIdOrUsername),
"impersonation_tokens",
tokenId);
return (response.readEntity(ImpersonationToken.class));
}
/**
* Get an impersonation token of a user as an Optional instance. Available only for admin users.
*
* <pre><code>GitLab Endpoint: GET /users/:user_id/impersonation_tokens/:impersonation_token_id</code></pre>
*
* @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance
* @param tokenId the impersonation token ID to get
* @return the specified impersonation token as an Optional instance
*/
public Optional<ImpersonationToken> getOptionalImpersonationToken(Object userIdOrUsername, Long tokenId) {
try {
return (Optional.ofNullable(getImpersonationToken(userIdOrUsername, tokenId)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}
/**
* Create an impersonation token. Available only for admin users.
*
* <pre><code>GitLab Endpoint: POST /users/:user_id/impersonation_tokens</code></pre>
*
* @param userIdOrUsername the user in the form of an Long(ID), String(username), or User instance
* @param name the name of the impersonation token, required
* @param expiresAt the expiration date of the impersonation token, optional
* @param scopes an array of scopes of the impersonation token
* @return the created ImpersonationToken instance
* @throws GitLabApiException if any exception occurs
*/
public ImpersonationToken createImpersonationToken(
Object userIdOrUsername, String name, Date expiresAt, Scope[] scopes) throws GitLabApiException {
return createPersonalAccessTokenOrImpersonationToken(userIdOrUsername, name, null, expiresAt, scopes, true);
}
/**