-
Notifications
You must be signed in to change notification settings - Fork 41
/
unperm.ts
90 lines (77 loc) · 4.65 KB
/
unperm.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { flags, SfdxCommand } from '@salesforce/command';
import chalk from 'chalk';
import { getExisting } from '@mshanemc/plugin-helpers/dist/getExisting';
import { writeJSONasXML } from '@mshanemc/plugin-helpers/dist/JSONXMLtools';
import { setupArray } from '@mshanemc/plugin-helpers/dist/setupArray';
import fs = require('fs-extra');
export default class UnPerm extends SfdxCommand {
public static description = 'remove references to an object from profiles/permsets (all or a specific one)';
public static examples = [
`sfdx shane:object:unperm -o OpportunitySplit
// go through all the profiles/permsets in force-app/main/default and remove the object, field, recordtypes and layout assignments (profile only) for the named object
`
];
protected static flagsConfig = {
object: flags.string({ char: 'o', required: true, description: 'remove all references to an object from profiles or permsets' }),
directory: flags.directory({
char: 'd',
default: 'force-app/main/default',
description: 'Where is all this metadata? defaults to force-app/main/default'
}),
specific: flags.string({ char: 's', description: 'specify a profile or permset by name to only remove it from that one' })
};
protected static requiresProject = true;
public async run(): Promise<any> {
const profileDirectory = `${this.flags.directory}/profiles`;
const permsetDirectory = `${this.flags.directory}/permissionsets`;
if (!this.flags.specific) {
// just do all of them
const [profiles, permsets] = await Promise.all([fs.readdir(profileDirectory), fs.readdir(permsetDirectory)]);
await Promise.all([
...profiles.map(profile => this.removePerms(`${profileDirectory}/${profile}`, 'Profile')),
...permsets.map(permSet => this.removePerms(`${permsetDirectory}/${permSet}`, 'PermissionSet'))
]);
} else if (this.flags.specific) {
// ok, what kind is it and does it exist?
if (fs.existsSync(`${profileDirectory}/${this.flags.specific}`)) {
await this.removePerms(this.flags.specific, 'Profile');
} else if (fs.existsSync(`${permsetDirectory}/${this.flags.specific}`)) {
await this.removePerms(this.flags.specific, 'PermissionSet');
} else {
throw new Error(`not found: ${this.flags.specific}`);
}
}
}
public async removePerms(targetFilename: string, metadataType: string): Promise<void> {
let existing = await getExisting(targetFilename, metadataType);
existing = await setupArray(existing, 'objectPermissions');
existing = await setupArray(existing, 'fieldPermissions');
existing = await setupArray(existing, 'layoutAssignments');
existing = await setupArray(existing, 'recordTypes');
existing = await setupArray(existing, 'tabSettings');
// this.ux.logJson(existing.objectPermissions);
// this.ux.logJson(existing.fieldPermissions);
const objectBefore = existing.objectPermissions.length;
const fieldBefore = existing.fieldPermissions.length;
const layoutBefore = existing.layoutAssignments.length;
const recordTypeBefore = existing.recordTypeVisibilities.length;
existing.objectPermissions = existing.objectPermissions.filter(item => item.object !== this.flags.object);
existing.fieldPermissions = existing.fieldPermissions.filter(item => !item.field.startsWith(`${this.flags.object}.`));
existing.layoutAssignments = existing.layoutAssignments.filter(item => !item.layout.startsWith(`${this.flags.object}-`));
existing.recordTypeVisibilities = existing.recordTypeVisibilities.filter(item => !item.recordType.startsWith(`${this.flags.object}.`));
existing.tabSettings = existing.tabSettings.filter(item => item.tab !== this.flags.object && item.tab !== `standard-${this.flags.object}`);
existing.tabVisibilities = existing.tabVisibilities.filter(
item => item.tab !== this.flags.object && item.tab !== `standard-${this.flags.object}`
);
await writeJSONasXML({
path: targetFilename,
type: metadataType,
json: existing
});
this.ux.log(
`removed ${objectBefore - existing.objectPermissions.length} objects, ${recordTypeBefore -
existing.recordTypeVisibilities.length} recordTypes, ${layoutBefore - existing.layoutAssignments.length} layout, ${fieldBefore -
existing.fieldPermissions.length} fields from ${this.flags.object} ${chalk.blue(targetFilename)}`
);
}
}