Refactor permissions to use enums, permissions service

This commit is contained in:
Michael Shamoon
2022-11-12 04:03:35 -08:00
parent 59e359ff98
commit 96a29883cd
39 changed files with 335 additions and 134 deletions

View File

@@ -0,0 +1,49 @@
import { Injectable } from '@angular/core'
export enum PermissionAction {
Add = 'add',
View = 'view',
Change = 'change',
Delete = 'delete',
}
export enum PermissionType {
Document = 'documents.%s_document',
Tag = 'documents.%s_tag',
Correspondent = 'documents.%s_correspondent',
DocumentType = 'documents.%s_documenttype',
StoragePath = 'documents.%s_storagepath',
SavedView = 'documents.%s_savedview',
PaperlessTask = 'documents.%s_paperlesstask',
UISettings = 'documents.%s_uisettings',
Comment = 'documents.%s_comment',
Log = 'admin.%s_logentry',
MailAccount = 'paperless_mail.%s_mailaccount',
MailRule = 'paperless_mail.%s_mailrule',
Auth = 'auth.%s_user',
Admin = 'admin.%s_logentry',
}
export interface PaperlessPermission {
action: PermissionAction
type: PermissionType
}
@Injectable({
providedIn: 'root',
})
export class PermissionsService {
private permissions: string[]
public initialize(permissions: string[]) {
this.permissions = permissions
}
public currentUserCan(permission: PaperlessPermission): boolean {
return this.permissions.includes(this.getPermissionCode(permission))
}
private getPermissionCode(permission: PaperlessPermission): string {
return permission.type.replace('%s', permission.action)
}
}