Package Data | |
---|---|
Maintainer Username: | saritasa |
Maintainer Contact: | sergey@saritasa.com (Sergey Populov) |
Package Create Date: | 2017-05-11 |
Package Last Update: | 2021-03-09 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-03-26 03:15:39 |
Package Statistics | |
---|---|
Total Downloads: | 9,881 |
Monthly Downloads: | 128 |
Daily Downloads: | 4 |
Total Stars: | 0 |
Total Watchers: | 10 |
Total Forks: | 1 |
Total Open Issues: | 0 |
Simplified implementation of user roles for Laravel applications: User has role_id field, and corresponding roles lookup table exists.
Install the saritasa/php-roles-simple
package:
$ composer require saritasa/php-roles-simple
If you use Laravel 5.4 or less,
or 5.5+ with package discovery disabled,
add the RolesServiceProvider service provider in config/app.php
:
'providers' => array(
// ...
Saritasa\Roles\RolesServiceProvider::class,
)
Then publish DB migrations:
php artisan vendor:publish --provider=Saritasa\\Roles\\RolesServiceProvider
Provides hasRole method;
Example:
class User extends Model implements IHasRoles
{
uses HasRoles
}
then somewere in code:
if ($user->hasRole(Roles::ADMIN)) { ... }}
$user->role->name;
hasRole($role) method can accept either role ID (integer) or role slug (string). Using role ID is a bit faster, because does not require reading role record from lookup table.
You can use built-in class Saritasa\Roles\Models\Role to list of models or create new roles in migrations or in code.
Role model contains 3 fields:
class AddRoles extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Role::firstOrCreate(['name' => 'User', 'slug' => 'user']);
Role::firstOrCreate(['name' => 'Admin', 'slug' => 'admin']);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Role::whereSlug('user')->delete();
Role::whereSlug('admin')->delete();
}
}
Package contains enum Saritasa\Roles\Enums\Roles, which has 2 predefined roles: User and Admin, which is suitable for many applications.
But you are not limited to these roles, you can define your own enum (extending this one or create new from scratch) and use it.
class Roles extends Enum
{
const USER = 1;
const SUPER_ADMIN = 2;
const SCHOOL_ADMIN = 3;
}
You can use middleware in routes to limit access to certain pages:
Router::get('/admin', [
'as' => 'admin.dashboard',
'middlware' => 'role:admin'
]
Middleware with alias is registered by service provider, no need to register it manually.
Format is role:role_slug1,role_slug2,role_slug3.
If user does not have any of required roles, AccessDeniedHttpException will be thrown
If you have GNU Make installed, you can use following shortcuts:
make cs
(instead of php vendor/bin/phpcs
) -
run static code analysis with PHP_CodeSniffer
to check code stylemake csfix
(instead of php vendor/bin/phpcbf
) -
fix code style violations with PHP_CodeSniffer
automatically, where possible (ex. PSR-2 code formatting violations)make test
(instead of php vendor/bin/phpunit
) -
run tests with PHPUnit
make install
- instead of composer install
make all
or just make
without parameters -
invokes described above install, cs, test tasks sequentially -
project will be assembled, checked with linter and tested with one single command