2022-10-13 23:23:33 +02:00
|
|
|
import {
|
|
|
|
|
CanActivate,
|
|
|
|
|
ExecutionContext,
|
|
|
|
|
Injectable,
|
|
|
|
|
NotFoundException,
|
|
|
|
|
} from "@nestjs/common";
|
|
|
|
|
import { Request } from "express";
|
|
|
|
|
import * as moment from "moment";
|
|
|
|
|
import { PrismaService } from "src/prisma/prisma.service";
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class ShareTokenSecurity implements CanActivate {
|
2022-10-16 00:13:08 +02:00
|
|
|
constructor(private prisma: PrismaService) {}
|
2022-10-13 23:23:33 +02:00
|
|
|
|
|
|
|
|
async canActivate(context: ExecutionContext) {
|
|
|
|
|
const request: Request = context.switchToHttp().getRequest();
|
|
|
|
|
const shareId = Object.prototype.hasOwnProperty.call(
|
|
|
|
|
request.params,
|
2023-08-17 19:54:26 +07:00
|
|
|
"shareId",
|
2022-10-13 23:23:33 +02:00
|
|
|
)
|
|
|
|
|
? request.params.shareId
|
|
|
|
|
: request.params.id;
|
|
|
|
|
|
|
|
|
|
const share = await this.prisma.share.findUnique({
|
|
|
|
|
where: { id: shareId },
|
|
|
|
|
include: { security: true },
|
|
|
|
|
});
|
|
|
|
|
|
2023-01-31 15:22:08 +01:00
|
|
|
if (
|
|
|
|
|
!share ||
|
|
|
|
|
(moment().isAfter(share.expiration) &&
|
|
|
|
|
!moment(share.expiration).isSame(0))
|
|
|
|
|
)
|
|
|
|
|
throw new NotFoundException("Share not found");
|
2022-10-13 23:23:33 +02:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|