@inweb/client 25.4.11 → 25.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/Api/User.ts CHANGED
@@ -22,6 +22,7 @@
22
22
  ///////////////////////////////////////////////////////////////////////////////
23
23
 
24
24
  import { IHttpClient } from "./IHttpClient";
25
+ import { FetchError } from "./FetchError";
25
26
  import { userFullName, userInitials } from "./Utils";
26
27
 
27
28
  /**
@@ -59,8 +60,8 @@ export class User {
59
60
  }
60
61
 
61
62
  /**
62
- * User account registration time (UTC) in the format specified in <a
63
- * href="https://www.wikipedia.org/wiki/ISO_8601" target="_blank">ISO 8601</a>.
63
+ * Account registration time (UTC) in the format specified in
64
+ * {@link https://www.wikipedia.org/wiki/ISO_8601 | ISO 8601}.
64
65
  *
65
66
  * @readonly
66
67
  */
@@ -167,8 +168,8 @@ export class User {
167
168
  }
168
169
 
169
170
  /**
170
- * User last update time (UTC) in the format specified in <a
171
- * href="https://www.wikipedia.org/wiki/ISO_8601" target="_blank">ISO 8601</a>.
171
+ * User last update time (UTC) in the format specified in
172
+ * {@link https://www.wikipedia.org/wiki/ISO_8601 | ISO 8601}.
172
173
  */
173
174
  get lastModified(): string {
174
175
  return this.data.lastModified;
@@ -186,8 +187,8 @@ export class User {
186
187
  }
187
188
 
188
189
  /**
189
- * User last sign in time (UTC) in the format specified in <a
190
- * href="https://www.wikipedia.org/wiki/ISO_8601" target="_blank">ISO 8601</a>.
190
+ * User last sign in time (UTC) in the format specified in
191
+ * {@link https://www.wikipedia.org/wiki/ISO_8601 | ISO 8601}.
191
192
  */
192
193
  get lastSignIn(): string {
193
194
  return this.data.lastSignIn;
@@ -202,6 +203,13 @@ export class User {
202
203
  return this.data.projectsLimit;
203
204
  }
204
205
 
206
+ /**
207
+ * The identity provider used to create the account. Can be `ldap`, `oauth`, `saml` or empty.
208
+ */
209
+ get providerType(): string {
210
+ return this.data.providerType;
211
+ }
212
+
205
213
  /**
206
214
  * The user's access token (API key). Use {@link Client.signInWithToken()} to log in using token.
207
215
  *
@@ -223,47 +231,55 @@ export class User {
223
231
  }
224
232
 
225
233
  /**
226
- * Refresh user data. Only admins can checkout other users, if the current logged in user
227
- * does not have administrator rights, hi can only checkout himself, otherwise an exception
228
- * will be thrown.
234
+ * Refresh user data.
235
+ *
236
+ * Only admins can checkout other users, if the current logged in user does not have
237
+ * administrator rights, hi can only checkout himself, otherwise an exception will be thrown.
229
238
  */
230
239
  async checkout(): Promise<this> {
231
- if (this.id === this.httpClient.signInUserId) {
240
+ if (this.httpClient.signInUserIsAdmin) {
241
+ const response = await this.httpClient.get(`/users/${this.id}`);
242
+ const data = await response.json();
243
+ this.data = { id: data.id, ...data.userBrief };
244
+ } else if (this.id === this.httpClient.signInUserId) {
232
245
  const response = await this.httpClient.get("/user");
233
246
  const data = await response.json();
234
247
  this.data = { id: this.id, ...data };
235
248
  } else {
236
- const response = await this.httpClient.get(`/users/${this.id}`);
237
- const data = await response.json();
238
- this.data = { id: data.id, ...data.userBrief };
249
+ return Promise.reject(new FetchError(403));
239
250
  }
240
251
  return this;
241
252
  }
242
253
 
243
254
  /**
244
- * Update user data on the server. Only admins can update other users, if the current logged
245
- * in user does not have administrator rights, hi can only update himself, otherwise an
246
- * exception will be thrown.
255
+ * Update user data on the server.
256
+ *
257
+ * Only admins can update other users, if the current logged in user does not have
258
+ * administrator rights, hi can only update himself, otherwise an exception will be thrown.
247
259
  *
248
260
  * @async
249
261
  * @param data - Raw user data.
250
262
  */
251
263
  async update(data: any): Promise<this> {
252
- if (this.id === this.httpClient.signInUserId) {
264
+ if (this.httpClient.signInUserIsAdmin) {
265
+ const response = await this.httpClient.put(`/users/${this.id}`, { userBrief: data });
266
+ const newData = await response.json();
267
+ this.data = { id: newData.id, ...newData.userBrief };
268
+ } else if (this.id === this.httpClient.signInUserId) {
253
269
  const response = await this.httpClient.put("/user", data);
254
270
  const newData = await response.json();
255
271
  this.data = { id: this.id, ...newData };
256
272
  } else {
257
- const response = await this.httpClient.put(`/users/${this.id}`, { userBrief: data });
258
- const newData = await response.json();
259
- this.data = { id: newData.id, ...newData.userBrief };
273
+ return Promise.reject(new FetchError(403));
260
274
  }
261
275
  return this;
262
276
  }
263
277
 
264
278
  /**
265
- * Delete a user from the server. Only admins can delete users, if the current logged in user
266
- * does not have administrator rights, an exception will be thrown.
279
+ * Delete a user from the server.
280
+ *
281
+ * Only admins can delete users, if the current logged in user does not have administrator
282
+ * rights, an exception will be thrown.
267
283
  *
268
284
  * Admins can delete themselves or other admins. An admin can only delete himself if he is
269
285
  * not the last administrator.
@@ -274,16 +290,21 @@ export class User {
274
290
  * @returns Returns the raw data of a deleted user.
275
291
  */
276
292
  delete(): Promise<any> {
277
- return this.httpClient
278
- .delete(`/users/${this.id}`)
279
- .then((response) => response.json())
280
- .then((data) => {
281
- if (this.id === this.httpClient.signInUserId) {
282
- this.httpClient.headers = {};
283
- this.httpClient.signInUserId = "";
284
- }
285
- return data;
286
- });
293
+ if (this.httpClient.signInUserIsAdmin) {
294
+ return this.httpClient
295
+ .delete(`/users/${this.id}`)
296
+ .then((response) => response.json())
297
+ .then((data) => {
298
+ if (this.id === this.httpClient.signInUserId) {
299
+ this.httpClient.headers = {};
300
+ this.httpClient.signInUserId = "";
301
+ this.httpClient.signInUserIsAdmin = false;
302
+ }
303
+ return data;
304
+ });
305
+ } else {
306
+ return Promise.reject(new FetchError(403));
307
+ }
287
308
  }
288
309
 
289
310
  /**
@@ -297,41 +318,57 @@ export class User {
297
318
  }
298
319
 
299
320
  /**
300
- * Set or remove the user avatar. Only admins can set the avatar of other users, if the
301
- * current logged in user does not have administrator rights, he can only set his own avatar,
302
- * otherwise an exception will be thrown.
321
+ * Set or remove the user avatar.
322
+ *
323
+ * Only admins can set the avatar of other users, if the current logged in user does not have
324
+ * administrator rights, he can only set his own avatar, otherwise an exception will be thrown.
303
325
  *
304
326
  * @async
305
- * @param image - Avatar image. Can be a <a
306
- * href="https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/Data_URIs"
307
- * target="_blank">Data URL</a> string, <a
308
- * href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"
309
- * target="_blank">ArrayBuffer</a>, <a
310
- * href="https://developer.mozilla.org/docs/Web/API/Blob/Blob" target="_blank">Blob</a> or
311
- * Web API <a href="https://developer.mozilla.org/docs/Web/API/File"
312
- * target="_blank">File</a> object. Setting the `image` to `null` will remove the avatar.
327
+ * @param image - Avatar image. Can be a
328
+ * {@link https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/Data_URIs | Data URL}
329
+ * string,
330
+ * {@link https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer | ArrayBuffer},
331
+ * {@link https://developer.mozilla.org/docs/Web/API/Blob/Blob | Blob} or Web API
332
+ * {@link https://developer.mozilla.org/docs/Web/API/File | File} object. Setting the
333
+ * `image` to `null` will remove the avatar.
313
334
  */
314
- async setAvatar(image: BodyInit | null): Promise<this> {
315
- if (image) {
316
- if (this.id === this.httpClient.signInUserId) {
317
- const response = await this.httpClient.post("/user/avatar", image);
318
- const data = await response.json();
319
- this.data = { id: this.id, ...data };
320
- } else {
321
- const response = await this.httpClient.post(`/users/${this.id}/avatar`, image);
322
- const data = await response.json();
323
- this.data = { id: data.id, ...data.userBrief };
324
- }
335
+ async setAvatar(image?: BodyInit | null): Promise<this> {
336
+ if (!image) {
337
+ await this.deleteAvatar();
338
+ } else if (this.httpClient.signInUserIsAdmin) {
339
+ const response = await this.httpClient.post(`/users/${this.id}/avatar`, image);
340
+ const data = await response.json();
341
+ this.data = { id: data.id, ...data.userBrief };
342
+ } else if (this.id === this.httpClient.signInUserId) {
343
+ const response = await this.httpClient.post("/user/avatar", image);
344
+ const data = await response.json();
345
+ this.data = { id: this.id, ...data };
346
+ } else {
347
+ return Promise.reject(new FetchError(403));
348
+ }
349
+ return this;
350
+ }
351
+
352
+ /**
353
+ * Remove the user avatar.
354
+ *
355
+ * Only admins can remove the avatar of other users, if the current logged in user does not
356
+ * have administrator rights, they can only remove their own avatar, otherwise an exception
357
+ * will be thrown.
358
+ *
359
+ * @async
360
+ */
361
+ async deleteAvatar(): Promise<this> {
362
+ if (this.httpClient.signInUserIsAdmin) {
363
+ const response = await this.httpClient.delete(`/users/${this.id}/avatar`);
364
+ const data = await response.json();
365
+ this.data = { id: data.id, ...data.userBrief };
366
+ } else if (this.id === this.httpClient.signInUserId) {
367
+ const response = await this.httpClient.delete("/user/avatar");
368
+ const data = await response.json();
369
+ this.data = { id: this.id, ...data };
325
370
  } else {
326
- if (this.id === this.httpClient.signInUserId) {
327
- const response = await this.httpClient.delete("/user/avatar");
328
- const data = await response.json();
329
- this.data = { id: this.id, ...data };
330
- } else {
331
- const response = await this.httpClient.delete(`/users/${this.id}/avatar`);
332
- const data = await response.json();
333
- this.data = { id: data.id, ...data.userBrief };
334
- }
371
+ return Promise.reject(new FetchError(403));
335
372
  }
336
373
  return this;
337
374
  }