From ae0fe6cbb4e5d2984d82552518abb38b38fad05c Mon Sep 17 00:00:00 2001 From: sumeyyeKurtulus Date: Thu, 22 Aug 2024 16:44:00 +0300 Subject: [PATCH 1/4] add: hierarchical check for permission selection --- .../permission-management.component.ts | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/npm/ng-packs/packages/permission-management/src/lib/components/permission-management.component.ts b/npm/ng-packs/packages/permission-management/src/lib/components/permission-management.component.ts index 8503a2bd81d..4ea265fc7e0 100644 --- a/npm/ng-packs/packages/permission-management/src/lib/components/permission-management.component.ts +++ b/npm/ng-packs/packages/permission-management/src/lib/components/permission-management.component.ts @@ -195,13 +195,13 @@ export class PermissionManagementComponent }, 0); } - setParentClicked(clickedPermissions: PermissionGrantInfoDto) { + setParentClicked(clickedPermission: PermissionGrantInfoDto) { let childPermissionGrantedCount = 0; let parentPermission: PermissionGrantInfoDto; - if (clickedPermissions.parentName) { + if (clickedPermission.parentName) { this.permissions.forEach(per => { - if (per.name === clickedPermissions.parentName) { + if (per.name === clickedPermission.parentName) { parentPermission = per; } }); @@ -220,9 +220,11 @@ export class PermissionManagementComponent } return; } + this.permissions = this.permissions.map(per => { - if (per.parentName === clickedPermissions.name) { - per.isGranted = false; + const root = findRootParent(this.permissions, per); + if (root && root.name === clickedPermission.name) { + per.isGranted = root.isGranted; } return per; }); @@ -387,6 +389,28 @@ export class PermissionManagementComponent } } +function findRootParent( + permissions: PermissionGrantInfoDto[], + permission: PermissionGrantInfoDto, +): PermissionGrantInfoDto | null { + if (!permissions.length) { + return null; + } + + const permissionMap = new Map(permissions.map(p => [p.name, p])); + let currentPermission = permissionMap.get(permission.name) ?? null; + + while (currentPermission && currentPermission.parentName) { + const parentPermission = permissionMap.get(currentPermission.parentName); + if (!parentPermission) { + break; + } + currentPermission = parentPermission; + } + + return currentPermission; +} + function findMargin( permissions: PermissionGrantInfoDto[], permission: PermissionGrantInfoDto, From 8becd973973882baf5aca378b2b215a670bf1cbb Mon Sep 17 00:00:00 2001 From: sumeyyeKurtulus Date: Tue, 27 Aug 2024 08:55:54 +0300 Subject: [PATCH 2/4] =?UTF-8?q?update:=20permission=20selection=20rules=20?= =?UTF-8?q?are=20met=20=F0=9F=99=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../permission-management.component.ts | 64 +++++++++++-------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/npm/ng-packs/packages/permission-management/src/lib/components/permission-management.component.ts b/npm/ng-packs/packages/permission-management/src/lib/components/permission-management.component.ts index 4ea265fc7e0..cabc903d954 100644 --- a/npm/ng-packs/packages/permission-management/src/lib/components/permission-management.component.ts +++ b/npm/ng-packs/packages/permission-management/src/lib/components/permission-management.component.ts @@ -196,25 +196,19 @@ export class PermissionManagementComponent } setParentClicked(clickedPermission: PermissionGrantInfoDto) { - let childPermissionGrantedCount = 0; - let parentPermission: PermissionGrantInfoDto; + const parentPermissions: PermissionGrantInfoDto[] = findAllParentPermissions( + this.permissions, + clickedPermission, + ); if (clickedPermission.parentName) { - this.permissions.forEach(per => { - if (per.name === clickedPermission.parentName) { - parentPermission = per; - } - }); - this.permissions.forEach(per => { - if (parentPermission.name === per.parentName) { - per.isGranted && childPermissionGrantedCount++; - } - }); - if (childPermissionGrantedCount === 1 && !parentPermission.isGranted) { + if (parentPermissions.length) { this.permissions = this.permissions.map(per => { - if (per.name === parentPermission.name) { - per.isGranted = true; - } + parentPermissions.forEach(parent => { + if (per.name === parent.name) { + per.isGranted = true; + } + }); return per; }); } @@ -223,8 +217,10 @@ export class PermissionManagementComponent this.permissions = this.permissions.map(per => { const root = findRootParent(this.permissions, per); - if (root && root.name === clickedPermission.name) { - per.isGranted = root.isGranted; + if (root) { + if (root.name === clickedPermission.name && !root.isGranted) { + per.isGranted = false; + } } return per; }); @@ -389,26 +385,44 @@ export class PermissionManagementComponent } } -function findRootParent( +function traverseParentPermissions( permissions: PermissionGrantInfoDto[], permission: PermissionGrantInfoDto, -): PermissionGrantInfoDto | null { - if (!permissions.length) { - return null; - } - + collectAll = false, +): PermissionGrantInfoDto[] { const permissionMap = new Map(permissions.map(p => [p.name, p])); let currentPermission = permissionMap.get(permission.name) ?? null; + const parentPermissions: PermissionGrantInfoDto[] = []; while (currentPermission && currentPermission.parentName) { const parentPermission = permissionMap.get(currentPermission.parentName); if (!parentPermission) { break; } + parentPermissions.push(parentPermission); currentPermission = parentPermission; + + if (!collectAll) { + break; + } } - return currentPermission; + return parentPermissions; +} + +function findRootParent( + permissions: PermissionGrantInfoDto[], + permission: PermissionGrantInfoDto, +): PermissionGrantInfoDto | null { + const parentPermissions = traverseParentPermissions(permissions, permission); + return parentPermissions.length ? parentPermissions[parentPermissions.length - 1] : null; +} + +function findAllParentPermissions( + permissions: PermissionGrantInfoDto[], + permission: PermissionGrantInfoDto, +): PermissionGrantInfoDto[] { + return traverseParentPermissions(permissions, permission, true); } function findMargin( From 7867d473414261899a9c38b8b1bf16c124fc5097 Mon Sep 17 00:00:00 2001 From: sumeyyeKurtulus Date: Tue, 3 Sep 2024 11:04:17 +0300 Subject: [PATCH 3/4] fix: logic to grant permissions in a right way --- .../permission-management.component.ts | 100 +++++++++--------- 1 file changed, 49 insertions(+), 51 deletions(-) diff --git a/npm/ng-packs/packages/permission-management/src/lib/components/permission-management.component.ts b/npm/ng-packs/packages/permission-management/src/lib/components/permission-management.component.ts index cabc903d954..31bbc19ab45 100644 --- a/npm/ng-packs/packages/permission-management/src/lib/components/permission-management.component.ts +++ b/npm/ng-packs/packages/permission-management/src/lib/components/permission-management.component.ts @@ -171,23 +171,14 @@ export class PermissionManagementComponent } onClickCheckbox(clickedPermission: PermissionGrantInfoDto) { - if ( - clickedPermission.isGranted && - this.isGrantedByOtherProviderName(clickedPermission.grantedProviders) - ) + const { isGranted, grantedProviders } = clickedPermission; + if (isGranted && this.isGrantedByOtherProviderName(grantedProviders)) { return; + } + this.setSelectedGroup(this.selectedGroup); setTimeout(() => { - this.permissions = this.permissions.map(per => { - if (clickedPermission.name === per.name) { - return { ...per, isGranted: !per.isGranted }; - } else if (clickedPermission.name === per.parentName && clickedPermission.isGranted) { - return { ...per, isGranted: false }; - } else if (clickedPermission.parentName === per.name && !clickedPermission.isGranted) { - return { ...per, isGranted: true }; - } - return per; - }); + this.updatePermissionStatus(clickedPermission); this.updateSelectedGroupPermissions(clickedPermission); this.setTabCheckboxState(); this.setGrantCheckboxState(); @@ -195,31 +186,58 @@ export class PermissionManagementComponent }, 0); } - setParentClicked(clickedPermission: PermissionGrantInfoDto) { - const parentPermissions: PermissionGrantInfoDto[] = findAllParentPermissions( - this.permissions, - clickedPermission, - ); + updatePermissionStatus(clickedPermission: PermissionGrantInfoDto) { + this.permissions = this.permissions.map(permission => { + const isExactMatch = clickedPermission.name == permission.name; + const isParentOfPermission = clickedPermission.parentName === permission.name; + const isChildOfPermission = clickedPermission.name === permission.parentName; + + if (isExactMatch) { + return { ...permission, isGranted: !permission.isGranted }; + } + if (isChildOfPermission && permission.isGranted) { + return { ...permission, isGranted: false }; + } + + if (isParentOfPermission && !permission.isGranted) { + return { ...permission, isGranted: true }; + } + + return permission; + }); + } + + setParentClicked(clickedPermission: PermissionGrantInfoDto) { if (clickedPermission.parentName) { - if (parentPermissions.length) { + const parentPermissions = findParentPermissions(this.permissions, clickedPermission); + if (parentPermissions.length > 0) { + const parentNames = new Set(parentPermissions.map(parent => parent.name)); + this.permissions = this.permissions.map(per => { - parentPermissions.forEach(parent => { - if (per.name === parent.name) { - per.isGranted = true; - } - }); - return per; + let updatedIsGranted = per.isGranted; + + if (per.parentName === clickedPermission.name && !clickedPermission.isGranted) { + updatedIsGranted = false; + } + + if (parentNames.has(per.name)) { + updatedIsGranted = true; + } + + return { ...per, isGranted: updatedIsGranted }; }); } return; } this.permissions = this.permissions.map(per => { - const root = findRootParent(this.permissions, per); - if (root) { - if (root.name === clickedPermission.name && !root.isGranted) { - per.isGranted = false; + const parents = findParentPermissions(this.permissions, per); + if (parents.length > 0) { + const rootParent = parents[parents.length - 1]; + + if (rootParent.name === clickedPermission.name && !rootParent.isGranted) { + return { ...per, isGranted: false }; } } return per; @@ -385,10 +403,9 @@ export class PermissionManagementComponent } } -function traverseParentPermissions( +function findParentPermissions( permissions: PermissionGrantInfoDto[], permission: PermissionGrantInfoDto, - collectAll = false, ): PermissionGrantInfoDto[] { const permissionMap = new Map(permissions.map(p => [p.name, p])); let currentPermission = permissionMap.get(permission.name) ?? null; @@ -401,30 +418,11 @@ function traverseParentPermissions( } parentPermissions.push(parentPermission); currentPermission = parentPermission; - - if (!collectAll) { - break; - } } return parentPermissions; } -function findRootParent( - permissions: PermissionGrantInfoDto[], - permission: PermissionGrantInfoDto, -): PermissionGrantInfoDto | null { - const parentPermissions = traverseParentPermissions(permissions, permission); - return parentPermissions.length ? parentPermissions[parentPermissions.length - 1] : null; -} - -function findAllParentPermissions( - permissions: PermissionGrantInfoDto[], - permission: PermissionGrantInfoDto, -): PermissionGrantInfoDto[] { - return traverseParentPermissions(permissions, permission, true); -} - function findMargin( permissions: PermissionGrantInfoDto[], permission: PermissionGrantInfoDto, From 3ba90465c015fef9bd670f5d0eb73daf79099b80 Mon Sep 17 00:00:00 2001 From: sumeyyeKurtulus Date: Tue, 3 Sep 2024 16:40:51 +0300 Subject: [PATCH 4/4] update: fixing the logic error for `setTabCheckboxState` function --- .../src/lib/components/permission-management.component.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/npm/ng-packs/packages/permission-management/src/lib/components/permission-management.component.ts b/npm/ng-packs/packages/permission-management/src/lib/components/permission-management.component.ts index 31bbc19ab45..8ad2fd23200 100644 --- a/npm/ng-packs/packages/permission-management/src/lib/components/permission-management.component.ts +++ b/npm/ng-packs/packages/permission-management/src/lib/components/permission-management.component.ts @@ -180,9 +180,9 @@ export class PermissionManagementComponent setTimeout(() => { this.updatePermissionStatus(clickedPermission); this.updateSelectedGroupPermissions(clickedPermission); + this.setParentClicked(clickedPermission); this.setTabCheckboxState(); this.setGrantCheckboxState(); - this.setParentClicked(clickedPermission); }, 0); } @@ -254,13 +254,13 @@ export class PermissionManagementComponent } setTabCheckboxState() { - const selectableGroupPermissions = this.selectedGroupPermissions.filter(per => + const selectablePermissions = this.permissions.filter(per => per.grantedProviders.every(p => p.providerName === this.providerName), ); - const selectedPermissions = selectableGroupPermissions.filter(per => per.isGranted); + const selectedPermissions = selectablePermissions.filter(per => per.isGranted); const element = document.querySelector('#select-all-in-this-tabs') as any; - if (selectedPermissions.length === selectableGroupPermissions.length) { + if (selectedPermissions.length === selectablePermissions.length) { element.indeterminate = false; this.selectThisTab = true; } else if (selectedPermissions.length === 0) {