mirror of
https://github.com/CaramelFur/Picsur.git
synced 2025-11-15 16:05:49 +01:00
part of frontend permission guard
This commit is contained in:
@@ -8,13 +8,12 @@ import {
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { plainToClass } from 'class-transformer';
|
||||
import { isArray, isEnum, isString, validate } from 'class-validator';
|
||||
import { validate } from 'class-validator';
|
||||
import {
|
||||
Permissions,
|
||||
PermissionsList
|
||||
Permissions
|
||||
} from 'picsur-shared/dist/dto/permissions';
|
||||
import { Roles } from 'picsur-shared/dist/dto/roles.dto';
|
||||
import { Fail, Failable, HasFailed } from 'picsur-shared/dist/types';
|
||||
import { isPermissionsArray } from 'picsur-shared/dist/util/permissions';
|
||||
import { UsersService } from '../../../collections/userdb/userdb.service';
|
||||
import { EUserBackend } from '../../../models/entities/user.entity';
|
||||
|
||||
@@ -42,13 +41,13 @@ export class MainAuthGuard extends AuthGuard(['jwt', 'guest']) {
|
||||
|
||||
const permissions = this.extractPermissions(context);
|
||||
if (HasFailed(permissions)) {
|
||||
this.logger.warn("222"+permissions.getReason());
|
||||
this.logger.warn('222' + permissions.getReason());
|
||||
throw new InternalServerErrorException();
|
||||
}
|
||||
|
||||
const userPermissions = await this.usersService.getPermissions(user);
|
||||
if (HasFailed(userPermissions)) {
|
||||
this.logger.warn("111"+userPermissions.getReason());
|
||||
this.logger.warn('111' + userPermissions.getReason());
|
||||
throw new InternalServerErrorException();
|
||||
}
|
||||
|
||||
@@ -69,21 +68,13 @@ export class MainAuthGuard extends AuthGuard(['jwt', 'guest']) {
|
||||
);
|
||||
}
|
||||
|
||||
if (!this.isPermissionsArray(permissions)) {
|
||||
if (!isPermissionsArray(permissions)) {
|
||||
return Fail(`Permissions for ${handlerName} is not a string array`);
|
||||
}
|
||||
|
||||
return permissions;
|
||||
}
|
||||
|
||||
private isPermissionsArray(value: any): value is Roles {
|
||||
if (!isArray(value)) return false;
|
||||
if (!value.every((item: unknown) => isString(item))) return false;
|
||||
if (!value.every((item: string) => isEnum(item, PermissionsList)))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private async validateUser(user: EUserBackend): Promise<EUserBackend> {
|
||||
const userClass = plainToClass(EUserBackend, user);
|
||||
const errors = await validate(userClass, {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { Permission } from 'picsur-shared/dist/dto/permissions';
|
||||
import { ImageDBService } from '../../collections/imagedb/imagedb.service';
|
||||
import { RolesService } from '../../collections/roledb/roledb.service';
|
||||
|
||||
@@ -15,7 +16,7 @@ export class DemoManagerService {
|
||||
this.logger.warn(
|
||||
'Modifying roles for demo mode, this will not be reverted automatically',
|
||||
);
|
||||
this.rolesService.addPermissions('guest', ['image-upload']);
|
||||
this.rolesService.addPermissions('guest', [Permission.ImageUpload]);
|
||||
}
|
||||
|
||||
public execute() {
|
||||
|
||||
Reference in New Issue
Block a user