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
-
Role-based access control with three user roles:
ROLE_USER: Default user roleROLE_COMPANY_ADMIN: Manages users within their companyROLE_SUPER_ADMIN: Manages all users with elevated privileges
-
Two main entities: User and Company
-
Comprehensive API endpoints with role-based access
-
Strict validation constraints
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
id: Integer (auto-generated)name:- Required
- String (5-100 characters)
- Unique in the database
-
GET /users- Accessible by:
ROLE_COMPANY_ADMIN,ROLE_SUPER_ADMIN - Description: Fetches a paginated list of users.
- Visibility Restrictions:
ROLE_COMPANY_ADMINsees users within their company.ROLE_SUPER_ADMINsees all users.
- Accessible by:
-
GET /users/{id}- Accessible by: All roles.
- Description: Fetches details of a specific user by ID.
- Visibility Restrictions:
ROLE_USERcan only access their own data.ROLE_COMPANY_ADMINcan access users within their company.ROLE_SUPER_ADMINcan 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_ADMINcan only create users within their company.
- Accessible by:
-
PUT /users/{id}- Accessible by:
ROLE_COMPANY_ADMIN,ROLE_SUPER_ADMIN - Description: Updates details of an existing user.
- Restrictions:
ROLE_COMPANY_ADMINcan only update users within their company.
- Accessible by:
-
PUT /users/{id}/set-company/{companyId}- Accessible by:
ROLE_SUPER_ADMIN - Description: Assigns a user to a specific company.
- Accessible by:
-
PUT /users/{id}/unset-company- Accessible by:
ROLE_SUPER_ADMIN - Description: Removes a user from their assigned company.
- Accessible by:
-
DELETE /users/{id}- Accessible by:
ROLE_SUPER_ADMIN - Description: Deletes a user by ID.
- Accessible by:
-
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_ADMINcan only fetch users from their own company.
- Accessible by:
-
POST /companies- Accessible by:
ROLE_SUPER_ADMIN - Description: Creates a new company.
- Accessible by:
-
PUT /companies/{id}- Accessible by:
ROLE_SUPER_ADMIN - Description: Updates details of a specific company.
- Accessible by:
-
DELETE /companies/{id}- Accessible by:
ROLE_SUPER_ADMIN - Description: Deletes a company by ID.
- Accessible by:
- PHP 8.1+
- Symfony CLI
- Composer
- PostgresSQL
-
Clone the repository
git clone https://github.com/ariantron/CompanyUsersAPI cd CompanyUsersAPI -
Install dependencies
composer install
-
Copy environment files
cp .env.example .env cp .env.test.example .env.test
-
Configure database connection and another environment in .env files
-
Create database and run migrations
php bin/console make:migration php bin/console doctrine:migrations:migrate
-
Generate Fake Data
php bin/console doctrine:fixtures:loadsymfony server:start-
Create the test database:
php bin/console doctrine:database:create --env=test
-
Create the schema for the test database:
php bin/console doctrine:schema:create --env=test
php bin/phpunit- Endpoint access permissions
- Validation constraints
- Role-based restrictions
- Entity relationships
- Role-based access control
- Validation of user inputs
- Prevention of unauthorized actions
This project has been deployed to the domain sapi.mtrade.ir.
The deployment process is automated using GitHub Actions. You can view the GitHub Actions workflow configuration in the .github/workflows directory of the repository.
- SUPER_ADMIN can impersonate any user
- Implemented with careful security checks
Distributed under the MIT License. See LICENSE.txt for more information.