Refactoring - Split computePrivileges into two functions

This commit is contained in:
Emmanuel BENOîT 2021-02-13 23:18:05 +01:00
parent d3466b9609
commit aac67dd277

View file

@ -25,6 +25,12 @@ type (
External bool External bool
} }
} }
// Privilege information
privInfo struct {
otp, oid string // Type and identifier of object
priv int // Privilege level
}
) )
var ( var (
@ -140,12 +146,8 @@ func computeRoles(mapping groupMapping, membership []string) (roles []string) {
return return
} }
// Compute privileges on Graylog objects that should be granted to an user // Compute privilege levels for each Graylog object based on the user's group membership
func computePrivileges(mapping groupMapping, membership []string) (privileges []string) { func getObjectPrivileges(mapping groupMapping, membership []string) map[string]privInfo {
type privInfo struct {
otp, oid string
priv int
}
rset := make(map[string]privInfo) rset := make(map[string]privInfo)
for _, group := range membership { for _, group := range membership {
for _, priv := range mapping[group].Privileges { for _, priv := range mapping[group].Privileges {
@ -163,16 +165,20 @@ func computePrivileges(mapping groupMapping, membership []string) (privileges []
rset[key] = record rset[key] = record
} }
} }
return rset
}
privileges = make([]string, 0) // Compute privileges on Graylog objects that should be granted to an user
for _, record := range rset { func computePrivileges(mapping groupMapping, membership []string) []string {
privileges := make([]string, 0)
for _, record := range getObjectPrivileges(mapping, membership) {
key := fmt.Sprintf("%s:%s", record.otp, privStr[record.priv]) key := fmt.Sprintf("%s:%s", record.otp, privStr[record.priv])
for _, p := range graylogPriv[key] { for _, p := range graylogPriv[key] {
pval := fmt.Sprintf(p, record.oid) pval := fmt.Sprintf(p, record.oid)
privileges = append(privileges, pval) privileges = append(privileges, pval)
} }
} }
return return privileges
} }
// Delete a Graylog user account // Delete a Graylog user account