Phase 12, part 5: roles from the Core's groups
Settings → Sign-in: roles follow the Core's groups, with a prefix. <prefix>-admins makes an admin, <prefix>-<module> edits a module, <prefix>-<module>-view reads it. Set at each sign-in with the Core; someone in a group joins; the owner never changes; someone in none of them keeps the access set here. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+113
-3
@@ -37,6 +37,50 @@ type coreSignin struct {
|
||||
AutoJoin bool `json:"auto_join"` // anyone at the Core's domain joins
|
||||
AutoModules map[string]users.Access `json:"auto_modules"`
|
||||
Issuer string `json:"issuer,omitempty"` // empty: https://authelia.<the Core's domain>
|
||||
// Roles follow the Core's groups: <prefix>-admins, <prefix>-<module>
|
||||
// (edit), <prefix>-<module>-view.
|
||||
Groups bool `json:"groups"`
|
||||
GroupPrefix string `json:"group_prefix,omitempty"` // empty: supermarket
|
||||
}
|
||||
|
||||
func (c coreSignin) prefix() string {
|
||||
if c.GroupPrefix == "" {
|
||||
return "supermarket"
|
||||
}
|
||||
return c.GroupPrefix
|
||||
}
|
||||
|
||||
// groupAccess is the role and modules the Core's groups give, and whether
|
||||
// any of them is one of Supermarket's (if none is, access stays as it was
|
||||
// set here).
|
||||
func groupAccess(prefix string, groups []string) (users.Role, map[string]users.Access, bool) {
|
||||
role, mods, any := users.Member, map[string]users.Access{}, false
|
||||
for _, g := range groups {
|
||||
rest, ok := strings.CutPrefix(strings.ToLower(strings.TrimSpace(g)), strings.ToLower(prefix)+"-")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if rest == "admins" {
|
||||
role, any = users.Admin, true
|
||||
continue
|
||||
}
|
||||
id, view := strings.CutSuffix(rest, "-view")
|
||||
for _, gr := range grants() {
|
||||
if gr.ID != id {
|
||||
continue
|
||||
}
|
||||
any = true
|
||||
if !view {
|
||||
mods[id] = users.Edit
|
||||
} else if mods[id] != users.Edit {
|
||||
mods[id] = users.View
|
||||
}
|
||||
}
|
||||
}
|
||||
if role == users.Admin {
|
||||
mods = map[string]users.Access{}
|
||||
}
|
||||
return role, mods, any
|
||||
}
|
||||
|
||||
func (s *Server) coreSignin() coreSignin {
|
||||
@@ -134,6 +178,7 @@ type signinSettings struct {
|
||||
Grants []grant
|
||||
Problem string
|
||||
Tested string
|
||||
Prefix string // the groups'
|
||||
}
|
||||
|
||||
func (s *Server) signinSettingsPage(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -147,6 +192,7 @@ func (s *Server) renderSigninSettings(w http.ResponseWriter, r *http.Request, pr
|
||||
}
|
||||
d := signinSettings{Core: s.core() != nil, Issuer: s.coreIssuer(), Callback: s.coreCallback(), C: s.coreSignin(),
|
||||
Block: s.clientBlock(), Grants: grants(), Problem: problem, Tested: tested}
|
||||
d.Prefix = d.C.prefix()
|
||||
if c := s.core(); c != nil {
|
||||
d.Domain = c.Domain
|
||||
}
|
||||
@@ -207,6 +253,15 @@ func (s *Server) signinSettingsAction(w http.ResponseWriter, r *http.Request) {
|
||||
c.Enabled = r.PostFormValue("enabled") == "on"
|
||||
c.RequireCore = r.PostFormValue("require_core") == "on"
|
||||
c.AutoJoin = r.PostFormValue("auto_join") == "on"
|
||||
c.Groups = r.PostFormValue("groups") == "on"
|
||||
c.GroupPrefix = strings.ToLower(strings.TrimSpace(r.PostFormValue("group_prefix")))
|
||||
if c.GroupPrefix == "supermarket" {
|
||||
c.GroupPrefix = ""
|
||||
}
|
||||
if strings.ContainsAny(c.GroupPrefix, " ,") {
|
||||
s.renderSigninSettings(w, r, "The groups' prefix is one word, such as supermarket.", "")
|
||||
return
|
||||
}
|
||||
if i := strings.TrimSpace(r.PostFormValue("issuer")); i == "" || strings.HasPrefix(i, "https://") {
|
||||
c.Issuer = strings.TrimRight(i, "/")
|
||||
} else {
|
||||
@@ -234,7 +289,7 @@ func (s *Server) signinSettingsAction(w http.ResponseWriter, r *http.Request) {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
s.audit(r, "signin-settings", "", fmt.Sprintf("with the Core %v, the Core only %v, anyone at the Core's domain joins %v", c.Enabled, c.RequireCore, c.AutoJoin))
|
||||
s.audit(r, "signin-settings", "", fmt.Sprintf("with the Core %v, the Core only %v, anyone at the Core's domain joins %v, roles follow the Core's groups %v", c.Enabled, c.RequireCore, c.AutoJoin, c.Groups))
|
||||
redirect(w, r, "/settings/signin", "saved")
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
@@ -403,9 +458,45 @@ func (s *Server) finishCore(w http.ResponseWriter, r *http.Request, p *oidc.Prov
|
||||
return
|
||||
}
|
||||
member := false
|
||||
var was users.Membership
|
||||
if u != nil {
|
||||
_, merr := users.MembershipOf(s.platform(), s.team(), u.ID)
|
||||
member = merr == nil
|
||||
m, merr := users.MembershipOf(s.platform(), s.team(), u.ID)
|
||||
member, was = merr == nil, m
|
||||
}
|
||||
gRole, gMods, inGroups := groupAccess(cfg.prefix(), id.Groups)
|
||||
inGroups = inGroups && cfg.Groups
|
||||
if !member && inGroups {
|
||||
// In one of Supermarket's groups: invited by the Core.
|
||||
if !id.Verified || id.Email == "" {
|
||||
refuse("The Core didn't vouch for an email address, so Supermarket can't tell who you are here.")
|
||||
return
|
||||
}
|
||||
if problem, _, _ := s.seatRoom(); problem != "" {
|
||||
refuse("The team has no seat free for you. Ask its owner.")
|
||||
return
|
||||
}
|
||||
if u == nil {
|
||||
u = &users.User{Email: id.Email, Name: id.Name, Status: users.Active}
|
||||
if err := users.Create(s.platform(), u, now); err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if err := users.Join(s.platform(), s.team(), u.ID, gRole, gMods, now); err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
s.record(audit.Entry{UserID: u.ID, UserName: displayName(u), Action: "joined", Target: u.Email,
|
||||
Detail: "with the Core, from its groups: " + accessText(users.Membership{Role: gRole, Modules: gMods}), IP: clientIP(r)})
|
||||
member, was = true, users.Membership{Role: gRole, Modules: gMods}
|
||||
} else if member && inGroups && was.Role != users.Owner && !sameAccess(was, gRole, gMods) {
|
||||
// The Core's groups changed: so does their access.
|
||||
if err := users.Join(s.platform(), s.team(), u.ID, gRole, gMods, now); err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
s.record(audit.Entry{UserID: u.ID, UserName: displayName(u), Action: "access-changed", Target: u.Email,
|
||||
Detail: accessText(users.Membership{Role: gRole, Modules: gMods}) + ", from the Core's groups", IP: clientIP(r)})
|
||||
}
|
||||
if !member {
|
||||
// Anyone at the Core's domain may join, if the team says so.
|
||||
@@ -504,3 +595,22 @@ func (s *Server) coreSignOut(ctx context.Context) string {
|
||||
}
|
||||
return oidc.SignOutURL(p, "", s.PublicURL+"/signin?msg=signed-out")
|
||||
}
|
||||
|
||||
// sameAccess says whether a membership already has a role and modules.
|
||||
func sameAccess(m users.Membership, role users.Role, mods map[string]users.Access) bool {
|
||||
if m.Role != role {
|
||||
return false
|
||||
}
|
||||
if role != users.Member {
|
||||
return true
|
||||
}
|
||||
if len(m.Modules) != len(mods) {
|
||||
return false
|
||||
}
|
||||
for k, v := range mods {
|
||||
if m.Modules[k] != v {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user