UNPKG

@openinc/parse-server-opendash

Version:
62 lines (61 loc) 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.init = init; const types_1 = require("../types"); /** * This function deregisters a permission in the database on a tenant level. * @param name The function name derived from the file name */ async function init(name) { Parse.Cloud.define(name, async function (request) { if (request.user === undefined) { return { success: false, error: "User is not provided." }; } //Get the current user const user = request.user; if (!user) { return { success: false, error: "User is required." }; } //Get the tenant of the current user const tenant = user.get("tenant"); const permissionKey = request.params.permissionKey; if (!permissionKey) { return { success: false, error: "Permission key is required." }; } //Find the permission with key permissionKey and delete it const permissions = await new Parse.Query(types_1.Permission) .equalTo("key", permissionKey) .equalTo("tenant", tenant) .find({ useMasterKey: true }); const role = await new Parse.Query(Parse.Role) .equalTo("name", "od-tenant-admin-" + tenant.id) .first({ useMasterKey: true }); // Check if the permission can be written publically or by the current user or the tenant role const filteredpermissions = permissions.filter((permission) => { const acl = permission.getACL(); return acl?.getPublicWriteAccess() || acl?.getWriteAccess(user.id) || role !== undefined ? acl?.getRoleWriteAccess(role) : false; }); if (permissions.length === 0) { console.info(`Permissions with key ${permissionKey} not found.`); } else { console.info(`Permission with key ${permissionKey} found. Deleting...`); for await (const permission of filteredpermissions) { await permission.destroy({ useMasterKey: true }); } } return { success: true }; }, { requireUser: true, fields: { permissionKey: { required: true, type: String, }, }, }); }