using System; using System.Collections.Generic; using System.Diagnostics; using System.Threading; using System.Threading.Tasks; using InfluxDB.Client.Api.Domain; using InfluxDB.Client.Api.Service; using InfluxDB.Client.Core; namespace InfluxDB.Client { public interface IUsersApi { /// /// Creates a new user and sets with the new identifier. /// /// name of the user /// Cancellation token /// Created user Task CreateUserAsync(string name, CancellationToken cancellationToken = default); /// /// Creates a new user and sets with the new identifier. /// /// name of the user /// Cancellation token /// Created user Task CreateUserAsync(User user, CancellationToken cancellationToken = default); /// /// Update an user. /// /// user update to apply /// Cancellation token /// user updated Task UpdateUserAsync(User user, CancellationToken cancellationToken = default); /// /// Update password to an user. /// /// user to update password /// old password /// new password /// Cancellation token /// user updated Task UpdateUserPasswordAsync(User user, string oldPassword, string newPassword, CancellationToken cancellationToken = default); /// /// Update password to an user. /// /// ID of user to update password /// old password /// new password /// Cancellation token /// user updated Task UpdateUserPasswordAsync(string userId, string oldPassword, string newPassword, CancellationToken cancellationToken = default); /// /// Delete an user. /// /// ID of user to delete /// Cancellation token /// async task Task DeleteUserAsync(string userId, CancellationToken cancellationToken = default); /// /// Delete an user. /// /// user to delete /// Cancellation token /// async task Task DeleteUserAsync(User user, CancellationToken cancellationToken = default); /// /// Clone an user. /// /// name of cloned user /// ID of user to clone /// Cancellation token /// cloned user Task CloneUserAsync(string clonedName, string userId, CancellationToken cancellationToken = default); /// /// Clone an user. /// /// name of cloned user /// user to clone /// Cancellation token /// cloned user Task CloneUserAsync(string clonedName, User user, CancellationToken cancellationToken = default); /// /// Returns currently authenticated user. /// /// Cancellation token /// currently authenticated user Task MeAsync(CancellationToken cancellationToken = default); /// /// Update the password to a currently authenticated user. /// /// old password /// new password /// Cancellation token /// currently authenticated user Task MeUpdatePasswordAsync(string oldPassword, string newPassword, CancellationToken cancellationToken = default); /// /// Retrieve an user. /// /// ID of user to get /// Cancellation token /// User Details Task FindUserByIdAsync(string userId, CancellationToken cancellationToken = default); /// /// List all users. /// /// (optional) /// (optional, default to 20) /// The last resource ID from which to seek from (but not including). This is to be used instead of `offset`. (optional) /// (optional) /// (optional) /// Cancellation token /// List all users Task> FindUsersAsync(int? offset = null, int? limit = null, string after = null, string name = null, string id = null, CancellationToken cancellationToken = default); } public class UsersApi : IUsersApi { private readonly UsersService _service; protected internal UsersApi(UsersService service) { Arguments.CheckNotNull(service, nameof(service)); _service = service; } /// /// Creates a new user and sets with the new identifier. /// /// name of the user /// Cancellation token /// Created user public Task CreateUserAsync(string name, CancellationToken cancellationToken = default) { Arguments.CheckNonEmptyString(name, nameof(name)); var user = new User(name: name); return CreateUserAsync(user, cancellationToken); } /// /// Creates a new user and sets with the new identifier. /// /// name of the user /// Cancellation token /// Created user public Task CreateUserAsync(User user, CancellationToken cancellationToken = default) { Arguments.CheckNotNull(user, nameof(user)); return _service.PostUsersAsync(ToPostUser(user), cancellationToken: cancellationToken); } /// /// Update an user. /// /// user update to apply /// Cancellation token /// user updated public Task UpdateUserAsync(User user, CancellationToken cancellationToken = default) { Arguments.CheckNotNull(user, nameof(user)); return _service.PatchUsersIDAsync(user.Id, ToPostUser(user), cancellationToken: cancellationToken); } /// /// Update password to an user. /// /// user to update password /// old password /// new password /// Cancellation token /// user updated public Task UpdateUserPasswordAsync(User user, string oldPassword, string newPassword, CancellationToken cancellationToken = default) { Arguments.CheckNotNull(user, nameof(user)); Arguments.CheckNotNull(oldPassword, nameof(oldPassword)); Arguments.CheckNotNull(newPassword, nameof(newPassword)); return UpdateUserPasswordAsync(user.Id, user.Name, oldPassword, newPassword, cancellationToken); } /// /// Update password to an user. /// /// ID of user to update password /// old password /// new password /// Cancellation token /// user updated public Task UpdateUserPasswordAsync(string userId, string oldPassword, string newPassword, CancellationToken cancellationToken = default) { Arguments.CheckNotNull(userId, nameof(userId)); Arguments.CheckNotNull(oldPassword, nameof(oldPassword)); Arguments.CheckNotNull(newPassword, nameof(newPassword)); return FindUserByIdAsync(userId, cancellationToken) .ContinueWith(t => UpdateUserPasswordAsync(t.Result, oldPassword, newPassword), cancellationToken); } /// /// Delete an user. /// /// ID of user to delete /// Cancellation token /// async task public Task DeleteUserAsync(string userId, CancellationToken cancellationToken = default) { Arguments.CheckNotNull(userId, nameof(userId)); return _service.DeleteUsersIDAsync(userId, cancellationToken: cancellationToken); } /// /// Delete an user. /// /// user to delete /// Cancellation token /// async task public Task DeleteUserAsync(User user, CancellationToken cancellationToken = default) { Arguments.CheckNotNull(user, nameof(user)); return DeleteUserAsync(user.Id, cancellationToken); } /// /// Clone an user. /// /// name of cloned user /// ID of user to clone /// Cancellation token /// cloned user public async Task CloneUserAsync(string clonedName, string userId, CancellationToken cancellationToken = default) { Arguments.CheckNonEmptyString(clonedName, nameof(clonedName)); Arguments.CheckNonEmptyString(userId, nameof(userId)); var user = await FindUserByIdAsync(userId, cancellationToken).ConfigureAwait(false); return await CloneUserAsync(clonedName, user, cancellationToken).ConfigureAwait(false); } /// /// Clone an user. /// /// name of cloned user /// user to clone /// Cancellation token /// cloned user public Task CloneUserAsync(string clonedName, User user, CancellationToken cancellationToken = default) { Arguments.CheckNonEmptyString(clonedName, nameof(clonedName)); Arguments.CheckNotNull(user, nameof(user)); var cloned = new User(name: clonedName); return CreateUserAsync(cloned, cancellationToken); } /// /// Returns currently authenticated user. /// /// Cancellation token /// currently authenticated user public Task MeAsync(CancellationToken cancellationToken = default) { return _service.GetMeAsync(cancellationToken: cancellationToken); } /// /// Update the password to a currently authenticated user. /// /// old password /// new password /// Cancellation token /// currently authenticated user public async Task MeUpdatePasswordAsync(string oldPassword, string newPassword, CancellationToken cancellationToken = default) { Arguments.CheckNotNull(oldPassword, nameof(oldPassword)); Arguments.CheckNotNull(newPassword, nameof(newPassword)); var me = await MeAsync(cancellationToken).ConfigureAwait(false); if (me == null) { Trace.WriteLine("User is not authenticated."); return; } var header = InfluxDBClient.AuthorizationHeader(me.Name, oldPassword); await _service.PutMePasswordAsync(new PasswordResetBody(newPassword), null, header, cancellationToken) .ConfigureAwait(false); } /// /// Retrieve an user. /// /// ID of user to get /// Cancellation token /// User Details public Task FindUserByIdAsync(string userId, CancellationToken cancellationToken = default) { Arguments.CheckNonEmptyString(userId, nameof(userId)); return _service.GetUsersIDAsync(userId, cancellationToken: cancellationToken); } /// /// List all users. /// /// (optional) /// (optional, default to 20) /// The last resource ID from which to seek from (but not including). This is to be used instead of `offset`. (optional) /// (optional) /// (optional) /// Cancellation token /// List all users public async Task> FindUsersAsync(int? offset = null, int? limit = null, string after = null, string name = null, string id = null, CancellationToken cancellationToken = default) { var response = await _service.GetUsersAsync(offset: offset, limit: limit, after: after, name: name, id: id, cancellationToken: cancellationToken) .ConfigureAwait(false); return response._Users; } private Task UpdateUserPasswordAsync(string userId, string userName, string oldPassword, string newPassword, CancellationToken cancellationToken = default) { Arguments.CheckNotNull(userId, nameof(userId)); Arguments.CheckNotNull(userName, nameof(userName)); Arguments.CheckNotNull(oldPassword, nameof(oldPassword)); Arguments.CheckNotNull(newPassword, nameof(newPassword)); var header = InfluxDBClient.AuthorizationHeader(userName, oldPassword); return _service.PostUsersIDPasswordAsync(userId, new PasswordResetBody(newPassword), null, header, cancellationToken); } private PostUser ToPostUser(User user) { Enum.TryParse(user.Status.ToString(), true, out PostUser.StatusEnum status); var postUser = new PostUser(user.OauthID, user.Name, status); return postUser; } } }