|
1 | 1 | import {ListBox} from '@enonic/lib-admin-ui/ui/selector/list/ListBox';
|
2 |
| -import {DivEl} from '@enonic/lib-admin-ui/dom/DivEl'; |
3 |
| -import * as Q from 'q'; |
4 |
| -import {PrincipalViewer} from '@enonic/lib-admin-ui/ui/security/PrincipalViewer'; |
5 |
| -import {SpanEl} from '@enonic/lib-admin-ui/dom/SpanEl'; |
6 |
| -import {Principal} from '@enonic/lib-admin-ui/security/Principal'; |
7 |
| -import {Permission} from '../../access/Permission'; |
8 |
| -import {i18n} from '@enonic/lib-admin-ui/util/Messages'; |
9 |
| -import {AccessControlEntryView} from '../../view/AccessControlEntryView'; |
10 |
| -import {Access} from '../../security/Access'; |
11 |
| -import {PermissionsHelper} from '../../access/PermissionsHelper'; |
12 |
| - |
13 |
| -export interface AccessControlChangedPermissions { |
14 |
| - persisted?: Permission[]; |
15 |
| - updated?: Permission[]; |
16 |
| -} |
17 |
| - |
18 |
| -export class AccessControlChangedItem { |
| 2 | +import {ApplyPermissionsScope} from './PermissionsData'; |
| 3 | +import {RoleKeys} from '@enonic/lib-admin-ui/security/RoleKeys'; |
| 4 | +import {AccessControlEntry} from '../../access/AccessControlEntry'; |
| 5 | +import {AccessControlChangedItem} from './AccessControlChangedItem'; |
| 6 | +import {AccessControlChangedItemView} from './AccessControlChangedItemView'; |
19 | 7 |
|
20 |
| - private readonly principal: Principal; |
| 8 | +export class AccessControlChangedItemsList |
| 9 | + extends ListBox<AccessControlChangedItem> { |
21 | 10 |
|
22 |
| - private readonly permissions: AccessControlChangedPermissions; |
| 11 | + private applyTo: ApplyPermissionsScope; |
23 | 12 |
|
24 |
| - constructor(principal: Principal, permissions: AccessControlChangedPermissions) { |
25 |
| - this.principal = principal; |
26 |
| - this.permissions = permissions; |
27 |
| - } |
| 13 | + private resetChildPermissions: boolean; |
28 | 14 |
|
29 |
| - getPrincipal(): Principal { |
30 |
| - return this.principal; |
31 |
| - } |
| 15 | + private originalValues: AccessControlEntry[] = []; |
32 | 16 |
|
33 |
| - getPermissions(): AccessControlChangedPermissions { |
34 |
| - return this.permissions; |
35 |
| - } |
36 |
| -} |
37 |
| - |
38 |
| -export class AccessControlChangedItemsList |
39 |
| - extends ListBox<AccessControlChangedItem> { |
| 17 | + private currentValues: AccessControlEntry[] = []; |
40 | 18 |
|
41 | 19 | constructor() {
|
42 | 20 | super('access-control-changed-items-list');
|
43 | 21 | }
|
44 | 22 |
|
45 | 23 | protected createItemView(item: AccessControlChangedItem, readOnly: boolean): AccessControlChangedItemView {
|
46 |
| - return new AccessControlChangedItemView(item); |
| 24 | + return new AccessControlChangedItemView(item, this.applyTo, this.resetChildPermissions); |
47 | 25 | }
|
48 | 26 |
|
49 | 27 | protected getItemId(item: AccessControlChangedItem): string {
|
50 | 28 | return item.getPrincipal().getKey().toString();
|
51 | 29 | }
|
52 | 30 |
|
53 |
| -} |
54 |
| - |
55 |
| -export class AccessControlChangedItemView |
56 |
| - extends DivEl { |
57 |
| - |
58 |
| - private readonly item: AccessControlChangedItem; |
59 |
| - |
60 |
| - constructor(item: AccessControlChangedItem) { |
61 |
| - super('access-control-changed-item-view'); |
62 |
| - |
63 |
| - this.item = item; |
| 31 | + setApplyTo(applyTo: ApplyPermissionsScope): void { |
| 32 | + this.applyTo = applyTo; |
64 | 33 | }
|
65 | 34 |
|
66 |
| - private getAccessValue(): string { |
67 |
| - if (!this.item.getPermissions().updated || this.item.getPermissions().updated?.length === 0) { |
68 |
| - return i18n('dialog.permissions.step.strategy.item.removed'); |
69 |
| - } |
70 |
| - |
71 |
| - if (!this.item.getPermissions().persisted && this.item.getPermissions().updated) { |
72 |
| - return i18n('dialog.permissions.step.strategy.item.added'); |
73 |
| - } |
74 |
| - |
75 |
| - const access = AccessControlEntryView.getAccessValueFromPermissions(this.item.getPermissions().updated); |
76 |
| - return i18n(`security.access.${Access[access].toLowerCase()}`); |
| 35 | + setResetChildPermissions(resetChildPermissions: boolean): void { |
| 36 | + this.resetChildPermissions = resetChildPermissions; |
77 | 37 | }
|
78 | 38 |
|
79 |
| - private isRemoved(): boolean { |
80 |
| - return !this.item.getPermissions().updated || this.item.getPermissions().updated?.length === 0; |
| 39 | + setOriginalValues(originalValues: AccessControlEntry[]): void { |
| 40 | + this.originalValues = originalValues; |
81 | 41 | }
|
82 | 42 |
|
83 |
| - private getPermissionState(permission: Permission): PermissionState { |
84 |
| - const isPermInPersisted = this.item.getPermissions().persisted && this.item.getPermissions().persisted?.indexOf(permission) !== -1; |
85 |
| - const isPermInUpdated = this.item.getPermissions().updated && this.item.getPermissions().updated?.indexOf(permission) !== -1; |
86 |
| - |
87 |
| - if (!isPermInPersisted) { // permission was not present before |
88 |
| - return isPermInUpdated ? PermissionState.ADDED : PermissionState.UNSET; |
89 |
| - } |
90 |
| - |
91 |
| - // permission was present before |
92 |
| - |
93 |
| - if (isPermInUpdated) { |
94 |
| - return PermissionState.UNCHANGED; |
95 |
| - } |
96 |
| - |
97 |
| - return PermissionState.REMOVED; |
| 43 | + setCurrentValues(currentValues: AccessControlEntry[]): void { |
| 44 | + this.currentValues = currentValues; |
| 45 | + this.setItems(this.getChangedItems()); |
98 | 46 | }
|
99 | 47 |
|
100 |
| - doRender(): Q.Promise<boolean> { |
101 |
| - return super.doRender().then((rendered: boolean) => { |
102 |
| - this.toggleClass('removed', this.isRemoved()); |
103 |
| - const principalViewer = new PrincipalViewer(); |
104 |
| - principalViewer.setObject(this.item.getPrincipal()); |
105 |
| - |
106 |
| - const accessEl = new SpanEl('access-control-changed-item-access'); |
107 |
| - accessEl.setHtml(this.getAccessValue()); |
108 |
| - |
109 |
| - const principalAndStatusEl = new DivEl('access-control-changed-item-principal-with-status'); |
110 |
| - principalAndStatusEl.appendChildren(principalViewer, accessEl); |
111 |
| - this.appendChild(principalAndStatusEl); |
112 |
| - |
113 |
| - if (!this.isRemoved()) { |
114 |
| - const permissionsEl = new DivEl('access-control-changed-item-permissions'); |
115 |
| - |
116 |
| - PermissionsHelper.getAllPermissions().forEach(permission => { |
117 |
| - permissionsEl.appendChild(new PermissionStateView(permission, this.getPermissionState(permission))); |
118 |
| - }); |
119 |
| - |
120 |
| - this.appendChild(permissionsEl); |
| 48 | + private getChangedItems(): AccessControlChangedItem[] { |
| 49 | + const result: AccessControlChangedItem[] = []; |
| 50 | + |
| 51 | + this.originalValues.forEach((originalVal) => { |
| 52 | + const found = this.currentValues.find((currentVal) => originalVal.getPrincipalKey().equals(currentVal.getPrincipalKey())); |
| 53 | + if (found) { // item was present before and remains |
| 54 | + if (!originalVal.equals(found) || this.isOverwriteForChildren()) { // item's permissions were changed or children to be reset |
| 55 | + result.push(new AccessControlChangedItem(originalVal.getPrincipal(), |
| 56 | + {persisted: originalVal.getAllowedPermissions(), updated: found.getAllowedPermissions()})); |
| 57 | + } |
| 58 | + } else { // item was removed |
| 59 | + if (!RoleKeys.isEveryone(originalVal.getPrincipalKey())) { |
| 60 | + result.push(new AccessControlChangedItem(originalVal.getPrincipal(), {persisted: originalVal.getAllowedPermissions()})); |
| 61 | + } |
121 | 62 | }
|
122 |
| - |
123 |
| - return rendered; |
124 | 63 | });
|
125 |
| - } |
126 |
| - |
127 |
| -} |
128 |
| - |
129 |
| -enum PermissionState { |
130 |
| - ADDED = 'added', |
131 |
| - REMOVED = 'removed', |
132 |
| - UNCHANGED = 'unchanged', |
133 |
| - UNSET = 'unset' |
134 |
| -} |
135 | 64 |
|
136 |
| -class PermissionStateView extends SpanEl { |
137 |
| - |
138 |
| - private readonly permission: Permission; |
139 |
| - |
140 |
| - private readonly state: PermissionState; |
141 |
| - |
142 |
| - constructor(permission: Permission, state: PermissionState) { |
143 |
| - super('permission-state-view'); |
144 |
| - |
145 |
| - this.permission = permission; |
146 |
| - this.state = state; |
147 |
| - } |
| 65 | + // check for newly added items |
| 66 | + this.currentValues.forEach((currentValue) => { |
| 67 | + const found = this.originalValues.find((originalVal) => originalVal.getPrincipalKey().equals(currentValue.getPrincipalKey())); |
| 68 | + if (!found && !RoleKeys.isEveryone(currentValue.getPrincipalKey())) { // item was added |
| 69 | + result.push(new AccessControlChangedItem(currentValue.getPrincipal(), {updated: currentValue.getAllowedPermissions()})); |
| 70 | + } |
| 71 | + }); |
148 | 72 |
|
149 |
| - private permissionToString(permission: Permission): string { |
150 |
| - return Permission[permission]?.toLowerCase().replace('_', '') ?? ''; |
| 73 | + return result; |
151 | 74 | }
|
152 | 75 |
|
153 |
| - doRender(): Q.Promise<boolean> { |
154 |
| - return super.doRender().then((rendered: boolean) => { |
155 |
| - this.addClass(this.state); |
156 |
| - this.setHtml(i18n(`security.permission.${this.permissionToString(this.permission)}`)); |
157 |
| - |
158 |
| - return rendered; |
159 |
| - }); |
| 76 | + private isOverwriteForChildren(): boolean { |
| 77 | + return this.applyTo !== 'single' && this.resetChildPermissions; |
160 | 78 | }
|
161 |
| - |
162 | 79 | }
|
0 commit comments