Merge branch 'dev' into feature/13-runner_controllers
This commit is contained in:
76
src/models/creation/CreateUser.ts
Normal file
76
src/models/creation/CreateUser.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import * as argon2 from "argon2";
|
||||
import { IsEmail, IsOptional, IsPhoneNumber, IsString, IsUUID } from 'class-validator';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import * as uuid from 'uuid';
|
||||
import { UserGroupNotFoundError, UsernameOrEmailNeededError } from '../../errors/UserErrors';
|
||||
import { User } from '../entities/User';
|
||||
import { UserGroup } from '../entities/UserGroup';
|
||||
|
||||
export class CreateUser {
|
||||
@IsString()
|
||||
firstname: string;
|
||||
@IsString()
|
||||
middlename?: string;
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
username?: string;
|
||||
@IsPhoneNumber("ZZ")
|
||||
@IsOptional()
|
||||
phone?: string;
|
||||
@IsString()
|
||||
password: string;
|
||||
@IsString()
|
||||
lastname: string;
|
||||
@IsEmail()
|
||||
@IsString()
|
||||
email?: string;
|
||||
@IsOptional()
|
||||
groupId?: number[] | number
|
||||
@IsUUID("4")
|
||||
uuid: string;
|
||||
|
||||
public async toUser(): Promise<User> {
|
||||
let newUser: User = new User();
|
||||
|
||||
if (this.email === undefined && this.username === undefined) {
|
||||
throw new UsernameOrEmailNeededError();
|
||||
}
|
||||
|
||||
if (this.groupId) {
|
||||
if (!Array.isArray(this.groupId)) {
|
||||
this.groupId = [this.groupId]
|
||||
}
|
||||
const groupIDs: number[] = this.groupId
|
||||
let errors = 0
|
||||
const validateusergroups = async () => {
|
||||
let foundgroups = []
|
||||
for (const g of groupIDs) {
|
||||
const found = await getConnectionManager().get().getRepository(UserGroup).find({ id: g });
|
||||
if (found.length === 0) {
|
||||
errors++
|
||||
} else {
|
||||
foundgroups.push(found[0])
|
||||
}
|
||||
}
|
||||
newUser.groups = foundgroups
|
||||
}
|
||||
await validateusergroups()
|
||||
if (errors !== 0) {
|
||||
throw new UserGroupNotFoundError();
|
||||
}
|
||||
}
|
||||
|
||||
const new_uuid = uuid.v4()
|
||||
|
||||
newUser.email = this.email
|
||||
newUser.username = this.username
|
||||
newUser.firstname = this.firstname
|
||||
newUser.middlename = this.middlename
|
||||
newUser.lastname = this.lastname
|
||||
newUser.uuid = new_uuid
|
||||
newUser.password = await argon2.hash(this.password + new_uuid);
|
||||
|
||||
console.log(newUser)
|
||||
return newUser;
|
||||
}
|
||||
}
|
||||
28
src/models/creation/CreateUserGroup.ts
Normal file
28
src/models/creation/CreateUserGroup.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { IsOptional, IsString } from 'class-validator';
|
||||
import { GroupNameNeededError } from '../../errors/UserGroupErrors';
|
||||
import { UserGroup } from '../entities/UserGroup';
|
||||
|
||||
export class CreateUserGroup {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
name: string;
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
description?: string;
|
||||
|
||||
public async toUserGroup(): Promise<UserGroup> {
|
||||
let newUserGroup: UserGroup = new UserGroup();
|
||||
|
||||
if (this.name === undefined) {
|
||||
throw new GroupNameNeededError();
|
||||
}
|
||||
|
||||
newUserGroup.name = this.name
|
||||
if (this.description) {
|
||||
newUserGroup.description = this.description
|
||||
}
|
||||
|
||||
console.log(newUserGroup)
|
||||
return newUserGroup;
|
||||
}
|
||||
}
|
||||
@@ -1,130 +1,128 @@
|
||||
import { Entity, Column, OneToMany, ManyToOne, PrimaryGeneratedColumn, Generated, Unique, JoinTable, ManyToMany } from "typeorm";
|
||||
import { IsBoolean, IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString, isUUID, } from "class-validator";
|
||||
import { UserGroup } from './UserGroup';
|
||||
import { Permission } from './Permission';
|
||||
import { UserAction } from './UserAction';
|
||||
|
||||
/**
|
||||
* Defines a admin user.
|
||||
*/
|
||||
@Entity()
|
||||
export class User {
|
||||
/**
|
||||
* autogenerated unique id (primary key).
|
||||
*/
|
||||
@PrimaryGeneratedColumn()
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
id: number;
|
||||
|
||||
/**
|
||||
* autogenerated uuid
|
||||
*/
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
@Generated("uuid")
|
||||
uuid: string;
|
||||
|
||||
/**
|
||||
* user email
|
||||
*/
|
||||
@IsEmail()
|
||||
email: string;
|
||||
|
||||
/**
|
||||
* user phone
|
||||
*/
|
||||
@IsPhoneNumber("ZZ")
|
||||
@IsOptional()
|
||||
phone: string;
|
||||
|
||||
/**
|
||||
* username
|
||||
*/
|
||||
@IsString()
|
||||
username: string;
|
||||
|
||||
/**
|
||||
* firstname
|
||||
*/
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
firstname: string;
|
||||
|
||||
/**
|
||||
* middlename
|
||||
*/
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
middlename: string;
|
||||
|
||||
/**
|
||||
* lastname
|
||||
*/
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
lastname: string;
|
||||
|
||||
/**
|
||||
* password
|
||||
*/
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
password: string;
|
||||
|
||||
/**
|
||||
* permissions
|
||||
*/
|
||||
@ManyToOne(() => Permission, permission => permission.users, { nullable: true })
|
||||
permissions: Permission[];
|
||||
|
||||
/**
|
||||
* groups
|
||||
*/
|
||||
@ManyToMany(() => UserGroup)
|
||||
@JoinTable()
|
||||
groups: UserGroup[];
|
||||
|
||||
/**
|
||||
* is user enabled?
|
||||
*/
|
||||
@IsBoolean()
|
||||
enabled: boolean;
|
||||
|
||||
/**
|
||||
* jwt refresh count
|
||||
*/
|
||||
@IsInt()
|
||||
@Column({ default: 1 })
|
||||
refreshTokenCount: number;
|
||||
|
||||
/**
|
||||
* profilepic
|
||||
*/
|
||||
@IsString()
|
||||
profilepic: string;
|
||||
|
||||
/**
|
||||
* actions
|
||||
*/
|
||||
@OneToMany(() => UserAction, action => action.user)
|
||||
actions: UserAction
|
||||
|
||||
/**
|
||||
* calculate all permissions
|
||||
*/
|
||||
public get calc_permissions(): Permission[] {
|
||||
let final_permissions = []
|
||||
this.groups.forEach((permission) => {
|
||||
if (!final_permissions.includes(permission)) {
|
||||
final_permissions.push(permission)
|
||||
}
|
||||
})
|
||||
this.permissions.forEach((permission) => {
|
||||
if (!final_permissions.includes(permission)) {
|
||||
final_permissions.push(permission)
|
||||
}
|
||||
})
|
||||
return final_permissions
|
||||
}
|
||||
}
|
||||
import { IsBoolean, IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString, IsUUID } from "class-validator";
|
||||
import { Column, Entity, JoinTable, ManyToMany, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { Permission } from './Permission';
|
||||
import { UserAction } from './UserAction';
|
||||
import { UserGroup } from './UserGroup';
|
||||
|
||||
/**
|
||||
* Defines a admin user.
|
||||
*/
|
||||
@Entity()
|
||||
export class User {
|
||||
/**
|
||||
* autogenerated unique id (primary key).
|
||||
*/
|
||||
@PrimaryGeneratedColumn()
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
id: number;
|
||||
|
||||
/**
|
||||
* uuid
|
||||
*/
|
||||
@IsUUID("4")
|
||||
uuid: string;
|
||||
|
||||
/**
|
||||
* user email
|
||||
*/
|
||||
@IsEmail()
|
||||
email: string;
|
||||
|
||||
/**
|
||||
* user phone
|
||||
*/
|
||||
@IsPhoneNumber("ZZ")
|
||||
@IsOptional()
|
||||
phone: string;
|
||||
|
||||
/**
|
||||
* username
|
||||
*/
|
||||
@IsString()
|
||||
username: string;
|
||||
|
||||
/**
|
||||
* firstname
|
||||
*/
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
firstname: string;
|
||||
|
||||
/**
|
||||
* middlename
|
||||
*/
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
middlename: string;
|
||||
|
||||
/**
|
||||
* lastname
|
||||
*/
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
lastname: string;
|
||||
|
||||
/**
|
||||
* password
|
||||
*/
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
password: string;
|
||||
|
||||
/**
|
||||
* permissions
|
||||
*/
|
||||
@ManyToOne(() => Permission, permission => permission.users, { nullable: true })
|
||||
permissions: Permission[];
|
||||
|
||||
/**
|
||||
* groups
|
||||
*/
|
||||
@ManyToMany(() => UserGroup)
|
||||
@JoinTable()
|
||||
groups: UserGroup[];
|
||||
|
||||
/**
|
||||
* is user enabled?
|
||||
*/
|
||||
@IsBoolean()
|
||||
enabled: boolean;
|
||||
|
||||
/**
|
||||
* jwt refresh count
|
||||
*/
|
||||
@IsInt()
|
||||
@Column({ default: 1 })
|
||||
refreshTokenCount: number;
|
||||
|
||||
/**
|
||||
* profilepic
|
||||
*/
|
||||
@IsString()
|
||||
profilepic: string;
|
||||
|
||||
/**
|
||||
* actions
|
||||
*/
|
||||
@OneToMany(() => UserAction, action => action.user)
|
||||
actions: UserAction
|
||||
|
||||
/**
|
||||
* calculate all permissions
|
||||
*/
|
||||
public get calc_permissions(): Permission[] {
|
||||
let final_permissions = []
|
||||
this.groups.forEach((permission) => {
|
||||
if (!final_permissions.includes(permission)) {
|
||||
final_permissions.push(permission)
|
||||
}
|
||||
})
|
||||
this.permissions.forEach((permission) => {
|
||||
if (!final_permissions.includes(permission)) {
|
||||
final_permissions.push(permission)
|
||||
}
|
||||
})
|
||||
return final_permissions
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
import { PrimaryGeneratedColumn, Column, OneToMany, Entity, ManyToOne } from "typeorm";
|
||||
import {
|
||||
IsInt,
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
IsString,
|
||||
IsString
|
||||
} from "class-validator";
|
||||
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { Permission } from "./Permission";
|
||||
|
||||
/**
|
||||
* Defines the UserGroup interface.
|
||||
*/
|
||||
@Entity()
|
||||
export abstract class UserGroup {
|
||||
export class UserGroup {
|
||||
/**
|
||||
* Autogenerated unique id (primary key).
|
||||
*/
|
||||
@@ -37,7 +37,7 @@ export abstract class UserGroup {
|
||||
/**
|
||||
* The group's description
|
||||
*/
|
||||
@Column({nullable: true})
|
||||
@Column({ nullable: true })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
description?: string;
|
||||
|
||||
Reference in New Issue
Block a user