UNPKG

4.66 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright (c) 2018, salesforce.com, inc.
4 * All rights reserved.
5 * SPDX-License-Identifier: BSD-3-Clause
6 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
7 */
8Object.defineProperty(exports, "__esModule", { value: true });
9const kit_1 = require("@salesforce/kit");
10const ts_types_1 = require("@salesforce/ts-types");
11const os_1 = require("os");
12const logger_1 = require("./logger");
13const messages_1 = require("./messages");
14const sfdxError_1 = require("./sfdxError");
15/**
16 * A class for assigning a Salesforce User to one or more permission sets.
17 */
18class PermissionSetAssignment {
19 /**
20 * Creates a new instance of PermissionSetAssignment.
21 * @param org The target org for the assignment.
22 */
23 static async init(org) {
24 if (!org) {
25 throw sfdxError_1.SfdxError.create('@salesforce/core', 'permissionSetAssignment', 'orgRequired');
26 }
27 return new PermissionSetAssignment(org, await logger_1.Logger.child('PermissionSetAssignment'));
28 }
29 constructor(org, logger) {
30 this.logger = logger;
31 this.org = org;
32 }
33 /**
34 * Assigns a user to one or more permission sets.
35 * @param id A user id
36 * @param permSetString An array of permission set names.
37 */
38 async create(id, permSetString) {
39 if (!id) {
40 throw sfdxError_1.SfdxError.create('@salesforce/core', 'permissionSetAssignment', 'userIdRequired');
41 }
42 if (!permSetString) {
43 throw sfdxError_1.SfdxError.create('@salesforce/core', 'permissionSetAssignment', 'permSetRequired');
44 }
45 const { nsPrefix, permSetName } = this.parsePermissionSetString(permSetString);
46 let query = `SELECT Id FROM PermissionSet WHERE Name='${permSetName}'`;
47 if (nsPrefix) {
48 query += ` AND NamespacePrefix='${nsPrefix}'`;
49 }
50 const result = await this.org.getConnection().query(query);
51 const permissionSetId = ts_types_1.getString(result, 'records[0].Id');
52 if (!permissionSetId) {
53 if (nsPrefix) {
54 throw sfdxError_1.SfdxError.create('@salesforce/core', 'permissionSetAssignment', 'assignCommandPermissionSetNotFoundForNSError', [permSetName, nsPrefix]);
55 }
56 else {
57 throw sfdxError_1.SfdxError.create('@salesforce/core', 'permissionSetAssignment', 'assignCommandPermissionSetNotFoundError', [permSetName]);
58 }
59 }
60 const assignment = {
61 assigneeId: id,
62 permissionSetId
63 };
64 let createResponse;
65 createResponse = await this.org
66 .getConnection()
67 .sobject('PermissionSetAssignment')
68 .create(kit_1.mapKeys(assignment, (value, key) => kit_1.upperFirst(key)));
69 if (ts_types_1.hasArray(createResponse, 'errors')) {
70 const messages = messages_1.Messages.loadMessages('@salesforce/core', 'permissionSetAssignment');
71 let message = messages.getMessage('errorsEncounteredCreatingAssignment');
72 const errors = createResponse.errors;
73 if (errors && errors.length > 0) {
74 message = `${message}:${os_1.EOL}`;
75 errors.forEach(_message => {
76 message = `${message}${_message}${os_1.EOL}`;
77 });
78 throw new sfdxError_1.SfdxError(message, 'errorsEncounteredCreatingAssignment');
79 }
80 else {
81 throw sfdxError_1.SfdxError.create('@salesforce/core', 'permissionSetAssignment', 'notSuccessfulButNoErrorsReported');
82 }
83 }
84 else {
85 return assignment;
86 }
87 }
88 /**
89 * Parses a permission set name based on if it has a namespace or not.
90 * @param permSetString The permission set string.
91 */
92 parsePermissionSetString(permSetString) {
93 const nsPrefixMatch = permSetString.match(/(\w+(?=__))(__)(.*)/);
94 let nsPrefix;
95 let permSetName;
96 if (nsPrefixMatch) {
97 try {
98 nsPrefix = nsPrefixMatch[1];
99 permSetName = nsPrefixMatch[3];
100 this.logger.debug(`Using namespacePrefix ${nsPrefix} for permission set ${permSetName}`);
101 }
102 catch (e) {
103 // Don't fail if we parse wrong.
104 this.logger.debug(e);
105 }
106 }
107 else {
108 permSetName = permSetString;
109 }
110 return { nsPrefix, permSetName };
111 }
112}
113exports.PermissionSetAssignment = PermissionSetAssignment;
114//# sourceMappingURL=permissionSetAssignment.js.map
\No newline at end of file