Implemented new Permission system on the DB side.

ref #22
This commit is contained in:
Nicolai Ort 2020-12-17 20:46:54 +01:00
parent 39b932a81c
commit cc5a30980a
6 changed files with 64 additions and 56 deletions

View File

@ -1,12 +1,11 @@
import {
IsInt,
IsNotEmpty,
IsString
IsNotEmpty
} from "class-validator";
import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { User } from './User';
import { UserGroup } from './UserGroup';
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { PermissionAction } from '../enums/PermissionAction';
import { PermissionTarget } from '../enums/PermissionTargets';
import { Principal } from './Principal';
/**
* Defines the Permission interface.
*/
@ -20,30 +19,27 @@ export abstract class Permission {
id: number;
/**
* users
* The permissions principal
*/
@OneToMany(() => User, user => user.permissions, { nullable: true })
users: User[]
/**
* groups
*/
@OneToMany(() => UserGroup, group => group.permissions, { nullable: true })
groups: UserGroup[]
@ManyToOne(() => Principal, principal => principal.permissions)
principal: Principal[]
/**
* The target
*/
@Column()
@Column({
type: 'simple-enum',
enum: PermissionTarget
})
@IsNotEmpty()
@IsString()
target: string;
target: PermissionTarget;
/**
* The action type
*/
@Column()
@IsNotEmpty()
@IsString()
action: string;
@Column({
type: 'simple-enum',
enum: PermissionAction
})
action: PermissionAction;
}

View File

@ -0,0 +1,24 @@
import { IsInt, IsOptional } from 'class-validator';
import { Entity, OneToMany, PrimaryGeneratedColumn, TableInheritance } from 'typeorm';
import { Permission } from './Permission';
/**
* Defines a admin user.
*/
@Entity()
@TableInheritance({ column: { name: "type", type: "varchar" } })
export abstract class Principal {
/**
* autogenerated unique id (primary key).
*/
@PrimaryGeneratedColumn()
@IsInt()
id: number;
/**
* permissions
*/
@IsOptional()
@OneToMany(() => Permission, permission => permission.principal, { nullable: true })
permissions?: Permission[];
}

View File

@ -1,22 +1,16 @@
import { IsBoolean, IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString, IsUUID } from "class-validator";
import { Column, Entity, JoinTable, ManyToMany, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { ChildEntity, Column, JoinTable, ManyToMany, OneToMany } from "typeorm";
import { config } from '../../config';
import { Permission } from './Permission';
import { Principal } from './Principal';
import { UserAction } from './UserAction';
import { UserGroup } from './UserGroup';
/**
* Defines a admin user.
*/
@Entity()
export class User {
/**
* autogenerated unique id (primary key).
*/
@PrimaryGeneratedColumn()
@IsInt()
id: number;
@ChildEntity()
export class User extends Principal {
/**
* uuid
*/
@ -78,13 +72,6 @@ export class User {
@IsNotEmpty()
password: string;
/**
* permissions
*/
@IsOptional()
@ManyToOne(() => Permission, permission => permission.users, { nullable: true })
permissions?: Permission[];
/**
* groups
*/

View File

@ -1,29 +1,16 @@
import {
IsInt,
IsNotEmpty,
IsOptional,
IsString
} from "class-validator";
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { Permission } from "./Permission";
import { ChildEntity, Column } from "typeorm";
import { Principal } from './Principal';
/**
* Defines the UserGroup interface.
*/
@Entity()
export class UserGroup {
/**
* Autogenerated unique id (primary key).
*/
@PrimaryGeneratedColumn()
@IsInt()
id: number;
/**
* permissions
*/
@ManyToOne(() => Permission, permission => permission.groups, { nullable: true })
permissions: Permission[];
@ChildEntity()
export class UserGroup extends Principal {
/**
* The group's name

View File

@ -0,0 +1,6 @@
export enum PermissionAction {
READ = 'READ',
ADD = 'ADD',
UPDATE = 'UPDATE',
DELETE = 'DELETE'
}

View File

@ -0,0 +1,8 @@
export enum PermissionTarget {
RUNNER = 'RUNNER',
ORGANISATION = 'RUNNERORGANISATION',
TEAM = 'RUNNERTEAM',
TRACK = 'TRACK',
USER = 'USER',
GROUP = 'USERGROUP'
}