mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-07-30 18:27:45 -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)
|
||||
}
|
||||
}
|
@@ -23,6 +23,7 @@ import {
|
||||
SETTINGS,
|
||||
SETTINGS_KEYS,
|
||||
} from '../data/paperless-uisettings'
|
||||
import { PermissionsService } from './permissions.service'
|
||||
import { SavedViewService } from './rest/saved-view.service'
|
||||
import { ToastService } from './toast.service'
|
||||
|
||||
@@ -45,7 +46,6 @@ export class SettingsService {
|
||||
protected baseUrl: string = environment.apiBaseUrl + 'ui_settings/'
|
||||
|
||||
private settings: Object = {}
|
||||
private permissions: string[]
|
||||
|
||||
public displayName: string
|
||||
|
||||
@@ -59,7 +59,8 @@ export class SettingsService {
|
||||
@Inject(LOCALE_ID) private localeId: string,
|
||||
protected http: HttpClient,
|
||||
private toastService: ToastService,
|
||||
private savedViewService: SavedViewService
|
||||
private savedViewService: SavedViewService,
|
||||
private permissionsService: PermissionsService
|
||||
) {
|
||||
this.renderer = rendererFactory.createRenderer(null, null)
|
||||
}
|
||||
@@ -75,7 +76,7 @@ export class SettingsService {
|
||||
if (this.settings['language']?.length)
|
||||
this.setLanguage(this.settings['language'])
|
||||
this.displayName = uisettings.display_name.trim()
|
||||
this.permissions = uisettings.permissions
|
||||
this.permissionsService.initialize(uisettings.permissions)
|
||||
})
|
||||
)
|
||||
}
|
||||
@@ -457,8 +458,4 @@ export class SettingsService {
|
||||
this.savedViewService.dashboardViews.length == 0
|
||||
)
|
||||
}
|
||||
|
||||
currentUserCan(permission: string): boolean {
|
||||
return this.permissions.includes(permission)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user