Add guest permission mock and checking

mocked by attribute of permission service

Signed-off-by: Yannick Bungers <git@innay.de>
This commit is contained in:
Yannick Bungers 2021-02-13 14:00:29 +01:00 committed by David Mehren
parent 48dedfead8
commit 606d271296
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3

View file

@ -7,16 +7,25 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { User } from '../users/user.entity'; import { User } from '../users/user.entity';
import { Note } from '../notes/note.entity'; import { Note } from '../notes/note.entity';
import { ConsoleLoggerService } from '../logger/console-logger.service';
// TODO move to config or remove
export enum GuestPermission {
DENY = 'deny',
READ = 'read',
WRITE = 'write',
CREATE = 'create',
CREATE_ALIAS = 'createAlias',
}
@Injectable() @Injectable()
export class PermissionsService { export class PermissionsService {
constructor(private readonly logger: ConsoleLoggerService) {} public guestPermission: GuestPermission; // TODO change to configOption
mayRead(user: User, note: Note): boolean { mayRead(user: User, note: Note): boolean {
if (this.isOwner(user, note)) return true; if (this.isOwner(user, note)) return true;
if (this.hasPermissionUser(user, note, false)) return true; if (this.hasPermissionUser(user, note, false)) return true;
// noinspection RedundantIfStatementJS
if (this.hasPermissionGroup(user, note, false)) return true; if (this.hasPermissionGroup(user, note, false)) return true;
return false; return false;
@ -27,19 +36,30 @@ export class PermissionsService {
if (this.hasPermissionUser(user, note, true)) return true; if (this.hasPermissionUser(user, note, true)) return true;
// noinspection RedundantIfStatementJS
if (this.hasPermissionGroup(user, note, true)) return true; if (this.hasPermissionGroup(user, note, true)) return true;
return false; return false;
} }
mayCreate(user: User): boolean { mayCreate(user: User): boolean {
if (user) { if (user) {
// TODO: (config.guestPermission == "create")
return true; return true;
} else {
if (
this.guestPermission == GuestPermission.CREATE ||
this.guestPermission == GuestPermission.CREATE_ALIAS
) {
// TODO change to guestPermission to config option
return true;
}
} }
return false; return false;
} }
isOwner(user: User, note: Note): boolean { isOwner(user: User, note: Note): boolean {
if (!user) return false; if (!user) return false;
if (!note.owner) return false;
return note.owner.id === user.id; return note.owner.id === user.id;
} }
@ -68,7 +88,16 @@ export class PermissionsService {
wantEdit: boolean, wantEdit: boolean,
): boolean { ): boolean {
// TODO: Get real config value // TODO: Get real config value
const guestsAllowed = false; // (config.guestPermission == "write" || config.guestPermission == "read" && !wantEdit) let guestsAllowed = false;
switch (this.guestPermission) {
case GuestPermission.CREATE_ALIAS:
case GuestPermission.CREATE:
case GuestPermission.WRITE:
guestsAllowed = true;
break;
case GuestPermission.READ:
guestsAllowed = !wantEdit;
}
for (const groupPermission of note.groupPermissions) { for (const groupPermission of note.groupPermissions) {
if (groupPermission.canEdit || !wantEdit) { if (groupPermission.canEdit || !wantEdit) {
// Handle special groups // Handle special groups