11package org .gitlab4j .api ;
22
3+ import java .util .Date ;
34import java .util .List ;
45
56import javax .ws .rs .core .Form ;
67import javax .ws .rs .core .GenericType ;
78import javax .ws .rs .core .Response ;
89
910import org .gitlab4j .api .GitLabApi .ApiVersion ;
11+ import org .gitlab4j .api .models .ImpersonationToken ;
12+ import org .gitlab4j .api .models .ImpersonationToken .Scope ;
1013import org .gitlab4j .api .models .SshKey ;
1114import org .gitlab4j .api .models .User ;
1215
@@ -109,6 +112,44 @@ public Pager<User> getActiveUsers(int itemsPerPage) throws GitLabApiException{
109112 return (new Pager <User >(this , User .class , itemsPerPage , formData .asMap (), "users" ));
110113 }
111114
115+ /**
116+ * Blocks the specified user. Available only for admin.
117+ *
118+ * POST /users/:id/block
119+ *
120+ * @param userId the ID of the user to block
121+ * @return the User instance for the blocked user
122+ * @throws GitLabApiException if any exception occurs
123+ */
124+ public User blockUser (Integer userId ) throws GitLabApiException {
125+
126+ if (userId == null ) {
127+ throw new RuntimeException ("userId cannot be null" );
128+ }
129+
130+ Response response = post (Response .Status .CREATED , (Form ) null , "users" , userId , "block" );
131+ return (response .readEntity (User .class ));
132+ }
133+
134+ /**
135+ * Unblocks the specified user. Available only for admin.
136+ *
137+ * POST /users/:id/unblock
138+ *
139+ * @param userId the ID of the user to unblock
140+ * @return the User instance for the unblocked user
141+ * @throws GitLabApiException if any exception occurs
142+ */
143+ public User unblockUser (Integer userId ) throws GitLabApiException {
144+
145+ if (userId == null ) {
146+ throw new RuntimeException ("userId cannot be null" );
147+ }
148+
149+ Response response = post (Response .Status .CREATED , (Form ) null , "users" , userId , "unblock" );
150+ return (response .readEntity (User .class ));
151+ }
152+
112153 /**
113154 * Get a list of blocked users. Only returns the first page
114155 *
@@ -443,7 +484,8 @@ public void deleteSshKey(Integer keyId) throws GitLabApiException {
443484 throw new RuntimeException ("keyId cannot be null" );
444485 }
445486
446- delete (Response .Status .OK , null , "user" , "keys" , keyId );
487+ Response .Status expectedStatus = (isApiVersion (ApiVersion .V3 ) ? Response .Status .OK : Response .Status .NO_CONTENT );
488+ delete (expectedStatus , null , "user" , "keys" , keyId );
447489 }
448490
449491 /**
@@ -465,7 +507,125 @@ public void deleteSshKey(Integer userId, Integer keyId) throws GitLabApiExceptio
465507 throw new RuntimeException ("keyId cannot be null" );
466508 }
467509
468- delete (Response .Status .OK , null , "users" , userId , "keys" , keyId );
510+ Response .Status expectedStatus = (isApiVersion (ApiVersion .V3 ) ? Response .Status .OK : Response .Status .NO_CONTENT );
511+ delete (expectedStatus , null , "users" , userId , "keys" , keyId );
512+ }
513+
514+ /**
515+ * Get a list of a specified user's impersonation tokens. Available only for admin users.
516+ *
517+ * GET /users/:id/impersonation_tokens
518+ *
519+ * @param userId the ID of the user to get impersonation tokens for
520+ * @return a list of a specified user's impersonation tokens
521+ * @throws GitLabApiException if any exception occurs
522+ */
523+ public List <ImpersonationToken > getImpersonationTokens (Integer userId ) throws GitLabApiException {
524+ return (getImpersonationTokens (userId , null ));
525+ }
526+
527+ /**
528+ * Get a list of a specified user's impersonation tokens. Available only for admin users.
529+ *
530+ * GET /users/:id/impersonation_tokens
531+ *
532+ * @param userId the ID of the user to get impersonation tokens for
533+ * @param state the state of impersonation tokens to list (ALL, ACTIVE, INACTIVE)
534+ * @return a list of a specified user's impersonation tokens
535+ * @throws GitLabApiException if any exception occurs
536+ */
537+ public List <ImpersonationToken > getImpersonationTokens (Integer userId , ImpersonationState state ) throws GitLabApiException {
538+
539+ if (userId == null ) {
540+ throw new RuntimeException ("userId cannot be null" );
541+ }
542+
543+ GitLabApiForm formData = new GitLabApiForm ()
544+ .withParam ("state" , state )
545+ .withParam (PER_PAGE_PARAM , getDefaultPerPage ());
546+ Response response = get (Response .Status .OK , formData .asMap (), "users" , userId , "impersonation_tokens" );
547+ return (response .readEntity (new GenericType <List <ImpersonationToken >>() {}));
548+ }
549+
550+ /**
551+ * Get an impersonation token of a user. Available only for admin users.
552+ *
553+ * GET /users/:user_id/impersonation_tokens/:impersonation_token_id
554+ *
555+ * @param userId the ID of the user to get SSH keys for
556+ * @param tokenId the impersonation token ID to get
557+ * @return the specified impersonation token
558+ * @throws GitLabApiException if any exception occurs
559+ */
560+ public ImpersonationToken getImpersonationToken (Integer userId , Integer tokenId ) throws GitLabApiException {
561+
562+ if (userId == null ) {
563+ throw new RuntimeException ("userId cannot be null" );
564+ }
565+
566+ if (tokenId == null ) {
567+ throw new RuntimeException ("tokenId cannot be null" );
568+ }
569+
570+ Response response = get (Response .Status .OK , null , "users" , userId , "impersonation_tokens" , tokenId );
571+ return (response .readEntity (ImpersonationToken .class ));
572+ }
573+
574+ /**
575+ * Create an impersonation token. Available only for admin users.
576+ *
577+ * POST /users/:user_id/impersonation_tokens
578+ *
579+ * @param userId the ID of the user to get SSH keys for
580+ * @param name the name of the impersonation token, required
581+ * @param expiresAt the expiration date of the impersonation token, optional
582+ * @param scopes an array of scopes of the impersonation token
583+ * @return the created ImpersonationToken instance
584+ * @throws GitLabApiException if any exception occurs
585+ */
586+ public ImpersonationToken createImpersonationToken (Integer userId , String name , Date expiresAt , Scope [] scopes ) throws GitLabApiException {
587+
588+ if (userId == null ) {
589+ throw new RuntimeException ("userId cannot be null" );
590+ }
591+
592+ if (scopes == null || scopes .length == 0 ) {
593+ throw new RuntimeException ("scopes cannot be null or empty" );
594+ }
595+
596+ GitLabApiForm formData = new GitLabApiForm ()
597+ .withParam ("name" , name , true )
598+ .withParam ("expires_at" , expiresAt );
599+
600+ for (Scope scope : scopes ) {
601+ formData .withParam ("scopes[]" , scope .toString ());
602+ }
603+
604+ Response response = post (Response .Status .CREATED , formData , "users" , userId , "impersonation_tokens" );
605+ return (response .readEntity (ImpersonationToken .class ));
606+ }
607+
608+ /**
609+ * Deletes an impersonation token. Available only for admin users.
610+ *
611+ * DELETE /users/:user_id/impersonation_tokens/:impersonation_token_id
612+ *
613+ * @param userId the user ID of the user to delete the impersonation token for
614+ * @param tokenId the impersonation token ID to delete
615+ * @throws GitLabApiException if any exception occurs
616+ */
617+ public void deleteImpersonationToken (Integer userId , Integer tokenId ) throws GitLabApiException {
618+
619+ if (userId == null ) {
620+ throw new RuntimeException ("userId cannot be null" );
621+ }
622+
623+ if (tokenId == null ) {
624+ throw new RuntimeException ("tokenId cannot be null" );
625+ }
626+
627+ Response .Status expectedStatus = (isApiVersion (ApiVersion .V3 ) ? Response .Status .OK : Response .Status .NO_CONTENT );
628+ delete (expectedStatus , null , "users" , userId , "impersonation_tokens" , tokenId );
469629 }
470630
471631 /**
0 commit comments