Phase 10, part 5: access enforced on every page, form and fragment
One table maps every address to an area (a module, Settings, anything signed in, or public); each route is checked against it, and the app won't start with a route missing from it. Changing needs edit on the module. For members who may only view, forms that would change something are shown disabled and htmx buttons hidden. The menu, the Overview, Search and the SEO emails follow each person's modules; Settings is for admins. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+28
-4
@@ -125,7 +125,7 @@ func (s *Server) Handler() (http.Handler, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux := &routes{s: s, mux: http.NewServeMux()}
|
||||
static, _ := fs.Sub(staticFS, "static")
|
||||
mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServerFS(static)))
|
||||
// Browsers ask for /favicon.ico whatever the page says.
|
||||
@@ -186,6 +186,10 @@ func (s *Server) Handler() (http.Handler, error) {
|
||||
|
||||
// Settings: the profile, social accounts, AI, and the Core & Hermes.
|
||||
mux.HandleFunc("GET /settings", func(w http.ResponseWriter, r *http.Request) {
|
||||
if !viewerOf(r).Member.Admin() {
|
||||
http.Redirect(w, r, "/settings/account", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/settings/profile", http.StatusSeeOther)
|
||||
})
|
||||
mux.HandleFunc("GET /settings/profile", s.profilePage)
|
||||
@@ -345,7 +349,10 @@ func (s *Server) Handler() (http.Handler, error) {
|
||||
})
|
||||
}
|
||||
|
||||
return s.guard(s.csrf(s.sessions(mux))), nil
|
||||
if err := mux.err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.guard(s.csrf(s.sessions(mux.mux))), nil
|
||||
}
|
||||
|
||||
// guard protects the app, which has no login because only this machine can
|
||||
@@ -582,8 +589,25 @@ var modules = []module{
|
||||
{"metrics", "Metrics", "Phase 4", "MRR, NRR, CAC and CLV from Stripe, with pipeline influence from EspoCRM.", "/metrics"},
|
||||
}
|
||||
|
||||
// Modules is for the menu.
|
||||
func (p page) Modules() []module { return modules }
|
||||
// Modules is for the menu: the viewer's.
|
||||
func (p page) Modules() []module { return visibleModules(p.Viewer) }
|
||||
|
||||
// visibleModules are the modules a viewer may see.
|
||||
func visibleModules(v *viewer) []module {
|
||||
if v == nil {
|
||||
return modules
|
||||
}
|
||||
out := make([]module, 0, len(modules))
|
||||
for _, m := range modules {
|
||||
if v.can(m.ID, false) {
|
||||
out = append(out, m)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Can is for templates: whether the viewer may see an area.
|
||||
func (p page) Can(area string) bool { return p.Viewer == nil || p.Viewer.can(area, false) }
|
||||
|
||||
// render writes a full page, or only its "content" block for htmx requests
|
||||
// that ask for a fragment.
|
||||
|
||||
Reference in New Issue
Block a user