backend/src/models/entities/Permission.ts

54 lines
1.4 KiB
TypeScript

import {
IsEnum,
IsInt,
IsNotEmpty
} from "class-validator";
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { PermissionAction } from '../enums/PermissionAction';
import { PermissionTarget } from '../enums/PermissionTargets';
import { Principal } from './Principal';
/**
* Defines the Permission entity.
* Permissions can be granted to principals.
* The permissions possible targets and actions are defined in enums.
*/
@Entity()
export class Permission {
/**
* Autogenerated unique id (primary key).
*/
@PrimaryGeneratedColumn()
@IsInt()
id: number;
/**
* The permission's principal.
*/
@ManyToOne(() => Principal, principal => principal.permissions)
principal: Principal;
/**
* The permission's target.
* This get's stored as the enum value's string representation for compatability reasons.
*/
@Column({ type: 'varchar' })
@IsNotEmpty()
@IsEnum(PermissionTarget)
target: PermissionTarget;
/**
* The permission's action.
* This get's stored as the enum value's string representation for compatability reasons.
*/
@Column({ type: 'varchar' })
@IsEnum(PermissionAction)
action: PermissionAction;
/**
* Turn this into a string for exporting and jwts.
* Mainly used to shrink the size of jwts (otherwise the would contain entire objects).
*/
public toString(): string {
return this.target + ":" + this.action;
}
}