1 import {Injectable} from '@angular/core';
2 import {NetService} from './net.service';
3 import {OrgService} from './org.service';
4 import {AuthService} from './auth.service';
6 interface HasPermAtResult {
7 [permName: string]: any[]; // org IDs or org unit objects
10 interface HasPermHereResult {
11 [permName: string]: boolean;
14 @Injectable({providedIn: 'root'})
15 export class PermService {
18 private net: NetService,
19 private org: OrgService,
20 private auth: AuthService,
23 // workstation not required.
24 hasWorkPermAt(permNames: string[], asId?: boolean): Promise<HasPermAtResult> {
25 return this.net.request(
27 'open-ils.actor.user.has_work_perm_at.batch',
28 this.auth.token(), permNames
29 ).toPromise().then(resp => {
30 const answer: HasPermAtResult = {};
31 permNames.forEach(perm => {
33 resp[perm].forEach(oneOrg => {
34 orgs = orgs.concat(this.org.descendants(oneOrg, asId));
43 // workstation required
44 hasWorkPermHere(permNames: string | string[]): Promise<HasPermHereResult> {
45 permNames = [].concat(permNames);
46 const wsId: number = +this.auth.user().wsid();
49 return Promise.reject('hasWorkPermHere requires a workstation');
52 return this.hasWorkPermAt(permNames, true).then(resp => {
53 const answer: HasPermHereResult = {};
54 Object.keys(resp).forEach(perm => {
55 answer[perm] = resp[perm].indexOf(wsId) > -1;