add: role in jwt

This commit is contained in:
ilham 2021-12-09 21:30:10 +07:00
parent 19c951c331
commit a057088a93
2 changed files with 56 additions and 38 deletions

View File

@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
import { UsersService } from '../users/users.service'; import { UsersService } from '../users/users.service';
import { hashPassword } from '../helper/hash_password'; import { hashPassword } from '../helper/hash_password';
import { JwtService } from '@nestjs/jwt'; import { JwtService } from '@nestjs/jwt';
import { User } from '../users/entities/user.entity';
@Injectable() @Injectable()
export class AuthService { export class AuthService {
@ -22,10 +23,11 @@ export class AuthService {
return null; return null;
} }
async login(user: any) { async login(user: User) {
const payload = { const payload = {
username: user.username, username: user.username,
sub: user.userId, sub: user.id,
role: user.roles.name,
}; };
return { return {

View File

@ -1,4 +1,10 @@
import { forwardRef, HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common'; import {
forwardRef,
HttpException,
HttpStatus,
Inject,
Injectable,
} from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto'; import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto'; import { UpdateUserDto } from './dto/update-user.dto';
import { Connection, EntityNotFoundError, Repository } from 'typeorm'; import { Connection, EntityNotFoundError, Repository } from 'typeorm';
@ -10,14 +16,18 @@ import { CoaService } from 'src/transaction/coa.service';
import { balanceType, coaType } from 'src/helper/enum-list'; import { balanceType, coaType } from 'src/helper/enum-list';
import { RoleService } from 'src/configurable/roles.service'; import { RoleService } from 'src/configurable/roles.service';
import { InputCoaDto } from 'src/transaction/dto/input-coa.dto'; import { InputCoaDto } from 'src/transaction/dto/input-coa.dto';
import * as uuid from "uuid"; import * as uuid from 'uuid';
@Injectable() @Injectable()
export class UsersService { export class UsersService {
constructor( constructor(
@InjectRepository(User) @InjectRepository(User)
private usersRepository: Repository<User>, private usersRepository: Repository<User>,
@Inject(forwardRef(() => CoaService)) @Inject(
forwardRef(() => {
return CoaService;
}),
)
private coaService: CoaService, private coaService: CoaService,
private roleService: RoleService, private roleService: RoleService,
private connection: Connection, private connection: Connection,
@ -27,51 +37,54 @@ export class UsersService {
const roles = await this.roleService.findOne(createUserDto.roleId); const roles = await this.roleService.findOne(createUserDto.roleId);
const superior = await this.findByUsername(currentUser.username); const superior = await this.findByUsername(currentUser.username);
const salt = randomStringGenerator(); const salt = randomStringGenerator();
let userData = new User(); const userData = new User();
userData.id = uuid.v4(); userData.id = uuid.v4();
userData.username = createUserDto.username, (userData.username = createUserDto.username),
userData.password = await hashPassword(createUserDto.password, salt), (userData.password = await hashPassword(createUserDto.password, salt)),
userData.salt = salt, (userData.salt = salt),
userData.superior = superior, (userData.superior = superior),
userData.roles = roles (userData.roles = roles);
await this.connection.transaction(async (manager) => { await this.connection.transaction(async (manager) => {
const result = await manager.insert(User,userData); const result = await manager.insert(User, userData);
let dataCoaWallet = new InputCoaDto(); const dataCoaWallet = new InputCoaDto();
dataCoaWallet.user = userData; dataCoaWallet.user = userData;
dataCoaWallet.balanceType = balanceType.CREDIT; dataCoaWallet.balanceType = balanceType.CREDIT;
dataCoaWallet.type = coaType.WALLET; dataCoaWallet.type = coaType.WALLET;
dataCoaWallet.coaEntityManager = manager; dataCoaWallet.coaEntityManager = manager;
if(createUserDto.superior){ if (createUserDto.superior) {
let dataCoaAP = new InputCoaDto(); const dataCoaAP = new InputCoaDto();
dataCoaAP.user = userData; dataCoaAP.user = userData;
dataCoaAP.balanceType = balanceType.CREDIT; dataCoaAP.balanceType = balanceType.CREDIT;
dataCoaAP.relatedUserId = superior.id; dataCoaAP.relatedUserId = superior.id;
dataCoaAP.type = coaType.ACCOUNT_PAYABLE; dataCoaAP.type = coaType.ACCOUNT_PAYABLE;
dataCoaAP.coaEntityManager = manager; dataCoaAP.coaEntityManager = manager;
let dataCoaAR = new InputCoaDto(); const dataCoaAR = new InputCoaDto();
dataCoaAR.user = userData; dataCoaAR.user = userData;
dataCoaAR.balanceType = balanceType.DEBIT; dataCoaAR.balanceType = balanceType.DEBIT;
dataCoaAR.relatedUserId = superior.id; dataCoaAR.relatedUserId = superior.id;
dataCoaAR.type = coaType.ACCOUNT_RECEIVABLE; dataCoaAR.type = coaType.ACCOUNT_RECEIVABLE;
dataCoaAR.coaEntityManager = manager; dataCoaAR.coaEntityManager = manager;
await this.coaService.create(dataCoaAP); await this.coaService.create(dataCoaAP);
await this.coaService.create(dataCoaAR); await this.coaService.create(dataCoaAR);
} }
await this.coaService.create(dataCoaWallet);
}) await this.coaService.create(dataCoaWallet);
});
return userData; return userData;
} }
findAll(page:number) { findAll(page: number) {
return this.usersRepository.findAndCount({ return this.usersRepository.findAndCount({
skip: page * 10, skip: page * 10,
take: 10, take: 10,
@ -81,12 +94,12 @@ export class UsersService {
}); });
} }
findByRoles(relationId:string,page:number) { findByRoles(relationId: string, page: number) {
return this.usersRepository.findAndCount({ return this.usersRepository.findAndCount({
skip: page * 10, skip: page * 10,
take: 10, take: 10,
where:{ where: {
roles:relationId roles: relationId,
}, },
order: { order: {
updatedAt: 'DESC', updatedAt: 'DESC',
@ -96,7 +109,7 @@ export class UsersService {
async findExist(id: string) { async findExist(id: string) {
try { try {
return await this.usersRepository.findOneOrFail(id); return await this.usersRepository.findOneOrFail(id);
} catch (e) { } catch (e) {
if (e instanceof EntityNotFoundError) { if (e instanceof EntityNotFoundError) {
throw new HttpException( throw new HttpException(
@ -114,9 +127,9 @@ export class UsersService {
async findByUsername(username: string) { async findByUsername(username: string) {
try { try {
return await this.usersRepository.findOneOrFail({ return await this.usersRepository.findOneOrFail({
username:username username: username,
}); });
} catch (e) { } catch (e) {
if (e instanceof EntityNotFoundError) { if (e instanceof EntityNotFoundError) {
throw new HttpException( throw new HttpException(
@ -133,17 +146,19 @@ export class UsersService {
} }
async findOne(id: string) { async findOne(id: string) {
const coa = await this.coaService.findByUser(id,coaType.WALLET); const coa = await this.coaService.findByUser(id, coaType.WALLET);
try { try {
const userData = await this.usersRepository.findOneOrFail({ const userData = await this.usersRepository.findOneOrFail({
where: { where: {
id: id id: id,
}, },
relations:["roles","superior"] relations: ['roles', 'superior'],
}); });
return { return {
...userData, ...userData,
wallet:coa.amount, wallet: coa.amount,
}; };
} catch (e) { } catch (e) {
if (e instanceof EntityNotFoundError) { if (e instanceof EntityNotFoundError) {
@ -207,6 +222,7 @@ export class UsersService {
where: { where: {
username, username,
}, },
relations: ['roles'],
}); });
} }
} }