Skip to content

Commit

Permalink
feat: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryT-CG committed Feb 28, 2024
1 parent e55b4ad commit 5dd8c70
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 88 deletions.
12 changes: 6 additions & 6 deletions src/app/permission/app-detail/app-detail.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@
pbutton
type="button"
*ngIf="myPermissions.includes('ROLE#DELETE')"
[id]="'app_detail_permission_table_col_' + role.name + '_delete_action'"
[id]="'app_detail_permission_table_col_' + role.name + '_action_delete'"
(click)="onDeleteRole($event, role)"
[title]="'ACTIONS.DELETE.ROLE' | translate"
class="p-1 mr-0 p-button-rounded font-medium p-button-text p-button p-component p-button-icon-only"
Expand All @@ -271,7 +271,7 @@
pbutton
type="button"
*ngIf="myPermissions.includes('ROLE#EDIT')"
[id]="'app_detail_permission_table_col_' + role.id + '_edit_action'"
[id]="'app_detail_permission_table_col_' + role.id + '_action_edit'"
(click)="onEditRole($event, role)"
[title]="'ACTIONS.EDIT.ROLE' | translate"
class="p-1 p-button-rounded font-medium p-button-text p-button p-component p-button-icon-only"
Expand All @@ -281,8 +281,8 @@
<button
pbutton
type="button"
*ngIf="myPermissions.includes('ROLE#EDIT') && permissionTable.totalRecords > 0"
[id]="'app_detail_permission_table_col_' + role.id + '_set_all_action'"
*ngIf="myPermissions.includes('PERMISSION#GRANT') && permissionTable.totalRecords > 0"
[id]="'app_detail_permission_table_col_' + role.id + '_action_grant_all'"
(click)="onGrantAllPermissions($event, role)"
[title]="'PERMISSION.ASSIGNMENTS.GRANT_ALL_FOR_ROLE' | translate"
class="p-1 p-button-rounded font-medium p-button-text p-button p-component p-button-icon-only"
Expand All @@ -293,11 +293,11 @@
pbutton
type="button"
*ngIf="
myPermissions.includes('ROLE#EDIT') &&
myPermissions.includes('PERMISSION#GRANT') &&
role.appId === urlParamAppId &&
permissionTable.totalRecords > 0
"
[id]="'app_detail_permission_table_col_' + role.id + '_reset_all_action'"
[id]="'app_detail_permission_table_col_' + role.id + '_action_revoke_all'"
(click)="onRevokeAllPermissions($event, role)"
[title]="'PERMISSION.ASSIGNMENTS.REVOKE_ALL_FOR_ROLE' | translate"
class="p-1 p-button-rounded font-medium p-button-text p-button p-component p-button-icon-only"
Expand Down
101 changes: 39 additions & 62 deletions src/app/permission/app-detail/app-detail.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ActivatedRoute, Router } from '@angular/router'
import { Location } from '@angular/common'
import { HttpErrorResponse } from '@angular/common/http'
import { FormGroup, FormControl, Validators } from '@angular/forms'
import { Subject, catchError, combineLatest, finalize, map, of, Observable, startWith } from 'rxjs'
import { Subject, catchError, combineLatest, finalize, map, of, Observable } from 'rxjs'
import { TranslateService } from '@ngx-translate/core'
import { FilterMatchMode, SelectItem } from 'primeng/api'
import { Table } from 'primeng/table'
Expand All @@ -18,11 +18,7 @@ import {
Permission,
Assignment,
CreateAssignmentRequestParams,
DeleteAssignmentRequestParams,
/*
AssignmentSearchCriteria,
AssignmentPageResult,
CreateAssignmentRequest, */
//DeleteAssignmentRequestParams,
Application,
Product,
ApplicationAPIService,
Expand All @@ -32,8 +28,7 @@ import {
WorkspaceAPIService,
WorkspaceDetails
} from 'src/app/shared/generated'
//import { dropDownSortItemsByLabel, limitText } from 'src/app/shared/utils'
import { limitText } from 'src/app/shared/utils'
import { dropDownSortItemsByLabel, limitText } from 'src/app/shared/utils'

export type App = Application & {
isApp: boolean
Expand All @@ -47,7 +42,7 @@ export type RoleAssignments = { [key: string]: boolean }
export type ChangeMode = 'VIEW' | 'CREATE' | 'EDIT' | 'COPY' | 'DELETE'
export type PermissionViewRow = Permission & {
key: string // combined resource and action => resource#action
roles: RoleAssignments
roles: RoleAssignments // true if assignment exist
appType: ServiceAppType
}

Expand Down Expand Up @@ -219,15 +214,6 @@ export class AppDetailComponent implements OnInit, OnDestroy {
this.loading = true
this.loadingServerIssue = false
this.loadingExceptionKey = ''
// check parameter
if (!this.urlParamAppId || this.urlParamAppId === '') {
this.msgService.error({ summaryKey: 'TODO_MISSING_URL_PARAMETER_APP_ID' })
return
}
if (!this.urlParamAppType || !',APP,WORKSPACE,'.includes(this.urlParamAppType)) {
this.msgService.error({ summaryKey: 'TODO_MISSING_URL_PARAMETER_APP_TYPE' })
return
}
// on workspace: create a dummy app
if (this.urlParamAppType === 'WORKSPACE') {
this.currentApp = {
Expand All @@ -239,7 +225,6 @@ export class AppDetailComponent implements OnInit, OnDestroy {
isMfe: false
} as App
this.log('loadApp => Workspace:', this.currentApp)
this.prepareActionButtons()
this.loadWorkspaceDetails()
} else {
this.appApi
Expand All @@ -253,12 +238,11 @@ export class AppDetailComponent implements OnInit, OnDestroy {
} else if (result instanceof Object) {
this.currentApp = { ...result.stream[0], appType: 'APP', isApp: true } as App
this.log('loadApp => App:', this.currentApp)
this.prepareActionButtons()
this.loadAppDetails()
//this.preparePermissionTable(this.currentApp)
} else {
this.loadingServerIssue = true
this.loadingExceptionKey = 'EXCEPTIONS.HTTP_STATUS_0.APPS'
this.loadingExceptionKey = 'EXCEPTIONS.HTTP_STATUS_0.APP'
console.error('getApplicationById() => unknown response:', result)
}
this.loading = false
Expand All @@ -271,15 +255,17 @@ export class AppDetailComponent implements OnInit, OnDestroy {
.pipe(catchError((error) => of(error)))
.subscribe((result) => {
if (result instanceof HttpErrorResponse) {
this.loadingExceptionKey = 'EXCEPTIONS.HTTP_STATUS_' + result.status + '.APP'
this.loadingServerIssue = true
this.loadingExceptionKey = 'EXCEPTIONS.HTTP_STATUS_' + result.status + '.WORKSPACE'
console.error('getDetailsByWorkspaceName() result:', result)
} else if (result instanceof Object) {
this.currentApp.workspaceDetails = { ...result }
this.log('getDetailsByWorkspaceName => App:', this.currentApp)
this.prepareActionButtons()
this.prepareWorkspaceApps()
this.loadRolesAndPermissions()
} else {
this.loadingExceptionKey = 'EXCEPTIONS.HTTP_STATUS_0.APPS'
this.loadingExceptionKey = 'EXCEPTIONS.HTTP_STATUS_0.WORKSPACE'
console.error('getDetailsByWorkspaceName() => unknown response:', result)
}
})
Expand All @@ -304,15 +290,18 @@ export class AppDetailComponent implements OnInit, OnDestroy {
})
}
})
this.workspaceProductFilterItems.sort(dropDownSortItemsByLabel)
this.workspaceAppFilterItems.sort(dropDownSortItemsByLabel)
}

this.log('workspaceApps: ', this.workspaceApps)
}

/**
* COLUMNS => Roles, ROWS => Permissions
*/
private declareRoleObservable(): void {
this.roles$ = this.roleApi.searchRoles({ roleSearchCriteria: {} }).pipe(
startWith({} as RolePageResult),
catchError((err) => {
this.loadingServerIssue = true
this.loadingExceptionKey = 'EXCEPTIONS.HTTP_STATUS_' + err.status + '.ROLES'
Expand All @@ -326,7 +315,6 @@ export class AppDetailComponent implements OnInit, OnDestroy {
this.permissions$ = this.permApi
.searchPermissions({ permissionSearchCriteria: { appId: appIds, pageSize: this.pageSize } })
.pipe(
startWith({} as PermissionPageResult),
catchError((err) => {
this.loadingServerIssue = true
this.loadingExceptionKey = 'EXCEPTIONS.HTTP_STATUS_' + err.status + '.PERMISSIONS'
Expand Down Expand Up @@ -371,16 +359,16 @@ export class AppDetailComponent implements OnInit, OnDestroy {
this.log('loadRolesAndPermissions completed')
this.log('roles', this.roles)
this.log('permissions', this.permissions)
//this.prepareRoles()
//this.createNonExistingRoles
this.preparePermissionTable()
}
)
}
private prepareRoles() {
private createNonExistingRoles() {
if (this.currentApp.workspaceDetails?.workspaceRoles) {
let created = false
for (let wRole of this.currentApp.workspaceDetails?.workspaceRoles) {
this.log('check role ' + wRole)
// this.log('check role ' + wRole)
if (this.roles.filter((r) => r.name === wRole).length === 0) {
this.roleApi
.createRole({
Expand All @@ -397,7 +385,7 @@ export class AppDetailComponent implements OnInit, OnDestroy {
if (created) this.loadRolesAndPermissions()
else {
this.roles.sort(this.sortRoleByName)
this.log('prepareRoles this.roles', this.roles)
this.log('createNonExistingRoles this.roles', this.roles)
}
}
}
Expand All @@ -409,6 +397,7 @@ export class AppDetailComponent implements OnInit, OnDestroy {
() => {}, // next
() => {}, // error
() => {
this.prepareActionButtons()
this.preparePermissionTable() // on complete
}
)
Expand All @@ -432,51 +421,39 @@ export class AppDetailComponent implements OnInit, OnDestroy {
} as PermissionViewRow)
}
this.log('permissionRows:', this.permissionRows)
// proceed only on workspaces
if (!this.currentApp.isApp) {
const permissionRows: Map<string, PermissionViewRow> = new Map()
for (const permissionRow of this.permissionRows) {
permissionRows.set(permissionRow.resource + '#' + permissionRow.action, permissionRow)
}
this.loadAssignments()
}
if (!this.currentApp.isApp) this.loadRoleAssignments()
}
private loadAssignments() {

private loadRoleAssignments() {
if (this.workspaceApps.length === 0) {
console.warn('No workspace apps found - stop processing')
return
}
const appList = this.workspaceApps.map((app) => app.appId ?? '')
const appList = this.workspaceApps.map((app) => app.appId!)
this.assApi
.searchAssignments({ assignmentSearchCriteria: { appIds: appList, pageSize: this.pageSize } })
.pipe(catchError((error) => of(error)))
.subscribe((result) => {
if (result instanceof HttpErrorResponse) {
this.loadingServerIssue = true
this.loadingExceptionKey = 'EXCEPTIONS.HTTP_STATUS_' + result.status + '.PERMISSIONS'
this.loadingExceptionKey = 'EXCEPTIONS.HTTP_STATUS_' + result.status + '.ASSIGNMENTS'
console.error('searchAssignments() result:', result)
} else if (result instanceof Object) {
this.log('loadAssignments result.stream:', result.stream)
// result.stream => assignments => roleId, permissionId, appId
// this.permissionRows => Permission + key, roles
// Permission (row): id, appId, resource, action
this.permissionRows.forEach((permission) => {
this.log('loadAssignments row.id:' + permission.id)
result.stream?.forEach((assignment: Assignment) => {
if (assignment.permissionId === permission.id) {
permission.roles[assignment.roleId!] = assignment.permissionId === permission.id
this.log('loadAssignments assignment.permissionId:' + assignment.permissionId)
this.log('loadAssignments assignment.roleId:' + assignment.roleId)
this.log('loadAssignments row.roles[assignment.roleId!]:' + permission.roles[assignment.roleId!])
}
result.stream?.forEach((assignment: Assignment) => {
const permissions = this.permissionRows.filter((p) => p.id === assignment.permissionId)
permissions.map((permission) => {
permission.roles[assignment.roleId!] = true
})
})
this.log('loadAssignments permission rows:', this.permissionRows)
this.log('loadRoleAssignments permission rows:', this.permissionRows)
//this.prepareWorkspaceAppFilter()
this.loading = false // TODO
} else {
this.loadingServerIssue = true
this.loadingExceptionKey = 'EXCEPTIONS.HTTP_STATUS_0.ROLES'
this.loadingExceptionKey = 'EXCEPTIONS.HTTP_STATUS_0.ASSIGNMENTS'
console.error('searchAssignments() => unknown response:', result)
}
})
Expand Down Expand Up @@ -621,7 +598,7 @@ export class AppDetailComponent implements OnInit, OnDestroy {
* ASSIGNMENTS => grant + revoke permissions
****************************************************************************
*/
public onAssignPermission(ev: MouseEvent, permRow: PermissionViewRow, role: Role, silent?: boolean): void {
public onAssignPermission(ev: MouseEvent, permRow: PermissionViewRow, role: Role): void {
this.log('onAssignPermission()')
this.assApi
.createAssignment({
Expand All @@ -632,29 +609,29 @@ export class AppDetailComponent implements OnInit, OnDestroy {
} as CreateAssignmentRequestParams)
.subscribe({
next: () => {
if (!silent || silent === undefined)
this.msgService.success({ summaryKey: 'PERMISSION.ASSIGNMENTS.GRANT_SUCCESS' })
if (role.id) permRow.roles[role.id] = !permRow.roles[role.id]
this.msgService.success({ summaryKey: 'PERMISSION.ASSIGNMENTS.GRANT_SUCCESS' })
permRow.roles[role.id!] = true
},
error: (err) => {
this.msgService.error({ summaryKey: 'PERMISSION.ASSIGNMENTS.GRANT_ERROR' })
console.error(err)
}
})
}
public onRemovePermission(ev: MouseEvent, permRow: PermissionViewRow, role: Role, silent?: boolean): void {
public onRemovePermission(ev: MouseEvent, permRow: PermissionViewRow, role: Role): void {
this.log('onRemovePermission()')
this.assApi.deleteAssignment({ id: permRow.id } as DeleteAssignmentRequestParams).subscribe({
/*
// get assignmentt id => permRow.roles[role.id]
this.assApi.deleteAssignment({ id: permRow.roles[role.id!] } as DeleteAssignmentRequestParams).subscribe({
next: () => {
if (!silent || silent === undefined)
this.msgService.success({ summaryKey: 'PERMISSION.ASSIGNMENTS.REVOKE_SUCCESS' })
if (role.id) permRow.roles[role.id] = !permRow.roles[role.id]
this.msgService.success({ summaryKey: 'PERMISSION.ASSIGNMENTS.REVOKE_SUCCESS' })
permRow.roles[role.id!] = false
},
error: (err) => {
this.msgService.error({ summaryKey: 'PERMISSION.ASSIGNMENTS.REVOKE_ERROR' })
console.error(err)
}
})
})*/
}
public onGrantAllPermissions(ev: MouseEvent, role: Role): void {
this.log('onGrantAllPermissions()')
Expand Down
23 changes: 13 additions & 10 deletions src/assets/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,35 +217,38 @@
},
"EXCEPTIONS": {
"HTTP_STATUS_0": {
"APPS": "Unbekanntes Problem beim Abrufen von Apps - Bitte versuchen sie es noch einmal.",
"APP": "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."
"ASSIGNMENTS": "Unbekanntes Problem beim Abrufen von Berechtigungen - Bitte versuchen sie es noch einmal.",
"WORKSPACE": "Unbekanntes Problem beim Abrufen von Workspace Daten - Bitte versuchen sie es noch einmal."
},
"HTTP_STATUS_400": {
"ROLES": "Die Rolle konnte nicht verarbeitet werden.",
"PERMISSIONS": "Die Berechtigung konnte nicht verarbeitet werden."
},
"HTTP_STATUS_403": {
"APPS": "Sie haben keine Rechte um Apps zu sehen.",
"APP": "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."
"ASSIGNMENTS": "Sie haben keine Rechte um Berechtigungen zu sehen.",
"WORKSPACE": "Sie haben keine Rechte um Workspace Daten 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."
"ASSIGNMENTS": "Es konnten keine Berechtigungen gefunden werden.",
"WORKSPACE": "Der Workspace konnte nicht 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."
"APP": "Unbekanntes Problem beim Abrufen von Apps - Bitte versuchen sie es erneut.",
"ROLES": "Unbekanntes Problem beim Abrufen von Rollen - Bitte versuchen sie es erneut.",
"PERMISSIONS": "Unbekanntes Problem beim Abrufen von Berechtigungen - Bitte versuchen sie es erneut.",
"ASSIGNMENTS": "Unbekanntes Problem beim Abrufen von Berechtigungen - Bitte versuchen sie es erneut.",
"WORKSPACE": "Unbekanntes Problem beim Abrufen der Workspace Daten - Bitte versuchen sie es erneut."
}
}
}
Loading

0 comments on commit 5dd8c70

Please sign in to comment.