1
0
mirror of https://github.com/stonith404/pingvin-share.git synced 2024-06-30 06:30:11 +02:00

refactor: move guard checks to service

This commit is contained in:
Elias Schneider 2023-01-31 13:53:23 +01:00
parent 233c26e5cf
commit cd9d828686
No known key found for this signature in database
GPG Key ID: 07E623B294202B6C
3 changed files with 16 additions and 19 deletions

View File

@ -34,12 +34,10 @@ export class ShareSecurityGuard implements CanActivate {
include: { security: true },
});
if (
!share ||
(moment().isAfter(share.expiration) &&
moment(share.expiration).unix() !== 0)
)
throw new NotFoundException("Share not found");
const isExpired =
moment().isAfter(share.expiration) && !moment(share.expiration).isSame(0);
if (!share || isExpired) throw new NotFoundException("Share not found");
if (share.security?.password && !shareToken)
throw new ForbiddenException(

View File

@ -1,7 +1,6 @@
import {
CanActivate,
ExecutionContext,
ForbiddenException,
Injectable,
NotFoundException,
} from "@nestjs/common";
@ -27,18 +26,10 @@ export class ShareTokenSecurity implements CanActivate {
include: { security: true },
});
if (
!share ||
(moment().isAfter(share.expiration) &&
!moment(share.expiration).isSame(0))
)
throw new NotFoundException("Share not found");
const isExpired =
moment().isAfter(share.expiration) && !moment(share.expiration).isSame(0);
if (share.security?.maxViews && share.security.maxViews <= share.views)
throw new ForbiddenException(
"Maximum views exceeded",
"share_max_views_exceeded"
);
if (!share || isExpired) throw new NotFoundException("Share not found");
return true;
}

View File

@ -273,8 +273,16 @@ export class ShareService {
if (
share?.security?.password &&
!(await argon.verify(share.security.password, password))
)
) {
throw new ForbiddenException("Wrong password");
}
if (share.security?.maxViews && share.security.maxViews <= share.views) {
throw new ForbiddenException(
"Maximum views exceeded",
"share_max_views_exceeded"
);
}
const token = await this.generateShareToken(shareId);
await this.increaseViewCount(share);