Download the PHP package mimachh/guardians without Composer
On this page you can find all versions of the php package mimachh/guardians. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mimachh/guardians
More information about mimachh/guardians
Files in mimachh/guardians
Package guardians
Short Description Handle multi user roles in your Laravel app easily
License
Informations about the package guardians
Mimachh/Guardians
Mimachh/Guardians is a Laravel package designed to simplify the management of user roles within your application. It provides an easy-to-use interface for associating roles with users, including methods for adding, removing, updating roles, and checking user roles. This package leverages a many-to-many relationship between users and roles to offer flexibility and control over user permissions.
Features
- Manage User Roles: Easily add, remove, update, and manage user roles.
- Many-to-Many Relationship: Automatically handles the many-to-many relationship between users and roles.
- Role Checking: Provides methods to check user roles for use in policies, middlewares, controllers, and more.
- Default Roles: Automatically assign default roles to new users.
- Configurable: Customize role model, user model, default roles, and pivot table name via the config file.
Installation
Install the package via Composer:
Publish the package's configuration file:
This will publish the configuration file to config/guardians.php.
Run the migrations:
This will create the necessary tables for roles and the pivot table role_user.
Register the Service Provider:
Add the service provider to the providers array in your config/app.php:
Configuration
The configuration file config/guardians.php
includes:
- role_model: The role model used by the package. By default, it uses \Mimachh\Guardians\Models\Role.
- user_model: The user model that will have roles attached. By default, it uses \App\Models\User.
- default_roles: Array of default roles to be seeded into the database. Format: [['name' => 'admin', 'slug' => 'admin'], ...].
- default_roles_attached: Roles to be automatically attached to new users upon creation.
- role_user_table: The name of the pivot table for the many-to-many relationship. Default is role_user.
- cache: Cache settings for roles and permissions to improve performance.
Usage
Use the trait
First you will need to add the trait HasRoles
to the user model.
Seed the roles
You can seed the default roles using :
Defining the Relationships
Ensure that your User and Role models define the many-to-many relationship:
User Model:
By default the relation $user->roles()
is already loaded.
But you can override it, if you need to.
Role Model:
Relation is many to many, and the Role Model is already attach to user model.
Assigning Roles
Attach a role to a user:
Attach several roles to a user:
Detach a role from a user:
Check user role:
Using Observers
The package includes an observer to automatically assign default roles to new users:
Ensure the observer is registered in the GuardiansServiceProvider:
Middleware
The package gives you a middleware to check the users's roles.
Register the middleware
Give the middleware an alias :
Use the middleware
Use it as you use any middleware, but ensure you pass the slug of the authorized role.
Testing
To test the package, you can create a new user and verify if the roles are attached correctly:
Roadmap
Incoming :
- Middleware to check is user have multiple roles eg. User must be author AND admin
Contributing
If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on GitHub.
License This package is licensed under the MIT License. See the LICENSE file for more details.