Users

Users

Laravel Users (Roles & Permissions, Devices, Password Hashing, Password History).

Moamen Eltouny (Raggi)
PHP Version : >= 7.2 Laravel Version : >= 6.0 License Support
Source Packagist Version Packagist Downloads

Devices Password Hashing Password History Roles & Permissions

Install

Install the latest version using Composer

composer require pharaonic/laravel-users
php artisan migrate

OptionalPublish the migrations files.

php artisan vendor:publish --tag=laravel-users
Devices (Agents)

Depends on Agents Detector < take a look at here >.

Just add this trait HasDevices to your auth model and you will manage his/her devices information.

namespace App\Models;

...
use Pharaonic\Laravel\Users\Traits\HasDevices;

class User extends Authenticatable
{
    use HasDevices;

    ...
}

Then you have many functions helps you to manipulate this devices information.

$user->hasDetectedDevice()      // Check if the current device has been detected or not.
// Returns Boolean


$user->detectDevice($fcm);      // Detect the current user device and store info in database.
// $fcm (Optional)              Firebase Cloud Messaging Token
// You have to use it on login or registeration.
// Returns Pharaonic\Laravel\Users\Models\UserAgent  Model


$user->devices;                 // Getting list of Devices (Agents) List.
// Returns Illuminate\Database\Eloquent\Collection


$user->fcmList;                 // Getting list of devices FCM (Firebase Cloud Messaging).
// Returns array


$user->removeDevice(3);         // Remove a device with the real agent id.
// You can get the agent id from agent()->id
// Returns boolean

$user->removeAllDevices();      // Remove all devices.
// Returns boolean
Auto Hashing Password

For Auto-Hashing your users password without using Hash:make on create or update

Just add this trait HasHashedPassword to your auth model.

namespace App\Models;

...
use Pharaonic\Laravel\Users\Traits\HasHashedPassword;

class User extends Authenticatable
{
    use HasHashedPassword;

    ...
}
Hashed-Password History

And if you want to manage the history of user hashed passwords For Security Issues.

Just add this trait HasPasswordHistory to your auth model.

namespace App\Models;

...
use Pharaonic\Laravel\Users\Traits\HasPasswordHistory;

class User extends Authenticatable
{
    use HasPasswordHistory;

    ...
}

Then you have a way to manipulate the user hashed-passwords history with agents info and IP.

$user->passwordHistory;     // Getting list of hashed-passwords history with agents info and IP.
// Returns \Illuminate\Database\Eloquent\Relations\MorphMany

//  created_at      DateTime of changing password
//  pass_from       The old password
//  pass_to         the new password
//  ip              IP of the password changer
//  agent           The agent model of who changed the password
//      - device
//      - operationSystem
//      - browser
Permissions

Most of big projects needs to users permissions.

So you can create a new permission by one of these ways.

use Pharaonic\Laravel\Users\Models\Permission;

// Way 1 : Create a permission with the default locale [code, title].
Permission::create('post.create', 'Create a new post');

// Way 2 : Create a permission with a specified locale [code, title, locale].
Permission::create('post.create', 'Create a new post', 'en');

// Way 3 : Create a permissions with multiple locales [code, localized titles].
Permission::create('post.create', [
    'ar'    => 'إنشاء منشور جديد',
    'en'    => 'Create a new post'
]);

Just add this trait HasPermissions to your auth model.

namespace App\Models;

...
use Pharaonic\Laravel\Users\Traits\HasPermissions;

class User extends Authenticatable
{
    use HasPermissions;

    ...
}

Then you have a way to manipulate the user permissions.

$user->permissionsList;
// Getting list of user permissions
// If the auth model uses HasRoles trait too so will display the roles permissions too.
// Returns Array


$user->permit('post.create', 'post.edit');
// Giving the user one or many permissions.
// Accepts Variable-length argument/Array of permissions codes.
// Returns Boolean


$user->permitted(['post.create', 'post.delete']);
// Check if the user has all these permissions.
// Accepts Variable-length argument/Array of permissions codes.
// Returns Boolean


$user->permittedAny(['post.create', 'post.delete']);
// Check if the user has one of these permissions.
// Accepts Variable-length argument/Array of permissions codes.
// Returns Boolean


$user->forbid('post.edit', 'post.view');
// Forbid the user to have these permissions.
// Accepts Variable-length argument/Array of permissions codes.
// Returns Boolean


$user->forbad(['post.create', 'post.delete']);
// Check if the user has not all these permissions.
// Accepts Variable-length argument/Array of permissions codes.
// Returns Boolean


$user->forbadAny(['post.create', 'post.delete']);
// Check if the user has not one of these permissions.
// Accepts Variable-length argument/Array of permissions codes.
// Returns Boolean


$user->syncPermissions('post.*', 'article.*');
// Clear all user permissions and give him/her these list.
// Accepts Variable-length argument/Array of permissions codes.
// Returns Boolean

And you can use this one of these middlewares permittedpermittedAny to strict your routes.

// Allow only for who have all these permissions.
Route::middleware('permitted:post.create,post.edit')->group(...);

// Allow only for who have one of these permissions.
Route::middleware('permittedAny:post.create,post.edit')->group(...);
Roles

Most of big projects needs to users roles too.

So you can create a new role by one of these ways.

use Pharaonic\Laravel\Users\Models\Role;

// Way 1 : Create a role with the default locale [code, title].
Role::create('admin', 'Administrator');

// Way 2 : Create a role with a specified locale [code, title, locale].
Role::create('admin', 'Administrator', 'en');

// Way 3 : Create a roles with multiple locales [code, localized titles].
Role::create('admin', [
    'ar'    => 'إداري',
    'en'    => 'Administrator'
]);

And you can attach some permission to the role.

Role's permissions has same functions of User's permissions too.

use Pharaonic\Laravel\Users\Models\Role;

$role = Role::findByCode('admin');
$role->permit('post.create', 'post.edit', 'post.delete');

To use roles with auth model, just add this trait HasRoles to your auth model.

namespace App\Models;

...
use Pharaonic\Laravel\Users\Traits\HasRoles;

class User extends Authenticatable
{
    use HasRoles;

    ...
}

Then you have a way to manipulate the user roles.

$user->rolesList;
// Getting list of user roles
// Returns Array


$user->entrust('DevOps', 'Developer');
// Giving the user one or many roles.
// Accepts Variable-length argument/Array of roles codes.
// Returns Boolean


$user->entrusted('DevOps');
// Check if the user has all these roles.
// Accepts Variable-length argument/Array of roles codes.
// Returns Boolean


$user->entrustedAny(['Developer', 'Musician']);
// Check if the user has one of these roles.
// Accepts Variable-length argument/Array of roles codes.
// Returns Boolean


$user->distrust('Musician');
// Distrusting the user from having these roles.
// Accepts Variable-length argument/Array of roles codes.
// Returns Boolean


$user->distrusted(['CEO', 'CFO']);
// Check if the user has not all these roles.
// Accepts Variable-length argument/Array of roles codes.
// Returns Boolean


$user->distrustedAny(['CEO', 'CFO', 'Musician']);
// Check if the user has not one of these roles.
// Accepts Variable-length argument/Array of roles codes.
// Returns Boolean


$user->syncRoles('CEO', 'Developer', 'Musician');
// Clear all user roles and give him/her these list.
// Accepts Variable-length argument/Array of roles codes.
// Returns Boolean

And you can use this one of these middlewares entrustedentrustedAny to strict your routes.

// Allow only for who have all these roles.
Route::middleware('entrusted:CFO')->group(...);

// Allow only for who have one of these roles.
Route::middleware('entrustedAny:CFO,CEO')->group(...);