Skip to content

Implement role-based access control (owner > admin > member) - #119

Merged
neuromechanist merged 8 commits into
devfrom
feature/user-roles
Feb 12, 2026
Merged

neuromechanist merged 8 commits into
devfrom
feature/user-roles

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Feb 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Replaces the binary is_admin flag with a hierarchical role system: owner > admin > member. Closes #81, addresses #35.

  • Add role column to users table via migration (0009), then fully drop is_admin column (0010)
  • Initial assignments: yahya + nemarOwner = owner, nemarAdmin = admin
  • Add ownerMiddleware for owner-only operations
  • Add POST /admin/users/:username/role endpoint (owner-only) with safety checks: cannot self-demote, cannot remove last owner, token revocation on demotion
  • Replace all ~15 inline is_admin checks with hasRole() helper
  • Add parseRole() for validated role parsing with null-safety and error logging
  • Add isDemotion() helper for checking role downgrades
  • Fix privilege escalation: only owners can revoke other owners
  • Update notification queries to include both owners and admins
  • Add nemar admin role <username> <role> CLI command
  • Show role in login, status, and user list output
  • Remove is_admin from all API responses, CLI types, and test assertions
  • Update test scripts to use role-only schema

Test plan

  • Build passes (bun run build)
  • Apply migration locally: wrangler d1 migrations apply nemar-db --local (all 11 migrations including 0010)
  • Verify is_admin column dropped: PRAGMA table_info(users) shows no is_admin
  • Run existing tests: bun test (305 pass, 28 skip, 35 fail - all failures pre-existing)
  • Deploy backend and apply remote migrations
  • Test login as owner shows "Owner access enabled"
  • Test nemar admin users shows role badges
  • Test nemar admin role <user> admin works as owner
  • Test nemar admin role fails as admin (403)
  • Test self-demotion is blocked
  • Test removing last owner is blocked
  • Test demotion revokes tokens

@github-actions

github-actions Bot commented Feb 12, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR119

Version: 0.3.11-PR119.52778ed
Commit: 52778ed


🤖 Updated on each push.

Adds role column to users table, migrates existing is_admin data,
and sets initial owner/admin assignments for yahya, nemarOwner, nemarAdmin.
Adds UserRole type (owner/admin/member), hasRole() hierarchy check,
isValidRole() validator, and role field to AuthUser interface.
Keeps is_admin as deprecated computed field for backward compatibility.
Read role column from DB instead of is_admin. Compute is_admin from
role for backward compatibility. Add ownerMiddleware for owner-only routes.
Add POST /admin/users/:username/role (owner-only) with safety checks:
cannot self-demote, cannot remove last owner, token revocation on
demotion. Update user listing to include role, update regenerate-iam
to use role instead of is_admin.
Update datasets.ts: replace all is_admin inline checks with hasRole(),
update IAM query to read role column, update notification queries.
Update auth.ts: return role in login response, update notification query.
Update users.ts: return role in /users/me response.
Add role field to API types (LoginResponse, UserInfo, UserListItem).
Add changeUserRole() API function and 'nemar admin role' command.
Show role badges in user list, role-specific login messages, and
role in 'auth status --refresh' output.
Add test-owner user to test setup, add role column to all test users,
update test assertions to check role field alongside is_admin for
backward compatibility.
- Add parseRole() for validated role parsing instead of unsafe casts
- Add isDemotion() helper, export ROLE_HIERARCHY
- Fix privilege escalation: only owners can revoke other owners
- Wrap token revocation and audit log in try-catch
- Remove is_admin from AuthUser, API responses, CLI types, and tests
- Fix IAM null-role logic: use validated user.role from middleware
- Add requireAuth() to role command
- Add migration 0010 to drop is_admin column from users table
- Update test scripts to remove is_admin references
@neuromechanist
neuromechanist merged commit 4c28c3f into dev Feb 12, 2026
1 check passed
@neuromechanist
neuromechanist deleted the feature/user-roles branch March 6, 2026 23:56

This branch was previously deployed

1 inactive deployment
npm — cecd5daf Deployed Feb 12, 2026 by neuromechanist via publish #202
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant