paperless-ngx/src-ui/src/app/services/permissions.service.ts
2023-03-18 13:59:17 -07:00

102 lines
2.5 KiB
TypeScript

import { Injectable } from '@angular/core'
import { ObjectWithPermissions } from '../data/object-with-permissions'
import { PaperlessUser } from '../data/paperless-user'
export enum PermissionAction {
Add = 'add',
View = 'view',
Change = 'change',
Delete = 'delete',
}
export enum PermissionType {
Document = '%s_document',
Tag = '%s_tag',
Correspondent = '%s_correspondent',
DocumentType = '%s_documenttype',
StoragePath = '%s_storagepath',
SavedView = '%s_savedview',
PaperlessTask = '%s_paperlesstask',
UISettings = '%s_uisettings',
Note = '%s_note',
MailAccount = '%s_mailaccount',
MailRule = '%s_mailrule',
User = '%s_user',
Group = '%s_group',
Admin = '%s_logentry',
}
@Injectable({
providedIn: 'root',
})
export class PermissionsService {
private permissions: string[]
private currentUser: PaperlessUser
public initialize(permissions: string[], currentUser: PaperlessUser) {
this.permissions = permissions
this.currentUser = currentUser
}
public currentUserCan(
action: PermissionAction,
type: PermissionType
): boolean {
return this.permissions.includes(this.getPermissionCode(action, type))
}
public currentUserOwnsObject(object: ObjectWithPermissions): boolean {
return (
!object ||
!object.owner ||
this.currentUser.is_superuser ||
object.owner === this.currentUser.id
)
}
public currentUserHasObjectPermissions(
action: string,
object: ObjectWithPermissions
): boolean {
return (
this.currentUserOwnsObject(object) ||
(object.permissions[action]['users'] as Array<number>)?.includes(
this.currentUser.id
)
)
}
public getPermissionCode(
action: PermissionAction,
type: PermissionType
): string {
return type.replace('%s', action)
}
public getPermissionKeys(permissionStr: string): {
actionKey: string
typeKey: string
} {
const matches = permissionStr.match(/(.+)_/)
let typeKey
let actionKey
if (matches?.length > 0) {
const action = matches[1]
const actionIndex = Object.values(PermissionAction).indexOf(
action as PermissionAction
)
if (actionIndex > -1) {
actionKey = Object.keys(PermissionAction)[actionIndex]
}
const typeIndex = Object.values(PermissionType).indexOf(
permissionStr.replace(action, '%s') as PermissionType
)
if (typeIndex > -1) {
typeKey = Object.keys(PermissionType)[typeIndex]
}
}
return { actionKey, typeKey }
}
}