package cabana import ( "context" "strings" "git.golem15.com/golem15/summercms/modules/backpack" "git.golem15.com/golem15/summercms/modules/phrasebook" ) // TokenScopeNames answers the names of the personal API token scope catalog // in catalog order: *, admin:read, then every plugin's TokenScopes in // activation order. It compiles the catalog from the activated plugins // (party.Activated), so it works at plugin Boot, before cabana.Activate. A // plugin that advertises the same scopes elsewhere (an OAuth server whose // grants act as administrators, for example) reads them here. The error is // the catalog's compile error (an invalid or duplicate plugin scope). func TokenScopeNames(app *backpack.App) ([]string, error) { reg, err := tokenCatalogForApp(app) if err != nil { return nil, err } if reg == nil || reg.tokenScopes == nil { return []string{}, nil } out := make([]string, 0, len(reg.tokenScopes.ordered)) for _, scope := range reg.tokenScopes.ordered { out = append(out, scope.Name) } return out, nil } // TokenScopeLabels answers the translated label of each name, in input // order: the label the token screens show for a catalog scope, and the name // itself for a name the catalog does not know (or when the catalog fails to // compile). The locale is the one ctx carries. func TokenScopeLabels(ctx context.Context, app *backpack.App, names []string) []string { if ctx == nil { ctx = context.Background() } var tr *phrasebook.Translator if app != nil { tr, _ = app.Lookup[*phrasebook.Translator]() } reg, err := tokenCatalogForApp(app) out := make([]string, 0, len(names)) for _, name := range names { label := name if err == nil && reg != nil && reg.tokenScopes != nil { if scope, ok := reg.tokenScopes.byName[name]; ok { label = scope.Label if strings.Contains(label, "::") { label = resolveScopePhrase(ctx, tr, label, label) } if strings.TrimSpace(label) == "" { label = name } } } out = append(out, label) } return out }