@rnaga/wp-node
Version:
👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**
228 lines (227 loc) • 9.64 kB
JavaScript
"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.SignupUtil = void 0;
const zod_1 = require("zod");
const common_1 = require("../../common");
const config_1 = require("../../config");
const component_1 = require("../../decorators/component");
const val = __importStar(require("../../validators"));
const components_1 = require("../components");
const current_1 = require("../current");
const query_util_1 = require("./query.util");
const site_util_1 = require("./site.util");
const options_1 = require("../options");
let SignupUtil = class SignupUtil {
components;
config;
siteUtil;
constructor(components, config, siteUtil) {
this.components = components;
this.config = config;
this.siteUtil = siteUtil;
}
// $active_signup
/**
*
* @returns string returns registration type. The value can be
* 'all', 'none', 'blog', or 'user'.
*/
async getRegistrationType() {
if (!this.config.isMultiSite()) {
return "none";
}
const options = this.components.get(options_1.Options);
const current = this.components.get(current_1.Current);
const activeSignup = await options.get("registration", {
siteId: current.siteId,
});
return activeSignup &&
["all", "none", "blog", "user"].includes(activeSignup)
? activeSignup
: "none";
}
// users_can_register_signup_filter
async canUserSignup() {
const registrationType = await this.getRegistrationType();
return registrationType === "all" || registrationType === "user";
}
async alreadySignedUp(args, options) {
const { userLoginOrEmail, domain, path } = args;
const { days = 2 } = options ?? {};
const queryUtil = this.components.get(query_util_1.QueryUtil);
if (!userLoginOrEmail && (!domain || !path)) {
return false;
}
const signup = await queryUtil.common("signups", (query) => {
if (userLoginOrEmail) {
query
.where("user_email", userLoginOrEmail)
.or.where("user_login", userLoginOrEmail);
}
else if (domain && path) {
query.where("domain", domain).where("path", path);
}
query.builder.first();
}, val.database.wpSignups);
if (!signup || !signup.registered) {
return false;
}
const registered = (typeof signup.registered == "string"
? new Date(signup.registered)
: signup.registered)?.getTime();
// Throw error if email was recently registered
if (new Date().getTime() - registered <= days * 24 * 60 * 60 * 1000) {
return true;
}
return false;
}
// wpmu_validate_user_signup
async validateUser(name, email) {
if (!this.config.isMultiSite()) {
return [false, "Not multisite"];
}
const formattedName = common_1.formatting.username(name).replace(/\s+/g, "");
if (0 >= name.length || name !== formattedName || /[^a-z0-9]/.test(name)) {
return [
false,
"Usernames can only contain lowercase letters (a-z) and numbers",
];
}
const reservedNames = await this.siteUtil.getReservedNames();
if (reservedNames.includes(name)) {
return [false, "Username is not allowed (reserved)"];
}
const parsedEmail = zod_1.z.email().min(4).max(60).safeParse(email);
if (!parsedEmail.success || (await this.siteUtil.isEmailUnsafe(email))) {
return [false, "Invalid email address (unsafe or format)"];
}
if (/^[0-9]*$/.test(name)) {
return [false, "Invalid name (numbers only)"];
}
if (!(await this.siteUtil.isLimitedEmailDomains(email))) {
return [false, "Invalid email address (limited email domains)"];
}
const parsedName = val.trx.userLogin.safeParse(name);
if (!parsedName.success) {
return [false, parsedName.error.message];
}
const queryUtil = this.components.get(query_util_1.QueryUtil);
const users = await queryUtil.users((query) => {
query.where("user_login", name).or.where("user_email", email);
});
if (users && users.length > 0) {
return [false, "User already exists"];
}
if ((await this.alreadySignedUp({ userLoginOrEmail: name })) ||
(await this.alreadySignedUp({ userLoginOrEmail: email }))) {
return [false, "User already signed up"];
}
return [true, undefined];
}
// wpmu_validate_blog_signup
async validateBlog(name, title, user) {
const current = this.components.get(current_1.Current);
const currentSiteId = current.siteId;
if (!current.site?.props.site.domain || !current.site?.props.site.path) {
return [false, "Invalid current domain or path"];
}
const basePath = current.site.props.site.path;
const domain = current.site.props.site.domain;
title = common_1.formatting.stripTags(title);
const reservedNames = await this.siteUtil.getReservedNames();
if (/[^a-z0-9]+/.test(name)) {
return [false, "Invalid blogname (numbers and lowercase letters)"];
}
if (reservedNames.includes(name) || name.length < 4) {
return [false, "Invalid blogname (reserved or length)"];
}
const subdomainInstall = this.config.config.multisite.subdomainInstall;
const queryUtil = this.components.get(query_util_1.QueryUtil);
if (!subdomainInstall &&
(await queryUtil.usingBlog(currentSiteId).posts((query) => {
query
.where("post_type", "page")
.where("post_name", name)
.builder.limit(1);
}))) {
return [false, "Invalid blogname (site name)"];
}
queryUtil.resetBlog();
if (/^[0-9]*$/.test(name)) {
return [false, "Invalid name (numbers only)"];
}
title = common_1.formatting.unslash(title);
if (0 >= title.length) {
return [false, "Invalid title"];
}
const myDomain = subdomainInstall
? `${name}.${domain.replace(/^www\./, "")}`
: domain;
const path = subdomainInstall ? basePath : `${basePath}${name}/`;
const sites = await queryUtil.sites((query) => {
query.where("domain", myDomain).where("path", path);
});
if (sites) {
return [false, "Site already exists"];
}
const existingUser = await queryUtil.users((query) => {
query.where("user_login", name).builder.first();
}, val.database.wpUsers);
if (existingUser && (!user || user.props?.user_login !== name)) {
return [false, "Invalid name (user_login)"];
}
if (await this.alreadySignedUp({ domain, path })) {
return [false, "Already signed up"];
}
return [true, undefined];
}
};
exports.SignupUtil = SignupUtil;
exports.SignupUtil = SignupUtil = __decorate([
(0, component_1.component)(),
__metadata("design:paramtypes", [components_1.Components,
config_1.Config,
site_util_1.SiteUtil])
], SignupUtil);