Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 30, 2025

Adds role-based access control (RBAC) infrastructure with JWT authentication and fixes the failing Docker Cloud Build & Push workflow by specifying the correct Dockerfile path and skipping registry push on pull requests.

Database & Models

  • UserRole: UUID primary key, unique name, description, timestamps
  • User: UUID primary key, username, email, hashed_password (bcrypt), role_id foreign key, is_active
  • SQLAlchemy ORM with PostgreSQL/SQLite support via app/database.py

API Endpoints

REST API at /api/roles:

  • GET /roles - list roles (paginated)
  • POST /roles - create role (validates unique name)
  • GET /roles/{id} - get role by UUID
  • PUT /roles/{id} - update role
  • DELETE /roles/{id} - delete role

All endpoints require API key authentication.

Authentication & Authorization

Enhanced app/auth/dependencies.py:

  • create_access_token() - generates JWT with 30min expiration
  • get_current_user() - validates JWT, loads user with role relationship
  • require_role(*roles) - decorator factory for flexible role checks
  • Convenience decorators: admin_only, customer_only, auditor_only

Example usage:

from app.auth.dependencies import require_role, admin_only

@router.get("/admin-only", dependencies=[Depends(admin_only)])
async def admin_endpoint():
    return {"message": "Admin access granted"}

@router.get("/multi-role", dependencies=[Depends(require_role("admin", "auditor"))])
async def multi_role_endpoint():
    return {"message": "Admin or auditor access"}

Docker Workflow Fix

Created docker/Dockerfile.api:

  • Multi-stage build with Python 3.11-slim and virtual environment
  • Non-root user execution with correct PATH (/opt/venv/bin)
  • ARG support for BUILD_DATE and VCS_REF labels
  • Health check at /health endpoint

Updated .github/workflows/production-pipeline.yml:

  • Added file: ./docker/Dockerfile.api to both docker-security and docker-cloud-build jobs
  • Added push: ${{ github.event_name != 'pull_request' }} to prevent registry push on PRs
  • Conditional Docker Hub login (skipped for pull_request events)

Dependencies

  • sqlalchemy==2.0.36 - ORM and database toolkit
  • psycopg2-binary==2.9.10 - PostgreSQL adapter
  • python-jose[cryptography]==3.4.0 - JWT tokens (fixes CVE-2024-33663)
  • passlib[bcrypt]==1.7.4 - password hashing
  • email-validator==2.2.0 - email validation for Pydantic

Tests

16 new tests covering role CRUD, JWT auth, password hashing, role validation, and access control. All 23 tests passing with 0 CodeQL alerts.

Original prompt

This section details on the original issue you should resolve

<issue_title>Feature Request: Implement User Role Management</issue_title>
<issue_description>## Feature Request: User Role Management

Description

The current NeuroBank FastAPI Toolkit provides a robust foundation for a banking application, but it lacks a user role management system that would allow for different access levels and permissions based on user roles (e.g., admin, customer, auditor).

Motivation

Implementing user roles would enhance security and administration control within the application, allowing organizations to manage permissions more effectively and ensuring that users can only access the features relevant to their responsibilities.

Proposed Solution

  • Add a new model for user roles in the database.
  • Include APIs to create, read, update, and delete user roles.
  • Adjust the authentication and authorization mechanisms to check user roles before granting access to specific API endpoints.
  • Update documentation to reflect changes in user management and provide example use cases.

Additional Notes

This feature could significantly improve the functionality and security of the NeuroBank toolkit, making it more attractive for enterprise-level applications.</issue_description>

<agent_instructions>Implement user role management in the NeuroBank FastAPI Toolkit using SQLAlchemy and Pydantic models.
Create a new UserRole model with fields: id (UUID), name (str, unique), and description (str).
Extend the existing User model to include a foreign key to UserRole.
Add REST API endpoints under /roles for CRUD operations:

  • GET /roles (list all roles)
  • POST /roles (create new role)
  • GET /roles/{id}
  • PUT /roles/{id}
  • DELETE /roles/{id}

Integrate role-based access control (RBAC) with FastAPI’s dependency injection system.
Add a get_current_user dependency that verifies the user's JWT and attaches the user role.
Restrict protected routes with role checks (e.g., admin_only, customer_only, auditor_only decorators).

Update OpenAPI documentation to include role information in the JWT schema.
Include database migrations if Alembic is configured, and add unit tests for role CRUD and authorization rules.
Follow existing code style and directory structure of the NeuroBank FastAPI Toolkit (app/models, app/api/routes, app/core/security).
</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@Neiland85 Neiland85 marked this pull request as ready for review October 30, 2025 08:34
Copilot AI review requested due to automatic review settings October 30, 2025 08:34
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review any files in this pull request.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov-commenter
Copy link

codecov-commenter commented Oct 30, 2025

Copilot AI and others added 3 commits October 30, 2025 08:46
… endpoints, JWT auth, and tests

Co-authored-by: Neiland85 <164719485+Neiland85@users.noreply.github.com>
…e workflow configuration

Co-authored-by: Neiland85 <164719485+Neiland85@users.noreply.github.com>
Co-authored-by: Neiland85 <164719485+Neiland85@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement user role management system Implement user role management with RBAC and fix Docker Cloud Build workflow Oct 30, 2025
Copilot AI requested a review from Neiland85 October 30, 2025 08:57
Copy link
Owner

@Neiland85 Neiland85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! all new config it's done!

@Neiland85 Neiland85 self-requested a review October 30, 2025 16:24
@Neiland85
Copy link
Owner

Closing this PR. The changes no longer align with the updated architecture and dependency model introduced in the December 2025 refactor.

This contribution is now obsolete and would add noise rather than value to the current codebase.

@Neiland85 Neiland85 closed this Dec 10, 2025
@Neiland85 Neiland85 deleted the copilot/implement-user-role-management branch December 10, 2025 16:47
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.

Feature Request: Implement User Role Management

3 participants