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
|
||||
}
|
||||
|
||||
@@ -237,3 +237,78 @@ func TestCoreSigninMultiTenant(t *testing.T) {
|
||||
t.Errorf("session %+v", sess)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGroupAccess(t *testing.T) {
|
||||
role, mods, any := groupAccess("supermarket", []string{"staff", "supermarket-content", "Supermarket-SEO-view", "supermarket-seo", "supermarket-metrics-view", "supermarket-nope"})
|
||||
if !any || role != users.Member || mods["content"] != users.Edit || mods["seo"] != users.Edit || mods["metrics"] != users.View || len(mods) != 3 {
|
||||
t.Errorf("%v %v %v", role, mods, any)
|
||||
}
|
||||
if role, mods, _ := groupAccess("supermarket", []string{"supermarket-admins", "supermarket-content"}); role != users.Admin || len(mods) != 0 {
|
||||
t.Errorf("admin: %v %v", role, mods)
|
||||
}
|
||||
if _, _, any := groupAccess("supermarket", []string{"admins", "nextcloud-users", "supermarket-nope"}); any {
|
||||
t.Error("someone else's groups counted")
|
||||
}
|
||||
if _, mods, _ := groupAccess("mkt", []string{"mkt-sites"}); mods["sites"] != users.Edit {
|
||||
t.Errorf("prefix: %v", mods)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRolesFromCoreGroups(t *testing.T) {
|
||||
s, h := newServerEdition(t)
|
||||
owner := addUser(t, s, "owner@example.com", users.Owner, nil)
|
||||
addUser(t, s, "sam@example.com", users.Member, map[string]users.Access{"seo": users.View})
|
||||
oc := sessionFor(t, s, owner)
|
||||
f := newFakeCore(t)
|
||||
setUpCore(t, s, h, oc, f, url.Values{"groups": {"on"}})
|
||||
access := func(email string) users.Membership {
|
||||
t.Helper()
|
||||
u, err := users.ByEmail(s.DB, email)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m, _ := users.MembershipOf(s.DB, users.Team, u.ID)
|
||||
return m
|
||||
}
|
||||
signIn := func(sub, email string, groups ...string) int {
|
||||
f.who = map[string]any{"sub": sub, "email": email, "email_verified": true, "name": sub, "groups": groups}
|
||||
return f.signInWithCore(t, h, url.Values{}).Code
|
||||
}
|
||||
|
||||
// In a group: joins, with its access, though never invited.
|
||||
if code := signIn("lee", "lee@anywhere.example", "staff", "supermarket-content"); code != http.StatusSeeOther {
|
||||
t.Fatalf("lee: %d", code)
|
||||
}
|
||||
if m := access("lee@anywhere.example"); m.Role != users.Member || m.Modules["content"] != users.Edit || len(m.Modules) != 1 {
|
||||
t.Errorf("lee %+v", m)
|
||||
}
|
||||
// Not in one, not invited: refused.
|
||||
if code := signIn("out", "out@anywhere.example", "staff"); code != http.StatusForbidden {
|
||||
t.Errorf("out: %d", code)
|
||||
}
|
||||
// Sam's groups make an admin; later, back to reading SEO and editing Sites.
|
||||
if signIn("sam", "sam@example.com", "supermarket-admins"); access("sam@example.com").Role != users.Admin {
|
||||
t.Error("sam isn't an admin")
|
||||
}
|
||||
signIn("sam", "sam@example.com", "supermarket-seo-view", "supermarket-sites")
|
||||
if m := access("sam@example.com"); m.Role != users.Member || m.Modules["seo"] != users.View || m.Modules["sites"] != users.Edit {
|
||||
t.Errorf("sam %+v", m)
|
||||
}
|
||||
// In none of them: as set here.
|
||||
signIn("sam", "sam@example.com", "staff")
|
||||
if m := access("sam@example.com"); m.Modules["sites"] != users.Edit {
|
||||
t.Errorf("sam lost access: %+v", m)
|
||||
}
|
||||
// The owner's role is theirs.
|
||||
signIn("own", "owner@example.com", "supermarket-content-view")
|
||||
if m := access("owner@example.com"); m.Role != users.Owner {
|
||||
t.Errorf("owner %+v", m)
|
||||
}
|
||||
// Turned off, the groups change nothing.
|
||||
cfg := s.coreSignin()
|
||||
cfg.Groups = false
|
||||
s.DB.PutJSON(coreSigninKey, cfg)
|
||||
if code := signIn("pat", "pat@anywhere.example", "supermarket-content"); code != http.StatusForbidden {
|
||||
t.Errorf("pat joined with groups off: %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,9 @@
|
||||
<label class="check"><input type="checkbox" name="auto_join" {{if $.Data.C.AutoJoin}}checked{{end}}> <span>Anyone with an address at <strong>{{$.Data.Domain}}</strong> may join, as a member with these modules:</span></label>
|
||||
{{template "access-fields" (dict "Role" "member" "Modules" $.Data.C.AutoModules "Grants" $.Data.Grants)}}
|
||||
<p class="help">Otherwise only people invited may sign in: matched by their email the first time, then by their account at the Core.</p>
|
||||
<label class="check"><input type="checkbox" name="groups" {{if $.Data.C.Groups}}checked{{end}}> <span>Roles follow the Core's groups</span></label>
|
||||
<label class="small">Groups' prefix <input name="group_prefix" value="{{$.Data.C.GroupPrefix}}" placeholder="supermarket" size="16"></label>
|
||||
<p class="help">In Authelia's users, <code>{{$.Data.Prefix}}-admins</code> makes an admin; <code>{{$.Data.Prefix}}-<em>module</em></code> lets someone edit a module, and <code>{{$.Data.Prefix}}-<em>module</em>-view</code> read it ({{range $i, $g := $.Data.Grants}}{{if $i}}, {{end}}<code>{{$g.ID}}</code>{{end}}). Each time someone signs in with the Core, their role and modules are set from their groups, and someone not yet in the team who is in one joins (if there's a seat free). The owner is never changed; someone in none of these groups keeps the access set here.</p>
|
||||
<button type="submit">Save</button>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
Reference in New Issue
Block a user