hedgedoc/commons/src/permissions/permissions.ts
Tilman Vatteroth 34bf8f16b1 fix: format code
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
2023-07-19 12:36:32 +02:00

53 lines
1.5 KiB
TypeScript

/*
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { NotePermissions, SpecialGroup } from './permissions.types.js'
/**
* Checks if the given user is the owner of a note.
*
* @param permissions The permissions of the note to check
* @param user The username of the user
* @return True if the user is the owner of the note
*/
export const userIsOwner = (
permissions: NotePermissions,
user?: string
): boolean => {
return !!user && permissions.owner === user
}
/**
* Checks if the given user may edit a note.
*
* @param permissions The permissions of the note to check
* @param user The username of the user
* @return True if the user has the permission to edit the note
*/
export const userCanEdit = (
permissions: NotePermissions,
user?: string
): boolean => {
const isOwner = userIsOwner(permissions, user)
const mayWriteViaUserPermission = permissions.sharedToUsers.some(
(value) => value.canEdit && value.username === user
)
const mayWriteViaGroupPermission =
!!user &&
permissions.sharedToGroups.some(
(value) =>
value.groupName === (SpecialGroup.LOGGED_IN as string) && value.canEdit
)
const everyoneMayWriteViaGroupPermission = permissions.sharedToGroups.some(
(value) =>
value.groupName === (SpecialGroup.EVERYONE as string) && value.canEdit
)
return (
isOwner ||
mayWriteViaUserPermission ||
mayWriteViaGroupPermission ||
everyoneMayWriteViaGroupPermission
)
}