move to required ids

This commit is contained in:
rubikscraft 2022-04-01 12:04:49 +02:00
parent c6d1206e29
commit 9bcf09ff4f
No known key found for this signature in database
GPG key ID: 1463EBE9200A5CD4
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 })

View file

@ -55,8 +55,6 @@ export class SettingsRolesEditComponent implements OnInit {
this.mode = EditMode.add;
return;
}
// Set data thats already known
this.mode = EditMode.edit;
this.model.putRoleName(rolename);

View file

@ -4,11 +4,10 @@ import { isSemVer } from 'class-validator';
import { InfoResponse } from 'picsur-shared/dist/dto/api/info.dto';
import {
AsyncFailable,
Fail,
Failable,
HasFailed
Fail, HasFailed
} from 'picsur-shared/dist/types';
import { BehaviorSubject } from 'rxjs';
import { SnackBarType } from 'src/app/models/dto/snack-bar-type.dto';
import { UtilService } from 'src/app/util/util.service';
import pkg from '../../../../package.json';
import { ServerInfo } from '../../models/dto/server-info.dto';
@ -28,12 +27,7 @@ export class InfoService {
private infoSubject = new BehaviorSubject<ServerInfo>(new ServerInfo());
constructor(private api: ApiService, private utilService: UtilService) {
this.init().catch(this.logger.error);
}
private async init() {
await this.pollInfo();
this.checkCompatibility();
this.checkCompatibility().catch(this.logger.error);
}
public async pollInfo(): AsyncFailable<ServerInfo> {
@ -52,8 +46,8 @@ export class InfoService {
// If either version starts with 0. it has to be exactly the same
// If both versions start with something else, they have to match the first part
public isCompatibleWithServer(): Failable<boolean> {
const info = this.infoSubject.getValue();
public async isCompatibleWithServer(): AsyncFailable<boolean> {
const info = await this.pollInfo();
if (HasFailed(info)) return info;
const serverVersion = info.version;
@ -81,10 +75,18 @@ export class InfoService {
}
}
private checkCompatibility(): void {
const isCompatible = this.isCompatibleWithServer();
private async checkCompatibility() {
const isCompatible = await this.isCompatibleWithServer();
if (HasFailed(isCompatible) || !isCompatible) {
if (HasFailed(isCompatible)) {
this.utilService.showSnackBar(
'There was an error checking compatibility',
SnackBarType.Warning
);
return;
}
if (!isCompatible) {
this.utilService
.showDialog({
title: 'Server is not compatible',

View file

@ -2,5 +2,5 @@ import { EntityID } from '../validators/entity-id.validator';
export class EntityIDObject {
@EntityID()
id: number;
id: string;
}

View file

@ -1,12 +1,12 @@
import { Type } from 'class-transformer';
import { IsDefined, IsInt, IsOptional, ValidateNested } from 'class-validator';
import { NameRolesUser } from '../entities/user.entity';
import { EUser } from '../entities/user.entity';
export class JwtDataDto {
@IsDefined()
@ValidateNested()
@Type(() => NameRolesUser)
user: NameRolesUser;
@Type(() => EUser)
user: EUser;
@IsOptional()
@IsInt()

View file

@ -4,7 +4,7 @@ import { EntityID } from '../validators/entity-id.validator';
export class EImage {
@EntityID()
id?: string;
id: string;
@IsHash('sha256')
hash: string;

View file

@ -19,5 +19,5 @@ export class RoleNamePermsObject extends RoleNameObject {
export class ERole extends RoleNamePermsObject {
@EntityID()
id?: string;
id: string;
}

View file

@ -3,7 +3,7 @@ import { EntityID } from '../validators/entity-id.validator';
export class ESysPreference {
@EntityID()
id?: string;
id: string;
@IsNotEmpty()
@IsString()

View file

@ -28,7 +28,7 @@ export class NameRolesUser extends UsernameUser {
// Actual entity that goes in the db
export class EUser extends NameRolesUser {
@EntityID()
id?: string;
id: string;
@IsOptional()
@Exclude()

View file

@ -4,7 +4,7 @@ import { IsPosInt } from '../validators/positive-int.validator';
export class EUsrPreference {
@EntityID()
id?: string;
id: string;
@IsNotEmpty()
@IsString()

View file

@ -1,5 +1,4 @@
import { IsNotEmpty, IsOptional, IsUUID } from 'class-validator';
import { IsNotEmpty, IsUUID } from 'class-validator';
import { CombinePDecorators } from '../util/decorator';
export const EntityID = CombinePDecorators(IsOptional(), IsUUID('4'));
export const EntityIDRequired = CombinePDecorators(IsNotEmpty(), IsUUID('4'));
export const EntityID = CombinePDecorators(IsNotEmpty(), IsUUID('4'));