mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-09-16 21:55:37 -05:00
Refactor permissions to use enums, permissions service
This commit is contained in:
49
src-ui/src/app/services/permissions.service.ts
Normal file
49
src-ui/src/app/services/permissions.service.ts
Normal 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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user