UNPKG

nextcloud-node-client

Version:

Nextcloud client API for node.js TypeScript applications

604 lines (603 loc) 19.4 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UserProperty = void 0; const client_1 = require("./client"); const fileSizeFormatter_1 = __importDefault(require("./fileSizeFormatter")); var UserProperty; (function (UserProperty) { UserProperty["email"] = "email"; UserProperty["quota"] = "quota"; UserProperty["displayName"] = "displayname"; UserProperty["phone"] = "phone"; UserProperty["address"] = "address"; UserProperty["website"] = "website"; UserProperty["twitter"] = "twitter"; UserProperty["password"] = "password"; UserProperty["language"] = "language"; UserProperty["locale"] = "locale"; })(UserProperty = exports.UserProperty || (exports.UserProperty = {})); /** * The user class represents a user in nextcloud. * async getGroups * async isDisabled * async getLastLogin * async getEmail * getId * async getDisplayName */ class User { /** * 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, id, options) { 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} */ isEnabled() { return __awaiter(this, void 0, void 0, function* () { return (yield this.getUserData()).enabled; }); } /** * disables the user * @async * @returns {Promise<void>} * @throws {UserNotFoundError} */ disable() { return __awaiter(this, void 0, void 0, function* () { delete this.memento; return yield this.client.disableUser(this.id); }); } /** * enables the user * @async * @returns {Promise<void>} * @throws {UserNotFoundError} */ enable() { return __awaiter(this, void 0, void 0, function* () { delete this.memento; return yield 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} */ getLastLogin() { return __awaiter(this, void 0, void 0, function* () { const data = yield this.getUserData(); if (data.lastLogin) { return data.lastLogin; } return null; }); } /** * returns the display name * @async * @returns {Promise<string>} display name * @throws {UserNotFoundError} */ getDisplayName() { return __awaiter(this, void 0, void 0, function* () { return (yield this.getUserData()).displayName; }); } /** * set the display name * @async * @param {string} value the display name * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserUpdateError} */ setDisplayName(value) { return __awaiter(this, void 0, void 0, function* () { yield 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} */ getQuota() { return __awaiter(this, void 0, void 0, function* () { return (yield this.getUserData()).quota; }); } /** * returns information on quota, usage and available space in a user friendly format * @async * @returns {Promise<IUserQuotaUserFriendly>} * @throws {UserNotFoundError} */ getQuotaUserFriendly() { return __awaiter(this, void 0, void 0, function* () { const q = (yield this.getUserData()).quota; const qUF = { used: new fileSizeFormatter_1.default(q.used).getUserFriendlyFileSize(), quota: new fileSizeFormatter_1.default(q.quota).getUserFriendlyFileSize(), relative: Math.round(q.relative) + " %" }; if (q.total) { qUF.total = new fileSizeFormatter_1.default(q.total).getUserFriendlyFileSize(); } if (q.free) { qUF.free = new fileSizeFormatter_1.default(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} */ setQuota(value) { return __awaiter(this, void 0, void 0, function* () { yield this.client.updateUserProperty(this.id, UserProperty.quota, value); delete this.memento; }); } /** * returns the email address * @async * @returns {Promise<string>} email adress * @throws {UserNotFoundError} */ getEmail() { return __awaiter(this, void 0, void 0, function* () { return (yield this.getUserData()).email; }); } /** * set the email address * @async * @param {string} value the email address * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserUpdateError} */ setEmail(value) { return __awaiter(this, void 0, void 0, function* () { yield this.client.updateUserProperty(this.id, UserProperty.email, value); delete this.memento; }); } // ********************************** // phone // ********************************** /** * returns the phone number * @async * @returns {Promise<string>} phone number * @throws {UserNotFoundError} */ getPhone() { return __awaiter(this, void 0, void 0, function* () { return (yield this.getUserData()).phone; }); } /** * set phone number * @async * @param {string} value the phone number * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserUpdateError} */ setPhone(value) { return __awaiter(this, void 0, void 0, function* () { yield this.client.updateUserProperty(this.id, UserProperty.phone, value); delete this.memento; }); } // ********************************** // address // ********************************** /** * returns the phone number * @async * @returns {Promise<string>} address * @throws {UserNotFoundError} */ getAddress() { return __awaiter(this, void 0, void 0, function* () { return (yield this.getUserData()).address; }); } /** * set the address * @async * @param {string} value the address * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserUpdateError} */ setAddress(value) { return __awaiter(this, void 0, void 0, function* () { yield this.client.updateUserProperty(this.id, UserProperty.address, value); delete this.memento; }); } // ********************************** // website // ********************************** /** * returns the website * @async * @returns {Promise<string>} website * @throws {UserNotFoundError} */ getWebsite() { return __awaiter(this, void 0, void 0, function* () { return (yield this.getUserData()).website; }); } /** * set the website * @async * @param {string} value the website * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserUpdateError} */ setWebsite(value) { return __awaiter(this, void 0, void 0, function* () { yield this.client.updateUserProperty(this.id, UserProperty.website, value); delete this.memento; }); } // ********************************** // twitter // ********************************** /** * returns the twitter handle * @async * @returns {Promise<string>} twitter handle * @throws {UserNotFoundError} */ getTwitter() { return __awaiter(this, void 0, void 0, function* () { return (yield this.getUserData()).twitter; }); } /** * set the twitter handle * @async * @param {string} value the twitter handle * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserUpdateError} */ setTwitter(value) { return __awaiter(this, void 0, void 0, function* () { yield this.client.updateUserProperty(this.id, UserProperty.twitter, value); delete this.memento; }); } // ********************************** // language // ********************************** /** * returns the langauge code * @async * @returns {Promise<string>} language code * @throws {UserNotFoundError} */ getLanguage() { return __awaiter(this, void 0, void 0, function* () { return (yield 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} */ setLanguage(value) { return __awaiter(this, void 0, void 0, function* () { yield this.client.updateUserProperty(this.id, UserProperty.language, value); delete this.memento; }); } // ********************************** // locale // ********************************** /** * returns the locale * @async * @returns {Promise<string>} locale * @throws {UserNotFoundError} */ getLocale() { return __awaiter(this, void 0, void 0, function* () { return (yield this.getUserData()).locale; }); } /** * set the locale like EN, DE, FR... * @async * @param {string} value the locale * @returns {Promise<void>} * @throws {UserNotFoundError} * @throws {UserUpdateError} */ setLocale(value) { return __awaiter(this, void 0, void 0, function* () { yield 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} */ setPassword(value) { return __awaiter(this, void 0, void 0, function* () { yield this.client.updateUserProperty(this.id, UserProperty.password, value); }); } // ********************************** // Resend the welcome email // ********************************** /** * resends the welcome email * @async * @returns {Promise<void>} * @throws {UserResendWelcomeEmailError} */ resendWelcomeEmail() { return __awaiter(this, void 0, void 0, function* () { yield 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} */ getMemberUserGroups() { return __awaiter(this, void 0, void 0, function* () { const groupIds = (yield this.getUserData()).memberGroups; const result = []; for (const groupId of groupIds) { result.push(new client_1.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} */ getMemberUserGroupIds() { return __awaiter(this, void 0, void 0, function* () { return (yield 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} */ addToMemberUserGroup(userGroup) { return __awaiter(this, void 0, void 0, function* () { yield 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} */ removeFromMemberUserGroup(userGroup) { return __awaiter(this, void 0, void 0, function* () { yield 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} */ isSuperAdmin() { return __awaiter(this, void 0, void 0, function* () { return (yield 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} */ promoteToSuperAdmin() { return __awaiter(this, void 0, void 0, function* () { yield this.addToMemberUserGroup(new client_1.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} */ demoteFromSuperAdmin() { return __awaiter(this, void 0, void 0, function* () { /* istanbul ignore else */ if (yield this.isSuperAdmin()) { yield this.removeFromMemberUserGroup(new client_1.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} */ getSubadminUserGroups() { return __awaiter(this, void 0, void 0, function* () { const groupIds = (yield this.getUserData()).subadminGroups; const result = []; for (const groupId of groupIds) { result.push(new client_1.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} */ getSubadminUserGroupIds() { return __awaiter(this, void 0, void 0, function* () { return (yield 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} */ promoteToUserGroupSubadmin(userGroup) { return __awaiter(this, void 0, void 0, function* () { yield 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} */ demoteFromSubadminUserGroup(userGroup) { return __awaiter(this, void 0, void 0, function* () { yield this.client.demoteUserFromSubadminUserGroup(this.id, userGroup.id); delete this.memento; return; }); } /** * deletes a user * @async * @returns {Promise<void>} * @throws {UserNotFoundError} */ delete() { return __awaiter(this, void 0, void 0, function* () { return yield this.client.deleteUser(this.id); }); } /** * returns the user data * @async * @returns {Promise<IUserOptions>} * @throws {UserNotFoundError} */ getUserData() { return __awaiter(this, void 0, void 0, function* () { if (!this.memento) { this.memento = yield this.client.getUserData(this.id); } return this.memento; }); } } exports.default = User;