refactor: replace permission check methods with ordered permission enum

This commit replaces the "mayWrite", "mayRead" and "checkPermissionOnNote"
functions with one that returns a sortable permission value.
This is done because many places in the code need to do actions based on the fact if
the user has no, read or write access. If done with the may-functions then the permission
data need to be looked through multiple times.

Also, the whole check code is split into more functions that are tested separately and make it easier
to understand the process.

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-05-10 22:50:03 +02:00
parent 4e298cccfb
commit a852c79947
15 changed files with 925 additions and 787 deletions

View file

@ -29,6 +29,7 @@ import { NotesModule } from '../../notes/notes.module';
import { NotesService } from '../../notes/notes.service';
import { Tag } from '../../notes/tag.entity';
import { NoteGroupPermission } from '../../permissions/note-group-permission.entity';
import { NotePermission } from '../../permissions/note-permission.enum';
import { NoteUserPermission } from '../../permissions/note-user-permission.entity';
import { PermissionsModule } from '../../permissions/permissions.module';
import { PermissionsService } from '../../permissions/permissions.service';
@ -221,15 +222,15 @@ describe('Websocket gateway', () => {
});
jest
.spyOn(permissionsService, 'mayRead')
.spyOn(permissionsService, 'determinePermission')
.mockImplementation(
(user: User | null, note: Note): Promise<boolean> =>
Promise.resolve(
(user === mockUser &&
note === mockedNote &&
userHasReadPermissions) ||
(user === null && note === mockedGuestNote),
),
async (user: User | null, note: Note): Promise<NotePermission> =>
(user === mockUser &&
note === mockedNote &&
userHasReadPermissions) ||
(user === null && note === mockedGuestNote)
? NotePermission.READ
: NotePermission.DENY,
);
const mockedRealtimeNote = Mock.of<RealtimeNote>({