Impersination backend
This commit is contained in:
@@ -22,7 +22,7 @@ import { LogModule } from './modules/log/log.module';
|
||||
envFilePath: ['.env'],
|
||||
isGlobal: true,
|
||||
}),
|
||||
CacheModule.register({ ttl: 1000, isGlobal: true }),
|
||||
// CacheModule.register({ ttl: 1000, isGlobal: true }),
|
||||
DatabaseModule,
|
||||
AuthModule,
|
||||
UserModule,
|
||||
@@ -38,10 +38,10 @@ import { LogModule } from './modules/log/log.module';
|
||||
providers: [
|
||||
AppService,
|
||||
AuthGuard,
|
||||
{
|
||||
provide: APP_INTERCEPTOR,
|
||||
useClass: CacheInterceptor,
|
||||
},
|
||||
// {
|
||||
// provide: APP_INTERCEPTOR,
|
||||
// useClass: CacheInterceptor,
|
||||
// },
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
||||
@@ -35,7 +35,7 @@ export class AuthGuard implements CanActivate {
|
||||
if (payload.type != 'access') {
|
||||
throw new UnauthorizedException('wrong token');
|
||||
}
|
||||
const user = await this.authService.getUserById(payload.id);
|
||||
const user = await this.authService.getUserById(payload.id, true);
|
||||
if (!user.isActive) {
|
||||
throw new HttpException('not active', HttpStatus.FORBIDDEN);
|
||||
}
|
||||
|
||||
22
api/src/model/entitites/impersination.entity.ts
Normal file
22
api/src/model/entitites/impersination.entity.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Entity, PrimaryGeneratedColumn, ManyToOne, JoinColumn, Column } from "typeorm";
|
||||
import { User } from "./user/user.entity";
|
||||
|
||||
@Entity()
|
||||
export class Impersonation {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@ManyToOne(() => User, { nullable: false, eager: true })
|
||||
@JoinColumn({ name: 'fromUserId' })
|
||||
fromUser: User;
|
||||
|
||||
@ManyToOne(() => User, { nullable: false, eager: true })
|
||||
@JoinColumn({ name: 'toUserId' })
|
||||
toUser: User;
|
||||
|
||||
@Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' })
|
||||
startedAt: Date;
|
||||
|
||||
@Column({ type: 'datetime', nullable: true })
|
||||
endedAt?: Date;
|
||||
}
|
||||
11
api/src/model/repositories/impersination.repository.ts
Normal file
11
api/src/model/repositories/impersination.repository.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Repository, DataSource } from 'typeorm';
|
||||
import { Impersonation } from '../entitites/impersination.entity';
|
||||
|
||||
@Injectable()
|
||||
export class ImpersonationRepository extends Repository<Impersonation> {
|
||||
constructor(dataSource: DataSource) {
|
||||
super(Impersonation, dataSource.createEntityManager());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import { AuthService } from './auth.service';
|
||||
import { AuthCodeDto } from 'src/model/dto';
|
||||
import { User } from 'src/model/entitites';
|
||||
import { AuthGuard } from 'src/core/guards/auth.guard';
|
||||
import { AuthenticatedRequest } from 'src/model/interface/authenticated-request.interface';
|
||||
|
||||
@Controller('auth')
|
||||
export class AuthController {
|
||||
@@ -30,7 +31,7 @@ export class AuthController {
|
||||
|
||||
@UseGuards(AuthGuard)
|
||||
@Get('me')
|
||||
getMe(@Req() req: any) {
|
||||
getMe(@Req() req: AuthenticatedRequest) {
|
||||
return req.user;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,11 +8,14 @@ import { JwtService } from '@nestjs/jwt';
|
||||
import { IExternalAccessPayload, IPayload } from 'src/model/interface';
|
||||
import { User } from 'src/model/entitites';
|
||||
import { LogService, LogType } from '../log/log.service';
|
||||
import { ImpersonationRepository } from 'src/model/repositories/impersination.repository';
|
||||
import { IsNull } from 'typeorm';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(
|
||||
private userRepo: UserRepository,
|
||||
private impersinationRepo: ImpersonationRepository,
|
||||
private readonly http: HttpService,
|
||||
private configService: ConfigService,
|
||||
private jwt: JwtService,
|
||||
@@ -119,9 +122,21 @@ export class AuthService {
|
||||
return bodyFormData;
|
||||
}
|
||||
|
||||
getUserById(id: string): Promise<User> {
|
||||
async getUserById(id: string, withImpersination = false): Promise<User> {
|
||||
|
||||
this.log.log(LogType.Auth, null);
|
||||
return this.userRepo.findById(id);
|
||||
let user = await this.userRepo.findById(id);
|
||||
if (withImpersination) {
|
||||
const impersination = await this.impersinationRepo.findOne({
|
||||
where: { fromUser: { id: user.id }, endedAt: IsNull() },
|
||||
relations: ['toUser']
|
||||
});
|
||||
if (impersination) {
|
||||
return this.userRepo.findById(impersination.toUser.id)
|
||||
}
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
async getNewToken(refresh: string) {
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
} from 'src/model/entitites';
|
||||
import { EmailLog } from 'src/model/entitites/log';
|
||||
import { KeySystem } from 'src/model/entitites/system.entity';
|
||||
import { Impersonation } from 'src/model/entitites/impersination.entity';
|
||||
import { UserSettings } from 'src/model/entitites/user/user.settings.entity';
|
||||
import {
|
||||
ActivityRepository,
|
||||
@@ -26,6 +27,7 @@ import {
|
||||
} from 'src/model/repositories';
|
||||
import { KeyHandoutRepository } from 'src/model/repositories/key-handout.repository';
|
||||
import { EmailLogRepository } from 'src/model/repositories/log';
|
||||
import { ImpersonationRepository } from 'src/model/repositories/impersination.repository';
|
||||
|
||||
const ENTITIES = [
|
||||
User,
|
||||
@@ -39,6 +41,7 @@ const ENTITIES = [
|
||||
Activity,
|
||||
EmailLog,
|
||||
UserSettings,
|
||||
Impersonation
|
||||
];
|
||||
const REPOSITORIES = [
|
||||
UserRepository,
|
||||
@@ -51,7 +54,8 @@ const REPOSITORIES = [
|
||||
KeyHandoutRepository,
|
||||
ActivityRepository,
|
||||
EmailLogRepository,
|
||||
UserSettingsRepository
|
||||
UserSettingsRepository,
|
||||
ImpersonationRepository
|
||||
];
|
||||
|
||||
@Module({
|
||||
|
||||
@@ -11,7 +11,7 @@ export class HelperService {
|
||||
private readonly systemRepository: KeySystemRepository,
|
||||
private readonly cylinderRepository: CylinderRepository,
|
||||
private readonly keyRepo: KeyRepository,
|
||||
private cacheManager: Cache
|
||||
// private cacheManager: Cache
|
||||
) {}
|
||||
|
||||
|
||||
@@ -61,11 +61,11 @@ export class HelperService {
|
||||
}
|
||||
|
||||
async cache() {
|
||||
const value = await this.cacheManager.store.keys()
|
||||
console.log(value)
|
||||
// const value = await this.cacheManager.store.keys()
|
||||
// console.log(value)
|
||||
}
|
||||
|
||||
async deleteKeyArchiveCache() {
|
||||
await this.cacheManager.del('/key/archive');
|
||||
// await this.cacheManager.del('/key/archive');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user