@rnaga/wp-node
Version:
👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**
91 lines (90 loc) • 4.73 kB
JavaScript
;
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 __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.ApplicationPasswordsCrud = void 0;
const components_1 = require("../core/components");
const application_passwords_util_1 = require("../core/utils/application-passwords.util");
const component_1 = require("../decorators/component");
const crud_1 = require("./crud");
const error_1 = require("./error");
let ApplicationPasswordsCrud = class ApplicationPasswordsCrud extends crud_1.Crud {
applicationPasswordsUtil;
constructor(components, applicationPasswordsUtil) {
super(components);
this.applicationPasswordsUtil = applicationPasswordsUtil;
}
async get(uuid) {
const { user: currentUser } = await this.getUser();
if (!(await currentUser?.can("read_app_password"))) {
throw new error_1.CrudError(error_1.StatusMessage.UNAUTHORIZED, "Not permitted");
}
const password = await this.applicationPasswordsUtil.getUserPasswordByUuid(currentUser.props.ID, uuid);
if (!password) {
throw new error_1.CrudError(error_1.StatusMessage.NOT_FOUND, "Password not found");
}
return this.returnValue(password);
}
async list() {
const { user: currentUser } = await this.getUser();
if (!(await currentUser?.can("list_app_passwords"))) {
throw new error_1.CrudError(error_1.StatusMessage.UNAUTHORIZED, "Not permitted");
}
const passwords = await this.applicationPasswordsUtil.getUserPasswords(currentUser.props.ID);
return this.returnValue(passwords);
}
async create(data) {
const { user: currentUser } = await this.getUser();
if (!(await currentUser?.can("create_app_password"))) {
throw new error_1.CrudError(error_1.StatusMessage.UNAUTHORIZED, "Not permitted");
}
const password = await this.applicationPasswordsUtil.createNewPassword(currentUser.props.ID, data);
return this.returnValue(password);
}
async update(uuid, data) {
const { user: currentUser } = await this.getUser();
if (!(await currentUser?.can("edit_app_password"))) {
throw new error_1.CrudError(error_1.StatusMessage.UNAUTHORIZED, "Not permitted");
}
const success = await this.applicationPasswordsUtil.updatePasswordName(currentUser.props.ID, uuid, data.name);
if (!success) {
throw new error_1.CrudError(error_1.StatusMessage.BAD_REQUEST, "Failed to update password");
}
return this.returnValue(success);
}
async delete(uuid) {
const { user: currentUser } = await this.getUser();
if (!(await currentUser?.can("delete_app_password"))) {
throw new error_1.CrudError(error_1.StatusMessage.UNAUTHORIZED, "Not permitted");
}
const success = await this.applicationPasswordsUtil.deletePassword(currentUser.props.ID, uuid);
if (!success) {
throw new error_1.CrudError(error_1.StatusMessage.BAD_REQUEST, "Failed to delete password");
}
return this.returnValue(success);
}
async deleteAll() {
const { user: currentUser } = await this.getUser();
if (!(await currentUser?.can("delete_app_passwords"))) {
throw new error_1.CrudError(error_1.StatusMessage.UNAUTHORIZED, "Not permitted");
}
const success = await this.applicationPasswordsUtil.deleteAllPasswords(currentUser.props.ID);
if (!success) {
throw new error_1.CrudError(error_1.StatusMessage.BAD_REQUEST, "Failed to delete passwords");
}
return this.returnValue(success);
}
};
exports.ApplicationPasswordsCrud = ApplicationPasswordsCrud;
exports.ApplicationPasswordsCrud = ApplicationPasswordsCrud = __decorate([
(0, component_1.component)(),
__metadata("design:paramtypes", [components_1.Components,
application_passwords_util_1.ApplicationPasswordsUtil])
], ApplicationPasswordsCrud);