package model import ( "errors" "fmt" "maps" "slices" "sort" "strings" ) type Action string const ( ActScale Action = "scale" ActStorage Action = "storage" ActMove Action = "move" ActKeep Action = "keep" ActManage Action = "manage" ) var ( AllActions = []Action{ActScale, ActStorage, ActMove, ActKeep} GrantActions = append(slices.Clone(AllActions), ActManage) ErrLastManager = errors.New("that would leave nobody able to manage permissions") ) type PermissionsConfig struct { Managers []string `toml:"managers" json:"managers"` Default map[string][]string `toml:"default" json:"default"` Channels map[string]map[string][]string `toml:"channels" json:"channels"` DM map[string]bool `toml:"dm" json:"dm"` } func (p PermissionsConfig) Validate() error { if len(p.Managers) == 0 { return fmt.Errorf("permissions.managers must name at least one user manager") } if len(p.Default) == 0 { return fmt.Errorf("[permissions.default] must name at least one super user, e.g. U0ADMIN = [\"all\"]") } check := func(where string, list map[string][]string) error { for user, actions := range list { for _, a := range actions { if a != "all" && !slices.Contains(GrantActions, Action(a)) { return fmt.Errorf("%s: unknown permission %q for %s (use %s, manage or all)", where, a, user, actionNames()) } } } return nil } if err := check("permissions.default", p.Default); err != nil { return err } for channel, list := range p.Channels { if err := check("permissions.channels."+channel, list); err != nil { return err } } for a := range p.DM { if !slices.Contains(AllActions, Action(a)) { return fmt.Errorf("permissions.dm: unknown permission %q (use %s)", a, actionNames()) } } return nil } func (p PermissionsConfig) Clone() PermissionsConfig { c := PermissionsConfig{ Managers: slices.Clone(p.Managers), Default: cloneList(p.Default), Channels: map[string]map[string][]string{}, DM: maps.Clone(p.DM), } for ch, list := range p.Channels { c.Channels[ch] = cloneList(list) } if c.DM == nil { c.DM = map[string]bool{} } return c } func cloneList(list map[string][]string) map[string][]string { out := map[string][]string{} for user, actions := range list { out[user] = slices.Clone(actions) } return out } func actionNames() string { names := make([]string, len(AllActions)) for i, a := range AllActions { names[i] = string(a) } return strings.Join(names, ", ") } func (p PermissionsConfig) SuperUsers(channel string) (list map[string][]string, own bool) { if l := p.Channels[channel]; len(l) > 0 { return l, true } return p.Default, false } // "all" covers the container actions only; managing permissions is always granted by name. func Grants(actions []string, a Action) bool { if a == ActManage { return slices.Contains(actions, string(ActManage)) } return slices.Contains(actions, "all") || slices.Contains(actions, string(a)) } func (p PermissionsConfig) Allowed(kind OwnerKind, channel, user string, a Action) bool { if kind == OwnerUser { allowed, set := p.DM[string(a)] return !set || allowed } list, _ := p.SuperUsers(channel) return Grants(list[user], a) } func (p PermissionsConfig) IsManager(user string) bool { return slices.Contains(p.Managers, user) } func (p PermissionsConfig) CanManage(channel, user string) bool { return p.IsManager(user) || p.Allowed(OwnerChannel, channel, user, ActManage) } func (p PermissionsConfig) Holders(channel string, a Action) []string { list, _ := p.SuperUsers(channel) var out []string for user, actions := range list { if Grants(actions, a) { out = append(out, user) } } sort.Strings(out) return out } func (p PermissionsConfig) ActionsFor(channel, user string) []Action { list, _ := p.SuperUsers(channel) var out []Action for _, a := range GrantActions { if Grants(list[user], a) { out = append(out, a) } } return out } func expand(actions []Action) []Action { if slices.Contains(actions, "all") { out := slices.Clone(AllActions) for _, a := range actions { if a != "all" && !slices.Contains(out, a) { out = append(out, a) } } return out } return actions } // An empty channel means the default list. The first grant in a channel that // still uses the default list copies it, so nobody loses access there. func (p *PermissionsConfig) Grant(channel string, users []string, actions []Action) { list := p.Default if channel != "" { if len(p.Channels[channel]) == 0 { p.Channels[channel] = cloneList(p.Default) } list = p.Channels[channel] } for _, user := range users { have := expandStrings(list[user]) for _, a := range expand(actions) { if !slices.Contains(have, string(a)) { have = append(have, string(a)) } } list[user] = have } } // No actions means everything. func (p *PermissionsConfig) Revoke(channel string, users []string, actions []Action) { list := p.Default if channel != "" { if len(p.Channels[channel]) == 0 { p.Channels[channel] = cloneList(p.Default) } list = p.Channels[channel] } remove := expand(actions) for _, user := range users { if len(actions) == 0 { delete(list, user) continue } var keep []string for _, a := range expandStrings(list[user]) { if !slices.Contains(remove, Action(a)) { keep = append(keep, a) } } if len(keep) == 0 { delete(list, user) } else { list[user] = keep } } } func expandStrings(actions []string) []string { as := make([]Action, len(actions)) for i, a := range actions { as[i] = Action(a) } var out []string for _, a := range expand(as) { out = append(out, string(a)) } return out } func (p *PermissionsConfig) AddManagers(users []string) { for _, u := range users { if !p.IsManager(u) { p.Managers = append(p.Managers, u) } } } func (p *PermissionsConfig) RemoveManagers(users []string) error { keep := slices.DeleteFunc(slices.Clone(p.Managers), func(u string) bool { return slices.Contains(users, u) }) if len(keep) == 0 { return ErrLastManager } p.Managers = keep return nil }