Files
gochat/backend/internal/auth/permission.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

191 lines
5.3 KiB
Go

package auth
// Reference: P2E §2 — RBAC permission system
// Replaces Chatwoot's Pundit Policy classes with a unified PolicyContext + permission matrix.
// Permission represents a single permission in the system.
type Permission string
// All permissions are organized by resource:action pattern.
// Reference: Chatwoot's Pundit policies → GoChat flat permission constants
const (
// Account permissions
PermAccountCreate Permission = "account:create"
PermAccountRead Permission = "account:read"
PermAccountUpdate Permission = "account:update"
PermAccountDelete Permission = "account:delete"
PermAccountManageUsers Permission = "account:manage_users"
// Inbox permissions
PermInboxCreate Permission = "inbox:create"
PermInboxRead Permission = "inbox:read"
PermInboxUpdate Permission = "inbox:update"
PermInboxDelete Permission = "inbox:delete"
// Conversation permissions
PermConversationRead Permission = "conversation:read"
PermConversationCreate Permission = "conversation:create"
PermConversationUpdate Permission = "conversation:update"
PermConversationAssign Permission = "conversation:assign"
PermConversationResolve Permission = "conversation:resolve"
PermConversationDelete Permission = "conversation:delete"
PermConversationManageLabels Permission = "conversation:manage_labels"
// Message permissions
PermMessageRead Permission = "message:read"
PermMessageCreate Permission = "message:create"
PermMessageUpdate Permission = "message:update"
PermMessageDelete Permission = "message:delete"
// Contact permissions
PermContactCreate Permission = "contact:create"
PermContactRead Permission = "contact:read"
PermContactUpdate Permission = "contact:update"
PermContactDelete Permission = "contact:delete"
PermContactMerge Permission = "contact:merge"
PermContactExport Permission = "contact:export"
// Report permissions
PermReportRead Permission = "report:read"
PermReportExport Permission = "report:export"
// Automation permissions
PermAutomationCreate Permission = "automation:create"
PermAutomationRead Permission = "automation:read"
PermAutomationUpdate Permission = "automation:update"
PermAutomationDelete Permission = "automation:delete"
// Team permissions
PermTeamCreate Permission = "team:create"
PermTeamRead Permission = "team:read"
PermTeamUpdate Permission = "team:update"
PermTeamDelete Permission = "team:delete"
// Captain AI permissions (Enterprise 🔒)
PermCaptainRead Permission = "captain:read"
PermCaptainManage Permission = "captain:manage"
PermCopilotUse Permission = "copilot:use"
// Super admin permissions
PermSuperAdminAll Permission = "super_admin:all"
PermPlatformManage Permission = "platform:manage"
)
// Role defines user roles in the system.
// Reference: Chatwoot's AccountUser roles (agent, administrator) + super_admin
type Role string
const (
RoleSuperAdmin Role = "super_admin"
RoleAdministrator Role = "administrator"
RoleAgent Role = "agent"
RoleCustom Role = "custom" // Enterprise 🔒
)
// PermissionMatrix defines which roles have which permissions.
// Reference: P2E §2.2 — replaces Chatwoot's 12 Pundit Policy classes
var PermissionMatrix = map[Role][]Permission{
RoleSuperAdmin: {
PermSuperAdminAll,
PermPlatformManage,
// All other permissions implicitly granted
},
RoleAdministrator: {
PermAccountRead,
PermAccountUpdate,
PermAccountManageUsers,
PermInboxCreate,
PermInboxRead,
PermInboxUpdate,
PermInboxDelete,
PermConversationRead,
PermConversationCreate,
PermConversationUpdate,
PermConversationAssign,
PermConversationResolve,
PermConversationManageLabels,
PermMessageRead,
PermMessageCreate,
PermMessageUpdate,
PermMessageDelete,
PermContactCreate,
PermContactRead,
PermContactUpdate,
PermContactDelete,
PermContactMerge,
PermContactExport,
PermReportRead,
PermReportExport,
PermAutomationCreate,
PermAutomationRead,
PermAutomationUpdate,
PermAutomationDelete,
PermTeamCreate,
PermTeamRead,
PermTeamUpdate,
PermTeamDelete,
PermCaptainRead,
PermCaptainManage,
PermCopilotUse,
},
RoleAgent: {
PermAccountRead,
PermInboxRead,
PermConversationRead,
PermConversationCreate,
PermConversationUpdate,
PermConversationAssign,
PermConversationResolve,
PermConversationManageLabels,
PermMessageRead,
PermMessageCreate,
PermMessageUpdate,
PermContactCreate,
PermContactRead,
PermContactUpdate,
PermContactMerge,
PermReportRead,
PermAutomationRead,
PermTeamRead,
PermCaptainRead,
PermCopilotUse,
},
}
// HasPermission checks if a role has a specific permission.
func HasPermission(role Role, perm Permission) bool {
// Super admin has all permissions
if role == RoleSuperAdmin {
return true
}
perms, ok := PermissionMatrix[role]
if !ok {
return false
}
for _, p := range perms {
if p == perm {
return true
}
}
return false
}
// GetPermissions returns all permissions for a role.
func GetPermissions(role Role) []Permission {
if role == RoleSuperAdmin {
// Return all permissions for super admin
all := []Permission{}
for _, perms := range PermissionMatrix {
all = append(all, perms...)
}
return all
}
perms, ok := PermissionMatrix[role]
if !ok {
return []Permission{}
}
return perms
}