format + lint
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
@Entity()
|
||||
export class Permission {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column({ unique: true })
|
||||
name: string;
|
||||
@Column({ unique: true })
|
||||
name: string;
|
||||
|
||||
@Column()
|
||||
description: string;
|
||||
}
|
||||
@Column()
|
||||
description: string;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Entity, Index, PrimaryColumn } from "typeorm";
|
||||
import { Entity, Index, PrimaryColumn } from 'typeorm';
|
||||
|
||||
@Entity()
|
||||
@Index(['roleId', 'permissionId'], { unique: true })
|
||||
export class RolePermission {
|
||||
@PrimaryColumn('uuid')
|
||||
roleId: string;
|
||||
@PrimaryColumn('uuid')
|
||||
roleId: string;
|
||||
|
||||
@PrimaryColumn('uuid')
|
||||
permissionId: string;
|
||||
}
|
||||
@PrimaryColumn('uuid')
|
||||
permissionId: string;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
@Entity()
|
||||
export class Role {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column({ unique: true })
|
||||
name: string;
|
||||
@Column({ unique: true })
|
||||
name: string;
|
||||
|
||||
@Column()
|
||||
localName: string;
|
||||
}
|
||||
@Column()
|
||||
localName: string;
|
||||
}
|
||||
|
||||
@@ -1,23 +1,29 @@
|
||||
import { Column, CreateDateColumn, Entity, Index, PrimaryGeneratedColumn } from "typeorm";
|
||||
import {
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
Entity,
|
||||
Index,
|
||||
PrimaryGeneratedColumn,
|
||||
} from 'typeorm';
|
||||
|
||||
@Entity()
|
||||
@Index(['userId', 'roleId'])
|
||||
export class UserRole {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column('uuid')
|
||||
roleId: string;
|
||||
@Column('uuid')
|
||||
roleId: string;
|
||||
|
||||
@Column('uuid')
|
||||
userId: string
|
||||
@Column('uuid')
|
||||
userId: string;
|
||||
|
||||
@Column()
|
||||
isEnabled: boolean;
|
||||
@Column()
|
||||
isEnabled: boolean;
|
||||
|
||||
@CreateDateColumn({ precision: 3 })
|
||||
createdAt: Date;
|
||||
@CreateDateColumn({ precision: 3 })
|
||||
createdAt: Date;
|
||||
|
||||
@Column({ nullable: true, precision: 3 })
|
||||
expiredAt?: Date;
|
||||
}
|
||||
@Column({ nullable: true, precision: 3 })
|
||||
expiredAt?: Date;
|
||||
}
|
||||
|
||||
@@ -10,8 +10,20 @@ import { UserRole } from './entities/user-role.entity';
|
||||
import { PermissionService } from './services/permission.service';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Role, Permission, RolePermission, UserRole])],
|
||||
providers: [RolePermissionService, RoleService, UserRoleService, PermissionService],
|
||||
exports: [RolePermissionService, RoleService, UserRoleService, PermissionService],
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([Role, Permission, RolePermission, UserRole]),
|
||||
],
|
||||
providers: [
|
||||
RolePermissionService,
|
||||
RoleService,
|
||||
UserRoleService,
|
||||
PermissionService,
|
||||
],
|
||||
exports: [
|
||||
RolePermissionService,
|
||||
RoleService,
|
||||
UserRoleService,
|
||||
PermissionService,
|
||||
],
|
||||
})
|
||||
export class RoleModule { }
|
||||
export class RoleModule {}
|
||||
|
||||
@@ -1,51 +1,59 @@
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Permission } from "../entities/permission.entity";
|
||||
import { In, Repository } from "typeorm";
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Permission } from '../entities/permission.entity';
|
||||
import { In, Repository } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class PermissionService {
|
||||
constructor(
|
||||
@InjectRepository(Permission)
|
||||
private readonly permissionRepository: Repository<Permission>,
|
||||
) {}
|
||||
|
||||
constructor(
|
||||
@InjectRepository(Permission)
|
||||
private readonly permissionRepository: Repository<Permission>,
|
||||
) { }
|
||||
async findPermissionNamesByPermissionIds(
|
||||
permissionIds: string[],
|
||||
): Promise<string[]> {
|
||||
const permissions =
|
||||
await this.findPermissionsByPermissionIds(permissionIds);
|
||||
return permissions.map((permission) => permission.name);
|
||||
}
|
||||
|
||||
async findPermissionNamesByPermissionIds(permissionIds: string[]): Promise<string[]> {
|
||||
const permissions = await this.findPermissionsByPermissionIds(permissionIds);
|
||||
return permissions.map(permission => permission.name);
|
||||
async findPermissionsByPermissionIds(
|
||||
permissionIds: string[],
|
||||
): Promise<Permission[]> {
|
||||
return this.permissionRepository.find({
|
||||
where: {
|
||||
id: In(permissionIds),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findPermissionByIds(permissionIds: string[]): Promise<Permission[]> {
|
||||
return this.permissionRepository.find({
|
||||
where: {
|
||||
id: In(permissionIds),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async list() {
|
||||
return this.permissionRepository.find();
|
||||
}
|
||||
|
||||
async create(
|
||||
permission: Pick<Permission, 'name' | 'description'>,
|
||||
): Promise<Permission> {
|
||||
const newPermission = this.permissionRepository.create(permission);
|
||||
return this.permissionRepository.save(newPermission);
|
||||
}
|
||||
|
||||
async delete(permissionId: string): Promise<void> {
|
||||
const existingPermission = await this.permissionRepository.findOne({
|
||||
where: { id: permissionId },
|
||||
});
|
||||
if (!existingPermission) {
|
||||
throw new BadRequestException('Permission not found');
|
||||
}
|
||||
|
||||
async findPermissionsByPermissionIds(permissionIds: string[]): Promise<Permission[]> {
|
||||
return this.permissionRepository.find({
|
||||
where: {
|
||||
id: In(permissionIds),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async findPermissionByIds(permissionIds: string[]): Promise<Permission[]> {
|
||||
return this.permissionRepository.find({
|
||||
where: {
|
||||
id: In(permissionIds),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async list() {
|
||||
return this.permissionRepository.find();
|
||||
}
|
||||
|
||||
async create(permission: Pick<Permission, 'name' | 'description'>): Promise<Permission> {
|
||||
const newPermission = this.permissionRepository.create(permission);
|
||||
return this.permissionRepository.save(newPermission);
|
||||
}
|
||||
|
||||
async delete(permissionId: string): Promise<void> {
|
||||
const existingPermission = await this.permissionRepository.findOne({ where: { id: permissionId } });
|
||||
if (!existingPermission) {
|
||||
throw new BadRequestException('Permission not found');
|
||||
}
|
||||
await this.permissionRepository.delete(existingPermission.id);
|
||||
}
|
||||
}
|
||||
await this.permissionRepository.delete(existingPermission.id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,42 +1,47 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { RolePermission } from "../entities/role-permission.entity";
|
||||
import { In, Repository } from "typeorm";
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { RolePermission } from '../entities/role-permission.entity';
|
||||
import { In, Repository } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class RolePermissionService {
|
||||
constructor(
|
||||
@InjectRepository(RolePermission)
|
||||
private readonly rolePermissionRepository: Repository<RolePermission>,
|
||||
) {}
|
||||
|
||||
constructor(
|
||||
@InjectRepository(RolePermission)
|
||||
private readonly rolePermissionRepository: Repository<RolePermission>,
|
||||
) { }
|
||||
async findPermissionIdsByRoleIds(roleIds: string[]): Promise<string[]> {
|
||||
const rolePermissions = await this.rolePermissionRepository.find({
|
||||
where: {
|
||||
roleId: In(roleIds),
|
||||
},
|
||||
});
|
||||
|
||||
async findPermissionIdsByRoleIds(roleIds: string[]): Promise<string[]> {
|
||||
const rolePermissions = await this.rolePermissionRepository.find({
|
||||
where: {
|
||||
roleId: In(roleIds),
|
||||
}
|
||||
});
|
||||
return rolePermissions.map((rp) => rp.permissionId);
|
||||
}
|
||||
|
||||
return rolePermissions.map(rp => rp.permissionId);
|
||||
}
|
||||
async addRolePermissions(
|
||||
roleId: string,
|
||||
permissionIds: string[],
|
||||
): Promise<void> {
|
||||
const rolePermissions = permissionIds.map((permissionId) => {
|
||||
const rolePermission = this.rolePermissionRepository.create({
|
||||
roleId,
|
||||
permissionId,
|
||||
});
|
||||
return rolePermission;
|
||||
});
|
||||
|
||||
async addRolePermissions(roleId: string, permissionIds: string[]): Promise<void> {
|
||||
const rolePermissions = permissionIds.map(permissionId => {
|
||||
const rolePermission = this.rolePermissionRepository.create({
|
||||
roleId,
|
||||
permissionId,
|
||||
});
|
||||
return rolePermission;
|
||||
});
|
||||
await this.rolePermissionRepository.save(rolePermissions);
|
||||
}
|
||||
|
||||
await this.rolePermissionRepository.save(rolePermissions);
|
||||
}
|
||||
|
||||
async deleteRolePermissions(roleId: string, permissionIds: string[]): Promise<void> {
|
||||
await this.rolePermissionRepository.delete({
|
||||
roleId,
|
||||
permissionId: In(permissionIds),
|
||||
});
|
||||
}
|
||||
}
|
||||
async deleteRolePermissions(
|
||||
roleId: string,
|
||||
permissionIds: string[],
|
||||
): Promise<void> {
|
||||
await this.rolePermissionRepository.delete({
|
||||
roleId,
|
||||
permissionId: In(permissionIds),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,43 +1,44 @@
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Role } from "../entities/role.entity";
|
||||
import { In, Repository } from "typeorm";
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Role } from '../entities/role.entity';
|
||||
import { In, Repository } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class RoleService {
|
||||
constructor(
|
||||
@InjectRepository(Role)
|
||||
private readonly roleRepository: Repository<Role>,
|
||||
) {}
|
||||
|
||||
constructor(
|
||||
@InjectRepository(Role)
|
||||
private readonly roleRepository: Repository<Role>,
|
||||
) { }
|
||||
async findRoleNamesByRoleIds(roleIds: string[]): Promise<string[]> {
|
||||
const roles = await this.findRolesByRoleIds(roleIds);
|
||||
return roles.map((role) => role.name);
|
||||
}
|
||||
|
||||
async findRoleNamesByRoleIds(roleIds: string[]): Promise<string[]> {
|
||||
const roles = await this.findRolesByRoleIds(roleIds);
|
||||
return roles.map(role => role.name);
|
||||
async findRolesByRoleIds(roleIds: string[]): Promise<Role[]> {
|
||||
return this.roleRepository.find({
|
||||
where: {
|
||||
id: In(roleIds),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async create(role: Pick<Role, 'name' | 'localName'>): Promise<Role> {
|
||||
const newRole = this.roleRepository.create(role);
|
||||
return this.roleRepository.save(newRole);
|
||||
}
|
||||
|
||||
async list(): Promise<Role[]> {
|
||||
return this.roleRepository.find();
|
||||
}
|
||||
|
||||
async delete(roleId: string): Promise<void> {
|
||||
const existingRole = await this.roleRepository.findOne({
|
||||
where: { id: roleId },
|
||||
});
|
||||
if (!existingRole) {
|
||||
throw new BadRequestException('Role not found');
|
||||
}
|
||||
|
||||
async findRolesByRoleIds(roleIds: string[]): Promise<Role[]> {
|
||||
return this.roleRepository.find({
|
||||
where: {
|
||||
id: In(roleIds),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async create(role: Pick<Role, 'name' | 'localName'>): Promise<Role> {
|
||||
const newRole = this.roleRepository.create(role);
|
||||
return this.roleRepository.save(newRole);
|
||||
}
|
||||
|
||||
async list(): Promise<Role[]> {
|
||||
return this.roleRepository.find();
|
||||
}
|
||||
|
||||
async delete(roleId: string): Promise<void> {
|
||||
const existingRole = await this.roleRepository.findOne({ where: { id: roleId } });
|
||||
if (!existingRole) {
|
||||
throw new BadRequestException('Role not found');
|
||||
}
|
||||
await this.roleRepository.delete(existingRole.id);
|
||||
}
|
||||
}
|
||||
await this.roleRepository.delete(existingRole.id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,57 +1,59 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { UserRole } from "src/role/entities/user-role.entity";
|
||||
import { IsNull, MoreThanOrEqual, Repository } from "typeorm";
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { UserRole } from 'src/role/entities/user-role.entity';
|
||||
import { IsNull, MoreThanOrEqual, Repository } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class UserRoleService {
|
||||
constructor(
|
||||
@InjectRepository(UserRole)
|
||||
private readonly userRoleRepository: Repository<UserRole>,
|
||||
) { }
|
||||
constructor(
|
||||
@InjectRepository(UserRole)
|
||||
private readonly userRoleRepository: Repository<UserRole>,
|
||||
) {}
|
||||
|
||||
async findRoleIdsByUserId(userId: string): Promise<string[]> {
|
||||
const userRoles = await this.userRoleRepository.find({
|
||||
where: {
|
||||
userId,
|
||||
}
|
||||
});
|
||||
async findRoleIdsByUserId(userId: string): Promise<string[]> {
|
||||
const userRoles = await this.userRoleRepository.find({
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
});
|
||||
|
||||
return userRoles.map(ur => ur.roleId);
|
||||
}
|
||||
return userRoles.map((ur) => ur.roleId);
|
||||
}
|
||||
|
||||
async findValidRoleIdsByUserId(userId: string): Promise<string[]> {
|
||||
return (await this.findValidRolesByUserId(userId)).map(ur => ur.roleId);
|
||||
}
|
||||
async findValidRoleIdsByUserId(userId: string): Promise<string[]> {
|
||||
return (await this.findValidRolesByUserId(userId)).map((ur) => ur.roleId);
|
||||
}
|
||||
|
||||
async findValidRolesByUserId(userId: string) {
|
||||
const now = new Date();
|
||||
async findValidRolesByUserId(userId: string) {
|
||||
const now = new Date();
|
||||
|
||||
return this.userRoleRepository.find({
|
||||
where: [
|
||||
{
|
||||
userId,
|
||||
isEnabled: true,
|
||||
expiredAt: MoreThanOrEqual(now),
|
||||
},
|
||||
{
|
||||
userId,
|
||||
isEnabled: true,
|
||||
expiredAt: IsNull(),
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
return this.userRoleRepository.find({
|
||||
where: [
|
||||
{
|
||||
userId,
|
||||
isEnabled: true,
|
||||
expiredAt: MoreThanOrEqual(now),
|
||||
},
|
||||
{
|
||||
userId,
|
||||
isEnabled: true,
|
||||
expiredAt: IsNull(),
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
async addUserRole(userRole: Pick<UserRole, 'roleId' | 'userId' | 'isEnabled' | 'expiredAt'>): Promise<void> {
|
||||
const newUserRole = this.userRoleRepository.create(userRole);
|
||||
await this.userRoleRepository.save(newUserRole);
|
||||
}
|
||||
async addUserRole(
|
||||
userRole: Pick<UserRole, 'roleId' | 'userId' | 'isEnabled' | 'expiredAt'>,
|
||||
): Promise<void> {
|
||||
const newUserRole = this.userRoleRepository.create(userRole);
|
||||
await this.userRoleRepository.save(newUserRole);
|
||||
}
|
||||
|
||||
async deleteUserRole(userId: string, roleId: string): Promise<void> {
|
||||
await this.userRoleRepository.delete({
|
||||
userId,
|
||||
roleId,
|
||||
});
|
||||
}
|
||||
}
|
||||
async deleteUserRole(userId: string, roleId: string): Promise<void> {
|
||||
await this.userRoleRepository.delete({
|
||||
userId,
|
||||
roleId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user