@webda/core
Version:
Expose API with Lambda
223 lines • 6.96 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;
};
import { User, WebdaError } from "../index.js";
import { Action, CoreModel } from "./coremodel.js";
/**
* Object that contains ACL to define its own permissions
*
* @WebdaModel
*/
export default class AclModel extends CoreModel {
constructor() {
super(...arguments);
/**
* Permissions on the object
*/
this.__acl = {};
}
/**
* Ensure creator has all permissions by default
*/
async _onSave() {
await super._onSave();
this._creator = this.getContext().getCurrentUserId();
if (Object.keys(this.__acl).length === 0 && this._creator) {
this.__acl[this._creator] = "all";
}
}
/**
* Add the permissions for current user
*/
async _onGet() {
const ctx = this.getContext();
if (!ctx.isGlobal()) {
this._permissions = await this.getPermissions(ctx);
}
else {
this._permissions = [];
}
}
/**
* Return object ACLs
* @returns
*/
getAcl() {
return this.__acl;
}
/**
* Set object ACLs
* @param acl
*/
setAcl(acl) {
this.__acl = acl;
}
/**
* Manage the ACL REST api actions
* @param ctx
* @returns
*/
async acl(ctx) {
if (ctx.getHttpContext().getMethod() === "PUT") {
return this._httpPutAcls(ctx);
}
else if (ctx.getHttpContext().getMethod() === "GET") {
return this._httpGetAcls(ctx);
}
}
/**
* GET
* @param ctx
*/
async _httpGetAcls(ctx) {
return {
raw: this.__acl,
resolved: await Promise.all(Object.keys(this.__acl).map(async (ace) => {
return {
permission: this.__acl[ace],
actor: await this.getUserPublicEntry(ace)
};
}))
};
}
/**
* Get the user public entry
*
* Override if using a custom User without backward compatibility
* @param ace
* @returns
*/
async getUserPublicEntry(ace) {
return (await User.ref(ace).get())?.toPublicEntry();
}
/**
*
*/
async _httpPutAcls(ctx) {
let acl = await ctx.getInput();
// This looks like a bad request
if (acl.raw) {
throw new WebdaError.BadRequest("ACL should have raw field");
}
this.__acl = acl;
await this.save();
}
// Should cache the user role in the session
getGroups(_ctx, user) {
if (!user) {
return [];
}
let groups = user.getGroups();
if (!groups) {
groups = [];
}
groups = groups.slice(0);
groups.push(user.getUuid());
return groups;
}
/**
* Get Permissions for one object
* @param ctx
* @param user
* @returns
*/
async getPermissions(ctx, user) {
if (!user) {
user = await ctx.getCurrentUser();
}
let permissions = new Set();
let groups = this.getGroups(ctx, user);
for (let i in this.__acl) {
if (groups.indexOf(i) >= 0) {
this.__acl[i].split(",").forEach(p => permissions.add(p));
}
}
return [...permissions.values()];
}
async hasPermission(ctx, user, action) {
let groups = this.getGroups(ctx, user);
for (let i in this.__acl) {
if (groups.indexOf(i) >= 0) {
if (this.__acl[i] === "all" || this.__acl[i].split(",").indexOf(action) >= 0) {
return true;
}
}
}
return false;
}
async canAct(ctx, action) {
if (action === "create" && ctx.getCurrentUserId()) {
return true;
}
if (!this.getAcl() || !ctx.getCurrentUserId()) {
return "No ACL or user";
}
let user = await ctx.getCurrentUser();
if (await this.hasPermission(ctx, user, action)) {
return true;
}
return "No permission";
}
}
__decorate([
Action({
methods: ["GET", "PUT"],
openapi: {
get: {
summary: "Get ACLs",
responses: {
"200": {
content: {
"application/json": {
schema: {
type: "object",
properties: {
raw: {
type: "object"
},
resolved: {
type: "array",
items: {
type: "object",
properties: {
permission: {
type: "string"
},
actor: {
type: "object"
}
}
}
}
}
}
}
}
}
}
},
put: {
summary: "Update ACLs",
requestBody: {
content: {
"application/json": {
schema: {
type: "object",
properties: {
raw: {
type: "object"
}
}
}
}
}
}
}
}
})
], AclModel.prototype, "acl", null);
export { AclModel };
//# sourceMappingURL=aclmodel.js.map