Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 43x 43x 43x 17x 4x 4x 3x 3x 2x 2x 1x 1x 7x 5x 4x 6x 7x 7x 7x 1x 7x 1x 7x 6x 4x 7x 5x 3x 7x 5x 4x 7x 4x 3x 7x 4x 3x 7x 4x 3x 6x 5x 2x 6x 5x 2x 7x 4x 3x 3x 3x 3x 3x 8x 11x 9x 9x 10x 7x 7x 15x 2x 1x 1x 3x 3x 2x 2x 3x 3x 3x 3x 3x 8x 9x 8x 8x 6x 4x 4x 9x 123x 71x 123x | import Client, { ClientError, IQuota, UserGroup, UserGroupDoesNotExistError } from "./client"; import FileSizeFormatter from "./fileSizeFormatter"; export enum UserProperty { email = "email", quota = "quota", displayName = "displayname", phone = "phone", address = "address", website = "website", twitter = "twitter", password = "password", language = "language", locale = "locale", } export interface IUserOptionsQuota { "free"?: number, "used": number, "total"?: number, "relative": number, "quota": number } export interface IUserQuotaUserFriendly { "free"?: string, "used": string, "total"?: string, "quota": string, "relative": string } export interface IUserOptions { "enabled": boolean; "lastLogin"?: Date, "subadminGroups": string[], "memberGroups": string[], "quota": IUserOptionsQuota, "email": string, "displayName": string, "phone": string, "address": string, "website": string, "twitter": string, "language": string "locale": string, } /** * The user class represents a user in nextcloud. * async getGroups * async isDisabled * async getLastLogin * async getEmail * getId * async getDisplayName */ export default class User { // https://docs.nextcloud.com/server/latest/admin_manual/configuration_user/user_provisioning_api.html private memento?: IUserOptions; private client: Client; readonly id: string; /** * the conscructor of the user * should only me invoked by the Client * @constructor * @param {Client} client * @param {string} id the user id * @param {IUserOptions} options optional options */ constructor(client: Client, id: string, options?: IUserOptions) { this.id = id; this.client = client; this.memento = options; } // ********************************** // enable disable // ********************************** /** * returns true if the user is enabled * @async * @returns {Promise<boolean>} true if the user is enabled * @throws {UserNotFoundError} */ async isEnabled(): Promise<boolean> { return (await this.getUserData()).enabled; } /** * disables the user * @async * @returns {Promise<void>} * @throws {UserNotFoundError} */ async disable(): Promise<void> { delete this.memento; return await this.client.disableUser(this.id); } /** * enables the user * @async * @returns {Promise<void>} * @throws {UserNotFoundError} */ async enable(): Promise<void> { delete this.memento; return await this.client.enableUser(this.id); } /** * get the last login date or null if the user has not been logged in yet * @async * @returns {Promise<Date | null>} last login date or null if the user has not been logged in yet * @throws {UserNotFoundError} */ async getLastLogin(): Promise<Date | null> { const data: IUserOptions = await this.getUserData(); if (data.lastLogin) { return data.lastLogin; } return null; } /** * returns the display name * @async * @returns {Promise<string>} display name * @throws {UserNotFoundError} */ async getDisplayName(): Promise<string> { return (await this.getUserData()).displayName; } /** * set the display name * @async * @param {string} value the display name * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserUpdateError} */ async setDisplayName(value: string): Promise<void> { await this.client.updateUserProperty(this.id, UserProperty.displayName, value); delete this.memento; } /** * returns information on quota, usage and available space * @async * @returns {Promise<IUserOptionsQuota>} * @throws {UserNotFoundError} */ async getQuota(): Promise<IUserOptionsQuota> { return (await this.getUserData()).quota; } /** * returns information on quota, usage and available space in a user friendly format * @async * @returns {Promise<IUserQuotaUserFriendly>} * @throws {UserNotFoundError} */ async getQuotaUserFriendly(): Promise<IUserQuotaUserFriendly> { const q: IUserOptionsQuota = (await this.getUserData()).quota; const qUF: IUserQuotaUserFriendly = { used: new FileSizeFormatter(q.used).getUserFriendlyFileSize(), quota: new FileSizeFormatter(q.quota).getUserFriendlyFileSize(), relative: Math.round(q.relative) + " %" } if (q.total) { qUF.total = new FileSizeFormatter(q.total).getUserFriendlyFileSize(); } if (q.free) { qUF.free = new FileSizeFormatter(q.free).getUserFriendlyFileSize(); } return qUF; } /** * sets the quota limit of the user * @async * @param {string} value the quota string like "1 GB", "100 MB" * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserUpdateError} */ async setQuota(value: string): Promise<void> { await this.client.updateUserProperty(this.id, UserProperty.quota, value); delete this.memento; } /** * returns the email address * @async * @returns {Promise<string>} email adress * @throws {UserNotFoundError} */ async getEmail(): Promise<string> { return (await this.getUserData()).email; } /** * set the email address * @async * @param {string} value the email address * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserUpdateError} */ async setEmail(value: string): Promise<void> { await this.client.updateUserProperty(this.id, UserProperty.email, value); delete this.memento; } // ********************************** // phone // ********************************** /** * returns the phone number * @async * @returns {Promise<string>} phone number * @throws {UserNotFoundError} */ async getPhone(): Promise<string> { return (await this.getUserData()).phone; } /** * set phone number * @async * @param {string} value the phone number * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserUpdateError} */ async setPhone(value: string): Promise<void> { await this.client.updateUserProperty(this.id, UserProperty.phone, value); delete this.memento; } // ********************************** // address // ********************************** /** * returns the phone number * @async * @returns {Promise<string>} address * @throws {UserNotFoundError} */ async getAddress(): Promise<string> { return (await this.getUserData()).address; } /** * set the address * @async * @param {string} value the address * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserUpdateError} */ async setAddress(value: string): Promise<void> { await this.client.updateUserProperty(this.id, UserProperty.address, value); delete this.memento; } // ********************************** // website // ********************************** /** * returns the website * @async * @returns {Promise<string>} website * @throws {UserNotFoundError} */ async getWebsite(): Promise<string> { return (await this.getUserData()).website; } /** * set the website * @async * @param {string} value the website * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserUpdateError} */ async setWebsite(value: string): Promise<void> { await this.client.updateUserProperty(this.id, UserProperty.website, value); delete this.memento; } // ********************************** // twitter // ********************************** /** * returns the twitter handle * @async * @returns {Promise<string>} twitter handle * @throws {UserNotFoundError} */ async getTwitter(): Promise<string> { return (await this.getUserData()).twitter; } /** * set the twitter handle * @async * @param {string} value the twitter handle * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserUpdateError} */ async setTwitter(value: string): Promise<void> { await this.client.updateUserProperty(this.id, UserProperty.twitter, value); delete this.memento; } // ********************************** // language // ********************************** /** * returns the langauge code * @async * @returns {Promise<string>} language code * @throws {UserNotFoundError} */ async getLanguage(): Promise<string> { return (await this.getUserData()).language; } /** * set the language code like EN, DE, FR... * @async * @param {string} value the language code * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserUpdateError} */ async setLanguage(value: string): Promise<void> { await this.client.updateUserProperty(this.id, UserProperty.language, value); delete this.memento; } // ********************************** // locale // ********************************** /** * returns the locale * @async * @returns {Promise<string>} locale * @throws {UserNotFoundError} */ async getLocale(): Promise<string> { return (await this.getUserData()).locale; } /** * set the locale like EN, DE, FR... * @async * @param {string} value the locale * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserUpdateError} */ async setLocale(value: string): Promise<void> { await this.client.updateUserProperty(this.id, UserProperty.locale, value); delete this.memento; } /** * set the password * @async * @param {string} value the password * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserUpdateError} */ async setPassword(value: string): Promise<void> { await this.client.updateUserProperty(this.id, UserProperty.password, value); } // ********************************** // Resend the welcome email // ********************************** /** * resends the welcome email * @async * @returns {Promise<void>} * @throws {UserResendWelcomeEmailError} */ async resendWelcomeEmail(): Promise<void> { await this.client.resendWelcomeEmail(this.id); } // ********************************** // user group member // ********************************** /** * returns a list of user groups where the user is member * @async * @returns {Promise<UserGroup[]} a list of user groups where the user is member * @throws {UserNotFoundError} */ async getMemberUserGroups(): Promise<UserGroup[]> { const groupIds: string[] = (await this.getUserData()).memberGroups; const result: UserGroup[] = []; for (const groupId of groupIds) { result.push(new UserGroup(this.client, groupId)); } return result; } /** * returns a list of user group ids where the user is member * @async * @returns {Promise<string[]} a list of user group ids where the user is member * @throws {UserNotFoundError} */ async getMemberUserGroupIds(): Promise<string[]> { return (await this.getUserData()).memberGroups; } /** * adds the user to a user group as member * @async * @param {UserGroup} userGroup the user group * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserGroupDoesNotExistError} * @throws {InsufficientPrivilegesError} * @throws {OperationFailedError} */ async addToMemberUserGroup(userGroup: UserGroup): Promise<void> { await this.client.addUserToMemberUserGroup(this.id, userGroup.id); delete this.memento; return; } /** * remove the user from a user group as member * @async * @param {UserGroup} userGroup the user group * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserGroupDoesNotExistError} * @throws {InsufficientPrivilegesError} * @throws {OperationFailedError} */ async removeFromMemberUserGroup(userGroup: UserGroup): Promise<void> { await this.client.removeUserFromMemberUserGroup(this.id, userGroup.id); delete this.memento; return } // ********************************** // user superadmin // ********************************** /** * true if the user is a superadmin * @async * @returns {Promise<boolean>} true if the user is a superadmin * @throws {UserNotFoundError} */ async isSuperAdmin(): Promise<boolean> { return (await this.getUserData()).memberGroups.indexOf("admin") === -1 ? false : true; } /** * promote user to super admin * @async * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserGroupDoesNotExistError} * @throws {InsufficientPrivilegesError} * @throws {OperationFailedError} */ async promoteToSuperAdmin(): Promise<void> { await this.addToMemberUserGroup(new UserGroup(this.client, "admin")); delete this.memento; return; } /** * demote user from being a super admin * @async * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserGroupDoesNotExistError} * @throws {InsufficientPrivilegesError} * @throws {OperationFailedError} */ async demoteFromSuperAdmin(): Promise<void> { /* istanbul ignore else */ if (await this.isSuperAdmin()) { await this.removeFromMemberUserGroup(new UserGroup(this.client, "admin")); delete this.memento; } return; } // ********************************** // user group subadmin // ********************************** /** * returns a list of user groups where the user is subadmin * @async * @returns {Promise<UserGroup[]} a list of user groups where the user is subadmin * @throws {UserNotFoundError} */ async getSubadminUserGroups(): Promise<UserGroup[]> { const groupIds: string[] = (await this.getUserData()).subadminGroups; const result: UserGroup[] = []; for (const groupId of groupIds) { result.push(new UserGroup(this.client, groupId)); } return result; } /** * returns a list of user group ids where the user is subadmin * @async * @returns {Promise<string[]} a list of user group ids where the user is subadmin * @throws {UserNotFoundError} */ async getSubadminUserGroupIds(): Promise<string[]> { return (await this.getUserData()).subadminGroups; } /** * promote the user to be a subadmin of the user group * @async * @param {UserGroup} userGroup the user group * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserGroupDoesNotExistError} * @throws {InsufficientPrivilegesError} * @throws {OperationFailedError} */ async promoteToUserGroupSubadmin(userGroup: UserGroup): Promise<void> { await this.client.promoteUserToUserGroupSubadmin(this.id, userGroup.id); delete this.memento; return } /** * demote the user from beeing a subadmin of the user group * @async * @param {UserGroup} userGroup the user group * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserGroupDoesNotExistError} * @throws {InsufficientPrivilegesError} * @throws {OperationFailedError} */ async demoteFromSubadminUserGroup(userGroup: UserGroup): Promise<void> { await this.client.demoteUserFromSubadminUserGroup(this.id, userGroup.id); delete this.memento; return } /** * deletes a user * @async * @returns {Promise<void>} * @throws {UserNotFoundError} */ async delete(): Promise<void> { return await this.client.deleteUser(this.id); } /** * returns the user data * @async * @returns {Promise<IUserOptions>} * @throws {UserNotFoundError} */ private async getUserData(): Promise<IUserOptions> { if (!this.memento) { this.memento = await this.client.getUserData(this.id); } return this.memento; } } |