mirror of
https://github.com/CaramelFur/Picsur.git
synced 2025-11-13 23:35:39 +01:00
move to required ids
This commit is contained in:
@@ -32,7 +32,7 @@ export class RolesModule implements OnModuleInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async nukeRoles() {
|
private async nukeRoles() {
|
||||||
this.logger.error('Nuking system roles');
|
this.logger.warn('Nuking system roles');
|
||||||
const result = await this.rolesService.nukeSystemRoles(true);
|
const result = await this.rolesService.nukeSystemRoles(true);
|
||||||
if (HasFailed(result)) {
|
if (HasFailed(result)) {
|
||||||
this.logger.error(`Failed to nuke roles because: ${result.getReason()}`);
|
this.logger.error(`Failed to nuke roles because: ${result.getReason()}`);
|
||||||
|
|||||||
@@ -43,10 +43,8 @@ export class RolesService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async delete(
|
public async delete(name: string): AsyncFailable<ERoleBackend> {
|
||||||
role: string | ERoleBackend,
|
const roleToModify = await this.findOne(name);
|
||||||
): AsyncFailable<ERoleBackend> {
|
|
||||||
const roleToModify = await this.resolve(role);
|
|
||||||
if (HasFailed(roleToModify)) return roleToModify;
|
if (HasFailed(roleToModify)) return roleToModify;
|
||||||
|
|
||||||
if (UndeletableRolesList.includes(roleToModify.name)) {
|
if (UndeletableRolesList.includes(roleToModify.name)) {
|
||||||
@@ -75,10 +73,10 @@ export class RolesService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async addPermissions(
|
public async addPermissions(
|
||||||
role: string | ERoleBackend,
|
name: string,
|
||||||
permissions: Permissions,
|
permissions: Permissions,
|
||||||
): AsyncFailable<ERoleBackend> {
|
): AsyncFailable<ERoleBackend> {
|
||||||
const roleToModify = await this.resolve(role);
|
const roleToModify = await this.findOne(name);
|
||||||
if (HasFailed(roleToModify)) return roleToModify;
|
if (HasFailed(roleToModify)) return roleToModify;
|
||||||
|
|
||||||
const newPermissions = makeUnique([
|
const newPermissions = makeUnique([
|
||||||
@@ -90,10 +88,10 @@ export class RolesService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async removePermissions(
|
public async removePermissions(
|
||||||
role: string | ERoleBackend,
|
name: string,
|
||||||
permissions: Permissions,
|
permissions: Permissions,
|
||||||
): AsyncFailable<ERoleBackend> {
|
): AsyncFailable<ERoleBackend> {
|
||||||
const roleToModify = await this.resolve(role);
|
const roleToModify = await this.findOne(name);
|
||||||
if (HasFailed(roleToModify)) return roleToModify;
|
if (HasFailed(roleToModify)) return roleToModify;
|
||||||
|
|
||||||
const newPermissions = roleToModify.permissions.filter(
|
const newPermissions = roleToModify.permissions.filter(
|
||||||
@@ -149,8 +147,8 @@ export class RolesService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async exists(username: string): Promise<boolean> {
|
public async exists(name: string): Promise<boolean> {
|
||||||
return HasSuccess(await this.findOne(username));
|
return HasSuccess(await this.findOne(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async nukeSystemRoles(IAmSure: boolean = false): AsyncFailable<true> {
|
public async nukeSystemRoles(IAmSure: boolean = false): AsyncFailable<true> {
|
||||||
@@ -168,18 +166,18 @@ export class RolesService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async resolve(
|
private async resolve(
|
||||||
user: string | ERoleBackend,
|
role: string | ERoleBackend,
|
||||||
): AsyncFailable<ERoleBackend> {
|
): AsyncFailable<ERoleBackend> {
|
||||||
if (typeof user === 'string') {
|
if (typeof role === 'string') {
|
||||||
return await this.findOne(user);
|
return await this.findOne(role);
|
||||||
} else {
|
} else {
|
||||||
user = plainToClass(ERoleBackend, user);
|
role = plainToClass(ERoleBackend, role);
|
||||||
const errors = await strictValidate(user);
|
const errors = await strictValidate(role);
|
||||||
if (errors.length > 0) {
|
if (errors.length > 0) {
|
||||||
this.logger.warn(errors);
|
this.logger.warn(errors);
|
||||||
return Fail('Invalid role');
|
return Fail('Invalid role');
|
||||||
}
|
}
|
||||||
return user;
|
return role;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,10 +14,7 @@ export class AuthManagerService {
|
|||||||
|
|
||||||
async createToken(user: EUserBackend): AsyncFailable<string> {
|
async createToken(user: EUserBackend): AsyncFailable<string> {
|
||||||
const jwtData: JwtDataDto = plainToClass(JwtDataDto, {
|
const jwtData: JwtDataDto = plainToClass(JwtDataDto, {
|
||||||
user: {
|
user,
|
||||||
username: user.username,
|
|
||||||
roles: user.roles,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Validate to be sure, this makes client experience better
|
// Validate to be sure, this makes client experience better
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm';
|
|||||||
@Entity()
|
@Entity()
|
||||||
export class EImageBackend extends EImage {
|
export class EImageBackend extends EImage {
|
||||||
@PrimaryGeneratedColumn("uuid")
|
@PrimaryGeneratedColumn("uuid")
|
||||||
override id?: string;
|
override id: string;
|
||||||
|
|
||||||
@Index()
|
@Index()
|
||||||
@Column({ unique: true, nullable: false })
|
@Column({ unique: true, nullable: false })
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { Permissions } from '../dto/permissions.dto';
|
|||||||
@Entity()
|
@Entity()
|
||||||
export class ERoleBackend extends ERole {
|
export class ERoleBackend extends ERole {
|
||||||
@PrimaryGeneratedColumn("uuid")
|
@PrimaryGeneratedColumn("uuid")
|
||||||
override id?: string;
|
override id: string;
|
||||||
|
|
||||||
@Index()
|
@Index()
|
||||||
@Column({ nullable: false, unique: true })
|
@Column({ nullable: false, unique: true })
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm';
|
|||||||
@Entity()
|
@Entity()
|
||||||
export class ESysPreferenceBackend extends ESysPreference {
|
export class ESysPreferenceBackend extends ESysPreference {
|
||||||
@PrimaryGeneratedColumn("uuid")
|
@PrimaryGeneratedColumn("uuid")
|
||||||
override id?: string;
|
override id: string;
|
||||||
|
|
||||||
@Index()
|
@Index()
|
||||||
@Column({ nullable: false, unique: true })
|
@Column({ nullable: false, unique: true })
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm';
|
|||||||
@Entity()
|
@Entity()
|
||||||
export class EUserBackend extends EUser {
|
export class EUserBackend extends EUser {
|
||||||
@PrimaryGeneratedColumn("uuid")
|
@PrimaryGeneratedColumn("uuid")
|
||||||
override id?: string;
|
override id: string;
|
||||||
|
|
||||||
@Index()
|
@Index()
|
||||||
@Column({ nullable: false, unique: true })
|
@Column({ nullable: false, unique: true })
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { Column, Index, PrimaryGeneratedColumn } from 'typeorm';
|
|||||||
|
|
||||||
export class EUsrPreferenceBackend extends EUsrPreference {
|
export class EUsrPreferenceBackend extends EUsrPreference {
|
||||||
@PrimaryGeneratedColumn("uuid")
|
@PrimaryGeneratedColumn("uuid")
|
||||||
override id?: string;
|
override id: string;
|
||||||
|
|
||||||
@Index()
|
@Index()
|
||||||
@Column({ nullable: false, unique: true })
|
@Column({ nullable: false, unique: true })
|
||||||
|
|||||||
@@ -55,8 +55,6 @@ export class SettingsRolesEditComponent implements OnInit {
|
|||||||
this.mode = EditMode.add;
|
this.mode = EditMode.add;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set data thats already known
|
|
||||||
this.mode = EditMode.edit;
|
this.mode = EditMode.edit;
|
||||||
this.model.putRoleName(rolename);
|
this.model.putRoleName(rolename);
|
||||||
|
|
||||||
|
|||||||
@@ -4,11 +4,10 @@ import { isSemVer } from 'class-validator';
|
|||||||
import { InfoResponse } from 'picsur-shared/dist/dto/api/info.dto';
|
import { InfoResponse } from 'picsur-shared/dist/dto/api/info.dto';
|
||||||
import {
|
import {
|
||||||
AsyncFailable,
|
AsyncFailable,
|
||||||
Fail,
|
Fail, HasFailed
|
||||||
Failable,
|
|
||||||
HasFailed
|
|
||||||
} from 'picsur-shared/dist/types';
|
} from 'picsur-shared/dist/types';
|
||||||
import { BehaviorSubject } from 'rxjs';
|
import { BehaviorSubject } from 'rxjs';
|
||||||
|
import { SnackBarType } from 'src/app/models/dto/snack-bar-type.dto';
|
||||||
import { UtilService } from 'src/app/util/util.service';
|
import { UtilService } from 'src/app/util/util.service';
|
||||||
import pkg from '../../../../package.json';
|
import pkg from '../../../../package.json';
|
||||||
import { ServerInfo } from '../../models/dto/server-info.dto';
|
import { ServerInfo } from '../../models/dto/server-info.dto';
|
||||||
@@ -28,12 +27,7 @@ export class InfoService {
|
|||||||
private infoSubject = new BehaviorSubject<ServerInfo>(new ServerInfo());
|
private infoSubject = new BehaviorSubject<ServerInfo>(new ServerInfo());
|
||||||
|
|
||||||
constructor(private api: ApiService, private utilService: UtilService) {
|
constructor(private api: ApiService, private utilService: UtilService) {
|
||||||
this.init().catch(this.logger.error);
|
this.checkCompatibility().catch(this.logger.error);
|
||||||
}
|
|
||||||
|
|
||||||
private async init() {
|
|
||||||
await this.pollInfo();
|
|
||||||
this.checkCompatibility();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async pollInfo(): AsyncFailable<ServerInfo> {
|
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 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
|
// If both versions start with something else, they have to match the first part
|
||||||
public isCompatibleWithServer(): Failable<boolean> {
|
public async isCompatibleWithServer(): AsyncFailable<boolean> {
|
||||||
const info = this.infoSubject.getValue();
|
const info = await this.pollInfo();
|
||||||
if (HasFailed(info)) return info;
|
if (HasFailed(info)) return info;
|
||||||
|
|
||||||
const serverVersion = info.version;
|
const serverVersion = info.version;
|
||||||
@@ -81,10 +75,18 @@ export class InfoService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private checkCompatibility(): void {
|
private async checkCompatibility() {
|
||||||
const isCompatible = this.isCompatibleWithServer();
|
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
|
this.utilService
|
||||||
.showDialog({
|
.showDialog({
|
||||||
title: 'Server is not compatible',
|
title: 'Server is not compatible',
|
||||||
|
|||||||
@@ -2,5 +2,5 @@ import { EntityID } from '../validators/entity-id.validator';
|
|||||||
|
|
||||||
export class EntityIDObject {
|
export class EntityIDObject {
|
||||||
@EntityID()
|
@EntityID()
|
||||||
id: number;
|
id: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import { Type } from 'class-transformer';
|
import { Type } from 'class-transformer';
|
||||||
import { IsDefined, IsInt, IsOptional, ValidateNested } from 'class-validator';
|
import { IsDefined, IsInt, IsOptional, ValidateNested } from 'class-validator';
|
||||||
import { NameRolesUser } from '../entities/user.entity';
|
import { EUser } from '../entities/user.entity';
|
||||||
|
|
||||||
export class JwtDataDto {
|
export class JwtDataDto {
|
||||||
@IsDefined()
|
@IsDefined()
|
||||||
@ValidateNested()
|
@ValidateNested()
|
||||||
@Type(() => NameRolesUser)
|
@Type(() => EUser)
|
||||||
user: NameRolesUser;
|
user: EUser;
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsInt()
|
@IsInt()
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { EntityID } from '../validators/entity-id.validator';
|
|||||||
|
|
||||||
export class EImage {
|
export class EImage {
|
||||||
@EntityID()
|
@EntityID()
|
||||||
id?: string;
|
id: string;
|
||||||
|
|
||||||
@IsHash('sha256')
|
@IsHash('sha256')
|
||||||
hash: string;
|
hash: string;
|
||||||
|
|||||||
@@ -19,5 +19,5 @@ export class RoleNamePermsObject extends RoleNameObject {
|
|||||||
|
|
||||||
export class ERole extends RoleNamePermsObject {
|
export class ERole extends RoleNamePermsObject {
|
||||||
@EntityID()
|
@EntityID()
|
||||||
id?: string;
|
id: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { EntityID } from '../validators/entity-id.validator';
|
|||||||
|
|
||||||
export class ESysPreference {
|
export class ESysPreference {
|
||||||
@EntityID()
|
@EntityID()
|
||||||
id?: string;
|
id: string;
|
||||||
|
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
@IsString()
|
@IsString()
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ export class NameRolesUser extends UsernameUser {
|
|||||||
// Actual entity that goes in the db
|
// Actual entity that goes in the db
|
||||||
export class EUser extends NameRolesUser {
|
export class EUser extends NameRolesUser {
|
||||||
@EntityID()
|
@EntityID()
|
||||||
id?: string;
|
id: string;
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@Exclude()
|
@Exclude()
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { IsPosInt } from '../validators/positive-int.validator';
|
|||||||
|
|
||||||
export class EUsrPreference {
|
export class EUsrPreference {
|
||||||
@EntityID()
|
@EntityID()
|
||||||
id?: string;
|
id: string;
|
||||||
|
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
@IsString()
|
@IsString()
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { IsNotEmpty, IsOptional, IsUUID } from 'class-validator';
|
import { IsNotEmpty, IsUUID } from 'class-validator';
|
||||||
import { CombinePDecorators } from '../util/decorator';
|
import { CombinePDecorators } from '../util/decorator';
|
||||||
|
|
||||||
export const EntityID = CombinePDecorators(IsOptional(), IsUUID('4'));
|
export const EntityID = CombinePDecorators(IsNotEmpty(), IsUUID('4'));
|
||||||
export const EntityIDRequired = CombinePDecorators(IsNotEmpty(), IsUUID('4'));
|
|
||||||
|
|||||||
Reference in New Issue
Block a user