Files
OwnCord/Server/permissions/checker.go
T
jevb 39658e919b refactor: extensibility overhaul — handler registry, permission checker, sidebar decomposition, DX improvements
Server:
- Unified permission checker (permissions/checker.go) replaces 3 duplicated implementations
- WS handler registry pattern (ws/registry.go) replaces monolithic switch (747→184 lines)
- Split handlers into domain files: handlers_chat.go, handlers_presence.go, handlers_reaction.go
- Shared message type constants (ws/message_types.go) — no more string literals
- Admin API split into helpers.go, types.go, middleware.go (api.go now 61 lines)
- Dev seed script (scripts/seed.go) with -confirm-dev safety flag
- Air hot reload config (.air.toml)
- Fix: DM attachment permission now uses participant check, not role check
- Fix: Typing broadcast now checks ReadMessages permission for non-DM channels

Client:
- Extract preferences to @lib/preferences.ts (fixes lib→component dependency)
- Extract roles to dedicated roles.store.ts (was mixed into channels store)
- Decompose SidebarArea (921→598 lines) into 4 sub-components
- Shared modal factory (lib/modalFactory.ts) with tests
- Global showToast() helper (lib/toast.ts) — 18 call sites migrated
- Protocol type constants (lib/protocolTypes.ts) synced with server
- Remove 38 unnecessary type casts across 17 files
- Component test harness (tests/helpers/test-harness.ts) with 8 tests
- Fix: DM section "View All" respects collapsed state
- Fix: Modal onClose fires on external signal abort
- Fix: savePref wrapped in try/catch for quota exceeded
- Fix: loadPref null guard added

Triple-reviewed: Claude code-review agent + OpenAI Codex CLI + GitHub Copilot
2026-03-29 12:19:08 +02:00

96 lines
3.6 KiB
Go

package permissions
import (
"errors"
"fmt"
)
// ─── Errors ─────────────────────────────────────────────────────────────────
// ErrNotDMParticipant is returned when a user is not a participant in a DM channel.
var ErrNotDMParticipant = errors.New("not a participant in this DM")
// ErrPermissionDenied is returned when a user lacks the required permission.
var ErrPermissionDenied = errors.New("permission denied")
// ─── DB interface ───────────────────────────────────────────────────────────
// ChannelOverride holds the allow/deny permission bits for a single channel.
type ChannelOverride struct {
Allow int64
Deny int64
}
// DB is the minimal database interface the Checker needs.
// Defined at the consumer (per Go convention: accept interfaces, return structs).
type DB interface {
GetChannelPermissions(channelID, roleID int64) (allow, deny int64, err error)
IsDMParticipant(userID, channelID int64) (bool, error)
}
// ─── Checker ────────────────────────────────────────────────────────────────
// Checker consolidates all channel permission checks into one reusable type.
// It is safe to share across goroutines because it holds no mutable state.
type Checker struct {
db DB
}
// NewChecker creates a Checker backed by the given database interface.
func NewChecker(db DB) *Checker {
return &Checker{db: db}
}
// HasChannelPerm reports whether the role (identified by rolePerms and roleID)
// has all the given permission bits on the specified channel. Administrator
// roles bypass all checks. Channel overrides (allow/deny) are fetched from the
// database per call.
func (ck *Checker) HasChannelPerm(rolePerms int64, roleID, channelID, perm int64) bool {
if HasAdmin(rolePerms) {
return true
}
allow, deny, err := ck.db.GetChannelPermissions(channelID, roleID)
if err != nil {
return false
}
effective := EffectivePerms(rolePerms, allow, deny)
return effective&perm == perm
}
// HasChannelPermBatch reports whether the role has the given permission on the
// channel using a pre-fetched overrides map. This avoids N+1 queries when
// filtering many channels in bulk. The zero-value ChannelOverride (no entry in
// map) is correct -- it means no override exists.
func (ck *Checker) HasChannelPermBatch(rolePerms int64, overrides map[int64]ChannelOverride, channelID, perm int64) bool {
if HasAdmin(rolePerms) {
return true
}
o := overrides[channelID] // zero-value (0, 0) when no override exists
effective := EffectivePerms(rolePerms, o.Allow, o.Deny)
return effective&perm == perm
}
// RequireChannelAccess checks whether the user can access the channel with the
// given permission. For DM channels (channelType == "dm"), it verifies
// participant membership via IsDMParticipant. For regular channels, it checks
// role-based permissions via HasChannelPerm.
//
// Returns nil on success, or a descriptive error on failure.
func (ck *Checker) RequireChannelAccess(userID, rolePerms, roleID int64, channelType string, channelID, perm int64) error {
if channelType == "dm" {
ok, err := ck.db.IsDMParticipant(userID, channelID)
if err != nil {
return fmt.Errorf("checking DM participation: %w", err)
}
if !ok {
return ErrNotDMParticipant
}
return nil
}
if !ck.HasChannelPerm(rolePerms, roleID, channelID, perm) {
return ErrPermissionDenied
}
return nil
}