mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-24 21:37:54 +00:00
Merge pull request #35 from SyncrowIOT/SP-201-be-handle-roles-new
Sp 201 be handle roles new
This commit is contained in:
@@ -4,4 +4,5 @@ export class AuthInterface {
|
||||
uuid: string;
|
||||
sessionId: string;
|
||||
id: number;
|
||||
roles?: string[];
|
||||
}
|
||||
|
||||
@@ -22,7 +22,9 @@ export class AuthService {
|
||||
where: {
|
||||
email,
|
||||
},
|
||||
relations: ['roles.roleType'],
|
||||
});
|
||||
|
||||
if (!user.isUserVerified) {
|
||||
throw new BadRequestException('User is not verified');
|
||||
}
|
||||
@@ -68,7 +70,9 @@ export class AuthService {
|
||||
uuid: user.uuid,
|
||||
type: user.type,
|
||||
sessionId: user.sessionId,
|
||||
roles: user?.roles,
|
||||
};
|
||||
|
||||
const tokens = await this.getTokens(payload);
|
||||
await this.updateRefreshToken(user.uuid, tokens.refreshToken);
|
||||
return tokens;
|
||||
|
||||
@@ -28,9 +28,10 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
|
||||
if (validateUser) {
|
||||
return {
|
||||
email: payload.email,
|
||||
userId: payload.id,
|
||||
userUuid: payload.uuid,
|
||||
uuid: payload.uuid,
|
||||
sessionId: payload.sessionId,
|
||||
roles: payload?.roles,
|
||||
};
|
||||
} else {
|
||||
throw new BadRequestException('Unauthorized');
|
||||
|
||||
@@ -31,9 +31,10 @@ export class RefreshTokenStrategy extends PassportStrategy(
|
||||
if (validateUser) {
|
||||
return {
|
||||
email: payload.email,
|
||||
userId: payload.id,
|
||||
userUuid: payload.uuid,
|
||||
uuid: payload.uuid,
|
||||
sessionId: payload.sessionId,
|
||||
roles: payload?.roles,
|
||||
};
|
||||
} else {
|
||||
throw new BadRequestException('Unauthorized');
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
import emailConfig from './email.config';
|
||||
export default [emailConfig];
|
||||
import superAdminConfig from './super.admin.config';
|
||||
export default [emailConfig, superAdminConfig];
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import { registerAs } from '@nestjs/config';
|
||||
|
||||
export default registerAs(
|
||||
'super-admin',
|
||||
(): Record<string, any> => ({
|
||||
SUPER_ADMIN_EMAIL: process.env.SUPER_ADMIN_EMAIL,
|
||||
SUPER_ADMIN_PASSWORD: process.env.SUPER_ADMIN_PASSWORD,
|
||||
}),
|
||||
);
|
||||
@@ -0,0 +1,4 @@
|
||||
export enum RoleType {
|
||||
SUPER_ADMIN = 'SUPER_ADMIN',
|
||||
ADMIN = 'ADMIN',
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
export enum SpaceType {
|
||||
COMMUNITY = 'community',
|
||||
BUILDING = 'building',
|
||||
FLOOR = 'floor',
|
||||
UNIT = 'unit',
|
||||
ROOM = 'room',
|
||||
}
|
||||
@@ -14,6 +14,8 @@ import { UserSpaceEntity } from '../modules/user-space/entities';
|
||||
import { GroupEntity } from '../modules/group/entities';
|
||||
import { GroupDeviceEntity } from '../modules/group-device/entities';
|
||||
import { DeviceUserPermissionEntity } from '../modules/device-user-permission/entities';
|
||||
import { UserRoleEntity } from '../modules/user-role/entities';
|
||||
import { RoleTypeEntity } from '../modules/role-type/entities';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -42,6 +44,8 @@ import { DeviceUserPermissionEntity } from '../modules/device-user-permission/en
|
||||
GroupEntity,
|
||||
GroupDeviceEntity,
|
||||
DeviceUserPermissionEntity,
|
||||
UserRoleEntity,
|
||||
RoleTypeEntity,
|
||||
],
|
||||
namingStrategy: new SnakeNamingStrategy(),
|
||||
synchronize: Boolean(JSON.parse(configService.get('DB_SYNC'))),
|
||||
|
||||
@@ -25,8 +25,6 @@ export class DeviceEntity extends AbstractEntity<DeviceDto> {
|
||||
(permission) => permission.device,
|
||||
{
|
||||
nullable: true,
|
||||
onDelete: 'CASCADE',
|
||||
onUpdate: 'CASCADE',
|
||||
},
|
||||
)
|
||||
permission: DeviceUserPermissionEntity[];
|
||||
|
||||
@@ -19,7 +19,6 @@ export class GroupEntity extends AbstractEntity<GroupDto> {
|
||||
|
||||
@OneToMany(() => GroupDeviceEntity, (groupDevice) => groupDevice.group, {
|
||||
cascade: true,
|
||||
onDelete: 'CASCADE',
|
||||
})
|
||||
groupDevices: GroupDeviceEntity[];
|
||||
|
||||
|
||||
@@ -17,8 +17,6 @@ export class PermissionTypeEntity extends AbstractEntity<PermissionTypeDto> {
|
||||
(permission) => permission.permissionType,
|
||||
{
|
||||
nullable: true,
|
||||
onDelete: 'CASCADE',
|
||||
onUpdate: 'CASCADE',
|
||||
},
|
||||
)
|
||||
permission: DeviceUserPermissionEntity[];
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
export * from './role.type.dto';
|
||||
@@ -0,0 +1,11 @@
|
||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||
import { IsEnum, IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class RoleTypeDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public uuid: string;
|
||||
|
||||
@IsEnum(RoleType)
|
||||
public type: RoleType;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from './role.type.entity';
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Column, Entity, OneToMany, Unique } from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { RoleTypeDto } from '../dtos/role.type.dto';
|
||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||
import { UserRoleEntity } from '../../user-role/entities';
|
||||
|
||||
@Entity({ name: 'role-type' })
|
||||
@Unique(['type'])
|
||||
export class RoleTypeEntity extends AbstractEntity<RoleTypeDto> {
|
||||
@Column({
|
||||
nullable: false,
|
||||
enum: Object.values(RoleType),
|
||||
})
|
||||
type: string;
|
||||
@OneToMany(() => UserRoleEntity, (role) => role.roleType, {
|
||||
nullable: true,
|
||||
})
|
||||
roles: UserRoleEntity[];
|
||||
constructor(partial: Partial<RoleTypeEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from './role.type.repository';
|
||||
@@ -0,0 +1,10 @@
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { RoleTypeEntity } from '../entities/role.type.entity';
|
||||
|
||||
@Injectable()
|
||||
export class RoleTypeRepository extends Repository<RoleTypeEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(RoleTypeEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { RoleTypeEntity } from './entities/role.type.entity';
|
||||
|
||||
@Module({
|
||||
providers: [],
|
||||
exports: [],
|
||||
controllers: [],
|
||||
imports: [TypeOrmModule.forFeature([RoleTypeEntity])],
|
||||
})
|
||||
export class RoleTypeRepositoryModule {}
|
||||
@@ -0,0 +1 @@
|
||||
export * from './user.role.dto';
|
||||
@@ -0,0 +1,15 @@
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class UserRoleDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public uuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public userUuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public roleTypeUuid: string;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from './user.role.entity';
|
||||
@@ -0,0 +1,24 @@
|
||||
import { Entity, ManyToOne, Unique } from 'typeorm';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { UserRoleDto } from '../dtos';
|
||||
import { UserEntity } from '../../user/entities';
|
||||
import { RoleTypeEntity } from '../../role-type/entities';
|
||||
|
||||
@Entity({ name: 'user-role' })
|
||||
@Unique(['user', 'roleType'])
|
||||
export class UserRoleEntity extends AbstractEntity<UserRoleDto> {
|
||||
@ManyToOne(() => UserEntity, (user) => user.roles, {
|
||||
nullable: false,
|
||||
})
|
||||
user: UserEntity;
|
||||
|
||||
@ManyToOne(() => RoleTypeEntity, (roleType) => roleType.roles, {
|
||||
nullable: false,
|
||||
})
|
||||
roleType: RoleTypeEntity;
|
||||
|
||||
constructor(partial: Partial<UserRoleEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from './user.role.repository';
|
||||
@@ -0,0 +1,10 @@
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { UserRoleEntity } from '../entities';
|
||||
|
||||
@Injectable()
|
||||
export class UserRoleRepository extends Repository<UserRoleEntity> {
|
||||
constructor(private dataSource: DataSource) {
|
||||
super(UserRoleEntity, dataSource.createEntityManager());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { UserRoleEntity } from './entities';
|
||||
@Module({
|
||||
providers: [],
|
||||
exports: [],
|
||||
controllers: [],
|
||||
imports: [TypeOrmModule.forFeature([UserRoleEntity])],
|
||||
})
|
||||
export class UserRoleRepositoryModule {}
|
||||
@@ -3,6 +3,7 @@ import { Column, Entity, OneToMany } from 'typeorm';
|
||||
import { UserDto } from '../dtos';
|
||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||
import { UserSpaceEntity } from '../../user-space/entities';
|
||||
import { UserRoleEntity } from '../../user-role/entities';
|
||||
|
||||
@Entity({ name: 'user' })
|
||||
export class UserEntity extends AbstractEntity<UserDto> {
|
||||
@@ -57,6 +58,11 @@ export class UserEntity extends AbstractEntity<UserDto> {
|
||||
(userPermission) => userPermission.user,
|
||||
)
|
||||
userPermission: DeviceUserPermissionEntity[];
|
||||
|
||||
@OneToMany(() => UserRoleEntity, (role) => role.user, {
|
||||
nullable: true,
|
||||
})
|
||||
roles: UserRoleEntity[];
|
||||
constructor(partial: Partial<UserEntity>) {
|
||||
super();
|
||||
Object.assign(this, partial);
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { SeederService } from './services/seeder.service';
|
||||
import { PermissionTypeRepository } from '../modules/permission/repositories';
|
||||
import { PermissionTypeSeeder } from './services/permission.type.seeder';
|
||||
import { PermissionTypeRepositoryModule } from '../modules/permission/permission.repository.module';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { RoleTypeRepositoryModule } from '../modules/role-type/role.type.repository.module';
|
||||
import { RoleTypeRepository } from '../modules/role-type/repositories';
|
||||
import { RoleTypeSeeder } from './services/role.type.seeder';
|
||||
import { SpaceTypeRepository } from '../modules/space-type/repositories';
|
||||
import { SpaceTypeSeeder } from './services/space.type.seeder';
|
||||
import { SpaceTypeRepositoryModule } from '../modules/space-type/space.type.repository.module';
|
||||
import { SuperAdminSeeder } from './services/supper.admin.seeder';
|
||||
import { UserRepository } from '../modules/user/repositories';
|
||||
import { UserRoleRepository } from '../modules/user-role/repositories';
|
||||
import { UserRoleRepositoryModule } from '../modules/user-role/user.role.repository.module';
|
||||
import { UserRepositoryModule } from '../modules/user/user.repository.module';
|
||||
@Global()
|
||||
@Module({
|
||||
providers: [
|
||||
PermissionTypeSeeder,
|
||||
RoleTypeSeeder,
|
||||
SpaceTypeSeeder,
|
||||
SeederService,
|
||||
PermissionTypeRepository,
|
||||
RoleTypeRepository,
|
||||
SpaceTypeRepository,
|
||||
SuperAdminSeeder,
|
||||
UserRepository,
|
||||
UserRoleRepository,
|
||||
],
|
||||
exports: [SeederService],
|
||||
controllers: [],
|
||||
imports: [
|
||||
ConfigModule.forRoot(),
|
||||
PermissionTypeRepositoryModule,
|
||||
RoleTypeRepositoryModule,
|
||||
UserRepositoryModule,
|
||||
UserRoleRepositoryModule,
|
||||
SpaceTypeRepositoryModule,
|
||||
],
|
||||
})
|
||||
export class SeederModule {}
|
||||
@@ -0,0 +1,49 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PermissionTypeRepository } from '../../modules/permission/repositories';
|
||||
import { PermissionType } from '../../constants/permission-type.enum';
|
||||
|
||||
@Injectable()
|
||||
export class PermissionTypeSeeder {
|
||||
constructor(
|
||||
private readonly permissionTypeRepository: PermissionTypeRepository,
|
||||
) {}
|
||||
|
||||
async addPermissionTypeDataIfNotFound(): Promise<void> {
|
||||
try {
|
||||
const existingPermissionTypes =
|
||||
await this.permissionTypeRepository.find();
|
||||
|
||||
const permissionTypeNames = existingPermissionTypes.map((pt) => pt.type);
|
||||
|
||||
const missingPermissionTypes = [];
|
||||
if (!permissionTypeNames.includes(PermissionType.CONTROLLABLE)) {
|
||||
missingPermissionTypes.push(PermissionType.CONTROLLABLE);
|
||||
}
|
||||
if (!permissionTypeNames.includes(PermissionType.READ)) {
|
||||
missingPermissionTypes.push(PermissionType.READ);
|
||||
}
|
||||
|
||||
if (missingPermissionTypes.length > 0) {
|
||||
await this.addPermissionTypeData(missingPermissionTypes);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error while checking permission type data:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
private async addPermissionTypeData(
|
||||
permissionTypes: string[],
|
||||
): Promise<void> {
|
||||
try {
|
||||
const permissionTypeEntities = permissionTypes.map((type) => ({
|
||||
type,
|
||||
}));
|
||||
|
||||
await this.permissionTypeRepository.save(permissionTypeEntities);
|
||||
} catch (err) {
|
||||
console.error('Error while adding permission type data:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { RoleType } from '../../constants/role.type.enum';
|
||||
import { RoleTypeRepository } from '../../modules/role-type/repositories';
|
||||
|
||||
@Injectable()
|
||||
export class RoleTypeSeeder {
|
||||
constructor(private readonly roleTypeRepository: RoleTypeRepository) {}
|
||||
|
||||
async addRoleTypeDataIfNotFound(): Promise<void> {
|
||||
try {
|
||||
const existingRoleTypes = await this.roleTypeRepository.find();
|
||||
|
||||
const roleTypeNames = existingRoleTypes.map((pt) => pt.type);
|
||||
|
||||
const missingRoleTypes = [];
|
||||
if (!roleTypeNames.includes(RoleType.SUPER_ADMIN)) {
|
||||
missingRoleTypes.push(RoleType.SUPER_ADMIN);
|
||||
}
|
||||
if (!roleTypeNames.includes(RoleType.ADMIN)) {
|
||||
missingRoleTypes.push(RoleType.ADMIN);
|
||||
}
|
||||
|
||||
if (missingRoleTypes.length > 0) {
|
||||
await this.addRoleTypeData(missingRoleTypes);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error while checking role type data:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
private async addRoleTypeData(roleTypes: string[]): Promise<void> {
|
||||
try {
|
||||
const roleTypeEntities = roleTypes.map((type) => ({
|
||||
type,
|
||||
}));
|
||||
|
||||
await this.roleTypeRepository.save(roleTypeEntities);
|
||||
} catch (err) {
|
||||
console.error('Error while adding role type data:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PermissionTypeSeeder } from './permission.type.seeder';
|
||||
import { RoleTypeSeeder } from './role.type.seeder';
|
||||
import { SpaceTypeSeeder } from './space.type.seeder';
|
||||
import { SuperAdminSeeder } from './supper.admin.seeder';
|
||||
@Injectable()
|
||||
export class SeederService {
|
||||
constructor(
|
||||
private readonly permissionTypeSeeder: PermissionTypeSeeder,
|
||||
private readonly roleTypeSeeder: RoleTypeSeeder,
|
||||
private readonly spaceTypeSeeder: SpaceTypeSeeder,
|
||||
private readonly superAdminSeeder: SuperAdminSeeder,
|
||||
) {}
|
||||
|
||||
async seed() {
|
||||
await this.permissionTypeSeeder.addPermissionTypeDataIfNotFound();
|
||||
await this.roleTypeSeeder.addRoleTypeDataIfNotFound();
|
||||
await this.spaceTypeSeeder.addSpaceTypeDataIfNotFound();
|
||||
await this.superAdminSeeder.createSuperAdminIfNotFound();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { SpaceType } from '../../constants/space-type.enum';
|
||||
import { SpaceTypeRepository } from '../../modules/space-type/repositories';
|
||||
|
||||
@Injectable()
|
||||
export class SpaceTypeSeeder {
|
||||
constructor(private readonly spaceTypeRepository: SpaceTypeRepository) {}
|
||||
|
||||
async addSpaceTypeDataIfNotFound(): Promise<void> {
|
||||
try {
|
||||
const existingSpaceTypes = await this.spaceTypeRepository.find();
|
||||
|
||||
const spaceTypeNames = existingSpaceTypes.map((pt) => pt.type);
|
||||
|
||||
const missingSpaceTypes = [];
|
||||
if (!spaceTypeNames.includes(SpaceType.COMMUNITY)) {
|
||||
missingSpaceTypes.push(SpaceType.COMMUNITY);
|
||||
}
|
||||
if (!spaceTypeNames.includes(SpaceType.BUILDING)) {
|
||||
missingSpaceTypes.push(SpaceType.BUILDING);
|
||||
}
|
||||
if (!spaceTypeNames.includes(SpaceType.FLOOR)) {
|
||||
missingSpaceTypes.push(SpaceType.FLOOR);
|
||||
}
|
||||
if (!spaceTypeNames.includes(SpaceType.UNIT)) {
|
||||
missingSpaceTypes.push(SpaceType.UNIT);
|
||||
}
|
||||
if (!spaceTypeNames.includes(SpaceType.ROOM)) {
|
||||
missingSpaceTypes.push(SpaceType.ROOM);
|
||||
}
|
||||
if (missingSpaceTypes.length > 0) {
|
||||
await this.addSpaceTypeData(missingSpaceTypes);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error while checking space type data:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
private async addSpaceTypeData(spaceTypes: string[]): Promise<void> {
|
||||
try {
|
||||
const spaceTypeEntities = spaceTypes.map((type) => ({
|
||||
type,
|
||||
}));
|
||||
|
||||
await this.spaceTypeRepository.save(spaceTypeEntities);
|
||||
} catch (err) {
|
||||
console.error('Error while adding space type data:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||
import { UserRoleRepository } from '@app/common/modules/user-role/repositories';
|
||||
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { HelperHashService } from '../../helper/services';
|
||||
|
||||
@Injectable()
|
||||
export class SuperAdminSeeder {
|
||||
constructor(
|
||||
private readonly configService: ConfigService,
|
||||
private readonly userRepository: UserRepository,
|
||||
private readonly userRoleRepository: UserRoleRepository,
|
||||
private readonly roleTypeRepository: RoleTypeRepository,
|
||||
private readonly helperHashService: HelperHashService,
|
||||
) {}
|
||||
|
||||
async createSuperAdminIfNotFound(): Promise<void> {
|
||||
try {
|
||||
const superAdminData = await this.userRoleRepository.find({
|
||||
where: { roleType: { type: RoleType.SUPER_ADMIN } },
|
||||
relations: ['roleType'],
|
||||
});
|
||||
|
||||
if (superAdminData.length <= 0) {
|
||||
// Create the super admin user if not found
|
||||
console.log('Creating super admin user...');
|
||||
|
||||
await this.createSuperAdmin();
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error while checking super admin:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
private async getRoleUuidByRoleType(roleType: string) {
|
||||
const role = await this.roleTypeRepository.findOne({
|
||||
where: { type: roleType },
|
||||
});
|
||||
|
||||
return role.uuid;
|
||||
}
|
||||
private async createSuperAdmin(): Promise<void> {
|
||||
const salt = this.helperHashService.randomSalt(10); // Hash the password using bcrypt
|
||||
const hashedPassword = await this.helperHashService.bcrypt(
|
||||
this.configService.get<string>('super-admin.SUPER_ADMIN_PASSWORD'),
|
||||
salt,
|
||||
);
|
||||
try {
|
||||
const user = await this.userRepository.save({
|
||||
email: this.configService.get<string>('super-admin.SUPER_ADMIN_EMAIL'),
|
||||
password: hashedPassword,
|
||||
firstName: 'Super',
|
||||
lastName: 'Admin',
|
||||
isUserVerified: true,
|
||||
isActive: true,
|
||||
});
|
||||
const defaultUserRoleUuid = await this.getRoleUuidByRoleType(
|
||||
RoleType.SUPER_ADMIN,
|
||||
);
|
||||
|
||||
await this.userRoleRepository.save({
|
||||
user: { uuid: user.uuid },
|
||||
roleType: { uuid: defaultUserRoleUuid },
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Error while creating super admin:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,8 @@ import { CommunityModule } from './community/community.module';
|
||||
import { BuildingModule } from './building/building.module';
|
||||
import { FloorModule } from './floor/floor.module';
|
||||
import { UnitModule } from './unit/unit.module';
|
||||
import { RoleModule } from './role/role.module';
|
||||
import { SeederModule } from '@app/common/seed/seeder.module';
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
@@ -19,6 +21,7 @@ import { UnitModule } from './unit/unit.module';
|
||||
}),
|
||||
AuthenticationModule,
|
||||
UserModule,
|
||||
RoleModule,
|
||||
CommunityModule,
|
||||
BuildingModule,
|
||||
FloorModule,
|
||||
@@ -28,6 +31,7 @@ import { UnitModule } from './unit/unit.module';
|
||||
GroupModule,
|
||||
DeviceModule,
|
||||
UserDevicePermissionModule,
|
||||
SeederModule,
|
||||
],
|
||||
controllers: [AuthenticationController],
|
||||
})
|
||||
|
||||
@@ -9,6 +9,8 @@ import { UserAuthService } from './services';
|
||||
import { UserRepository } from '../../libs/common/src/modules/user/repositories';
|
||||
import { UserSessionRepository } from '../../libs/common/src/modules/session/repositories/session.repository';
|
||||
import { UserOtpRepository } from '../../libs/common/src/modules/user-otp/repositories/user-otp.repository';
|
||||
import { UserRoleRepository } from '@app/common/modules/user-role/repositories';
|
||||
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, UserRepositoryModule, CommonModule],
|
||||
@@ -19,6 +21,8 @@ import { UserOtpRepository } from '../../libs/common/src/modules/user-otp/reposi
|
||||
UserRepository,
|
||||
UserSessionRepository,
|
||||
UserOtpRepository,
|
||||
UserRoleRepository,
|
||||
RoleTypeRepository,
|
||||
],
|
||||
exports: [AuthenticationService, UserAuthService],
|
||||
})
|
||||
|
||||
@@ -14,9 +14,9 @@ import { UserSignUpDto } from '../dtos/user-auth.dto';
|
||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||
import { ResponseMessage } from '../../../libs/common/src/response/response.decorator';
|
||||
import { UserLoginDto } from '../dtos/user-login.dto';
|
||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||
import { ForgetPasswordDto, UserOtpDto, VerifyOtpDto } from '../dtos';
|
||||
import { RefreshTokenGuard } from '@app/common/guards/jwt-refresh.auth.guard';
|
||||
import { SuperAdminRoleGuard } from 'src/guards/super.admin.role.guard';
|
||||
|
||||
@Controller({
|
||||
version: '1',
|
||||
@@ -47,12 +47,12 @@ export class UserAuthController {
|
||||
return {
|
||||
statusCode: HttpStatus.CREATED,
|
||||
data: accessToken,
|
||||
message: 'User Loggedin Successfully',
|
||||
message: 'User Logged in Successfully',
|
||||
};
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@UseGuards(SuperAdminRoleGuard)
|
||||
@Delete('user/delete/:id')
|
||||
async userDelete(@Param('id') id: string) {
|
||||
await this.userAuthService.deleteUser(id);
|
||||
@@ -98,7 +98,7 @@ export class UserAuthController {
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@UseGuards(SuperAdminRoleGuard)
|
||||
@Get('user/list')
|
||||
async userList() {
|
||||
const userList = await this.userAuthService.userList();
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { RoleTypeRepository } from './../../../libs/common/src/modules/role-type/repositories/role.type.repository';
|
||||
import { UserRoleRepository } from './../../../libs/common/src/modules/user-role/repositories/user.role.repository';
|
||||
import { UserRepository } from '../../../libs/common/src/modules/user/repositories';
|
||||
import {
|
||||
BadRequestException,
|
||||
@@ -26,6 +28,8 @@ export class UserAuthService {
|
||||
private readonly helperHashService: HelperHashService,
|
||||
private readonly authService: AuthService,
|
||||
private readonly emailService: EmailService,
|
||||
private readonly userRoleRepository: UserRoleRepository,
|
||||
private readonly roleTypeRepository: RoleTypeRepository,
|
||||
) {}
|
||||
|
||||
async signUp(userSignUpDto: UserSignUpDto): Promise<UserEntity> {
|
||||
@@ -33,12 +37,22 @@ export class UserAuthService {
|
||||
if (findUser) {
|
||||
throw new BadRequestException('User already registered with given email');
|
||||
}
|
||||
const salt = this.helperHashService.randomSalt(10);
|
||||
const password = this.helperHashService.bcrypt(
|
||||
const salt = this.helperHashService.randomSalt(10); // Hash the password using bcrypt
|
||||
const hashedPassword = await this.helperHashService.bcrypt(
|
||||
userSignUpDto.password,
|
||||
salt,
|
||||
);
|
||||
return await this.userRepository.save({ ...userSignUpDto, password });
|
||||
|
||||
try {
|
||||
const user = await this.userRepository.save({
|
||||
...userSignUpDto,
|
||||
password: hashedPassword,
|
||||
});
|
||||
|
||||
return user;
|
||||
} catch (error) {
|
||||
throw new BadRequestException('Failed to register user');
|
||||
}
|
||||
}
|
||||
|
||||
async findUser(email: string) {
|
||||
@@ -67,6 +81,7 @@ export class UserAuthService {
|
||||
|
||||
async userLogin(data: UserLoginDto) {
|
||||
const user = await this.authService.validateUser(data.email, data.password);
|
||||
|
||||
if (!user) {
|
||||
throw new UnauthorizedException('Invalid login credentials.');
|
||||
}
|
||||
@@ -89,6 +104,9 @@ export class UserAuthService {
|
||||
email: user.email,
|
||||
userId: user.uuid,
|
||||
uuid: user.uuid,
|
||||
roles: user?.roles?.map((role) => {
|
||||
return { uuid: role.uuid, type: role.roleType.type };
|
||||
}),
|
||||
sessionId: session[1].uuid,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -12,12 +12,13 @@ import {
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||
import { AddBuildingDto, AddUserBuildingDto } from '../dtos/add.building.dto';
|
||||
import { GetBuildingChildDto } from '../dtos/get.building.dto';
|
||||
import { UpdateBuildingNameDto } from '../dtos/update.building.dto';
|
||||
import { CheckCommunityTypeGuard } from 'src/guards/community.type.guard';
|
||||
import { CheckUserBuildingGuard } from 'src/guards/user.building.guard';
|
||||
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { BuildingPermissionGuard } from 'src/guards/building.permission.guard';
|
||||
|
||||
@ApiTags('Building Module')
|
||||
@@ -29,7 +30,7 @@ export class BuildingController {
|
||||
constructor(private readonly buildingService: BuildingService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard, CheckCommunityTypeGuard)
|
||||
@UseGuards(AdminRoleGuard, CheckCommunityTypeGuard)
|
||||
@Post()
|
||||
async addBuilding(@Body() addBuildingDto: AddBuildingDto) {
|
||||
try {
|
||||
@@ -100,7 +101,7 @@ export class BuildingController {
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard, CheckUserBuildingGuard)
|
||||
@UseGuards(AdminRoleGuard, CheckUserBuildingGuard)
|
||||
@Post('user')
|
||||
async addUserBuilding(@Body() addUserBuildingDto: AddUserBuildingDto) {
|
||||
try {
|
||||
|
||||
@@ -12,7 +12,6 @@ import {
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||
import {
|
||||
AddCommunityDto,
|
||||
AddUserCommunityDto,
|
||||
@@ -20,6 +19,8 @@ import {
|
||||
import { GetCommunityChildDto } from '../dtos/get.community.dto';
|
||||
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
||||
import { CheckUserCommunityGuard } from 'src/guards/user.community.guard';
|
||||
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { CommunityPermissionGuard } from 'src/guards/community.permission.guard';
|
||||
|
||||
@ApiTags('Community Module')
|
||||
@@ -31,7 +32,7 @@ export class CommunityController {
|
||||
constructor(private readonly communityService: CommunityService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@UseGuards(AdminRoleGuard)
|
||||
@Post()
|
||||
async addCommunity(@Body() addCommunityDto: AddCommunityDto) {
|
||||
try {
|
||||
@@ -102,7 +103,7 @@ export class CommunityController {
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard, CheckUserCommunityGuard)
|
||||
@UseGuards(AdminRoleGuard, CheckUserCommunityGuard)
|
||||
@Post('user')
|
||||
async addUserCommunity(@Body() addUserCommunityDto: AddUserCommunityDto) {
|
||||
try {
|
||||
|
||||
@@ -23,9 +23,9 @@ import {
|
||||
import { ControlDeviceDto } from '../dtos/control.device.dto';
|
||||
import { CheckRoomGuard } from 'src/guards/room.guard';
|
||||
import { CheckGroupGuard } from 'src/guards/group.guard';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { CheckUserHavePermission } from 'src/guards/user.device.permission.guard';
|
||||
import { CheckUserHaveControllablePermission } from 'src/guards/user.device.controllable.permission.guard';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
|
||||
@ApiTags('Device Module')
|
||||
@Controller({
|
||||
|
||||
@@ -12,12 +12,13 @@ import {
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||
import { AddFloorDto, AddUserFloorDto } from '../dtos/add.floor.dto';
|
||||
import { GetFloorChildDto } from '../dtos/get.floor.dto';
|
||||
import { UpdateFloorNameDto } from '../dtos/update.floor.dto';
|
||||
import { CheckBuildingTypeGuard } from 'src/guards/building.type.guard';
|
||||
import { CheckUserFloorGuard } from 'src/guards/user.floor.guard';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
||||
import { FloorPermissionGuard } from 'src/guards/floor.permission.guard';
|
||||
|
||||
@ApiTags('Floor Module')
|
||||
@@ -29,7 +30,7 @@ export class FloorController {
|
||||
constructor(private readonly floorService: FloorService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard, CheckBuildingTypeGuard)
|
||||
@UseGuards(AdminRoleGuard, CheckBuildingTypeGuard)
|
||||
@Post()
|
||||
async addFloor(@Body() addFloorDto: AddFloorDto) {
|
||||
try {
|
||||
@@ -99,7 +100,7 @@ export class FloorController {
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard, CheckUserFloorGuard)
|
||||
@UseGuards(AdminRoleGuard, CheckUserFloorGuard)
|
||||
@Post('user')
|
||||
async addUserFloor(@Body() addUserFloorDto: AddUserFloorDto) {
|
||||
try {
|
||||
|
||||
@@ -12,11 +12,11 @@ import {
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||
import { AddGroupDto } from '../dtos/add.group.dto';
|
||||
import { ControlGroupDto } from '../dtos/control.group.dto';
|
||||
import { RenameGroupDto } from '../dtos/rename.group.dto copy';
|
||||
import { CheckProductUuidForAllDevicesGuard } from 'src/guards/device.product.guard';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
|
||||
@ApiTags('Group Module')
|
||||
@Controller({
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||
import { BadRequestException, UnauthorizedException } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
export class AdminRoleGuard extends AuthGuard('jwt') {
|
||||
handleRequest(err, user) {
|
||||
if (err || !user) {
|
||||
throw err || new UnauthorizedException();
|
||||
} else {
|
||||
const isAdmin = user.roles.some(
|
||||
(role) =>
|
||||
role.type === RoleType.SUPER_ADMIN || role.type === RoleType.ADMIN,
|
||||
);
|
||||
if (!isAdmin) {
|
||||
throw new BadRequestException('Only admin role can access this route');
|
||||
}
|
||||
}
|
||||
return user;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||
import { BadRequestException, UnauthorizedException } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
export class SuperAdminRoleGuard extends AuthGuard('jwt') {
|
||||
handleRequest(err, user) {
|
||||
if (err || !user) {
|
||||
throw err || new UnauthorizedException();
|
||||
} else {
|
||||
const isSuperAdmin = user.roles.some(
|
||||
(role) => role.type === RoleType.SUPER_ADMIN,
|
||||
);
|
||||
if (!isSuperAdmin) {
|
||||
throw new BadRequestException(
|
||||
'Only super admin role can access this route',
|
||||
);
|
||||
}
|
||||
}
|
||||
return user;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import rateLimit from 'express-rate-limit';
|
||||
import helmet from 'helmet';
|
||||
import { setupSwaggerAuthentication } from '../libs/common/src/util/user-auth.swagger.utils';
|
||||
import { ValidationPipe } from '@nestjs/common';
|
||||
import { SeederService } from '@app/common/seed/services/seeder.service';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AuthModule);
|
||||
@@ -34,6 +35,13 @@ async function bootstrap() {
|
||||
}),
|
||||
);
|
||||
|
||||
const seederService = app.get(SeederService);
|
||||
try {
|
||||
await seederService.seed();
|
||||
console.log('Seeding complete!');
|
||||
} catch (error) {
|
||||
console.error('Seeding failed!', error);
|
||||
}
|
||||
await app.listen(process.env.PORT || 4000);
|
||||
}
|
||||
console.log('Starting auth at port ...', process.env.PORT || 4000);
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
export * from './role.controller';
|
||||
@@ -0,0 +1,54 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
Post,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||
import { RoleService } from '../services/role.service';
|
||||
import { AddUserRoleDto } from '../dtos';
|
||||
import { SuperAdminRoleGuard } from 'src/guards/super.admin.role.guard';
|
||||
|
||||
@ApiTags('Role Module')
|
||||
@Controller({
|
||||
version: '1',
|
||||
path: 'role',
|
||||
})
|
||||
export class RoleController {
|
||||
constructor(private readonly roleService: RoleService) {}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(SuperAdminRoleGuard)
|
||||
@Get('types')
|
||||
async fetchRoleTypes() {
|
||||
try {
|
||||
const roleTypes = await this.roleService.fetchRoleTypes();
|
||||
return {
|
||||
statusCode: HttpStatus.OK,
|
||||
message: 'Role Types fetched Successfully',
|
||||
data: roleTypes,
|
||||
};
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(SuperAdminRoleGuard)
|
||||
@Post()
|
||||
async addUserRoleType(@Body() addUserRoleDto: AddUserRoleDto) {
|
||||
try {
|
||||
await this.roleService.addUserRoleType(addUserRoleDto);
|
||||
return {
|
||||
statusCode: HttpStatus.OK,
|
||||
message: 'User Role Added Successfully',
|
||||
};
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from './role.add.dto';
|
||||
@@ -0,0 +1,24 @@
|
||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsEnum, IsIn, IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class AddUserRoleDto {
|
||||
@ApiProperty({
|
||||
description: 'userUuid',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public userUuid: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'Role type (ADMIN)',
|
||||
enum: [RoleType.ADMIN],
|
||||
required: true,
|
||||
})
|
||||
@IsEnum(RoleType)
|
||||
@IsIn([RoleType.ADMIN], {
|
||||
message: 'roleType must be one of the following values: ADMIN',
|
||||
})
|
||||
roleType: RoleType;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { DeviceRepositoryModule } from '@app/common/modules/device';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { RoleService } from './services/role.service';
|
||||
import { RoleController } from './controllers/role.controller';
|
||||
import { DeviceUserPermissionRepository } from '@app/common/modules/device-user-permission/repositories';
|
||||
import { PermissionTypeRepository } from '@app/common/modules/permission/repositories';
|
||||
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
||||
import { UserRoleRepository } from '@app/common/modules/user-role/repositories';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, DeviceRepositoryModule],
|
||||
controllers: [RoleController],
|
||||
providers: [
|
||||
DeviceUserPermissionRepository,
|
||||
PermissionTypeRepository,
|
||||
DeviceRepository,
|
||||
RoleService,
|
||||
RoleTypeRepository,
|
||||
UserRoleRepository,
|
||||
],
|
||||
exports: [RoleService],
|
||||
})
|
||||
export class RoleModule {}
|
||||
@@ -0,0 +1 @@
|
||||
export * from './role.service';
|
||||
@@ -0,0 +1,54 @@
|
||||
import { RoleTypeRepository } from './../../../libs/common/src/modules/role-type/repositories/role.type.repository';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { AddUserRoleDto } from '../dtos/role.add.dto';
|
||||
import { UserRoleRepository } from '@app/common/modules/user-role/repositories';
|
||||
import { QueryFailedError } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class RoleService {
|
||||
constructor(
|
||||
private readonly roleTypeRepository: RoleTypeRepository,
|
||||
private readonly userRoleRepository: UserRoleRepository,
|
||||
) {}
|
||||
|
||||
async addUserRoleType(addUserRoleDto: AddUserRoleDto) {
|
||||
try {
|
||||
const roleType = await this.fetchRoleByType(addUserRoleDto.roleType);
|
||||
|
||||
if (roleType.uuid) {
|
||||
return await this.userRoleRepository.save({
|
||||
user: { uuid: addUserRoleDto.userUuid },
|
||||
roleType: { uuid: roleType.uuid },
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
if (
|
||||
error instanceof QueryFailedError &&
|
||||
error.driverError.code === '23505'
|
||||
) {
|
||||
// Postgres unique constraint violation error code
|
||||
throw new HttpException(
|
||||
'This role already exists for this user',
|
||||
HttpStatus.CONFLICT,
|
||||
);
|
||||
}
|
||||
throw new HttpException(
|
||||
error.message || 'Internal Server Error',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async fetchRoleTypes() {
|
||||
const roleTypes = await this.roleTypeRepository.find();
|
||||
|
||||
return roleTypes;
|
||||
}
|
||||
private async fetchRoleByType(roleType: string) {
|
||||
return await this.roleTypeRepository.findOne({
|
||||
where: {
|
||||
type: roleType,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -11,11 +11,12 @@ import {
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||
import { AddRoomDto, AddUserRoomDto } from '../dtos/add.room.dto';
|
||||
import { UpdateRoomNameDto } from '../dtos/update.room.dto';
|
||||
import { CheckUnitTypeGuard } from 'src/guards/unit.type.guard';
|
||||
import { CheckUserRoomGuard } from 'src/guards/user.room.guard';
|
||||
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { RoomPermissionGuard } from 'src/guards/room.permission.guard';
|
||||
|
||||
@ApiTags('Room Module')
|
||||
@@ -27,7 +28,7 @@ export class RoomController {
|
||||
constructor(private readonly roomService: RoomService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard, CheckUnitTypeGuard)
|
||||
@UseGuards(AdminRoleGuard, CheckUnitTypeGuard)
|
||||
@Post()
|
||||
async addRoom(@Body() addRoomDto: AddRoomDto) {
|
||||
try {
|
||||
@@ -76,7 +77,7 @@ export class RoomController {
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard, CheckUserRoomGuard)
|
||||
@UseGuards(AdminRoleGuard, CheckUserRoomGuard)
|
||||
@Post('user')
|
||||
async addUserRoom(@Body() addUserRoomDto: AddUserRoomDto) {
|
||||
try {
|
||||
|
||||
@@ -12,12 +12,13 @@ import {
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||
import { AddUnitDto, AddUserUnitDto } from '../dtos/add.unit.dto';
|
||||
import { GetUnitChildDto } from '../dtos/get.unit.dto';
|
||||
import { UpdateUnitNameDto } from '../dtos/update.unit.dto';
|
||||
import { CheckFloorTypeGuard } from 'src/guards/floor.type.guard';
|
||||
import { CheckUserUnitGuard } from 'src/guards/user.unit.guard';
|
||||
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { UnitPermissionGuard } from 'src/guards/unit.permission.guard';
|
||||
|
||||
@ApiTags('Unit Module')
|
||||
@@ -29,7 +30,7 @@ export class UnitController {
|
||||
constructor(private readonly unitService: UnitService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard, CheckFloorTypeGuard)
|
||||
@UseGuards(AdminRoleGuard, CheckFloorTypeGuard)
|
||||
@Post()
|
||||
async addUnit(@Body() addUnitDto: AddUnitDto) {
|
||||
try {
|
||||
@@ -95,7 +96,7 @@ export class UnitController {
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard, CheckUserUnitGuard)
|
||||
@UseGuards(AdminRoleGuard, CheckUserUnitGuard)
|
||||
@Post('user')
|
||||
async addUserUnit(@Body() addUserUnitDto: AddUserUnitDto) {
|
||||
try {
|
||||
|
||||
@@ -13,8 +13,8 @@ import {
|
||||
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
|
||||
import { UserDevicePermissionService } from '../services/user-device-permission.service';
|
||||
import { UserDevicePermissionAddDto } from '../dtos/user-device-permission.add.dto';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { UserDevicePermissionEditDto } from '../dtos/user-device-permission.edit.dto';
|
||||
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
||||
|
||||
@ApiTags('Device Permission Module')
|
||||
@Controller({
|
||||
@@ -27,7 +27,7 @@ export class UserDevicePermissionController {
|
||||
) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@UseGuards(AdminRoleGuard)
|
||||
@Post('add')
|
||||
async addDevicePermission(
|
||||
@Body() userDevicePermissionDto: UserDevicePermissionAddDto,
|
||||
@@ -51,7 +51,7 @@ export class UserDevicePermissionController {
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@UseGuards(AdminRoleGuard)
|
||||
@Put('edit/:devicePermissionUuid')
|
||||
async editDevicePermission(
|
||||
@Param('devicePermissionUuid') devicePermissionUuid: string,
|
||||
@@ -75,7 +75,7 @@ export class UserDevicePermissionController {
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@UseGuards(AdminRoleGuard)
|
||||
@Get(':deviceUuid/list')
|
||||
async fetchDevicePermission(@Param('deviceUuid') deviceUuid: string) {
|
||||
try {
|
||||
@@ -91,7 +91,7 @@ export class UserDevicePermissionController {
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@UseGuards(AdminRoleGuard)
|
||||
@Delete(':devicePermissionUuid')
|
||||
async deleteDevicePermission(
|
||||
@Param('devicePermissionUuid') devicePermissionUuid: string,
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Controller, Get, Query, UseGuards } from '@nestjs/common';
|
||||
import { UserService } from '../services/user.service';
|
||||
import { UserListDto } from '../dtos/user.list.dto';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
||||
|
||||
@ApiTags('User Module')
|
||||
@Controller({
|
||||
@@ -13,7 +13,7 @@ export class UserController {
|
||||
constructor(private readonly userService: UserService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@UseGuards(AdminRoleGuard)
|
||||
@Get('list')
|
||||
async userList(@Query() userListDto: UserListDto) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user