mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 07:07:21 +00:00
add client relation btw user and client
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
import { Entity, Column, Unique } from 'typeorm';
|
import { Entity, Column, Unique, OneToMany } from 'typeorm';
|
||||||
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
import { AbstractEntity } from '../../abstract/entities/abstract.entity';
|
||||||
import { ClientDto } from '../dtos';
|
import { ClientDto } from '../dtos';
|
||||||
|
import { UserEntity } from '../../user/entities';
|
||||||
|
|
||||||
@Entity({ name: 'clients' })
|
@Entity({ name: 'clients' })
|
||||||
@Unique(['clientId'])
|
@Unique(['clientId'])
|
||||||
@ -39,4 +40,7 @@ export class ClientEntity extends AbstractEntity<ClientDto> {
|
|||||||
|
|
||||||
@Column('simple-array')
|
@Column('simple-array')
|
||||||
scopes: string[];
|
scopes: string[];
|
||||||
|
|
||||||
|
@OneToMany(() => UserEntity, (user) => user.client)
|
||||||
|
users: UserEntity[];
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,7 @@ import { VisitorPasswordEntity } from '../../visitor-password/entities';
|
|||||||
import { InviteUserEntity } from '../../Invite-user/entities';
|
import { InviteUserEntity } from '../../Invite-user/entities';
|
||||||
import { ProjectEntity } from '../../project/entities';
|
import { ProjectEntity } from '../../project/entities';
|
||||||
import { SpaceEntity } from '../../space/entities/space.entity';
|
import { SpaceEntity } from '../../space/entities/space.entity';
|
||||||
|
import { ClientEntity } from '../../client/entities';
|
||||||
|
|
||||||
@Entity({ name: 'user' })
|
@Entity({ name: 'user' })
|
||||||
export class UserEntity extends AbstractEntity<UserDto> {
|
export class UserEntity extends AbstractEntity<UserDto> {
|
||||||
@ -143,6 +144,13 @@ export class UserEntity extends AbstractEntity<UserDto> {
|
|||||||
})
|
})
|
||||||
@JoinColumn({ name: 'project_uuid' })
|
@JoinColumn({ name: 'project_uuid' })
|
||||||
public project: ProjectEntity;
|
public project: ProjectEntity;
|
||||||
|
|
||||||
|
@ManyToOne(() => ClientEntity, (client) => client.users, {
|
||||||
|
nullable: true,
|
||||||
|
})
|
||||||
|
@JoinColumn({ name: 'client_uuid' })
|
||||||
|
public client: ClientEntity;
|
||||||
|
|
||||||
constructor(partial: Partial<UserEntity>) {
|
constructor(partial: Partial<UserEntity>) {
|
||||||
super();
|
super();
|
||||||
Object.assign(this, partial);
|
Object.assign(this, partial);
|
||||||
|
@ -37,8 +37,12 @@ export class UserAuthController {
|
|||||||
summary: ControllerRoute.AUTHENTICATION.ACTIONS.SIGN_UP_SUMMARY,
|
summary: ControllerRoute.AUTHENTICATION.ACTIONS.SIGN_UP_SUMMARY,
|
||||||
description: ControllerRoute.AUTHENTICATION.ACTIONS.SIGN_UP_DESCRIPTION,
|
description: ControllerRoute.AUTHENTICATION.ACTIONS.SIGN_UP_DESCRIPTION,
|
||||||
})
|
})
|
||||||
async signUp(@Body() userSignUpDto: UserSignUpDto) {
|
async signUp(@Body() userSignUpDto: UserSignUpDto, @Req() req: any) {
|
||||||
const signupUser = await this.userAuthService.signUp(userSignUpDto);
|
const clientUuid = req.client.uuid;
|
||||||
|
const signupUser = await this.userAuthService.signUp(
|
||||||
|
userSignUpDto,
|
||||||
|
clientUuid,
|
||||||
|
);
|
||||||
return {
|
return {
|
||||||
statusCode: HttpStatus.CREATED,
|
statusCode: HttpStatus.CREATED,
|
||||||
data: {
|
data: {
|
||||||
|
@ -34,7 +34,10 @@ export class UserAuthService {
|
|||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async signUp(userSignUpDto: UserSignUpDto): Promise<UserEntity> {
|
async signUp(
|
||||||
|
userSignUpDto: UserSignUpDto,
|
||||||
|
clientUuid?: string,
|
||||||
|
): Promise<UserEntity> {
|
||||||
const findUser = await this.findUser(userSignUpDto.email);
|
const findUser = await this.findUser(userSignUpDto.email);
|
||||||
|
|
||||||
if (findUser) {
|
if (findUser) {
|
||||||
@ -63,6 +66,7 @@ export class UserAuthService {
|
|||||||
hasAcceptedAppAgreement,
|
hasAcceptedAppAgreement,
|
||||||
password: hashedPassword,
|
password: hashedPassword,
|
||||||
roleType: { uuid: spaceMemberRole.uuid },
|
roleType: { uuid: spaceMemberRole.uuid },
|
||||||
|
client: { uuid: clientUuid },
|
||||||
region: regionUuid
|
region: regionUuid
|
||||||
? {
|
? {
|
||||||
uuid: regionUuid,
|
uuid: regionUuid,
|
||||||
|
@ -20,6 +20,8 @@ export class ClientGuard extends AuthGuard('jwt') {
|
|||||||
if (!this.validateToken(decoded)) {
|
if (!this.validateToken(decoded)) {
|
||||||
throw new UnauthorizedException('Invalid token');
|
throw new UnauthorizedException('Invalid token');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
request.client = (decoded as jwt.JwtPayload).client;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new UnauthorizedException('Invalid token');
|
throw new UnauthorizedException('Invalid token');
|
||||||
}
|
}
|
||||||
@ -36,14 +38,6 @@ export class ClientGuard extends AuthGuard('jwt') {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private validateToken(decoded: any): boolean {
|
private validateToken(decoded: any): boolean {
|
||||||
if (
|
return decoded?.client?.clientId && decoded?.client?.uuid;
|
||||||
decoded &&
|
|
||||||
decoded.client &&
|
|
||||||
decoded.client.clientId &&
|
|
||||||
decoded.client.uuid
|
|
||||||
) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user