Skip to content

Repository files navigation

Contributors Forks Stargazers Issues MIT License


User Management API

This is a Symfony-based API application for user and company management with role-based access control. The application is built using API Platform v3 and utilizes PostgreSQL as the database.
Explore the docs »

View Demo · Report Bug · Request Feature

Features

  • Role-based access control with three user roles:

    • ROLE_USER: Default user role
    • ROLE_COMPANY_ADMIN: Manages users within their company
    • ROLE_SUPER_ADMIN: Manages all users with elevated privileges
  • Two main entities: User and Company

  • Comprehensive API endpoints with role-based access

  • Strict validation constraints

Technical Stack

  • Symfony
  • Postgres

Entity Specifications

User Entity

  • id: Integer (auto-generated, immutable)
  • name:
    • Required
    • String (3-100 characters)
    • Must contain letters and spaces
    • Requires at least one uppercase letter
  • role:
    • Required
    • Choices: ROLE_USER, ROLE_COMPANY_ADMIN, ROLE_SUPER_ADMIN
    • Role restrictions apply
  • company:
    • Required for USER and COMPANY_ADMIN roles
    • Relationship to Company entity
    • Not allowed for SUPER_ADMIN

Company Entity

  • id: Integer (auto-generated)
  • name:
    • Required
    • String (5-100 characters)
    • Unique in the database

API Endpoints

User Endpoints

  • GET /users

    • Accessible by: ROLE_COMPANY_ADMIN, ROLE_SUPER_ADMIN
    • Description: Fetches a paginated list of users.
    • Visibility Restrictions:
      • ROLE_COMPANY_ADMIN sees users within their company.
      • ROLE_SUPER_ADMIN sees all users.
  • GET /users/{id}

    • Accessible by: All roles.
    • Description: Fetches details of a specific user by ID.
    • Visibility Restrictions:
      • ROLE_USER can only access their own data.
      • ROLE_COMPANY_ADMIN can access users within their company.
      • ROLE_SUPER_ADMIN can access all users.
  • GET /user

    • Accessible by: All roles.
    • Description: Fetches the currently authenticated user's details.
  • POST /users

    • Accessible by: ROLE_COMPANY_ADMIN, ROLE_SUPER_ADMIN
    • Description: Creates a new user.
    • Restrictions:
      • ROLE_COMPANY_ADMIN can only create users within their company.
  • PUT /users/{id}

    • Accessible by: ROLE_COMPANY_ADMIN, ROLE_SUPER_ADMIN
    • Description: Updates details of an existing user.
    • Restrictions:
      • ROLE_COMPANY_ADMIN can only update users within their company.
  • PUT /users/{id}/set-company/{companyId}

    • Accessible by: ROLE_SUPER_ADMIN
    • Description: Assigns a user to a specific company.
  • PUT /users/{id}/unset-company

    • Accessible by: ROLE_SUPER_ADMIN
    • Description: Removes a user from their assigned company.
  • DELETE /users/{id}

    • Accessible by: ROLE_SUPER_ADMIN
    • Description: Deletes a user by ID.

Company Endpoints

  • GET /companies

    • Accessible by: All roles.
    • Description: Fetches a paginated list of companies.
  • GET /companies/{id}

    • Accessible by: All roles.
    • Description: Fetches details of a specific company by ID.
  • GET /companies/{id}/users

    • Accessible by: ROLE_COMPANY_ADMIN, ROLE_SUPER_ADMIN
    • Description: Fetches a list of users belonging to a specific company.
    • Restrictions:
      • ROLE_COMPANY_ADMIN can only fetch users from their own company.
  • POST /companies

    • Accessible by: ROLE_SUPER_ADMIN
    • Description: Creates a new company.
  • PUT /companies/{id}

    • Accessible by: ROLE_SUPER_ADMIN
    • Description: Updates details of a specific company.
  • DELETE /companies/{id}

    • Accessible by: ROLE_SUPER_ADMIN
    • Description: Deletes a company by ID.

Development Setup

Prerequisites

  • PHP 8.1+
  • Symfony CLI
  • Composer
  • PostgresSQL

Installation Steps

  1. Clone the repository

    git clone https://github.com/ariantron/CompanyUsersAPI
    cd CompanyUsersAPI
  2. Install dependencies

    composer install
  3. Copy environment files

    cp .env.example .env
    cp .env.test.example .env.test
  4. Configure database connection and another environment in .env files

  5. Create database and run migrations

    php bin/console make:migration
    php bin/console doctrine:migrations:migrate
  6. Generate Fake Data

php bin/console doctrine:fixtures:load

Running the Application

symfony server:start

Testing

Setup Test Environment

  1. Create the test database:

    php bin/console doctrine:database:create --env=test
  2. Create the schema for the test database:

    php bin/console doctrine:schema:create --env=test

Running Tests

php bin/phpunit

Test Coverage

  • Endpoint access permissions
  • Validation constraints
  • Role-based restrictions
  • Entity relationships

Security Considerations

  • Role-based access control
  • Validation of user inputs
  • Prevention of unauthorized actions

Deployment

This project has been deployed to the domain sapi.mtrade.ir.

CI/CD with GitHub Actions

The deployment process is automated using GitHub Actions. You can view the GitHub Actions workflow configuration in the .github/workflows directory of the repository.

Impersonation

  • SUPER_ADMIN can impersonate any user
  • Implemented with careful security checks

License

Distributed under the MIT License. See LICENSE.txt for more information.

About

A Test Web API Application Project using PHP/Symfony framework and API-Platform Library

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages