delgont / auth
Larevel User - Permissions -Roles
Installs: 23
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:project
Requires
- php: ^7.2.5 || ^8.0
- illuminate/auth: ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
- illuminate/database: ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0
- laravel/helpers: ^1.5.0 || 1.6.0 || 1.7.0
README
Laravel Authentication Backend
Composer
Laravel Framework 6.0+
Introduction
Laravel authentication backend that provides the following features.
- Email or username authentication
- Access control using roles and permissions.
- Access control using roles and permissions.
Installation
composer require delgont/auth
php artisan vendor:publish --multiauth-config
Multi Username Authentication
username
email
- Login Controller.
Create your custom login controller and use
Delgont\Auth\Concerns\MultiAuthCredentials
. this will overide the credentials function
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Delgont\Auth\Concerns\MultiAuthCredentials;
use Illuminate\Http\Request;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller - Multi Authentication using email or username
|--------------------------------------------------------------------------
| Use Delgont\Auth\Concerns\MultiAuthCredentials trait
| You must override the credentials and username functions as shown below
|
*/
use AuthenticatesUsers, MultiAuthCredentials;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
protected function credentials(Request $request)
{
return $this->multiAuthCredentials($request);
}
public function username()
{
return 'username_email';
}
}
- Your login View.
<input id="username_email" type="text" class="form-control @error('username_email') is-invalid @enderror" name="username_email" value="{{ old('username_email') }}" required autocomplete="username_email" autofocus> @error('username_email') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror
Access Control
Regulate access to your laravel systems resources, features and functionality.
Access control basing on user type
- add
usertype
&user_id
columns to your authenticatable migration
<?php .............. Schema::table('users', function (Blueprint $table) { $table->nullableMorphs('user'); });
- Add
Delgont\Auth\Concerns\HasUserTypes
Trait to user model.
<?php namespace App; use Laravel\Passport\HasApiTokens; use Illuminate\Notifications\Notifiable; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; use Delgont\Cms\Notifications\Auth\ResetPassword as ResetPasswordNotification; use Delgont\Auth\Concerns\HasUserTypes; class User extends Authenticatable { use Notifiable, HasUserTypes, ModelHasPermissions, ModelHasSingleRole;
- Your usertype models
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Customer extends Model { public function user() { return $this->morphOne('App\User', 'user'); } }
User can have single role or multiple roles
Using role middleware to restrict access
Use Delgont\Auth\Concerns\ModelHasRoles
trait on your authenticable model
<?php namespace App; use Laravel\Passport\HasApiTokens; use Illuminate\Notifications\Notifiable; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; use Delgont\Auth\Concerns\ModelHasRoles; class User extends Authenticatable { /* | Use Delgont\Auth\Concerns\ModelHasRoles trait | */ use ModelHasRoles; }
Assigning roles
# Giving role using role names $model->assignRole(['admin','accountant']); auth()->user()->assignRole(['admin','accountant']);
Protecting routes using the role middleware
Route::get('/test', 'TestController@test')->middleware('role:admin'); Route::get('/test', 'TestController@test')->middleware('role:admin|hello');
Using permission middleware to restrict access
Route::get('/momo', 'Momo@index')->name('momo')->middleware('permission:access_momo_dashboard');
Configure your default permissions in the permissions configuration file
<?php
return [
'delimiter' => '|',
'permissions' => [
'manage_users',
'access_momo_dashbaord'
]
];
`
Artisan Commands
Roles
php artisan make:roleRegistrar Roles/ExampleRoleRegistrar
php artisan role:sync