LP#1775466 Add limit perm option to org select
authorBill Erickson <berickxx@gmail.com>
Wed, 1 Aug 2018 21:50:08 +0000 (17:50 -0400)
committerBill Erickson <berickxx@gmail.com>
Wed, 5 Sep 2018 14:05:23 +0000 (10:05 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/eg2/src/app/share/org-select/org-select.component.ts
Open-ILS/src/eg2/src/app/staff/sandbox/sandbox.component.html

index 539b2fe..bd63ed5 100644 (file)
@@ -12,6 +12,7 @@ import {AuthService} from '@eg/core/auth.service';
 import {StoreService} from '@eg/core/store.service';
 import {OrgService} from '@eg/core/org.service';
 import {IdlObject} from '@eg/core/idl.service';
+import {PermService} from '@eg/core/perm.service';
 import {NgbTypeahead, NgbTypeaheadSelectItemEvent} from '@ng-bootstrap/ng-bootstrap';
 
 // Use a unicode char for spacing instead of ASCII=32 so the browser
@@ -80,6 +81,11 @@ export class OrgSelectComponent implements OnInit {
         }
     }
 
+    permLimitOrgs: number[];
+    @Input() set limitPerms(perms: string[]) {
+        this.applyPermLimitOrgs(perms);
+    }
+
     // Modify the selected org unit by ID via data binding.
     // This WILL result in an onChange event firing.
     @Input() set applyOrgId(id: number) {
@@ -95,8 +101,11 @@ export class OrgSelectComponent implements OnInit {
     constructor(
       private auth: AuthService,
       private store: StoreService,
-      private org: OrgService
-    ) {}
+      private org: OrgService,
+      private perm: PermService
+    ) {
+        this.permLimitOrgs = [];
+    }
 
     ngOnInit() {
 
@@ -118,6 +127,23 @@ export class OrgSelectComponent implements OnInit {
         }
     }
 
+    // 
+    applyPermLimitOrgs(perms: string[]) {
+
+        if (!perms || perms.length === 0) {
+            this.permLimitOrgs = [];
+            return;
+        }
+
+        // NOTE: If permLimitOrgs is useful in a non-staff context
+        // we need to change this to support non-staff perm checks.
+        this.perm.hasWorkPermAt(perms, true).then(permMap => {
+            this.permLimitOrgs = 
+                // safari-friendly version of Array.flat()
+                Object.values(permMap).reduce((acc,val) => acc.concat(val), []);
+        });
+    }
+
     // Format for display in the selector drop-down and input.
     formatForDisplay(org: IdlObject): OrgDisplay {
         return {
@@ -155,6 +181,13 @@ export class OrgSelectComponent implements OnInit {
                     this.hidden.filter(id => org.id() === id).length === 0
                 );
 
+                if (this.permLimitOrgs && this.permLimitOrgs.length) {
+                    // Avoid showing org units where the user does
+                    // not have the requested permission.
+                    orgs = orgs.filter(org => 
+                        this.permLimitOrgs.includes(org.id()));
+                }
+
                 if (term !== '_CLICK_') {
                     // For search-driven events, limit to the matching
                     // org units.
index db47b59..0f76d51 100644 (file)
   <div class="col-lg-4">
    <button class="btn btn-info" (click)="testToast()">Test Toast Message</button>
   </div>
+  <div class="col-lg-2">
+    Org select with limit perms
+  </div>
+  <div class="col-lg-2">
+    <eg-org-select [limitPerms]="['REGISTER_WORKSTATION']">
+    </eg-org-select>
+  </div>
 </div>
 <!-- /Progress Dialog Experiments ----------------------------- -->