Skip to content

Implement user role/level assignment system #81

Closed

Description

@neuromechanist

Problem

Currently, NEMAR only has a binary is_admin flag for user permissions. This is insufficient for a proper role-based access control (RBAC) system. We need a hierarchical role system with clear permission boundaries.

Current State

User table:

users (
  id INTEGER PRIMARY KEY,
  username TEXT,
  email TEXT,
  is_admin INTEGER DEFAULT 0,  -- Binary: 0 or 1
  status TEXT DEFAULT 'pending',
  ...
)

Current permission model:

  • Regular users: is_admin = 0
  • Admins: is_admin = 1
  • No distinction between different admin levels
  • No concept of ownership or maintainership

Proposed Role Hierarchy

Role Levels (from highest to lowest)

  1. Owner (highest privilege)

    • Can onboard admins
    • Can perform all admin actions
    • Can manage system-wide settings
    • Typically: Project founders, organization leads
  2. Admin

    • Can verify/approve member registrations
    • Can manage datasets (publish, visibility changes)
    • Can create concept DOIs
    • Cannot onboard other admins (only owners can)
    • Typically: Trusted maintainers, senior researchers
  3. Maintainer (optional tier)

    • Can manage datasets they collaborate on
    • Can review and approve PRs for datasets
    • Can invite collaborators to datasets
    • Cannot approve new user registrations
    • Typically: Active contributors, dataset curators
  4. Member (base level)

    • Can create and upload datasets
    • Can request collaborator access
    • Can download public datasets
    • Requires admin approval after signup
    • Typically: Regular users, researchers

Permission Matrix

Action Owner Admin Maintainer Member
Onboard admins ✅ ❌ ❌ ❌
Approve user registrations ✅ ✅ ❌ ❌
Create concept DOIs ✅ ✅ ❌ ❌
Publish datasets (any) ✅ ✅ ❌ ❌
Change dataset visibility (any) ✅ ✅ ❌ ❌
Manage own datasets ✅ ✅ ✅ ✅
Invite collaborators to own datasets ✅ ✅ ✅ ✅
Approve PRs on collaborated datasets ✅ ✅ ✅ ❌
Upload datasets ✅ ✅ ✅ ✅
Request collaborator access ✅ ✅ ✅ ✅
Download public datasets ✅ ✅ ✅ ✅

Database Changes

Migration: Add role column

-- Migration: 0007_user_roles
-- Description: Replace binary is_admin with role-based system

-- Add role column
ALTER TABLE users ADD COLUMN role TEXT NOT NULL DEFAULT 'member'
  CHECK (role IN ('owner', 'admin', 'maintainer', 'member'));

-- Migrate existing users
UPDATE users SET role = CASE 
  WHEN is_admin = 1 THEN 'admin'
  ELSE 'member'
END;

-- First admin becomes owner (manually set)
-- UPDATE users SET role = 'owner' WHERE username = 'yahya';

-- Index for role-based queries
CREATE INDEX IF NOT EXISTS idx_users_role ON users(role);

Code Changes Required

1. Auth Middleware Enhancement

export type UserRole = 'owner' | 'admin' | 'maintainer' | 'member';

export function isOwner(user: AuthenticatedUser): boolean {
  return user.role === 'owner';
}

export function isAdmin(user: AuthenticatedUser): boolean {
  return ['owner', 'admin'].includes(user.role);
}

2. New Admin Routes

// Owner-only: Promote users to admin/maintainer
POST /admin/users/:username/promote

// Admin+: Approve member registrations
POST /admin/users/:username/approve

3. CLI Commands

# New commands (owner only)
nemar admin users promote <username> <role>
nemar admin users demote <username>
nemar admin users list --role admin

# Updated output
$ nemar whoami
Authenticated
  Username: yahya
  Role:     Owner

Migration Strategy

  1. Phase 1: Database migration (add role column)
  2. Phase 2: Backend updates (permission checks)
  3. Phase 3: CLI updates (new commands)
  4. Phase 4: Testing & validation
  5. Phase 5: Documentation
  6. Phase 6: Remove is_admin column (future)

Security Considerations

  1. First owner must be set manually during deployment
  2. Owners cannot demote themselves (prevents lockout)
  3. All role changes must be audit logged
  4. Consider token invalidation on role changes
  5. Prevent demoting the last owner

Acceptance Criteria

  • Database migration adds role column
  • Existing admins migrated to 'admin' role
  • At least one owner account exists
  • Owners can promote users to admin/maintainer
  • Admins cannot promote users to admin
  • CLI shows user roles
  • All role changes are audit logged
  • Documentation updated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions