/* @alwatr/nitrobase-user-management v7.5.4 */ "use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/main.ts var main_exports = {}; __export(main_exports, { NitrobaseUserManagement: () => NitrobaseUserManagement }); module.exports = __toCommonJS(main_exports); // src/user-management.ts var import_node_path = require("node:path"); var import_nanolib2 = require("@alwatr/nanolib"); var import_node_fs = require("@alwatr/nanolib/node-fs"); var import_nitrobase_types = require("@alwatr/nitrobase-types"); // src/logger.ts var import_nanolib = require("@alwatr/nanolib"); __dev_mode__: import_nanolib.packageTracer.add("@alwatr/nitrobase-user-management", "7.5.4"); var logger = /* @__PURE__ */ (0, import_nanolib.createLogger)("@alwatr/nitrobase-user-management"); // src/user-management.ts var _NitrobaseUserManagement = class _NitrobaseUserManagement { /** * Create a new instance of NitrobaseUserManagement. * @param nitrobase_ The Nitrobase instance. */ constructor(nitrobase_) { this.nitrobase_ = nitrobase_; this.logger_ = (0, import_nanolib2.createLogger)("user-management"); this.logger_.logMethod?.("constructor"); this.userListCollection_ = this.nitrobase_.openCollection(_NitrobaseUserManagement.nitrobaseIds.userList); } /** * Initialize the user management module. * @param nitrobase The Nitrobase instance. */ static async initialize(nitrobase) { logger.logMethod?.("NitrobaseUserManagement.initialize"); nitrobase.newCollection(this.nitrobaseIds.userList); } // Implementation of NitrobaseUserManagementInterface methods with JSDoc comments async newUser(user) { this.logger_.logMethodArgs?.("newUser", user); (await this.userListCollection_).addItem(user.userId, {}); this.newUserInfoDocument_(user.userId, user.data); await this.makeTokenFile_(user.userId, user.userToken, user.isManager); } async hasUser(userId) { const userExists = (await this.userListCollection_).hasItem(userId); this.logger_.logMethodFull?.("hasUser", { userId }, userExists); return userExists; } async getUserData(userId) { this.logger_.logMethodArgs?.("getUserData", userId); const userInfoDocument = await this.openUserInfoDocument_(userId); return userInfoDocument.getData(); } async getUserMeta(userId) { this.logger_.logMethodArgs?.("getUserMeta", userId); const document = await this.openUserInfoDocument_(userId); return document.getStoreMeta(); } save(userId) { this.logger_.logMethodArgs?.("save", userId); this.openUserInfoDocument_(userId).then((document) => { document.save(); }); } saveImmediate(userId) { this.logger_.logMethodArgs?.("saveImmediate", userId); this.openUserInfoDocument_(userId).then((document) => { document.saveImmediate(); }); } async userIds() { return (await this.userListCollection_).ids(); } async verifyUserToken(userId, token) { const tokenFileExist = (0, import_node_fs.existsSync)(await this.getUserTokenFilePath_(userId, token)); this.logger_.logMethodFull?.("verifyUserToken", { userId, token: token.slice(0, 12) + "..." }, tokenFileExist); return tokenFileExist; } async getUserDirectory(userId) { const userInfoDocument = await this.openUserInfoDocument_(userId); const userDirectoryPath = (0, import_node_path.dirname)((0, import_node_path.resolve)(this.nitrobase_.config.rootPath, userInfoDocument.path)); this.logger_.logMethodFull?.("getUserDirectory", { userId }, userDirectoryPath); return userDirectoryPath; } /** * Create a new user info document. * @param userId The user ID. * @param userInfo The user info data. */ newUserInfoDocument_(userId, userInfo) { this.logger_.logMethodArgs?.("newUserInfoDocument", { userId, userInfo }); const storeId = { ..._NitrobaseUserManagement.nitrobaseIds.userInfo, ownerId: userId }; if (this.nitrobase_.hasStore(storeId)) { this.logger_.accident?.("newUserInfoDocument_", "store_already_exists", storeId); return; } this.nitrobase_.newDocument(storeId, userInfo); } /** * Open the user info document. * @param userId The user ID. * @returns A promise resolving to the user info document. */ openUserInfoDocument_(userId) { this.logger_.logMethodArgs?.("openUserInfoDocument_", userId); return this.nitrobase_.openDocument({ ..._NitrobaseUserManagement.nitrobaseIds.userInfo, ownerId: userId }); } /** * Create a token file for the user. * @param userId The user ID. * @param token The user token. * @param isManager Whether the user is a manager. */ async makeTokenFile_(userId, token, isManager) { this.logger_.logMethodArgs?.("makeTokenFile", { userId, token: token.slice(0, 12) + "...", isManager }); const userDirectory = await this.getUserDirectory(userId); await (0, import_node_fs.makeEmptyFile)((0, import_node_path.resolve)(userDirectory, `.token/${token}.asn`)); if (isManager === true) { await (0, import_node_fs.makeEmptyFile)((0, import_node_path.resolve)(userDirectory, ".auth/manager.asn")); } } /** * Get the path to the user token file. * @param userId The user ID. * @param token The user token. * @returns The path to the user token file. */ async getUserTokenFilePath_(userId, token) { const userTokenFilePath = (0, import_node_path.resolve)(await this.getUserDirectory(userId), `.token/${token}.asn`); this.logger_.logMethodFull?.("getTokenFilePath", { userId, token: token.slice(0, 12) + "..." }, userTokenFilePath); return userTokenFilePath; } }; /** * Default Nitrobase store IDs used by this class. */ _NitrobaseUserManagement.nitrobaseIds = { userList: { name: "user-list", region: import_nitrobase_types.Region.Managers, schemaVer: 1 }, userInfo: { name: "user-info", region: import_nitrobase_types.Region.PerUser } }; var NitrobaseUserManagement = _NitrobaseUserManagement; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { NitrobaseUserManagement }); /*! For license information please see main.cjs.LEGAL.txt */ //# sourceMappingURL=main.cjs.map