package access import ( "sort" "strings" "gitlab.com/Timfa/bluegit/internal/i18n" ) const ( ScopeAPI = "api" ScopeReadAPI = "read_api" ScopeReadRepo = "read_repository" ScopeWriteRepo = "write_repository" ) // ScopeClass is het soort aanroep dat een token moet toestaan. type ScopeClass int const ( ScopePublic ScopeClass = iota ScopeRead ScopeWrite ScopeRepoRead ScopeRepoWrite ScopeAdmin ) // Scopes is de verzameling rechten op een toegangstoken. // api dekt de hele API en git. read_api is alleen lezen. // read_repository en write_repository gelden voor git en voor // repository-inhoud (bestanden, commits, branches, tags). type Scopes struct { set map[string]bool } // ParseScopes maakt een opgeslagen lijst. Leeg wordt api. func ParseScopes(in []string) (string, Scopes, error) { if len(in) == 0 { return ScopeAPI, Scopes{set: map[string]bool{ScopeAPI: true}}, nil } seen := map[string]bool{} var list []string for _, raw := range in { name := strings.TrimSpace(raw) if name == "" { continue } switch name { case ScopeAPI, ScopeReadAPI, ScopeReadRepo, ScopeWriteRepo: default: return "", Scopes{}, i18n.E("err.scopeName", name) } if seen[name] { continue } seen[name] = true list = append(list, name) } if len(list) == 0 { return ScopeAPI, Scopes{set: map[string]bool{ScopeAPI: true}}, nil } sort.Strings(list) return strings.Join(list, ","), Scopes{set: seen}, nil } // StoredScopes leest de kommagescheiden kolom. Leeg telt als api. func StoredScopes(raw string) Scopes { raw = strings.TrimSpace(raw) if raw == "" { return Scopes{set: map[string]bool{ScopeAPI: true}} } set := map[string]bool{} for _, part := range strings.Split(raw, ",") { part = strings.TrimSpace(part) if part != "" { set[part] = true } } if len(set) == 0 { set[ScopeAPI] = true } return Scopes{set: set} } func (s Scopes) List() []string { var out []string for name := range s.set { out = append(out, name) } sort.Strings(out) return out } func (s Scopes) APIWrite() bool { return s.set[ScopeAPI] } func (s Scopes) APIRead() bool { return s.set[ScopeAPI] || s.set[ScopeReadAPI] } func (s Scopes) RepoWrite() bool { return s.set[ScopeAPI] || s.set[ScopeWriteRepo] } func (s Scopes) RepoRead() bool { return s.RepoWrite() || s.set[ScopeReadRepo] } // Allows meldt of deze scopes de klasse dekken. func (s Scopes) Allows(class ScopeClass) bool { switch class { case ScopePublic: return true case ScopeRead: return s.APIRead() case ScopeWrite, ScopeAdmin: return s.APIWrite() case ScopeRepoRead: return s.APIRead() || s.RepoRead() case ScopeRepoWrite: return s.APIWrite() || s.RepoWrite() default: return false } } // AllScopes is wat een SSH-sessie mag: dezelfde rechten als de UI. func AllScopes() Scopes { return Scopes{set: map[string]bool{ScopeAPI: true}} }