mirror of https://github.com/ghostfolio/ghostfolio
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.4 KiB
52 lines
1.4 KiB
import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service';
|
|
import { hasPermission, permissions } from '@ghostfolio/common/permissions';
|
|
import type { RequestWithUser } from '@ghostfolio/common/types';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
import { REQUEST } from '@nestjs/core';
|
|
import { AccessType } from '@prisma/client';
|
|
|
|
@Injectable()
|
|
export class ImpersonationService {
|
|
public constructor(
|
|
private readonly prismaService: PrismaService,
|
|
@Inject(REQUEST) private readonly request: RequestWithUser
|
|
) {}
|
|
|
|
public async validateImpersonationId(aId = '') {
|
|
if (this.request.user) {
|
|
const accessObject = await this.prismaService.access.findFirst({
|
|
where: {
|
|
granteeUserId: this.request.user.id,
|
|
id: aId
|
|
}
|
|
});
|
|
|
|
if (accessObject?.userId) {
|
|
return accessObject.userId;
|
|
} else if (
|
|
hasPermission(
|
|
this.request.user.permissions,
|
|
permissions.impersonateAllUsers
|
|
)
|
|
) {
|
|
return aId;
|
|
}
|
|
} else {
|
|
// Public access
|
|
const accessObject = await this.prismaService.access.findFirst({
|
|
where: {
|
|
granteeUserId: null,
|
|
type: AccessType.PUBLIC,
|
|
user: { id: aId }
|
|
}
|
|
});
|
|
|
|
if (accessObject?.userId) {
|
|
return accessObject.userId;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|