Skip to content

Commit

Permalink
feat: load permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryT-CG committed Feb 17, 2024
1 parent 59b8786 commit 10d3c8d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 50 deletions.
18 changes: 3 additions & 15 deletions src/app/permission/app-detail/app-detail.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,10 @@
readonly
type="text"
class="w-full"
id="app_detail_create_permission_appName"
formControlName="appName"
id="app_detail_create_permission_app_id"
formControlName="appId"
/>
<label class="ocx-required-label" for="app_detail_create_permission_appName">
<label class="ocx-required-label" for="app_detail_create_permission_app_id">
{{ 'PERMISSION.APP_ID.APP' | translate }}</label
>
</span>
Expand Down Expand Up @@ -438,18 +438,6 @@
</label>
</span>
</div>
<div>
<span class="p-float-label">
<input
pInputText
type="text"
class="w-full"
id="app_detail_create_permission_name"
formControlName="name"
/>
<label for="app_detail_create_permission_name">{{ 'PERMISSION.NAME' | translate }} </label>
</span>
</div>
<div>
<span class="p-float-label">
<input
Expand Down
96 changes: 65 additions & 31 deletions src/app/permission/app-detail/app-detail.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ import {
Role,
CreateRoleRequest,
UpdateRoleRequest,
/* RolePageResult,
RoleSearchCriteria,
CreateRoleRequest,
Permission,
/*
PermissionSearchCriteria,
Assignment,
AssignmentSearchCriteria,
Expand All @@ -35,21 +33,14 @@ import {
//import { dropDownSortItemsByLabel, limitText } from 'src/app/shared/utils'
import { limitText } from 'src/app/shared/utils'

type RoleAssignments = { [key: string]: boolean }
type PermissionViewRow = {
id: string
key: string
resource?: string
action?: string
name?: string
description?: string
applicationId: string
applicationName?: string
roles: RoleAssignments
}
type App = Application & { isApp: boolean; type: AppType }
type AppType = 'WORKSPACE' | 'APP'
type AppRole = Role & { appId: string }
type RoleAssignments = { [key: string]: boolean }
type PermissionViewRow = Permission & {
key: string // combined resource and action => resource#action
roles: RoleAssignments
}

@Component({
templateUrl: './app-detail.component.html',
Expand Down Expand Up @@ -125,10 +116,9 @@ export class AppDetailComponent implements OnInit, OnDestroy {
if (userService.hasPermission('PERMISSION#DELETE')) this.myPermissions.push('PERMISSION#DELETE')

this.formGroupPermission = new FormGroup({
appName: new FormControl({ value: null, disabled: true }, [Validators.required]),
appId: new FormControl({ value: null, disabled: true }, [Validators.required]),
resource: new FormControl(null, [Validators.required, Validators.minLength(2), Validators.maxLength(50)]),
action: new FormControl(null, [Validators.required, Validators.minLength(2), Validators.maxLength(50)]),
name: new FormControl(null),
description: new FormControl(null)
})
this.formGroupRole = new FormGroup({
Expand Down Expand Up @@ -264,6 +254,8 @@ export class AppDetailComponent implements OnInit, OnDestroy {
this.currentApp = { ...result.stream[0], type: 'APP' } as App
this.log('loadApp => App:', this.currentApp)
this.prepareActionButtons()
this.permissionRows = []
this.preparePermissionTable(this.currentApp)
} else {
this.loadingServerIssue = true
this.loadingExceptionKey = 'EXCEPTIONS.HTTP_STATUS_0.APPS'
Expand Down Expand Up @@ -302,6 +294,54 @@ export class AppDetailComponent implements OnInit, OnDestroy {
})
}

/* 1. Prepare rows of the table: permissions of the <application> as Map
* key (resource#action): 'PERMISSION#READ'
* value: {resource: 'PERMISSION', action: 'READ', key: 'PERMISSION#READ', name: 'View permission matrix'
*/
private preparePermissionTable(app: App): void {
const permissionRows: Map<string, PermissionViewRow> = new Map()
// get permissions for the app
this.permApi
.searchPermissions({ permissionSearchCriteria: { appId: this.currentApp.appId } })
.pipe(catchError((error) => of(error)))
.subscribe((result) => {
if (result instanceof HttpErrorResponse) {
this.loadingServerIssue = true
this.loadingExceptionKey = 'EXCEPTIONS.HTTP_STATUS_' + result.status + '.PERMISSIONS'
console.error('searchPermissions() result:', result)
} else if (result instanceof Object) {
for (const permission of result.stream) {
permissionRows.set(permission.resource + '#' + permission.action, {
...permission,
key: permission.resource + '#' + permission.action,
roles: {}
})
}
this.roles.sort(this.sortRoleByName)
this.log('loadPermissions:', permissionRows)

// add permission rows of this app to permission table
this.permissionRows = this.permissionRows.concat(
Array.from(permissionRows.values()).sort(this.sortPermissionRowByKey)
)
this.loading = false
} else {
this.loadingServerIssue = true
this.loadingExceptionKey = 'EXCEPTIONS.HTTP_STATUS_0.ROLES'
console.error('searchPermissions() => unknown response:', result)
}
})
/*
// Fill permission rows with role assignments of the current application
permissionRows.forEach((row) => {
this.currentApp?.assignments?.forEach((ra) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
row.roles[ra.role!] = ra.permissionKeys?.includes(row.key) || false
})
})
*/
}

/*********************************************
* Table Filter
*/
Expand Down Expand Up @@ -343,7 +383,6 @@ export class AppDetailComponent implements OnInit, OnDestroy {
this.workspaceAppFilterValue = undefined
this.onSortPermissionTable()
this.permissionTable?.clear()
//this.preparePermissionTable()
}
public onSortPermissionTable() {
if (this.appSortIcon) this.appSortIcon.nativeElement.className = 'pi pi-fw pi-sort-alt' // reset icon
Expand Down Expand Up @@ -408,32 +447,27 @@ export class AppDetailComponent implements OnInit, OnDestroy {
* Create a new PERMISSION
*/
public onCopyPermission(ev: MouseEvent, permRow: PermissionViewRow): void {
//this.onEditPermission(ev, permRow)
//this.changeMode = 'CREATE'
this.onEditPermission(ev, permRow)
this.changeMode = 'CREATE'
}
public onCreatePermission(): void {
/*
this.formGroupPermission.reset()
this.formGroupPermission.controls['appName'].patchValue(this.urlParamAppId)
this.formGroupPermission.controls['appId'].patchValue(this.currentApp.appId)
this.changeMode = 'CREATE'
this.permissionRow = { key: '' } as PermissionViewRow
this.showPermissionDetailDialog = true
*/
}
/**
* View and Edit a PERMISSION
*/
public onEditPermission(ev: MouseEvent, permRow: PermissionViewRow): void {
/*
this.formGroupPermission.controls['appId'].patchValue(this.currentApp.appId)
this.formGroupPermission.controls['resource'].patchValue(permRow.resource)
this.formGroupPermission.controls['action'].patchValue(permRow.action)
this.formGroupPermission.controls['name'].patchValue(permRow.name)
this.formGroupPermission.controls['description'].patchValue(permRow.description)
this.formGroupPermission.controls['appName'].patchValue(permRow.applicationId)
this.changeMode = 'EDIT'
this.permissionRow = permRow
this.showPermissionDetailDialog = true
*/
}
/**
* Save a PERMISSION
Expand Down Expand Up @@ -573,13 +607,13 @@ export class AppDetailComponent implements OnInit, OnDestroy {
return (a.key ? (a.key as string).toUpperCase() : '').localeCompare(b.key ? (b.key as string).toUpperCase() : '')
}
private sortPermissionRowByAppIdAsc(a: PermissionViewRow, b: PermissionViewRow): number {
return (a.applicationId ? (a.applicationId as string).toUpperCase() : '').localeCompare(
b.applicationId ? (b.applicationId as string).toUpperCase() : ''
return (a.appId ? (a.appId as string).toUpperCase() : '').localeCompare(
b.appId ? (b.appId as string).toUpperCase() : ''
)
}
private sortPermissionRowByAppIdDesc(b: PermissionViewRow, a: PermissionViewRow): number {
return (a.applicationId ? (a.applicationId as string).toUpperCase() : '').localeCompare(
b.applicationId ? (b.applicationId as string).toUpperCase() : ''
return (a.appId ? (a.appId as string).toUpperCase() : '').localeCompare(
b.appId ? (b.appId as string).toUpperCase() : ''
)
}
private sortRoleByName(a: Role, b: Role): number {
Expand Down
8 changes: 6 additions & 2 deletions src/assets/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -220,25 +220,29 @@
"HTTP_STATUS_0": {
"APPS": "Unbekanntes Problem beim Abrufen von Apps - Bitte versuchen sie es noch einmal.",
"ROLES": "Unbekanntes Problem beim Abrufen von Rollen - Bitte versuchen sie es noch einmal.",
"PERMISSIONS": "Unbekanntes Problem beim Abrufen von Berechtigungen - Bitte versuchen sie es noch einmal.",
"WORKSPACES": "Unbekanntes Problem beim Abrufen von Workspace Daten - Bitte versuchen sie es noch einmal."
},
"HTTP_STATUS_403": {
"APPS": "Sie haben keine Rechte um Apps zu sehen.",
"ROLES": "Sie haben keine Rechte um Rollen zu sehen.",
"PERMISSIONS": "Sie haben keine Rechte um Berechtigungen zu sehen.",
"WORKSPACES": "Sie haben keine Rechte um Workspaces zu sehen."
},
"HTTP_STATUS_404": {
"APP": "Die App konnte nicht gefunden werden.",
"APPS": "Es konnten keine Apps gefunden werden.",
"ROLE": "Die Rolle konnte nicht gefunden werden.",
"ROLES": "Es konnten keine Rollen gefunden werden.",
"PERMISSIONS": "Es konnten keine Berechtigungen gefunden werden.",
"WORKSPACE": "Der Workspace konnte nicht gefunden werden.",
"WORKSPACES": "Es konnten keine Workspaces gefunden werden."
},
"HTTP_STATUS_500": {
"APPS": "Unbekanntes Server-Problem beim Abrufen von Apps - Bitte versuchen sie es noch einmal.",
"ROLES": "Unbekanntes Server-Problem beim Abrufen von Rollen - Bitte versuchen sie es noch einmal.",
"PERMISSIONS": "Unbekanntes Server-Problem beim Abrufen von Berechtigungen - Bitte versuchen sie es noch einmal.",
"WORKSPACES": "Unbekanntes Server-Problem beim Abrufen von Workspace Daten - Bitte versuchen sie es noch einmal."
},
"NO_APPLICATION_OR_APPLICATION_ROLE_FOUND": "Rolle konnte nicht gelöscht werden da sie nicht gefunden wurde"
}
}
}
8 changes: 6 additions & 2 deletions src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -221,25 +221,29 @@
"HTTP_STATUS_0": {
"APPS": "Unknown problem retrieving App data - please try again.",
"ROLES": "Unknown problem retrieving Role data - please try again.",
"PERMISSIONS": "Unknown problem retrieving Permission data - please try again.",
"WORKSPACES": "Unknown problem retrieving Workspace data - please try again."
},
"HTTP_STATUS_403": {
"APPS": "You have no permissions to see Apps.",
"ROLES": "You have no permissions to see Roles.",
"PERMISSIONS": "You have no permissions to see Permissions.",
"WORKSPACES": "You have no permissions to see Workspaces."
},
"HTTP_STATUS_404": {
"APP": "App could not be found.",
"APPS": "No Apps could be found.",
"ROLE": "Role could not be found.",
"ROLES": "No Roles could be found.",
"PERMISSIONS": "No Permissions could be found.",
"WORKSPACE": "Workspace could not be found.",
"WORKSPACES": "No Workspaces could be found."
},
"HTTP_STATUS_500": {
"APPS": "Unknown server problem retrieving App data - please try again.",
"ROLES": "Unknown server problem retrieving Role data - please try again.",
"PERMISSIONS": "Unknown server problem retrieving Permission data - please try again.",
"WORKSPACES": "Unknown server problem retrieving Workspace data - please try again."
},
"NO_APPLICATION_OR_APPLICATION_ROLE_FOUND": "Role could not be deleted because it was not found"
}
}
}

0 comments on commit 10d3c8d

Please sign in to comment.