* WIP: List * make toV0ResourcePermissions work with an ordered list of assignments to ensure consistency in the results * Test templates * Split list query in two. I clearly need scopePatterns * Add pagination with offsets * Remove unecessary comment * implement listiterator * add listiterator tests * return the correct resource version * use SkipIntegrationTestInShortMode * No need for the extra check on pagination being correctly set Co-authored-by: Ieva <ieva.vasiljeva@grafana.com> * Spec is out of date * Remove wrong comment * Add a test for the pagination token --------- Co-authored-by: mohammad-hamid <mohammad.hamid@grafana.com> Co-authored-by: Ieva <ieva.vasiljeva@grafana.com>
51 lines
997 B
Go
51 lines
997 B
Go
package resourcepermission
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
type Mapper interface {
|
|
ActionSets() []string
|
|
Scope(name string) string
|
|
ActionSet(level string) (string, error)
|
|
ScopePattern() string
|
|
}
|
|
|
|
type mapper struct {
|
|
resource string
|
|
actionSets []string
|
|
}
|
|
|
|
func NewMapper(resource string, levels []string) Mapper {
|
|
sets := make([]string, 0, len(levels))
|
|
for _, level := range levels {
|
|
sets = append(sets, resource+":"+level)
|
|
}
|
|
return mapper{
|
|
resource: resource,
|
|
actionSets: sets,
|
|
}
|
|
}
|
|
|
|
func (m mapper) ActionSets() []string {
|
|
return m.actionSets
|
|
}
|
|
|
|
func (m mapper) Scope(name string) string {
|
|
return m.resource + ":uid:" + name
|
|
}
|
|
|
|
func (m mapper) ActionSet(level string) (string, error) {
|
|
actionSet := m.resource + ":" + strings.ToLower(level)
|
|
if !slices.Contains(m.actionSets, actionSet) {
|
|
return "", fmt.Errorf("invalid level (%s): %w", level, errInvalidSpec)
|
|
}
|
|
return actionSet, nil
|
|
}
|
|
|
|
func (m mapper) ScopePattern() string {
|
|
return m.resource + ":uid:%"
|
|
}
|