PHPackages                             saritasa/roles-simple - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Security](/categories/security)
4. /
5. saritasa/roles-simple

AbandonedArchivedLibrary[Security](/categories/security)

saritasa/roles-simple
=====================

User has field role\_id and corresponding lookup table

1.2.1(5y ago)011.6k1MITPHPPHP &gt;=7.0

Since May 12Pushed 5y ago8 watchersCompare

[ Source](https://github.com/Saritasa/php-roles-simple)[ Packagist](https://packagist.org/packages/saritasa/roles-simple)[ RSS](/packages/saritasa-roles-simple/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (10)Dependencies (6)Versions (16)Used By (0)

Saritasa Roles for Laravel
==========================

[](#saritasa-roles-for-laravel)

[![PHP CodeSniffer](https://github.com/Saritasa/php-roles-simple/workflows/PHP%20Codesniffer/badge.svg)](https://github.com/Saritasa/php-roles-simple/actions)[![Release](https://camo.githubusercontent.com/35696e72445bf4eb60a83ecdec548d0c360912a0c45364dc9567a8140ca493b4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f73617269746173612f7068702d726f6c65732d73696d706c652e737667)](https://github.com/Saritasa/php-roles-simple/releases)[![PHPv](https://camo.githubusercontent.com/25a2545100af410efa2faa323e212c1bd80362fb6c8ac654bd1902ce38e01764/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f73617269746173612f726f6c65732d73696d706c652e737667)](http://www.php.net)[![Downloads](https://camo.githubusercontent.com/6aeef5a7f0d776361e6d4a56b086539e4eb2b48d3c8064ce3c28a5b2d4207edc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73617269746173612f726f6c65732d73696d706c652e737667)](https://packagist.org/packages/saritasa/roles-simple)

Simplified implementation of user roles for Laravel applications: User has role\_id field, and corresponding roles lookup table exists.

Usage
-----

[](#usage)

Install the `saritasa/roles-simple` package:

```
$ composer require saritasa/roles-simple
```

If you use Laravel 5.4 or less, or 5.5+ with [package discovery](https://laravel.com/docs/5.5/packages#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
```

Available classes
-----------------

[](#available-classes)

### HasRoles

[](#hasroles)

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.

### Role (model)

[](#role-model)

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:

- **id** (integer) - primary key
- **name** (string) - supposed to be visible to user and may change, as needed. May contain any characters (including spaces), in any case. Do not use role name as identifier (ex. to search by it).
- **slug** (string) - human-readable role identifier, supposed to be constant, while name can be changed. You can reference role by slug in code (ex. in *hasRole()* method). It's recommended to keep slugs in lowercase, use underscore or dash instead of spaces.

```
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();
    }
}

```

### Roles (enum)

[](#roles-enum)

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;
}

```

Middleware
----------

[](#middleware)

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

Contributing
------------

[](#contributing)

1. Create fork, checkout it
2. Develop locally as usual. **Code must follow [PSR-1](http://www.php-fig.org/psr/psr-1/), [PSR-2](http://www.php-fig.org/psr/psr-2/)** - run [PHP\_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) to ensure, that code follows style guides
3. **Cover added functionality with unit tests** and run [PHPUnit](https://phpunit.de/) to make sure, that all tests pass
4. Update [README.md](README.md) to describe new or changed functionality
5. Add changes description to [CHANGES.md](CHANGES.md) file. Use [Semantic Versioning](https://semver.org/) convention to determine next version number.
6. When ready, create pull request

### Make shortcuts

[](#make-shortcuts)

If you have [GNU Make](https://www.gnu.org/software/make/) installed, you can use following shortcuts:

- `make cs` (instead of `php vendor/bin/phpcs`) - run static code analysis with [PHP\_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer)to check code style
- `make csfix` (instead of `php vendor/bin/phpcbf`) - fix code style violations with [PHP\_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer)automatically, where possible (ex. PSR-2 code formatting violations)
- `make test` (instead of `php vendor/bin/phpunit`) - run tests with [PHPUnit](https://phpunit.de/)
- `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

Resources
---------

[](#resources)

- [Bug Tracker](http://github.com/saritasa/php-roles-simple/issues)
- [Code](http://github.com/saritasa/php-roles-simple)
- [Changes History](CHANGES.md)
- [Authors](http://github.com/saritasa/php-roles-simple/contributors)

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 92.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~78 days

Recently: every ~121 days

Total

15

Last Release

2189d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b7632cf2eb5dd552ace9d66947ed6919e19c6a2fdb5dc73b8c71e0f9886ccc83?d=identicon)[saritasa](/maintainers/saritasa)

---

Top Contributors

[![populov](https://avatars.githubusercontent.com/u/3766033?v=4)](https://github.com/populov "populov (50 commits)")[![maxermolenko](https://avatars.githubusercontent.com/u/11438109?v=4)](https://github.com/maxermolenko "maxermolenko (2 commits)")[![hollow-en](https://avatars.githubusercontent.com/u/87475798?v=4)](https://github.com/hollow-en "hollow-en (1 commits)")[![microdel](https://avatars.githubusercontent.com/u/3994643?v=4)](https://github.com/microdel "microdel (1 commits)")

---

Tags

laravelphpmiddlewarelaravelsecurityroles

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/saritasa-roles-simple/health.svg)

```
[![Health](https://phpackages.com/badges/saritasa-roles-simple/health.svg)](https://phpackages.com/packages/saritasa-roles-simple)
```

###  Alternatives

[alajusticia/laravel-logins

Session management in Laravel apps, user notifications on new access, support for multiple separate remember tokens, IP geolocation, User-Agent parser

2011.0k](/packages/alajusticia-laravel-logins)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
