move to required ids

This commit is contained in:
rubikscraft
2022-04-01 12:04:49 +02:00
parent c6d1206e29
commit 9bcf09ff4f
18 changed files with 48 additions and 54 deletions

View File

@@ -32,7 +32,7 @@ export class RolesModule implements OnModuleInit {
}
private async nukeRoles() {
this.logger.error('Nuking system roles');
this.logger.warn('Nuking system roles');
const result = await this.rolesService.nukeSystemRoles(true);
if (HasFailed(result)) {
this.logger.error(`Failed to nuke roles because: ${result.getReason()}`);

View File

@@ -43,10 +43,8 @@ export class RolesService {
}
}
public async delete(
role: string | ERoleBackend,
): AsyncFailable<ERoleBackend> {
const roleToModify = await this.resolve(role);
public async delete(name: string): AsyncFailable<ERoleBackend> {
const roleToModify = await this.findOne(name);
if (HasFailed(roleToModify)) return roleToModify;
if (UndeletableRolesList.includes(roleToModify.name)) {
@@ -75,10 +73,10 @@ export class RolesService {
}
public async addPermissions(
role: string | ERoleBackend,
name: string,
permissions: Permissions,
): AsyncFailable<ERoleBackend> {
const roleToModify = await this.resolve(role);
const roleToModify = await this.findOne(name);
if (HasFailed(roleToModify)) return roleToModify;
const newPermissions = makeUnique([
@@ -90,10 +88,10 @@ export class RolesService {
}
public async removePermissions(
role: string | ERoleBackend,
name: string,
permissions: Permissions,
): AsyncFailable<ERoleBackend> {
const roleToModify = await this.resolve(role);
const roleToModify = await this.findOne(name);
if (HasFailed(roleToModify)) return roleToModify;
const newPermissions = roleToModify.permissions.filter(
@@ -149,8 +147,8 @@ export class RolesService {
}
}
public async exists(username: string): Promise<boolean> {
return HasSuccess(await this.findOne(username));
public async exists(name: string): Promise<boolean> {
return HasSuccess(await this.findOne(name));
}
public async nukeSystemRoles(IAmSure: boolean = false): AsyncFailable<true> {
@@ -168,18 +166,18 @@ export class RolesService {
}
private async resolve(
user: string | ERoleBackend,
role: string | ERoleBackend,
): AsyncFailable<ERoleBackend> {
if (typeof user === 'string') {
return await this.findOne(user);
if (typeof role === 'string') {
return await this.findOne(role);
} else {
user = plainToClass(ERoleBackend, user);
const errors = await strictValidate(user);
role = plainToClass(ERoleBackend, role);
const errors = await strictValidate(role);
if (errors.length > 0) {
this.logger.warn(errors);
return Fail('Invalid role');
}
return user;
return role;
}
}
}

View File

@@ -14,10 +14,7 @@ export class AuthManagerService {
async createToken(user: EUserBackend): AsyncFailable<string> {
const jwtData: JwtDataDto = plainToClass(JwtDataDto, {
user: {
username: user.username,
roles: user.roles,
},
user,
});
// Validate to be sure, this makes client experience better

View File

@@ -4,7 +4,7 @@ import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class EImageBackend extends EImage {
@PrimaryGeneratedColumn("uuid")
override id?: string;
override id: string;
@Index()
@Column({ unique: true, nullable: false })

View File

@@ -5,7 +5,7 @@ import { Permissions } from '../dto/permissions.dto';
@Entity()
export class ERoleBackend extends ERole {
@PrimaryGeneratedColumn("uuid")
override id?: string;
override id: string;
@Index()
@Column({ nullable: false, unique: true })

View File

@@ -4,7 +4,7 @@ import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class ESysPreferenceBackend extends ESysPreference {
@PrimaryGeneratedColumn("uuid")
override id?: string;
override id: string;
@Index()
@Column({ nullable: false, unique: true })

View File

@@ -6,7 +6,7 @@ import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class EUserBackend extends EUser {
@PrimaryGeneratedColumn("uuid")
override id?: string;
override id: string;
@Index()
@Column({ nullable: false, unique: true })

View File

@@ -3,7 +3,7 @@ import { Column, Index, PrimaryGeneratedColumn } from 'typeorm';
export class EUsrPreferenceBackend extends EUsrPreference {
@PrimaryGeneratedColumn("uuid")
override id?: string;
override id: string;
@Index()
@Column({ nullable: false, unique: true })