UNPKG

@rnaga/wp-node

Version:

👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**

173 lines (172 loc) • 7.83 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UserSelfRegistrationCrud = void 0; const common_1 = require("../common"); const config_1 = require("../config"); const components_1 = require("../core/components"); const current_1 = require("../core/current"); const logger_1 = require("../core/logger"); const options_1 = require("../core/options"); const user_self_registration_util_1 = require("../core/utils/user-self-registration.util"); const user_util_1 = require("../core/utils/user.util"); const component_1 = require("../decorators/component"); const transactions_1 = require("../transactions"); const vals = __importStar(require("../validators")); const crud_1 = require("./crud"); const error_1 = require("./error"); let UserSelfRegistrationCrud = class UserSelfRegistrationCrud extends crud_1.Crud { config; logger; constructor(components, config, logger) { super(components); this.config = config; this.logger = logger; } async checkPermission(siteId) { const { user } = await this.getUser(); // Multi site if (this.config.isMultiSite()) { const current = this.components.get(current_1.Current); siteId = siteId ?? current.siteId; if (!siteId || !(await user.can("manage_site_users", [siteId]))) { throw new error_1.CrudError(error_1.StatusMessage.UNAUTHORIZED, "Not permitted"); } } // Single site if (!this.config.isMultiSite() && !(await user.can("edit_users"))) { throw new error_1.CrudError(error_1.StatusMessage.UNAUTHORIZED, "Not permitted"); } } async checkEligibility(userSelfRegistrationUtil) { if (!(await userSelfRegistrationUtil.canSignup())) { throw new error_1.CrudError(error_1.StatusMessage.BAD_REQUEST, "Not allowed"); } } async canSignup() { const userSelfRegistrationUtil = this.components.get(user_self_registration_util_1.UserSelfRegistrationUtil); return this.returnValue(await userSelfRegistrationUtil.canSignup()); } async update(input, options = {}) { await this.checkPermission(options.siteId); const userSelfRegistrationUtil = this.components.get(user_self_registration_util_1.UserSelfRegistrationUtil); return this.returnValue(await userSelfRegistrationUtil.changeEligibility(input.eligibility)); } /** * Register a new user without activation key * * Use this when user registers through external id provider * * @param args - user_login, email, blog_id * @returns boolean * */ async registerWithoutActivation(args) { const { email } = args; const userSelfRegistrationUtil = this.components.get(user_self_registration_util_1.UserSelfRegistrationUtil); await this.checkEligibility(userSelfRegistrationUtil); const parsedEmail = vals.trx.userInsert.shape.user_email.safeParse(email); if (!parsedEmail) { throw new error_1.CrudError(error_1.StatusMessage.BAD_REQUEST, "Invalid email"); } const userUtil = this.components.get(user_util_1.UserUtil); let userLogin = args.user_login; if (!userLogin) { userLogin = await userUtil.getUniqueUserLogin(); } else { const user = await userUtil.get(userLogin); // User already exists if (user.props?.ID) { throw new error_1.CrudError(error_1.StatusMessage.BAD_REQUEST, "User already exists"); } } const { firstName, lastName } = common_1.formatting.parseName(args.name ?? ""); // Create a new user const options = this.components.get(options_1.Options); const defaultRole = await options.get("default_role"); if (!defaultRole) { throw new error_1.CrudError(error_1.StatusMessage.BAD_REQUEST, "Default role not found"); } const userTrx = this.components.get(transactions_1.UserTrx); const result = await userTrx.upsert({ user_login: userLogin, display_name: userLogin, user_email: email, first_name: firstName, last_name: lastName, role: [defaultRole], }, { attachRole: true, }); return this.returnValue({ user_id: result, user_login: userLogin, user_email: email, first_name: firstName, last_name: lastName, }); } async register(args) { const userSelfRegistrationUtil = this.components.get(user_self_registration_util_1.UserSelfRegistrationUtil); await this.checkEligibility(userSelfRegistrationUtil); const { user_login: userLogin, email } = args; const result = await userSelfRegistrationUtil.registerNew(userLogin, email); return this.returnValue(!!result.id); } async activate(args) { const { key: activationKey, user_login: userLogin } = args; const userSelfRegistrationUtil = this.components.get(user_self_registration_util_1.UserSelfRegistrationUtil); await this.checkEligibility(userSelfRegistrationUtil); const result = await userSelfRegistrationUtil.activate(activationKey, userLogin); return this.returnValue(result); } }; exports.UserSelfRegistrationCrud = UserSelfRegistrationCrud; exports.UserSelfRegistrationCrud = UserSelfRegistrationCrud = __decorate([ (0, component_1.component)(), __metadata("design:paramtypes", [components_1.Components, config_1.Config, logger_1.Logger]) ], UserSelfRegistrationCrud);