mirror of
https://github.com/CaramelFur/Picsur.git
synced 2025-11-13 07:15:39 +01:00
delete userrolesservice
This commit is contained in:
@@ -7,7 +7,6 @@ import { EarlyConfigModule } from '../../config/early/earlyconfig.module';
|
|||||||
import { EUserBackend } from '../../models/entities/user.entity';
|
import { EUserBackend } from '../../models/entities/user.entity';
|
||||||
import { RolesModule } from '../roledb/roledb.module';
|
import { RolesModule } from '../roledb/roledb.module';
|
||||||
import { UsersService } from './userdb.service';
|
import { UsersService } from './userdb.service';
|
||||||
import { UserRolesService } from './userrolesdb.service';
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@@ -15,8 +14,8 @@ import { UserRolesService } from './userrolesdb.service';
|
|||||||
RolesModule,
|
RolesModule,
|
||||||
TypeOrmModule.forFeature([EUserBackend]),
|
TypeOrmModule.forFeature([EUserBackend]),
|
||||||
],
|
],
|
||||||
providers: [UsersService, UserRolesService],
|
providers: [UsersService],
|
||||||
exports: [UsersService, UserRolesService],
|
exports: [UsersService],
|
||||||
})
|
})
|
||||||
export class UsersModule implements OnModuleInit {
|
export class UsersModule implements OnModuleInit {
|
||||||
private readonly logger = new Logger('UsersModule');
|
private readonly logger = new Logger('UsersModule');
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import {
|
|||||||
import { makeUnique } from 'picsur-shared/dist/util/unique';
|
import { makeUnique } from 'picsur-shared/dist/util/unique';
|
||||||
import { strictValidate } from 'picsur-shared/dist/util/validate';
|
import { strictValidate } from 'picsur-shared/dist/util/validate';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
|
import { Permissions } from '../../models/dto/permissions.dto';
|
||||||
import {
|
import {
|
||||||
DefaultRolesList,
|
DefaultRolesList,
|
||||||
SoulBoundRolesList
|
SoulBoundRolesList
|
||||||
@@ -22,6 +23,7 @@ import {
|
|||||||
} from '../../models/dto/specialusers.dto';
|
} from '../../models/dto/specialusers.dto';
|
||||||
import { EUserBackend } from '../../models/entities/user.entity';
|
import { EUserBackend } from '../../models/entities/user.entity';
|
||||||
import { GetCols } from '../../models/util/collection';
|
import { GetCols } from '../../models/util/collection';
|
||||||
|
import { RolesService } from '../roledb/roledb.service';
|
||||||
|
|
||||||
// TODO: make this a configurable value
|
// TODO: make this a configurable value
|
||||||
const BCryptStrength = 12;
|
const BCryptStrength = 12;
|
||||||
@@ -33,6 +35,7 @@ export class UsersService {
|
|||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(EUserBackend)
|
@InjectRepository(EUserBackend)
|
||||||
private usersRepository: Repository<EUserBackend>,
|
private usersRepository: Repository<EUserBackend>,
|
||||||
|
private rolesService: RolesService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
// Creation and deletion
|
// Creation and deletion
|
||||||
@@ -116,6 +119,33 @@ export class UsersService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async removeRoleEveryone(role: string): AsyncFailable<true> {
|
||||||
|
try {
|
||||||
|
await this.usersRepository
|
||||||
|
.createQueryBuilder('user')
|
||||||
|
.update()
|
||||||
|
.set({
|
||||||
|
roles: () => 'ARRAY_REMOVE(roles, :role)',
|
||||||
|
})
|
||||||
|
.where('roles @> ARRAY[:role]', { role })
|
||||||
|
.execute();
|
||||||
|
} catch (e) {
|
||||||
|
this.logger.error(e);
|
||||||
|
return Fail("Couldn't remove role from everyone");
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getPermissions(
|
||||||
|
user: string | EUserBackend,
|
||||||
|
): AsyncFailable<Permissions> {
|
||||||
|
const userToModify = await this.resolve(user);
|
||||||
|
if (HasFailed(userToModify)) return userToModify;
|
||||||
|
|
||||||
|
return await this.rolesService.getPermissions(userToModify.roles);
|
||||||
|
}
|
||||||
|
|
||||||
public async updatePassword(
|
public async updatePassword(
|
||||||
user: string | EUserBackend,
|
user: string | EUserBackend,
|
||||||
password: string,
|
password: string,
|
||||||
@@ -204,7 +234,7 @@ export class UsersService {
|
|||||||
|
|
||||||
// Internal resolver
|
// Internal resolver
|
||||||
|
|
||||||
public async resolve(
|
private async resolve(
|
||||||
user: string | EUserBackend,
|
user: string | EUserBackend,
|
||||||
): AsyncFailable<EUserBackend> {
|
): AsyncFailable<EUserBackend> {
|
||||||
if (typeof user === 'string') {
|
if (typeof user === 'string') {
|
||||||
|
|||||||
@@ -1,76 +0,0 @@
|
|||||||
import { Injectable, Logger } from '@nestjs/common';
|
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
|
||||||
import { AsyncFailable, Fail, HasFailed } from 'picsur-shared/dist/types';
|
|
||||||
import { makeUnique } from 'picsur-shared/dist/util/unique';
|
|
||||||
import { Repository } from 'typeorm';
|
|
||||||
import { Permissions } from '../../models/dto/permissions.dto';
|
|
||||||
import { EUserBackend } from '../../models/entities/user.entity';
|
|
||||||
import { RolesService } from '../roledb/roledb.service';
|
|
||||||
import { UsersService } from './userdb.service';
|
|
||||||
|
|
||||||
// Move some code here so it doesnt make the userdb service gigantic
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class UserRolesService {
|
|
||||||
private readonly logger = new Logger('UserRolesService');
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
@InjectRepository(EUserBackend)
|
|
||||||
private usersRepository: Repository<EUserBackend>,
|
|
||||||
|
|
||||||
private usersService: UsersService,
|
|
||||||
private rolesService: RolesService,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
// Permissions and roles
|
|
||||||
public async getPermissions(
|
|
||||||
user: string | EUserBackend,
|
|
||||||
): AsyncFailable<Permissions> {
|
|
||||||
const userToModify = await this.usersService.resolve(user);
|
|
||||||
if (HasFailed(userToModify)) return userToModify;
|
|
||||||
|
|
||||||
return await this.rolesService.getPermissions(userToModify.roles);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async addRoles(
|
|
||||||
user: string | EUserBackend,
|
|
||||||
roles: string[],
|
|
||||||
): AsyncFailable<EUserBackend> {
|
|
||||||
const userToModify = await this.usersService.resolve(user);
|
|
||||||
if (HasFailed(userToModify)) return userToModify;
|
|
||||||
|
|
||||||
const newRoles = makeUnique([...userToModify.roles, ...roles]);
|
|
||||||
|
|
||||||
return this.usersService.setRoles(userToModify, newRoles);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async removeRoles(
|
|
||||||
user: string | EUserBackend,
|
|
||||||
roles: string[],
|
|
||||||
): AsyncFailable<EUserBackend> {
|
|
||||||
const userToModify = await this.usersService.resolve(user);
|
|
||||||
if (HasFailed(userToModify)) return userToModify;
|
|
||||||
|
|
||||||
const newRoles = userToModify.roles.filter((role) => !roles.includes(role));
|
|
||||||
|
|
||||||
return this.usersService.setRoles(userToModify, newRoles);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async removeRoleEveryone(role: string): AsyncFailable<true> {
|
|
||||||
try {
|
|
||||||
await this.usersRepository
|
|
||||||
.createQueryBuilder('user')
|
|
||||||
.update()
|
|
||||||
.set({
|
|
||||||
roles: () => 'ARRAY_REMOVE(roles, :role)',
|
|
||||||
})
|
|
||||||
.where('roles @> ARRAY[:role]', { role })
|
|
||||||
.execute();
|
|
||||||
} catch (e) {
|
|
||||||
this.logger.error(e);
|
|
||||||
return Fail("Couldn't remove role from everyone");
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,7 +7,7 @@ import {
|
|||||||
import * as multipart from 'fastify-multipart';
|
import * as multipart from 'fastify-multipart';
|
||||||
import { ValidateOptions } from 'picsur-shared/dist/util/validate';
|
import { ValidateOptions } from 'picsur-shared/dist/util/validate';
|
||||||
import { AppModule } from './app.module';
|
import { AppModule } from './app.module';
|
||||||
import { UserRolesService } from './collections/userdb/userrolesdb.service';
|
import { UsersService } from './collections/userdb/userdb.service';
|
||||||
import { HostConfigService } from './config/early/host.config.service';
|
import { HostConfigService } from './config/early/host.config.service';
|
||||||
import { MainExceptionFilter } from './layers/httpexception/httpexception.filter';
|
import { MainExceptionFilter } from './layers/httpexception/httpexception.filter';
|
||||||
import { SuccessInterceptor } from './layers/success/success.interceptor';
|
import { SuccessInterceptor } from './layers/success/success.interceptor';
|
||||||
@@ -36,7 +36,7 @@ async function bootstrap() {
|
|||||||
app.useGlobalGuards(
|
app.useGlobalGuards(
|
||||||
new MainAuthGuard(
|
new MainAuthGuard(
|
||||||
app.get(Reflector),
|
app.get(Reflector),
|
||||||
app.get(UserRolesService),
|
app.get(UsersService),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { AuthGuard } from '@nestjs/passport';
|
|||||||
import { plainToClass } from 'class-transformer';
|
import { plainToClass } from 'class-transformer';
|
||||||
import { Fail, Failable, HasFailed } from 'picsur-shared/dist/types';
|
import { Fail, Failable, HasFailed } from 'picsur-shared/dist/types';
|
||||||
import { strictValidate } from 'picsur-shared/dist/util/validate';
|
import { strictValidate } from 'picsur-shared/dist/util/validate';
|
||||||
import { UserRolesService } from '../../../collections/userdb/userrolesdb.service';
|
import { UsersService } from '../../../collections/userdb/userdb.service';
|
||||||
import { Permissions } from '../../../models/dto/permissions.dto';
|
import { Permissions } from '../../../models/dto/permissions.dto';
|
||||||
import { EUserBackend } from '../../../models/entities/user.entity';
|
import { EUserBackend } from '../../../models/entities/user.entity';
|
||||||
import { isPermissionsArray } from '../../../models/validators/permissions.validator';
|
import { isPermissionsArray } from '../../../models/validators/permissions.validator';
|
||||||
@@ -25,7 +25,7 @@ export class MainAuthGuard extends AuthGuard(['jwt', 'guest']) {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private reflector: Reflector,
|
private reflector: Reflector,
|
||||||
private userRolesService: UserRolesService,
|
private usersService: UsersService,
|
||||||
) {
|
) {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
@@ -50,7 +50,7 @@ export class MainAuthGuard extends AuthGuard(['jwt', 'guest']) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// These are the permissions the user has
|
// These are the permissions the user has
|
||||||
const userPermissions = await this.userRolesService.getPermissions(user);
|
const userPermissions = await this.usersService.getPermissions(user);
|
||||||
if (HasFailed(userPermissions)) {
|
if (HasFailed(userPermissions)) {
|
||||||
this.logger.warn('User Permissions: ' + userPermissions.getReason());
|
this.logger.warn('User Permissions: ' + userPermissions.getReason());
|
||||||
throw new InternalServerErrorException();
|
throw new InternalServerErrorException();
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import {
|
|||||||
} from 'picsur-shared/dist/dto/api/roles.dto';
|
} from 'picsur-shared/dist/dto/api/roles.dto';
|
||||||
import { HasFailed } from 'picsur-shared/dist/types';
|
import { HasFailed } from 'picsur-shared/dist/types';
|
||||||
import { RolesService } from '../../../collections/roledb/roledb.service';
|
import { RolesService } from '../../../collections/roledb/roledb.service';
|
||||||
import { UserRolesService } from '../../../collections/userdb/userrolesdb.service';
|
import { UsersService } from '../../../collections/userdb/userdb.service';
|
||||||
import { RequiredPermissions } from '../../../decorators/permissions.decorator';
|
import { RequiredPermissions } from '../../../decorators/permissions.decorator';
|
||||||
import { Permission } from '../../../models/dto/permissions.dto';
|
import { Permission } from '../../../models/dto/permissions.dto';
|
||||||
import {
|
import {
|
||||||
@@ -38,7 +38,7 @@ export class RolesController {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private rolesService: RolesService,
|
private rolesService: RolesService,
|
||||||
private userRolesService: UserRolesService,
|
private usersService: UsersService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@Get('list')
|
@Get('list')
|
||||||
@@ -115,7 +115,7 @@ export class RolesController {
|
|||||||
throw new InternalServerErrorException('Could not delete role');
|
throw new InternalServerErrorException('Could not delete role');
|
||||||
}
|
}
|
||||||
|
|
||||||
const success = await this.userRolesService.removeRoleEveryone(role.name);
|
const success = await this.usersService.removeRoleEveryone(role.name);
|
||||||
if (HasFailed(success)) {
|
if (HasFailed(success)) {
|
||||||
throw new InternalServerErrorException(
|
throw new InternalServerErrorException(
|
||||||
'Could not remove role from users',
|
'Could not remove role from users',
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ import {
|
|||||||
} from 'picsur-shared/dist/dto/api/user.dto';
|
} from 'picsur-shared/dist/dto/api/user.dto';
|
||||||
import { HasFailed } from 'picsur-shared/dist/types';
|
import { HasFailed } from 'picsur-shared/dist/types';
|
||||||
import { UsersService } from '../../../collections/userdb/userdb.service';
|
import { UsersService } from '../../../collections/userdb/userdb.service';
|
||||||
import { UserRolesService } from '../../../collections/userdb/userrolesdb.service';
|
|
||||||
import {
|
import {
|
||||||
NoPermissions,
|
NoPermissions,
|
||||||
RequiredPermissions,
|
RequiredPermissions,
|
||||||
@@ -32,7 +31,6 @@ export class UserController {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private usersService: UsersService,
|
private usersService: UsersService,
|
||||||
private userRolesSerivce: UserRolesService,
|
|
||||||
private authService: AuthManagerService,
|
private authService: AuthManagerService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@@ -90,7 +88,7 @@ export class UserController {
|
|||||||
async refresh(
|
async refresh(
|
||||||
@Request() req: AuthFasityRequest,
|
@Request() req: AuthFasityRequest,
|
||||||
): Promise<UserMePermissionsResponse> {
|
): Promise<UserMePermissionsResponse> {
|
||||||
const permissions = await this.userRolesSerivce.getPermissions(req.user);
|
const permissions = await this.usersService.getPermissions(req.user);
|
||||||
if (HasFailed(permissions)) {
|
if (HasFailed(permissions)) {
|
||||||
this.logger.warn(permissions.getReason());
|
this.logger.warn(permissions.getReason());
|
||||||
throw new InternalServerErrorException('Could not get permissions');
|
throw new InternalServerErrorException('Could not get permissions');
|
||||||
|
|||||||
Reference in New Issue
Block a user