UNPKG

@foal/typeorm

Version:

FoalTS integration of TypeORM

113 lines (112 loc) 4.4 kB
"use strict"; 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.UserWithPermissions = void 0; const typeorm_1 = require("typeorm"); // FoalTS const group_entity_1 = require("./group.entity"); const permission_entity_1 = require("./permission.entity"); /** * Abstract class to define a user entity with a system of groups and permissions. * * A group can have permissions. * A user can have permissions and belong to groups that have also permissions. * * @export * @abstract * @class UserWithPermissions */ class UserWithPermissions extends typeorm_1.BaseEntity { /** * Get all users with a given permission. * * This method returns all users that have this permission on their own or through the groups they belong to. * * @static * @template T * @param {string} codeName - The permission codename. * @returns {Promise<T[]>} * @memberof UserWithPermissions */ static async withPerm(codeName) { const userWithUserPermissionsQb = this .createQueryBuilder('user1') .select('user1.id') .innerJoin('user1.userPermissions', 'userPermission') .where('userPermission.codeName = :codeName'); const userWithGroupPermissionsQb = this .createQueryBuilder('user2') .select('user2.id') .innerJoin('user2.groups', 'group') .innerJoin('group.permissions', 'groupPermission') .where('groupPermission.codeName = :codeName'); return await this .createQueryBuilder('user') .where('user.id IN (' + userWithUserPermissionsQb.getQuery() + ')') .orWhere('user.id IN (' + userWithGroupPermissionsQb.getQuery() + ')') .setParameters({ codeName }) .getMany(); } static async findOneWithPermissionsBy({ id }) { return (await this .getRepository() .findOne({ where: { id }, relations: { userPermissions: true, groups: { permissions: true, } } })); } id; groups; userPermissions; /** * Check if a user has a given permission. The user must have been retreived from the db * with their groups and permissions. Otherwise, the method will always return false. * * @param {string} codeName - Name of the permission. * @returns {boolean} True if the user has the permission. False otherwise. * @memberof UserWithPermissions */ hasPerm(codeName) { for (const permission of this.userPermissions || []) { if (permission.codeName === codeName) { return true; } } for (const group of this.groups || []) { for (const permission of group.permissions || []) { if (permission.codeName === codeName) { return true; } } } return false; } } exports.UserWithPermissions = UserWithPermissions; __decorate([ (0, typeorm_1.PrimaryGeneratedColumn)(), __metadata("design:type", Number) ], UserWithPermissions.prototype, "id", void 0); __decorate([ (0, typeorm_1.ManyToMany)(type => group_entity_1.Group), (0, typeorm_1.JoinTable)(), __metadata("design:type", Array) ], UserWithPermissions.prototype, "groups", void 0); __decorate([ (0, typeorm_1.ManyToMany)(type => permission_entity_1.Permission), (0, typeorm_1.JoinTable)(), __metadata("design:type", Array) ], UserWithPermissions.prototype, "userPermissions", void 0);