LP#626157 ang2 perms
authorBill Erickson <berickxx@gmail.com>
Tue, 26 Dec 2017 23:09:38 +0000 (18:09 -0500)
committerBill Erickson <berickxx@gmail.com>
Mon, 5 Mar 2018 17:24:06 +0000 (17:24 +0000)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/eg2-src/src/app/app.module.ts
Open-ILS/eg2-src/src/app/core/auth.ts
Open-ILS/eg2-src/src/app/core/org.ts
Open-ILS/eg2-src/src/app/core/perm.ts [new file with mode: 0644]
Open-ILS/eg2-src/src/app/staff/admin/workstation/workstations/app.component.ts
Open-ILS/eg2-src/src/app/staff/app.module.ts
Open-ILS/eg2-src/src/app/staff/resolver.service.ts

index d9d06e3..ce8f2c1 100644 (file)
@@ -19,6 +19,7 @@ import {EgStoreService} from '@eg/core/store';
 import {EgIdlService} from '@eg/core/idl';
 import {EgNetService} from '@eg/core/net';
 import {EgAuthService} from '@eg/core/auth';
+import {EgPermService} from '@eg/core/perm';
 import {EgPcrudService} from '@eg/core/pcrud';
 import {EgOrgService} from '@eg/core/org';
 
@@ -39,6 +40,7 @@ import {EgOrgService} from '@eg/core/org';
     EgIdlService,
     EgNetService,
     EgAuthService,
+    EgPermService,
     EgPcrudService,
     EgOrgService
   ],
index ac21206..f6688c2 100644 (file)
@@ -204,8 +204,6 @@ export class EgAuthService {
         // to expire on the server.
         let pollTime = this.authtime() * 1000 + 5000;
 
-        console.debug('EgAuth session poll at ' + pollTime);
-
         this.pollTimeout = setTimeout(() => {
             this.net.request(
                 'open-ils.auth',
index bc1124a..8508c41 100644 (file)
@@ -11,6 +11,8 @@ interface OrgFilter {
     canHaveUsers?: boolean;
     canHaveVolumes?: boolean;
     opacVisible?: boolean;
+    inList?: number[];
+    notInList?: number[];
 }
 
 interface OrgSettingsBatch {
@@ -43,9 +45,10 @@ export class EgOrgService {
 
     /**
      * Returns a list of org units that match the selected criteria.
+     * All filters must match for an org to be included in the result set.
      * Unset filter options are ignored.
      */
-    filterList(filter: OrgFilter, asId: boolean): any[] {
+    filterList(filter: OrgFilter, asId?: boolean): any[] {
         let list = [];
         this.list().forEach(org => {
 
@@ -61,6 +64,12 @@ export class EgOrgService {
             if (ov && !this.opacVisible(org)) return;
             if (ov === false && this.opacVisible(org)) return;
 
+            if (filter.inList && filter.inList.indexOf(org.id()) == -1) 
+                return;
+
+            if (filter.notInList && filter.notInList.indexOf(org.id()) > -1) 
+                return;
+
             // All filter tests passed.  Add it to the list
             list.push(asId ? org.id() : org);
         });
diff --git a/Open-ILS/eg2-src/src/app/core/perm.ts b/Open-ILS/eg2-src/src/app/core/perm.ts
new file mode 100644 (file)
index 0000000..6f7cd9f
--- /dev/null
@@ -0,0 +1,58 @@
+import {Injectable} from '@angular/core';
+import {EgNetService} from './net';
+import {EgOrgService} from './org';
+import {EgAuthService} from './auth';
+
+interface HasPermAtResult {
+    [permName: string]: number[];
+}
+
+interface HasPermHereResult {
+    [permName: string]: boolean;
+}
+
+@Injectable()
+export class EgPermService {
+
+    constructor(
+        private net: EgNetService,
+        private org: EgOrgService,
+        private auth: EgAuthService,
+    ) {}
+
+    // workstation not required.
+    hasWorkPermAt(permNames: string[], asId?: boolean): Promise<HasPermAtResult> {
+        return this.net.request(
+            'open-ils.actor',
+            'open-ils.actor.user.has_work_perm_at.batch',
+            this.auth.token(), permNames
+        ).toPromise().then(resp => {
+            var answer: HasPermAtResult = {};
+            permNames.forEach(perm => {
+                var orgs = [];
+                resp[perm].forEach(oneOrg => {
+                    orgs = orgs.concat(this.org.descendants(oneOrg, asId));
+                });
+                answer[perm] = orgs;
+            });
+
+            return answer;
+        });
+    }
+
+    // workstation required
+    hasWorkPermHere(permNames: string[]): Promise<HasPermHereResult> {
+        let wsId: number = +this.auth.user().wsid(); 
+
+        if (!wsId) 
+            return Promise.reject('hasWorkPermHere requires a workstation');
+
+        return this.hasWorkPermAt(permNames, true).then(resp => {
+            let answer: HasPermHereResult = {};
+            Object.keys(resp).forEach(perm => {
+                answer[perm] = resp[perm].indexOf(wsId) > -1;
+            });
+            return answer;
+        });
+    }
+}
index 96d0784..73827a7 100644 (file)
@@ -3,6 +3,7 @@ import {Router, ActivatedRoute} from '@angular/router';
 import {EgStoreService} from '@eg/core/store';
 import {EgIdlObject} from '@eg/core/idl';
 import {EgNetService} from '@eg/core/net';
+import {EgPermService} from '@eg/core/perm';
 import {EgAuthService} from '@eg/core/auth';
 import {EgOrgService} from '@eg/core/org';
 import {EgEventService} from '@eg/core/event';
@@ -44,7 +45,8 @@ export class WorkstationsComponent implements OnInit {
         private net: EgNetService,
         private store: EgStoreService,
         private auth: EgAuthService,
-        private org: EgOrgService
+        private org: EgOrgService,
+        private perm: EgPermService
     ) {}
 
     ngOnInit() {
@@ -60,8 +62,15 @@ export class WorkstationsComponent implements OnInit {
             if (rm) this.removeSelected(this.removeWorkstation = rm)
         })
 
-        // TODO: perm limits required here too
-        this.disableOrgs = this.org.filterList({canHaveUsers : true}, true);
+        this.perm.hasWorkPermAt(['REGISTER_WORKSTATION'], true)
+        .then(perms => {
+            // Disable org units that cannot have users and any
+            // that this user does not have work perms for.
+            this.disableOrgs = 
+                this.org.filterList({canHaveUsers : false}, true)
+                .concat(this.org.filterList(
+                    {notInList : perms.REGISTER_WORKSTATION}, true));
+        });
     }
 
     selected(): Workstation {
index 39561db..6cefff5 100644 (file)
@@ -32,7 +32,6 @@ import {EgConfirmDialogComponent} from '@eg/share/confirm-dialog.component';
     // Components available to all staff/sub modules
     EgOrgSelectComponent,
     EgConfirmDialogComponent,
-    CommonModule,
     FormsModule,
     NgbModule
   ]
index 28e228f..a9c393e 100644 (file)
@@ -2,8 +2,7 @@ import {Injectable} from '@angular/core';
 import {Location} from '@angular/common';
 import {Observable, Observer} from 'rxjs/Rx';
 import {Router, Resolve, RouterStateSnapshot,
-        ActivatedRoute,
-        ActivatedRouteSnapshot} from '@angular/router';
+        ActivatedRoute, ActivatedRouteSnapshot} from '@angular/router';
 import {EgStoreService} from '@eg/core/store';
 import {EgNetService} from '@eg/core/net';
 import {EgAuthService} from '@eg/core/auth';