40 lines
819 B
Go
40 lines
819 B
Go
package acl
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
type ACL map[string][]string
|
|
|
|
func Load(data []byte) (ACL, error) {
|
|
var acl ACL
|
|
if err := json.Unmarshal(data, &acl); err != nil {
|
|
return nil, fmt.Errorf("parsing acl json: %w", err)
|
|
}
|
|
return acl, nil
|
|
}
|
|
|
|
// IsProtected checks if a path has specific access groups defined
|
|
func (a ACL) IsProtected(path string) bool {
|
|
groups, ok := a[path]
|
|
return ok && len(groups) > 0
|
|
}
|
|
|
|
// Allowed checks if a list of user groups has any overlap with the required groups for a path
|
|
func (a ACL) Allowed(path string, userGroups []string) bool {
|
|
requiredGroups, ok := a[path]
|
|
if !ok || len(requiredGroups) == 0 {
|
|
return true // public
|
|
}
|
|
|
|
for _, req := range requiredGroups {
|
|
for _, user := range userGroups {
|
|
if req == user {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|