45 lines
891 B
Go
45 lines
891 B
Go
package role
|
|
|
|
import (
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
func NormalizeName(name string) (string, error) {
|
|
trimmed := strings.TrimSpace(name)
|
|
if trimmed == "" {
|
|
return "", ErrInvalidRoleName
|
|
}
|
|
return trimmed, nil
|
|
}
|
|
|
|
func SameName(left string, right string) bool {
|
|
return strings.EqualFold(strings.TrimSpace(left), strings.TrimSpace(right))
|
|
}
|
|
|
|
func ValidateGrantTypes(grants []Grant, allowedTypeKeys []string) error {
|
|
for _, grant := range grants {
|
|
if !isTypedKey(grant.Key) {
|
|
continue
|
|
}
|
|
_, typeKey, err := ParseTypedKey(grant.Key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !slices.Contains(allowedTypeKeys, typeKey) {
|
|
return ErrUnknownContentType
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func TypedKeys(typeKey string) []string {
|
|
keys := []string{}
|
|
for _, permission := range catalogue {
|
|
if permission.PerType() {
|
|
keys = append(keys, permission.Key+typeSeparator+typeKey)
|
|
}
|
|
}
|
|
return keys
|
|
}
|