Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
148 lines
5.4 KiB
Go
148 lines
5.4 KiB
Go
package server
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func (s *Server) requestPrincipal(r *http.Request) *store.Principal {
|
|
domainUUID := s.domainUUIDForRequest(r)
|
|
principal, err := s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return principal
|
|
}
|
|
|
|
func (s *Server) canManageGroup(domainUUID string, principal *store.Principal, group *store.Group, ownerOnly bool) bool {
|
|
if principal == nil {
|
|
return false
|
|
}
|
|
if s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principal.UUID, Capability: "manage", TargetType: "domain", TargetID: domainUUID}) == nil {
|
|
return true
|
|
}
|
|
if s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin") {
|
|
return true
|
|
}
|
|
role := s.store.GroupMemberRole(group.UUID, principal.UUID)
|
|
return role == "owner" || (!ownerOnly && role == "manager")
|
|
}
|
|
|
|
func (s *Server) handleGroups(w http.ResponseWriter, r *http.Request) {
|
|
domainUUID := s.domainUUIDForRequest(r)
|
|
principal := s.requestPrincipal(r)
|
|
remainder := ""
|
|
if r.URL.Path != "/g" {
|
|
remainder = strings.Trim(strings.TrimPrefix(r.URL.Path, "/g/"), "/")
|
|
}
|
|
if remainder == "" {
|
|
if r.Method == http.MethodPost {
|
|
if principal == nil || !s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin") {
|
|
http.Error(w, "forbidden", http.StatusForbidden)
|
|
return
|
|
}
|
|
group, err := s.store.CreateGroup(domainUUID, principal.UUID, r.FormValue("name"), r.FormValue("display_name"), r.FormValue("description"), r.FormValue("visibility"))
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
http.Redirect(w, r, "/g/"+group.Name+"/_/edit", http.StatusSeeOther)
|
|
return
|
|
}
|
|
groups, err := s.store.ListGroups(domainUUID, func() string {
|
|
if principal != nil {
|
|
return principal.UUID
|
|
}
|
|
return ""
|
|
}())
|
|
if err != nil {
|
|
http.Error(w, err.Error(), 500)
|
|
return
|
|
}
|
|
canCreate := principal != nil && s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin")
|
|
s.renderWithContext(w, r, "groups.gohtml", map[string]any{"Groups": groups, "CanCreate": canCreate, "Locale": s.getLocale(domainUUID)})
|
|
return
|
|
}
|
|
parts := strings.Split(remainder, "/")
|
|
group, err := s.store.GroupByName(domainUUID, parts[0])
|
|
if err != nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
memberRole := ""
|
|
if principal != nil {
|
|
memberRole = s.store.GroupMemberRole(group.UUID, principal.UUID)
|
|
}
|
|
visible := group.Visibility == "public" || (group.Visibility == "domain" && principal != nil) || memberRole != "" || s.canManageGroup(domainUUID, principal, group, false)
|
|
if !visible {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if len(parts) >= 3 && parts[1] == "_" && parts[2] == "edit" {
|
|
if !s.canManageGroup(domainUUID, principal, group, false) {
|
|
http.Error(w, "forbidden", http.StatusForbidden)
|
|
return
|
|
}
|
|
if r.Method == http.MethodPost {
|
|
action := r.FormValue("action")
|
|
ownerAuthority := s.canManageGroup(domainUUID, principal, group, true)
|
|
switch action {
|
|
case "add_member":
|
|
if r.FormValue("member_role") == "owner" && !ownerAuthority {
|
|
err = store.ErrAccessDenied
|
|
break
|
|
}
|
|
err = s.store.AddGroupMemberByUsername(domainUUID, group.UUID, r.FormValue("username"), r.FormValue("member_role"))
|
|
case "remove_member":
|
|
if role := s.store.GroupMemberRoleByMembership(group.UUID, r.FormValue("membership_uuid")); role != "member" && !ownerAuthority {
|
|
err = store.ErrAccessDenied
|
|
break
|
|
}
|
|
err = s.store.RemoveGroupMemberWithAuthority(group.UUID, r.FormValue("membership_uuid"), ownerAuthority)
|
|
case "set_permission":
|
|
if !ownerAuthority {
|
|
err = store.ErrAccessDenied
|
|
break
|
|
}
|
|
err = s.store.SetGroupPermission(group.UUID, r.FormValue("scope_path"), r.FormValue("permission"))
|
|
case "set_campus_inheritance":
|
|
domain := s.domainForRequest(r)
|
|
if group.BuiltinKey != "campus_admin" || domain == nil || !s.store.IsCampusAdmin(domain.CampusUUID, principal.UUID) {
|
|
err = store.ErrAccessDenied
|
|
break
|
|
}
|
|
err = s.store.SetCampusAdminInheritance(group.UUID, r.FormValue("inherit_members") == "on")
|
|
default:
|
|
err = errors.New("invalid group operation")
|
|
}
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
http.Redirect(w, r, r.URL.Path+"?saved=1", http.StatusSeeOther)
|
|
return
|
|
}
|
|
members, _ := s.store.ListGroupMembers(group.UUID)
|
|
permissions, _ := s.store.ListGroupPermissions(group.UUID)
|
|
s.renderWithContext(w, r, "group.gohtml", map[string]any{"Group": group, "Editing": true, "Members": members, "Permissions": permissions, "CanOwn": s.canManageGroup(domainUUID, principal, group, true), "Saved": r.URL.Query().Get("saved") == "1", "Locale": s.getLocale(domainUUID)})
|
|
return
|
|
}
|
|
if len(parts) == 1 {
|
|
s.renderWithContext(w, r, "group.gohtml", map[string]any{"Group": group, "MemberRole": memberRole, "CanManage": s.canManageGroup(domainUUID, principal, group, false), "Locale": s.getLocale(domainUUID)})
|
|
return
|
|
}
|
|
if principal == nil || s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principal.UUID, Capability: "read", TargetType: "path", TargetID: r.URL.Path}) != nil {
|
|
mode, _ := s.store.GetConfigInDomain(domainUUID, "authorization_mode")
|
|
if mode != "enforcing" {
|
|
s.handleSemanticRoute(w, r)
|
|
return
|
|
}
|
|
http.Error(w, "forbidden", http.StatusForbidden)
|
|
return
|
|
}
|
|
s.handleSemanticRoute(w, r)
|
|
}
|