forked from ForceCLI/force
-
Notifications
You must be signed in to change notification settings - Fork 0
/
security.go
364 lines (318 loc) · 8.92 KB
/
security.go
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package main
import (
"container/list"
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
////////////////////////////////////////////////////////////////////////
// Parse the permissions for a given profile and return a Profile struct
////////////////////////////////////////////////////////////////////////
type XLS interface {
addProperty(name string, value string)
addToProfile(p Profile)
}
type OLS struct {
objectName string
allowCreate string
allowDelete string
allowEdit string
allowRead string
modifyAllRecords string
viewAllRecords string
}
func (o *OLS) addProperty(name string, value string) {
switch name {
case "object":
o.objectName = value
case "allowCreate":
o.allowCreate = value
case "allowDelete":
o.allowDelete = value
case "allowEdit":
o.allowEdit = value
case "allowRead":
o.allowRead = value
case "modifyAllRecords":
o.modifyAllRecords = value
case "viewAllRecords":
o.viewAllRecords = value
}
// fmt.Println("Object Property " + name + "=" + value)
}
func (o *OLS) getProperty(name string) string {
switch name {
case "Allow Create":
return o.allowCreate
case "Allow Delete":
return o.allowDelete
case "Allow Edit":
return o.allowEdit
case "Allow Read":
return o.allowRead
case "Modify All Records":
return o.modifyAllRecords
case "View All Records":
return o.viewAllRecords
}
return ""
}
func (o *OLS) addToProfile(p Profile) {
p.objectPermissions[o.objectName] = *o
}
type FLS struct {
field string
editable string
readable string
}
func (f *FLS) addProperty(name string, value string) {
switch name {
case "field":
f.field = value
case "editable":
f.editable = value
case "readable":
f.readable = value
}
// fmt.Println("Field Property " + name + "=" + value)
}
func (f *FLS) addToProfile(p Profile) {
p.fieldPermissions[f.field] = *f
}
type Profile struct {
name string
fieldPermissions map[string]FLS
objectPermissions map[string]OLS
}
func parseProfileXML(profileName string, text string) Profile {
p := new(Profile)
p.name = profileName
p.fieldPermissions = map[string]FLS{}
p.objectPermissions = map[string]OLS{}
var currentElement XLS
r := strings.NewReader(text)
parser := xml.NewDecoder(r)
depth := 0
eltType := ""
propertyName := ""
for {
token, err := parser.Token()
if err != nil {
break
}
switch t := token.(type) {
case xml.StartElement:
elmt := xml.StartElement(t)
name := elmt.Name.Local
if depth == 1 {
eltType = name
if eltType == "objectPermissions" {
currentElement = new(OLS)
} else if eltType == "fieldPermissions" {
currentElement = new(FLS)
} else {
currentElement = nil
}
}
if depth == 2 {
propertyName = name
}
depth++
case xml.EndElement:
if depth == 2 && currentElement != nil {
currentElement.addToProfile(*p)
}
depth--
case xml.CharData:
bytes := xml.CharData(t)
if currentElement != nil && depth == 3 {
currentElement.addProperty(propertyName, string(bytes))
}
default:
}
}
// fmt.Println(p)
return *p
}
//////////////////////////////////////////////////////////////////////
// Read information about an SObject and returns a CustomObject struct
//////////////////////////////////////////////////////////////////////
type CustomObject struct {
objectName string
fieldNames []string
nbFields int
}
func (co *CustomObject) addField(name string) {
co.fieldNames[co.nbFields] = name
co.nbFields++
}
func (co *CustomObject) getProfileFootprint(p Profile) string {
key := "OLS:" + p.objectPermissions[co.objectName].allowCreate + "," +
p.objectPermissions[co.objectName].allowDelete + "," +
p.objectPermissions[co.objectName].allowEdit + "," +
p.objectPermissions[co.objectName].allowRead + "," +
p.objectPermissions[co.objectName].modifyAllRecords + "," +
p.objectPermissions[co.objectName].viewAllRecords + ","
for idx := 0; idx < co.nbFields; idx++ {
f := co.fieldNames[idx]
key += f + ":" + p.fieldPermissions[co.objectName+"."+f].editable + "," +
p.fieldPermissions[co.objectName+"."+f].readable + ","
}
return key
}
func parseCustomObjectXML(objectName string, text string) CustomObject {
obj := CustomObject{objectName: objectName, nbFields: 0, fieldNames: make([]string, 512, 512)}
r := strings.NewReader(text)
parser := xml.NewDecoder(r)
depth := 0
var firstLevel, secondLevel string
for {
token, err := parser.Token()
if err != nil {
break
}
switch t := token.(type) {
case xml.StartElement:
elmt := xml.StartElement(t)
name := elmt.Name.Local
if depth == 1 {
firstLevel = name
} else if depth == 2 {
secondLevel = name
}
depth++
case xml.EndElement:
if depth == 3 {
secondLevel = ""
} else if depth == 2 {
firstLevel = ""
}
depth--
case xml.CharData:
bytes := xml.CharData(t)
if depth == 3 && firstLevel == "fields" && secondLevel == "fullName" {
obj.addField(string(bytes))
}
default:
}
}
// fmt.Println(obj)
return obj
}
/////////////////////////////////////////////////////////
var cmdSecurity = &Command{
Run: runSecurity,
Usage: "security [SObject]",
Short: "Displays the OLS and FLS for a give SObject",
Long: `
Displays the OLS and FLS for a given SObject
Examples:
force security Case
`,
}
func runSecurity(cmd *Command, args []string) {
wd, _ := os.Getwd()
root := filepath.Join(wd, ".")
var query ForceMetadataQuery
if len(args) == 1 {
query = ForceMetadataQuery{
{Name: "Profile", Members: []string{"*"}},
{Name: "CustomObject", Members: args},
}
} else {
fmt.Printf("Pass an SObject name")
return
}
force, _ := ActiveForce()
// Step 1: retrieve the desired metadata
files, err := force.Metadata.Retrieve(query)
if err != nil {
ErrorAndExit(err.Error())
}
// Step 2: go through the metadata and construct a list of Profile (profiles) and a CustomObject (theObject)
var profiles list.List
var theObject CustomObject
for name, data := range files {
if strings.HasPrefix(name, "profiles/") {
profileName := strings.TrimSuffix(strings.TrimPrefix(name, "profiles/"), ".profile")
profiles.PushBack(parseProfileXML(profileName, string(data)))
} else if strings.HasPrefix(name, "objects/") {
objectName := strings.TrimSuffix(strings.TrimPrefix(name, "objects/"), ".object")
if objectName == args[0] {
theObject = parseCustomObjectXML(objectName, string(data))
}
}
}
// Step 3: group the profiles that have the exact same OLS and FLS
// for the desired object together
allProfiles := map[string]list.List{}
var p Profile
var profileKeys list.List
for e := profiles.Front(); e != nil; e = e.Next() {
p = e.Value.(Profile)
key := theObject.getProfileFootprint(p)
tmpList, OK := allProfiles[key]
if !OK {
var tmpList2 list.List
tmpList2.PushBack(p)
allProfiles[key] = tmpList2
profileKeys.PushBack(key)
} else {
tmpList.PushBack(p)
}
}
// Step 4: generate an HTML file that shows the various groups of profiles
// as well as their OLS and FLS
HTMLoutput := "<html><body>" +
"<table border=\"1\" style=\"border-collapse:collapse;\">" +
"<tr><td></td>"
for key := profileKeys.Front(); key != nil; key = key.Next() {
val := allProfiles[key.Value.(string)]
profileNames := ""
for v := val.Front(); v != nil; v = v.Next() {
if v.Value == nil {
continue
}
theProfile := v.Value.(Profile)
profileNames += theProfile.name + "<br/>"
}
HTMLoutput += "<td>" + strings.Replace(profileNames, " ", " ", -1) + "</td>"
}
HTMLoutput += "</tr>"
OLSproperties := []string{"Allow Create", "Allow Delete", "Allow Edit", "Allow Read", "Modify All Records", "View All Records"}
for _, OLSproperty := range OLSproperties {
HTMLoutput += "<tr><td>[Object] " + OLSproperty + "</td>"
for key := profileKeys.Front(); key != nil; key = key.Next() {
val := allProfiles[key.Value.(string)]
theProfile := val.Front().Value.(Profile)
theOLS := theProfile.objectPermissions[theObject.objectName]
HTMLoutput += "<td>" + theOLS.getProperty(OLSproperty) + "</td>"
}
HTMLoutput += "</tr>"
}
for idx := 0; idx < theObject.nbFields; idx++ {
fieldName := theObject.fieldNames[idx]
HTMLoutput += "<tr><td>" + fieldName + "</td>"
for key := profileKeys.Front(); key != nil; key = key.Next() {
val := allProfiles[key.Value.(string)]
theProfile := val.Front().Value.(Profile)
if theProfile.fieldPermissions[theObject.objectName+"."+fieldName].editable == "true" {
HTMLoutput += "<td>Yes</td>"
} else if theProfile.fieldPermissions[theObject.objectName+"."+fieldName].readable == "true" {
HTMLoutput += "<td>Readonly</td>"
} else {
HTMLoutput += "<td>-</td>"
}
}
HTMLoutput += "</tr>"
}
HTMLoutput += "</table></body></html>"
// Last step: write the file on disk and display it inside a Web browser
if err := ioutil.WriteFile(filepath.Join(root, "security.html"), []byte(HTMLoutput), 0644); err != nil {
ErrorAndExit(err.Error())
}
Open(filepath.Join(root, "security.html"))
}