|
6 | 6 |
|
7 | 7 | import org.gitlab4j.api.Constants.TokenType; |
8 | 8 | import org.gitlab4j.api.models.Session; |
| 9 | +import org.gitlab4j.api.models.User; |
9 | 10 | import org.gitlab4j.api.models.Version; |
10 | 11 |
|
11 | 12 | /** |
@@ -48,6 +49,7 @@ public String getApiNamespace() { |
48 | 49 |
|
49 | 50 | private Session session; |
50 | 51 |
|
| 52 | + |
51 | 53 | /** |
52 | 54 | * Logs into GitLab using provided {@code username} and {@code password}, and creates a new {@code GitLabApi} instance |
53 | 55 | * using returned private token and the specified GitLab API version. |
@@ -288,6 +290,70 @@ public GitLabApi(ApiVersion apiVersion, String hostUrl, TokenType tokenType, Str |
288 | 290 | userApi = new UserApi(this); |
289 | 291 | } |
290 | 292 |
|
| 293 | + /** |
| 294 | + * Sets up all future calls to the GitLab API to be done as another user specified by sudoAsUsername. |
| 295 | + * To revert back to normal non-sudo operation you must call unsudo(), or pass null as the username. |
| 296 | + * |
| 297 | + * @param sudoAsUsername the username to sudo as, null will turn off sudo |
| 298 | + * @throws GitLabApiException if any exception occurs |
| 299 | + */ |
| 300 | + public void sudo(String sudoAsUsername) throws GitLabApiException { |
| 301 | + |
| 302 | + if (sudoAsUsername == null || sudoAsUsername.trim().length() == 0) { |
| 303 | + apiClient.setSudoAsId(null); |
| 304 | + return; |
| 305 | + } |
| 306 | + |
| 307 | + // Get the User specified by username, if you are not an admin or the username is not found, this will fail |
| 308 | + User user = getUserApi().getUser(sudoAsUsername); |
| 309 | + if (user == null || user.getId() == null) { |
| 310 | + throw new GitLabApiException("the specified username was not found"); |
| 311 | + } |
| 312 | + |
| 313 | + Integer sudoAsId = user.getId(); |
| 314 | + apiClient.setSudoAsId(sudoAsId); |
| 315 | + } |
| 316 | + |
| 317 | + |
| 318 | + /** |
| 319 | + * Turns off the currently configured sudo as ID. |
| 320 | + */ |
| 321 | + public void unsudo() { |
| 322 | + apiClient.setSudoAsId(null); |
| 323 | + } |
| 324 | + |
| 325 | + /** |
| 326 | + * Sets up all future calls to the GitLab API to be done as another user specified by provided user ID. |
| 327 | + * To revert back to normal non-sudo operation you must call unsudo(), or pass null as the sudoAsId. |
| 328 | + * |
| 329 | + * @param sudoAsId the ID of the user to sudo as, null will turn off sudo |
| 330 | + * @throws GitLabApiException if any exception occurs |
| 331 | + */ |
| 332 | + public void setSudoAsId(Integer sudoAsId) throws GitLabApiException { |
| 333 | + |
| 334 | + if (sudoAsId == null) { |
| 335 | + apiClient.setSudoAsId(null); |
| 336 | + return; |
| 337 | + } |
| 338 | + |
| 339 | + // Get the User specified by the sudoAsId, if you are not an admin or the username is not found, this will fail |
| 340 | + User user = getUserApi().getUser(sudoAsId); |
| 341 | + if (user == null || user.getId() != sudoAsId) { |
| 342 | + throw new GitLabApiException("the specified user ID was not found"); |
| 343 | + } |
| 344 | + |
| 345 | + apiClient.setSudoAsId(sudoAsId); |
| 346 | + } |
| 347 | + |
| 348 | + /** |
| 349 | + * Get the current sudo as ID, will return null if not in sudo mode. |
| 350 | + * |
| 351 | + * @return the current sudo as ID, will return null if not in sudo mode |
| 352 | + */ |
| 353 | + public Integer getSudoAsId() { |
| 354 | + return (this.apiClient.getSudoAsId()); |
| 355 | + } |
| 356 | + |
291 | 357 | /** |
292 | 358 | * Return the GitLab API version that this instance is using. |
293 | 359 | * |
|
0 commit comments