forked from influxdata/influxdb-client-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUsersApi.cs
268 lines (225 loc) · 9.5 KB
/
UsersApi.cs
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
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Api.Service;
using InfluxDB.Client.Core;
using InfluxDB.Client.Domain;
using Task = System.Threading.Tasks.Task;
namespace InfluxDB.Client
{
public class UsersApi
{
private readonly UsersService _service;
protected internal UsersApi(UsersService service)
{
Arguments.CheckNotNull(service, nameof(service));
_service = service;
}
/// <summary>
/// Creates a new user and sets <see cref="User.Id" /> with the new identifier.
/// </summary>
/// <param name="name">name of the user</param>
/// <returns>Created user</returns>
public async Task<User> CreateUserAsync(string name)
{
Arguments.CheckNonEmptyString(name, nameof(name));
var user = new User(name: name);
return await CreateUserAsync(user);
}
/// <summary>
/// Creates a new user and sets <see cref="User.Id" /> with the new identifier.
/// </summary>
/// <param name="user">name of the user</param>
/// <returns>Created user</returns>
public async Task<User> CreateUserAsync(User user)
{
Arguments.CheckNotNull(user, nameof(user));
return await _service.PostUsersAsync(user);
}
/// <summary>
/// Update an user.
/// </summary>
/// <param name="user">user update to apply</param>
/// <returns>user updated</returns>
public async Task<User> UpdateUserAsync(User user)
{
Arguments.CheckNotNull(user, nameof(user));
return await _service.PatchUsersIDAsync(user.Id, user);
}
/// <summary>
/// Update password to an user.
/// </summary>
/// <param name="user">user to update password</param>
/// <param name="oldPassword">old password</param>
/// <param name="newPassword">new password</param>
/// <returns>user updated</returns>
public async Task UpdateUserPasswordAsync(User user, string oldPassword, string newPassword)
{
Arguments.CheckNotNull(user, nameof(user));
Arguments.CheckNotNull(oldPassword, nameof(oldPassword));
Arguments.CheckNotNull(newPassword, nameof(newPassword));
await UpdateUserPasswordAsync(user.Id, user.Name, oldPassword, newPassword);
}
/// <summary>
/// Update password to an user.
/// </summary>
/// <param name="userId">ID of user to update password</param>
/// <param name="oldPassword">old password</param>
/// <param name="newPassword">new password</param>
/// <returns>user updated</returns>
public async Task UpdateUserPasswordAsync(string userId, string oldPassword, string newPassword)
{
Arguments.CheckNotNull(userId, nameof(userId));
Arguments.CheckNotNull(oldPassword, nameof(oldPassword));
Arguments.CheckNotNull(newPassword, nameof(newPassword));
await FindUserByIdAsync(userId).ContinueWith(t => UpdateUserPasswordAsync(t.Result, oldPassword, newPassword));
}
/// <summary>
/// Delete an user.
/// </summary>
/// <param name="userId">ID of user to delete</param>
/// <returns>async task</returns>
public async Task DeleteUserAsync(string userId)
{
Arguments.CheckNotNull(userId, nameof(userId));
await _service.DeleteUsersIDAsync(userId);
}
/// <summary>
/// Delete an user.
/// </summary>
/// <param name="user">user to delete</param>
/// <returns>async task</returns>
public async Task DeleteUserAsync(User user)
{
Arguments.CheckNotNull(user, nameof(user));
await DeleteUserAsync(user.Id);
}
/// <summary>
/// Clone an user.
/// </summary>
/// <param name="clonedName">name of cloned user</param>
/// <param name="userId">ID of user to clone</param>
/// <returns>cloned user</returns>
public async Task<User> CloneUserAsync(string clonedName, string userId)
{
Arguments.CheckNonEmptyString(clonedName, nameof(clonedName));
Arguments.CheckNonEmptyString(userId, nameof(userId));
return await FindUserByIdAsync(userId).ContinueWith(t => CloneUserAsync(clonedName, t.Result)).Unwrap();
}
/// <summary>
/// Clone an user.
/// </summary>
/// <param name="clonedName">name of cloned user</param>
/// <param name="user">user to clone</param>
/// <returns>cloned user</returns>
public async Task<User> CloneUserAsync(string clonedName, User user)
{
Arguments.CheckNonEmptyString(clonedName, nameof(clonedName));
Arguments.CheckNotNull(user, nameof(user));
var cloned = new User(name: clonedName);
return await CreateUserAsync(cloned);
}
/// <summary>
/// Returns currently authenticated user.
/// </summary>
/// <returns>currently authenticated user</returns>
public async Task<User> MeAsync()
{
return await _service.GetMeAsync();
}
/// <summary>
/// Update the password to a currently authenticated user.
/// </summary>
/// <param name="oldPassword">old password</param>
/// <param name="newPassword">new password</param>
/// <returns>currently authenticated user</returns>
public async Task MeUpdatePasswordAsync(string oldPassword, string newPassword)
{
Arguments.CheckNotNull(oldPassword, nameof(oldPassword));
Arguments.CheckNotNull(newPassword, nameof(newPassword));
await MeAsync().ContinueWith(async t =>
{
if (t.Result == null)
{
Trace.WriteLine("User is not authenticated.");
return;
}
var header = InfluxDBClient.AuthorizationHeader(t.Result.Name, oldPassword);
await _service.PutMePasswordAsync(new PasswordResetBody(newPassword), null, header);
}).Unwrap();
}
/// <summary>
/// Retrieve an user.
/// </summary>
/// <param name="userId">ID of user to get</param>
/// <returns>User Details</returns>
public async Task<User> FindUserByIdAsync(string userId)
{
Arguments.CheckNonEmptyString(userId, nameof(userId));
return await _service.GetUsersIDAsync(userId);
}
/// <summary>
/// List all users.
/// </summary>
/// <returns>List all users</returns>
public async Task<List<User>> FindUsersAsync()
{
return await _service.GetUsersAsync().ContinueWith(t => t.Result._Users);
}
/// <summary>
/// Retrieve an user's logs
/// </summary>
/// <param name="user">for retrieve logs</param>
/// <returns>logs</returns>
public async Task<List<OperationLog>> FindUserLogsAsync(User user)
{
Arguments.CheckNotNull(user, nameof(user));
return await FindUserLogsAsync(user.Id);
}
/// <summary>
/// Retrieve an user's logs
/// </summary>
/// <param name="user">for retrieve logs</param>
/// <param name="findOptions">the find options</param>
/// <returns>logs</returns>
public async Task<OperationLogs> FindUserLogsAsync(User user, FindOptions findOptions)
{
Arguments.CheckNotNull(user, nameof(user));
Arguments.CheckNotNull(findOptions, nameof(findOptions));
return await FindUserLogsAsync(user.Id, findOptions);
}
/// <summary>
/// Retrieve an user's logs
/// </summary>
/// <param name="userId">the ID of an user</param>
/// <returns>logs</returns>
public async Task<List<OperationLog>> FindUserLogsAsync(string userId)
{
Arguments.CheckNonEmptyString(userId, nameof(userId));
return await FindUserLogsAsync(userId, new FindOptions()).ContinueWith(t => t.Result.Logs);
}
/// <summary>
/// Retrieve an user's logs
/// </summary>
/// <param name="userId">the ID of an user</param>
/// <param name="findOptions">the find options</param>
/// <returns>logs</returns>
public async Task<OperationLogs> FindUserLogsAsync(string userId, FindOptions findOptions)
{
Arguments.CheckNonEmptyString(userId, nameof(userId));
Arguments.CheckNotNull(findOptions, nameof(findOptions));
return await _service.GetUsersIDLogsAsync(userId, null, findOptions.Offset, findOptions.Limit);
}
private async Task UpdateUserPasswordAsync(string userId, string userName, string oldPassword,
string newPassword)
{
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);
await _service.PostUsersIDPasswordAsync(userId, new PasswordResetBody(newPassword), null, header);
}
}
}