Skip to content

Commit 0e747e8

Browse files
committed
[Refactored] - Migrate roles creation
1 parent 9d00d22 commit 0e747e8

9 files changed

+180
-293
lines changed

angular/src/app/app.module.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ import { RightSideBarComponent } from '@app/layout/right-sidebar.component';
3030
import { CreateTenantDialogComponent } from './tenants/create-tenant/create-tenant-dialog.component';
3131
import { EditTenantDialogComponent } from './tenants/edit-tenant/edit-tenant-dialog.component';
3232
// roles
33-
import { RolesComponent } from "@app/roles/roles.component";
34-
import { CreateRoleDialogComponent } from "@app/roles/create-role/create-role-dialog.component";
35-
import { EditRoleDialogComponent } from "./roles/edit-role/edit-role-dialog.component";
36-
33+
import { RolesComponent } from '@app/roles/roles.component';
34+
import { CreateOrEditRoleDialogComponent } from '@app/roles/create-or-edit-role-dialog.component';
3735

3836
@NgModule({
3937
declarations: [
@@ -54,8 +52,7 @@ import { EditRoleDialogComponent } from "./roles/edit-role/edit-role-dialog.comp
5452
RightSideBarComponent,
5553
// roles
5654
RolesComponent,
57-
CreateRoleDialogComponent,
58-
EditRoleDialogComponent
55+
CreateOrEditRoleDialogComponent
5956
],
6057
imports: [
6158
CommonModule,
@@ -78,8 +75,7 @@ import { EditRoleDialogComponent } from "./roles/edit-role/edit-role-dialog.comp
7875

7976
// roles
8077
RolesComponent,
81-
CreateRoleDialogComponent,
82-
EditRoleDialogComponent
78+
CreateOrEditRoleDialogComponent
8379
]
8480
})
8581
export class AppModule { }

angular/src/app/roles/edit-role/edit-role-dialog.component.html renamed to angular/src/app/roles/create-or-edit-role-dialog.component.html

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<form autocomplete="off"
22
#editRoleForm="ngForm"
33
(ngSubmit)="save()">
4-
<h1 mat-dialog-title>{{ 'EditRole' | localize }}</h1>
4+
<h1 mat-dialog-title *ngIf="!role.id">{{"CreateNewRole" | localize}}</h1>
5+
<h1 mat-dialog-title *ngIf="role.id">{{"EditRole" | localize}}: {{permissionName}}</h1>
56
<mat-dialog-content>
67
<mat-form-field>
78
<input matInput
@@ -33,11 +34,9 @@ <h4>{{ 'Permissions' | localize }}</h4>
3334
<ng-template ngFor let-permission [ngForOf]="permissions">
3435
<div class="col-sm-6">
3536
<div class="checkbox-wrapper">
36-
<mat-checkbox name="permission"
37-
[value]="permission.name"
38-
[checked]="checkPermission(permission.name)"
37+
<mat-checkbox [checked]="isPermissionChecked(permission.name)"
3938
[disabled]="role.isStatic"
40-
style="padding-bottom: 5px">{{ permission.displayName }}</mat-checkbox>
39+
(change)="onPermissionsChange(permission, $event)">{{ permission.displayName }}</mat-checkbox>
4140
</div>
4241
</div>
4342
</ng-template>
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import { Component, Injector, Inject, OnInit, Optional } from '@angular/core';
2+
import {
3+
MatDialogRef,
4+
MAT_DIALOG_DATA,
5+
MatCheckboxChange
6+
} from '@angular/material';
7+
import * as _ from 'lodash';
8+
import {
9+
RoleServiceProxy,
10+
GetRoleForEditOutput,
11+
RoleDto,
12+
ListResultDtoOfPermissionDto,
13+
PermissionDto,
14+
CreateRoleDto
15+
} from '@shared/service-proxies/service-proxies';
16+
import { AppComponentBase } from '@shared/app-component-base';
17+
18+
@Component({
19+
templateUrl: 'create-or-edit-role-dialog.component.html',
20+
styles: [
21+
`
22+
mat-form-field {
23+
width: 100%;
24+
}
25+
mat-checkbox {
26+
padding-bottom: 5px;
27+
}
28+
`
29+
]
30+
})
31+
export class CreateOrEditRoleDialogComponent extends AppComponentBase
32+
implements OnInit {
33+
role: RoleDto = new RoleDto();
34+
permissions: PermissionDto[] = [];
35+
grantedPermissionNames: string[] = [];
36+
checkedPermissionsMap: { [key: string]: boolean } = {};
37+
defaultPermissionCheckedStatus = true;
38+
permissionName = '';
39+
40+
constructor(
41+
injector: Injector,
42+
private _roleService: RoleServiceProxy,
43+
private _dialogRef: MatDialogRef<CreateOrEditRoleDialogComponent>,
44+
@Optional() @Inject(MAT_DIALOG_DATA) private _id: number
45+
) {
46+
super(injector);
47+
}
48+
49+
ngOnInit(): void {
50+
if (this.isCreateDialog()) {
51+
this._roleService
52+
.getAllPermissions()
53+
.subscribe((result: ListResultDtoOfPermissionDto) => {
54+
this.permissions = result.items;
55+
this.setInitialPermissionsStatus();
56+
});
57+
} else {
58+
this._roleService
59+
.getRoleForEdit(this._id)
60+
.subscribe((result: GetRoleForEditOutput) => {
61+
this.role.init(result.role);
62+
_.map(result.permissions, item => {
63+
const permission = new PermissionDto();
64+
permission.init(item);
65+
this.permissions.push(permission);
66+
});
67+
this.grantedPermissionNames = result.grantedPermissionNames;
68+
this.permissionName = result.role.name;
69+
this.setInitialPermissionsStatus();
70+
});
71+
}
72+
}
73+
74+
isCreateDialog(): boolean {
75+
return this._id <= 0;
76+
}
77+
78+
setInitialPermissionsStatus(): void {
79+
_.map(this.permissions, item => {
80+
this.checkedPermissionsMap[item.name] = this.isCreateDialog()
81+
? this.defaultPermissionCheckedStatus
82+
: _.includes(this.grantedPermissionNames, item.name);
83+
});
84+
}
85+
86+
isPermissionChecked(permissionName: string): boolean {
87+
if (this.isCreateDialog()) {
88+
return this.defaultPermissionCheckedStatus;
89+
}
90+
return _.includes(this.grantedPermissionNames, permissionName);
91+
}
92+
93+
onPermissionsChange(permission: PermissionDto, $event: MatCheckboxChange) {
94+
this.checkedPermissionsMap[permission.name] = $event.checked;
95+
}
96+
97+
getCheckedPermissions(): string[] {
98+
const permissions: string[] = [];
99+
_.forEach(this.checkedPermissionsMap, function (value, key) {
100+
if (value) {
101+
permissions.push(key);
102+
}
103+
});
104+
return permissions;
105+
}
106+
107+
save(): void {
108+
const role = new RoleDto();
109+
role.permissions = this.getCheckedPermissions();
110+
role.init(this.role);
111+
112+
if (this.isCreateDialog()) {
113+
const createRole = new CreateRoleDto();
114+
createRole.init(role);
115+
this._roleService.create(createRole).subscribe(() => {
116+
this.notify.info(this.l('SavedSuccessfully'));
117+
this.close(true);
118+
});
119+
} else {
120+
this._roleService.update(role).subscribe(() => {
121+
this.notify.info(this.l('SavedSuccessfully'));
122+
this.close(true);
123+
});
124+
}
125+
}
126+
127+
close(result: any): void {
128+
this._dialogRef.close(result);
129+
}
130+
}

angular/src/app/roles/create-role/create-role-dialog.component.html

Lines changed: 0 additions & 59 deletions
This file was deleted.

angular/src/app/roles/create-role/create-role-dialog.component.ts

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)