feat(web): RBAC, tenant scope, and WS room hooks
Add configurable role⊥permission (five tables, gate permissions, register, /_rbac desk), multi-tenant path/header resolution with JSON tenant_scope, and route_ws on_message/presence/dynamic room_key — with author surfaces, demos, and smokes. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
27991a7822
commit
5cf84fe9b7
27 files changed
+2372
-80
No files matched your search
@@ -612,6 +612,20 @@ func RoleFromCookie(cookieHeader string) string {
|
||||
return "visitor"
|
||||
}
|
||||
|
||||
// UsernameFromCookie returns the session username or empty string.
|
||||
func UsernameFromCookie(cookieHeader string) string {
|
||||
sid, ok := IDFromCookie(cookieHeader)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
if v, ok := GetValue(sid, "username"); ok {
|
||||
if s, ok := v.(string); ok {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// RoleAllowed reports whether role is in allowed (empty allowed = all).
|
||||
func RoleAllowed(role string, allowed []string) bool {
|
||||
if len(allowed) == 0 {
|
||||
@@ -625,3 +639,51 @@ func RoleAllowed(role string, allowed []string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// PermissionsFromCookie returns session permission codes (empty if anonymous).
|
||||
func PermissionsFromCookie(cookieHeader string) []string {
|
||||
sid, ok := IDFromCookie(cookieHeader)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return PermissionsFromSession(sid)
|
||||
}
|
||||
|
||||
// PermissionsFromSession reads the permissions CSV from a session id.
|
||||
func PermissionsFromSession(sid string) []string {
|
||||
if v, ok := GetValue(sid, "permissions"); ok {
|
||||
if s, ok := v.(string); ok && s != "" {
|
||||
return ParseRolesCSV(s)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetPermissions stores permission codes on the session.
|
||||
func SetPermissions(sid string, codes []string) {
|
||||
SetValue(sid, "permissions", strings.Join(codes, ","))
|
||||
}
|
||||
|
||||
// PermissionAllowed reports whether the session holds any of needed.
|
||||
func PermissionAllowed(held, needed []string) bool {
|
||||
if len(needed) == 0 {
|
||||
return true
|
||||
}
|
||||
set := map[string]struct{}{}
|
||||
for _, h := range held {
|
||||
h = strings.ToLower(strings.TrimSpace(h))
|
||||
if h == "*" {
|
||||
return true
|
||||
}
|
||||
if h != "" {
|
||||
set[h] = struct{}{}
|
||||
}
|
||||
}
|
||||
for _, n := range needed {
|
||||
n = strings.ToLower(strings.TrimSpace(n))
|
||||
if _, ok := set[n]; ok {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in new issue
Block a user