Files
vitfit-r2/V2/planning/ROLES_AND_PERMISSIONS.md
T
2026-06-10 22:26:02 -04:00

245 lines
8.9 KiB
Markdown

# VitFit V2 — Roles & Permissions
## Role Hierarchy
```
SystemAdmin ← Platform operator (you). Cross-org visibility. Not a gym user.
│
Organization
├── Owner ← Gym owner/manager. Manages coaches and clients. Sees everything in their org.
├── Coach ← Trainer. Manages workouts and their own clients. Cannot manage other coaches.
└── Client ← Athlete/member. Views own workouts and schedule only.
```
Roles are **scoped to an organization**. A user belongs to exactly one organization. SystemAdmin is the only cross-organization role.
---
## Role Definitions
### SystemAdmin
- Platform-level administrator (the person who sells and deploys VitFit)
- There is exactly one SystemAdmin per deployment (could be multiple accounts, all flagged as SystemAdmin)
- Can access any organization's data for support purposes
- Manages the Organization record itself (creates, suspends, configures feature flags)
- Does NOT manage day-to-day gym operations — that is the Owner's job
- Has a dedicated `/sysadmin` panel separate from the normal app UI
- Actions are always audit-logged
### Owner
- The gym owner or head administrator who purchased the license
- Can do everything a Coach can do, plus:
- Manage all user accounts in their organization (create, deactivate, change roles)
- View all coaches' clients and workouts
- Configure organization branding
- View audit logs for their organization
- Set up billing/license info (if applicable)
- Grant Coach accounts
- Manage the client roster
- Cannot elevate their own account to SystemAdmin
### Coach
- A trainer or staff member
- Can manage workouts, timers, CrossFit workouts, the video library
- Can be assigned clients (by Owner or by self-assignment if the org allows it)
- Can view and manage their **own** clients' profiles and programming
- Cannot see other coaches' clients (unless they are also an Owner)
- Cannot manage other user accounts
- Cannot view audit logs
- Cannot change organization branding
### Client
- An athlete or gym member
- Read-only access to their own data
- Can view workouts assigned to them by their coach
- Can view their own calendar / upcoming sessions
- Cannot upload videos, create workouts, or manage any content
- Cannot see other clients' data
- Cannot see coach-only content (full video library, timer configs, etc.)
- Can update their own profile (name, password, preferences)
- Does NOT have access to the group display features (TV display, timers) — those are coach-facing
---
## Permission Matrix
Legend: ✅ Full access | 🔶 Own/assigned only | ❌ No access | 👑 SystemAdmin only
### Organization Management
| Action | SystemAdmin | Owner | Coach | Client |
|--------|------------|-------|-------|--------|
| Create organization | 👑 | ❌ | ❌ | ❌ |
| Edit organization name/settings | 👑 | ✅ | ❌ | ❌ |
| View organization details | 👑 | ✅ | 🔶 (name/logo only) | 🔶 (name/logo only) |
| Enable/disable feature flags | 👑 | ❌ | ❌ | ❌ |
| Suspend/deactivate organization | 👑 | ❌ | ❌ | ❌ |
### User Management
| Action | SystemAdmin | Owner | Coach | Client |
|--------|------------|-------|-------|--------|
| Create Coach account | 👑 | ✅ | ❌ | ❌ |
| Create Client account | 👑 | ✅ | ✅ (if org allows) | ❌ |
| View all users in org | 👑 | ✅ | ❌ | ❌ |
| View own profile | ✅ | ✅ | ✅ | ✅ |
| Edit own profile / password | ✅ | ✅ | ✅ | ✅ |
| Change any user's role | 👑 | ✅ | ❌ | ❌ |
| Reset any user's password | 👑 | ✅ | ❌ | ❌ |
| Deactivate a user | 👑 | ✅ | ❌ | ❌ |
| Assign coach to client | 👑 | ✅ | 🔶 (self-assign if allowed) | ❌ |
| View assigned clients | — | ✅ (all) | 🔶 (own clients) | ❌ |
| Revoke device tokens | 👑 | ✅ | 🔶 (own devices) | 🔶 (own devices) |
### Branding
| Action | SystemAdmin | Owner | Coach | Client |
|--------|------------|-------|-------|--------|
| View branding settings | 👑 | ✅ | ❌ | ❌ |
| Edit logo, colors, name | 👑 | ✅ | ❌ | ❌ |
### Video Library
| Action | SystemAdmin | Owner | Coach | Client |
|--------|------------|-------|-------|--------|
| Upload video | 👑 | ✅ | ✅ | ❌ |
| View all videos | 👑 | ✅ | ✅ | ❌ |
| View videos assigned to them | — | — | — | ✅ (via workouts) |
| Delete video | 👑 | ✅ | ✅ | ❌ |
| Manage categories | 👑 | ✅ | ✅ | ❌ |
### Workouts (Group / TV Display)
| Action | SystemAdmin | Owner | Coach | Client |
|--------|------------|-------|-------|--------|
| Create group workout | 👑 | ✅ | ✅ | ❌ |
| Edit group workout | 👑 | ✅ | ✅ | ❌ |
| Delete group workout | 👑 | ✅ | ✅ | ❌ |
| Launch workout to TV display | 👑 | ✅ | ✅ | ❌ |
| View group workouts | 👑 | ✅ | ✅ | ❌ |
### Client Workouts (1-on-1)
| Action | SystemAdmin | Owner | Coach | Client |
|--------|------------|-------|-------|--------|
| Create workout for a client | 👑 | ✅ | 🔶 (own clients) | ❌ |
| Edit client workout | 👑 | ✅ | 🔶 (own clients) | ❌ |
| Delete client workout | 👑 | ✅ | 🔶 (own clients) | ❌ |
| View own assigned workouts | — | — | — | ✅ |
| View all client workouts in org | 👑 | ✅ | ❌ | ❌ |
### Timers
| Action | SystemAdmin | Owner | Coach | Client |
|--------|------------|-------|-------|--------|
| Create / edit / delete timer | 👑 | ✅ | ✅ | ❌ |
| Launch timer to TV display | 👑 | ✅ | ✅ | ❌ |
| View timer list | 👑 | ✅ | ✅ | ❌ |
### CrossFit Workouts
| Action | SystemAdmin | Owner | Coach | Client |
|--------|------------|-------|-------|--------|
| Create CrossFit workout | 👑 | ✅ | ✅ | ❌ |
| Delete CrossFit workout | 👑 | ✅ | ❌ | ❌ |
| View CrossFit workouts | 👑 | ✅ | ✅ | ❌ |
### Calendar / Scheduling
| Action | SystemAdmin | Owner | Coach | Client |
|--------|------------|-------|-------|--------|
| Create session/appointment | 👑 | ✅ | 🔶 (own calendar) | ❌ |
| Edit session | 👑 | ✅ | 🔶 (own sessions) | ❌ |
| Cancel session | 👑 | ✅ | 🔶 (own sessions) | 🔶 (request cancellation) |
| View own calendar | — | ✅ | ✅ | ✅ |
| View all coaches' calendars | 👑 | ✅ | ❌ | ❌ |
| View assigned coach's calendar | — | — | — | 🔶 (coach's availability only) |
### Audit Logs
| Action | SystemAdmin | Owner | Coach | Client |
|--------|------------|-------|-------|--------|
| View org audit log | 👑 | ✅ | ❌ | ❌ |
| View cross-org audit log | 👑 | ❌ | ❌ | ❌ |
| Export audit log | 👑 | ✅ | ❌ | ❌ |
### Storage
| Action | SystemAdmin | Owner | Coach | Client |
|--------|------------|-------|-------|--------|
| View storage usage | 👑 | ✅ | ❌ | ❌ |
---
## UI Navigation by Role
### SystemAdmin UI
- `/sysadmin` — separate section
- Organizations list + detail
- Cross-org user lookup
- Feature flag management
- Full audit log (all orgs)
- Server health / storage
### Owner UI
Full app + additional Owner tabs in settings:
- User management (create coaches + clients, manage roles)
- Branding settings
- Audit log (own org)
- Client roster
- All coaches' calendars (read)
- Storage monitoring
### Coach UI
Standard app (matches V1 functionality):
- Group workouts, timers, CrossFit, video library
- My Clients section: list of assigned clients, their programming, their calendar
- Own calendar management
### Client UI
Minimal, client-only:
- My Workouts — workouts assigned by coach
- My Schedule — upcoming sessions
- My Profile — update name/password
- No nav items for group workouts, video library, or admin functions
---
## Role Enforcement
### Backend
Every route that touches org-specific data must:
1. Verify JWT (authenticateToken)
2. Verify role is sufficient (authenticateRole(['Owner', 'Coach']))
3. Verify the resource being accessed belongs to `req.user.organizationId`
Rule 3 is the critical one — it prevents a Coach at Gym A from accessing Gym B's data even if they somehow get a valid token.
Pattern:
```js
// Every data-access query includes the org filter
const workout = await Workout.findOne({
where: {
id: req.params.id,
organizationId: req.user.organizationId // ALWAYS required
}
});
if (!workout) return res.sendStatus(404); // 404 not 403 — don't reveal existence
```
### Frontend
Frontend hides UI elements based on role (stored in the decoded JWT payload). But hiding is cosmetic — all real enforcement is server-side. Frontend role checks are only for UX, never for security.
---
## Mapping V1 Roles → V2 Roles
| V1 Role | V2 Role | Notes |
|---------|---------|-------|
| Admin | Owner | The person managing the gym instance |
| Coach | Coach | Staff/trainer |
| (none) | Client | New in V2 |
| (none) | SystemAdmin | New — platform operator only |
The existing V1 "Admin" user maps to "Owner" in V2. No data is lost. The migration script sets their role to `Owner` and assigns them to the seeded Organization.