using System.Collections.Generic; using System.Net; using System.Threading.Tasks; namespace Octokit { /// /// A client for GitHub's User Administration API (GitHub Enterprise) /// /// /// See the Administration API documentation for more details. /// public class UserAdministrationClient : ApiClient, IUserAdministrationClient { /// /// Initializes a new instance of the class. /// /// The client's connection public UserAdministrationClient(IApiConnection apiConnection) : base(apiConnection) { } /// /// Create a new user (must be Site Admin user). /// /// /// See the API documentation /// for more information. /// /// The object describing the user to create /// The created object [ManualRoute("POST", "/admin/users")] public Task Create(NewUser newUser) { Ensure.ArgumentNotNull(newUser, nameof(newUser)); var endpoint = ApiUrls.UserAdministration(); return ApiConnection.Post(endpoint, newUser); } /// /// Rename an existing user (must be Site Admin user). /// /// /// See the API documentation /// for more information. /// Note that this queues a request to rename a user, rather than execute it straight away /// /// The username to rename /// The request, specifying the new login /// A object indicating the queued task message and Url to the user [ManualRoute("POST", "/admin/users/{username}")] public Task Rename(string login, UserRename userRename) { Ensure.ArgumentNotNullOrEmptyString(login, nameof(login)); Ensure.ArgumentNotNull(userRename, nameof(userRename)); var endpoint = ApiUrls.UserAdministration(login); return ApiConnection.Patch(endpoint, userRename); } /// /// Create an impersonation OAuth token (must be Site Admin user). /// /// /// See the API documentation /// for more information. /// /// The user to impersonate /// The request specifying the required scopes /// An object containing the impersonation token [ManualRoute("POST", "/admin/users/{username}/authorizations")] public Task CreateImpersonationToken(string login, NewImpersonationToken newImpersonationToken) { Ensure.ArgumentNotNullOrEmptyString(login, nameof(login)); Ensure.ArgumentNotNull(newImpersonationToken, nameof(newImpersonationToken)); var endpoint = ApiUrls.UserAdministrationAuthorization(login); return ApiConnection.Post(endpoint, newImpersonationToken); } /// /// Deletes an impersonation OAuth token (must be Site Admin user). /// /// /// See the API documentation /// for more information. /// /// The user to remove impersonation token from /// [ManualRoute("DELETE", "/admin/users/{username}/authorizations")] public async Task DeleteImpersonationToken(string login) { Ensure.ArgumentNotNullOrEmptyString(login, nameof(login)); var endpoint = ApiUrls.UserAdministrationAuthorization(login); var response = await Connection.Delete(endpoint).ConfigureAwait(false); if (response != HttpStatusCode.NoContent) { throw new ApiException("Invalid Status Code returned. Expected a 204", response); } } /// /// Promotes ordinary user to a site administrator (must be Site Admin user). /// /// /// See the API documentation /// for more information. /// /// The user to promote to administrator. /// [ManualRoute("PUT", "/users/{username}/site_admin")] public Task Promote(string login) { Ensure.ArgumentNotNullOrEmptyString(login, nameof(login)); var endpoint = ApiUrls.UserAdministrationSiteAdmin(login); return ApiConnection.Put(endpoint); } /// /// Demotes a site administrator to an ordinary user (must be Site Admin user). /// /// /// See the API documentation /// for more information. /// /// The user to demote from administrator. /// [ManualRoute("DELETE", "/users/{username}/site_admin")] public Task Demote(string login) { Ensure.ArgumentNotNullOrEmptyString(login, nameof(login)); var endpoint = ApiUrls.UserAdministrationSiteAdmin(login); return ApiConnection.Delete(endpoint); } /// /// Suspends a user (must be Site Admin user). /// /// /// See the API documentation /// for more information. /// /// The user to suspend. /// [ManualRoute("PUT", "/users/{username}/suspended")] public Task Suspend(string login) { Ensure.ArgumentNotNullOrEmptyString(login, nameof(login)); var endpoint = ApiUrls.UserAdministrationSuspension(login); return ApiConnection.Put(endpoint); } /// /// Unsuspends a user (must be Site Admin user). /// /// /// See the API documentation /// for more information. /// /// The user to unsuspend. /// [ManualRoute("DELETE", "/users/{username}/suspended")] public Task Unsuspend(string login) { Ensure.ArgumentNotNullOrEmptyString(login, nameof(login)); var endpoint = ApiUrls.UserAdministrationSuspension(login); return ApiConnection.Delete(endpoint); } /// /// List all public keys (must be Site Admin user). /// /// /// See the API documentation /// for more information. /// /// [ManualRoute("PUT", "/admin/keys")] public Task> ListAllPublicKeys() { var endpoint = ApiUrls.UserAdministrationPublicKeys(); return ApiConnection.GetAll(endpoint); } /// /// Delete a user (must be Site Admin user). /// /// /// See the API documentation /// for more information. /// /// The user to delete /// [ManualRoute("DELETE", "/admin/users/{username}")] public async Task Delete(string login) { Ensure.ArgumentNotNullOrEmptyString(login, nameof(login)); var endpoint = ApiUrls.UserAdministration(login); var response = await Connection.Delete(endpoint).ConfigureAwait(false); if (response != HttpStatusCode.NoContent) { throw new ApiException("Invalid Status Code returned. Expected a 204", response); } } /// /// Delete a public key (must be Site Admin user). /// /// /// See the API documentation /// for more information. /// /// The key to delete /// [ManualRoute("DELETE", "/admin/keys/{key_id}")] public async Task DeletePublicKey(int keyId) { Ensure.ArgumentNotNull(keyId, nameof(keyId)); var endpoint = ApiUrls.UserAdministrationPublicKeys(keyId); var response = await Connection.Delete(endpoint).ConfigureAwait(false); if (response != HttpStatusCode.NoContent) { throw new ApiException("Invalid Status Code returned. Expected a 204", response); } } } }