LocChat/server/providers/services/users.service.ts

75 lines
2.6 KiB
TypeScript
Raw Normal View History

2021-11-16 21:14:46 -05:00
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
2021-11-20 21:34:10 -05:00
import * as bcrypt from 'bcrypt';
2021-11-16 21:14:46 -05:00
import { User } from '../../entities/user.entity';
import { Role, RoleKey } from 'server/entities/role.entity';
import { UserRole } from 'server/entities/user_role.entity';
import { intersection, isEmpty } from 'lodash';
2021-11-16 21:14:46 -05:00
@Injectable()
export class UsersService {
constructor(
@InjectRepository(User)
private usersRespository: Repository<User>,
@InjectRepository(UserRole)
private userRolesRepository: Repository<UserRole>,
@InjectRepository(Role)
private rolesRepository: Repository<Role>,
2021-11-16 21:14:46 -05:00
) {}
2021-12-01 22:18:26 -05:00
findAll(relations: string[] = []) {
return this.usersRespository.find({ relations });
}
2021-11-23 16:04:12 -05:00
findBy(options: Record<string, any>, relations: string[] = []) {
return this.usersRespository.findOne(options, { relations });
2021-11-16 21:14:46 -05:00
}
2021-11-23 16:04:12 -05:00
find(id: number, relations: string[] = []) {
return this.usersRespository.findOne(id, { relations });
2021-11-16 21:14:46 -05:00
}
2021-11-20 20:18:58 -05:00
create(user: User) {
return this.usersRespository.save(user);
}
2021-11-16 21:14:46 -05:00
async verify(email: string, password: string) {
2021-12-01 22:18:26 -05:00
const user = await this.usersRespository.findOne({ email }, { relations: ['refreshTokens', 'userRoles'] });
2021-11-20 20:18:58 -05:00
if (!user) return { verified: false, user: null };
2021-11-23 16:04:12 -05:00
const verified: boolean = await bcrypt.compare(password, user.passwordHash);
2021-11-22 16:21:53 -05:00
return { verified, user: verified ? user : null };
2021-11-16 21:14:46 -05:00
}
addUserToRoleInContext(userId: number, contextId: string, ...roleKeys: RoleKey[]) {
return Promise.all(
roleKeys.map(async (key) => {
const role = await this.rolesRepository.findOne({ key });
const userRole = new UserRole();
userRole.userId = userId;
userRole.contextId = contextId;
userRole.role = role;
await this.userRolesRepository.save(userRole);
}),
);
}
addUserToRootRole(userId: number, ...roleKeys: RoleKey[]) {
return this.addUserToRoleInContext(userId, 'root', ...roleKeys);
}
// if multiple roles are passed then will return true if user has any of the listed roles.
async hasRoleInContext(userId: number, contextId: string, ...roleKeys: RoleKey[]) {
const userRoles = await this.userRolesRepository.find({
where: { userId, contextId },
relations: ['role'],
});
const usersRoleKeys = userRoles.map((userRole) => userRole.role.key);
return !isEmpty(intersection(roleKeys, usersRoleKeys));
}
async hasRootRole(userId: number, ...roleKeys: RoleKey[]) {
return this.hasRoleInContext(userId, 'root', ...roleKeys);
}
2021-11-16 21:14:46 -05:00
}