From cd7b15aadfe66353033e976393fc143368ba0ba8 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Wed, 13 Jan 2021 17:57:42 +0100 Subject: [PATCH] First part of resolving user inherited permissions ref #93 --- src/models/entities/User.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/models/entities/User.ts b/src/models/entities/User.ts index f82e899..c9111ff 100644 --- a/src/models/entities/User.ts +++ b/src/models/entities/User.ts @@ -3,6 +3,7 @@ import { ChildEntity, Column, JoinTable, ManyToMany, OneToMany } from "typeorm"; import { config } from '../../config'; import { ResponsePrincipal } from '../responses/ResponsePrincipal'; import { ResponseUser } from '../responses/ResponseUser'; +import { Permission } from './Permission'; import { Principal } from './Principal'; import { UserAction } from './UserAction'; import { UserGroup } from './UserGroup'; @@ -129,8 +130,24 @@ export class User extends Principal { @OneToMany(() => UserAction, action => action.user, { nullable: true }) actions: UserAction[] + /** + * Resolves all permissions granted to this user through groups. + */ + public get inheritedPermissions(): Permission[] { + let returnPermissions: Permission[] = new Array(); + + if (!this.groups) { return returnPermissions; } + for (let group of this.groups) { + for (let permission of group.permissions) { + returnPermissions.push(permission); + } + } + return returnPermissions; + } + /** * Resolves all permissions granted to this user through groups or directly to the string enum format. + * Also deduplicates the array. */ public get allPermissions(): string[] { let returnPermissions: string[] = new Array();