UNPKG

4.81 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23 return new (P || (P = Promise))(function (resolve, reject) {
24 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27 step((generator = generator.apply(thisArg, _arguments || [])).next());
28 });
29};
30Object.defineProperty(exports, "__esModule", { value: true });
31exports.hasPermissions = void 0;
32const bf_types_1 = require("bf-types");
33const system_1 = __importStar(require("../system"));
34const common_1 = require("../common");
35function getUser() {
36 return __awaiter(this, void 0, void 0, function* () {
37 return system_1.default.nexus.getUser();
38 });
39}
40function getUserDoc() {
41 return __awaiter(this, void 0, void 0, function* () {
42 const api = system_1.default.getLibModule(system_1.LibModule.API);
43 const userId = system_1.default.nexus.getUser().sub;
44 const personId = system_1.default.nexus.getUser().person_id;
45 const user = yield api.get(`core/user/entity/${userId}`);
46 if (personId && !user) {
47 const person = yield api.get(`crm/people/entity/${personId}`);
48 if (person) {
49 return person;
50 }
51 }
52 if (!user) {
53 throw new Error('Failed to retrieve the document for the authenticated user.');
54 }
55 return user;
56 });
57}
58function getUserDocs() {
59 return __awaiter(this, void 0, void 0, function* () {
60 const api = system_1.default.getLibModule(system_1.LibModule.API);
61 const users = yield api.get('users');
62 if (!users || !Array.isArray(users)) {
63 return [];
64 }
65 return users;
66 });
67}
68function getOrganization() {
69 return __awaiter(this, void 0, void 0, function* () {
70 return common_1.moduleLink(bf_types_1.CORE_MODULES.ORGANIZATION, system_1.default.nexus.getUser().organization[0]);
71 });
72}
73function getOrganizationDoc() {
74 return __awaiter(this, void 0, void 0, function* () {
75 const api = system_1.default.getLibModule(system_1.LibModule.API);
76 const organizationId = system_1.default.nexus.getUser().organization[0];
77 const organization = yield api.get(`core/organization/entity/${organizationId}`);
78 if (!organization) {
79 throw new Error("failed to retrieve the document for the authenticated user's organization");
80 }
81 return organization;
82 });
83}
84function hasPermissions(participants, securityLevel, ...validModules) {
85 let state = false;
86 const scopes = [];
87 const validModuleIds = validModules.map((m) => m.module_id);
88 for (const participant of participants) {
89 if (validModuleIds.includes(participant.module_id)) {
90 scopes.push(...participant.scopes);
91 }
92 }
93 for (const scope of scopes) {
94 // if disallows, return immediately
95 if (bf_types_1.ScopeDefinition[scope].disallows.indexOf(securityLevel) !== -1) {
96 return false;
97 }
98 // if allowed set state to true, but continue looking, in case a scope disallows it, Disallows trumps allows
99 if (bf_types_1.ScopeDefinition[scope].allows.indexOf(securityLevel) !== -1) {
100 state = true;
101 }
102 }
103 return state;
104}
105exports.hasPermissions = hasPermissions;
106function logOut() {
107 system_1.default.nexus.disconnect();
108}
109const auth = {
110 getUser,
111 getUserDoc,
112 getUserDocs,
113 getOrganizationDoc,
114 getOrganization,
115 hasPermissions,
116 logOut,
117};
118exports.default = system_1.default.sealModule(Object.freeze(auth));