feat(19.6.1-01): administrator form with server-side privilege rules

- create and edit administrators: password (min 4, max 72 bytes, bcrypt
  at admin.password.bcrypt_cost), role picker, radio permission overrides,
  superuser switch; no mail on create
- superusers hidden from non-superusers in list, lookups and schemas;
  is_superuser projected to superusers so an untouched save keeps it
- save-path rules: superuser-only switch, role and permission ceilings,
  no outranking, no own role/flag/overrides/password change, last active
  superuser not demoted (advisory lock + count in the write tx)
- password set for another admin bumps tokens_valid_after
- role, superuser, activation and last sign-in list filters
This commit is contained in:
Jakub Zych
2026-10-22 11:08:12 +02:00
parent 7340241f22
commit 88401737c9
12 changed files with 1032 additions and 7 deletions

View File

@@ -781,8 +781,12 @@ func (s *service) formSchema(w http.ResponseWriter, r *http.Request) {
// action this admin may not run is not offered. A new slice: the
// localized view must never share its backing array with the cache.
principal, _ := bouncer.User(r.Context())
hidden := hiddenSchemaFields(r.Context(), cc)
kept := make([]FormField, 0, len(view.Fields))
for _, field := range view.Fields {
if hidden[field.Name] {
continue
}
if field.Type == "widget" {
action, ok := cc.Actions[field.Action]
if !ok || !Allows(principal, action.Permissions) {
@@ -814,6 +818,28 @@ func (s *service) formSchema(w http.ResponseWriter, r *http.Request) {
})
}
// schemaFieldHider is implemented by cabana's own admin controllers that
// hide some fields from some administrators (the Administrators screen's
// is_superuser for non-superusers). hiddenSchemaFields names the form
// fields, list columns and list filters to leave out of this request's
// schemas. Unexported, like recordDataExtender.
type schemaFieldHider interface {
hiddenSchemaFields(ctx context.Context) map[string]bool
}
// hiddenSchemaFields asks cc's controller which schema entries to hide for
// the request; nil when it hides none.
func hiddenSchemaFields(ctx context.Context, cc *CompiledController) map[string]bool {
if cc == nil {
return nil
}
hider, ok := cc.Controller.(schemaFieldHider)
if !ok || hider == nil {
return nil
}
return hider.hiddenSchemaFields(ctx)
}
func (s *service) listSchema(w http.ResponseWriter, r *http.Request) {
s.protect(w, r, func(cc *CompiledController) {
schema := cc.List
@@ -848,6 +874,24 @@ func (s *service) listSchema(w http.ResponseWriter, r *http.Request) {
}
}
view.BulkActions = bulk
// Fields a controller hides from this administrator take their
// column and filter with them; new slices again.
if hidden := hiddenSchemaFields(r.Context(), cc); len(hidden) > 0 {
columns := make([]ListColumn, 0, len(view.Columns))
for _, column := range view.Columns {
if !hidden[column.Key] {
columns = append(columns, column)
}
}
view.Columns = columns
filters := make([]ListFilter, 0, len(view.Filters))
for _, filter := range view.Filters {
if !hidden[filter.Name] {
filters = append(filters, filter)
}
}
view.Filters = filters
}
view.Assets = s.controllerAssets(cc)
meta := map[string]any{}
if view.Meta != nil {