Installation
composer require sowork/yauth dev-master
Register the service provider and alias in config/app.php:
'providers' => [
// ...
Sowork\YAuth\YAuthServiceProvider::class,
],
'aliases' => [
// ...
'YAuth' => Sowork\YAuth\Facades\YAuth::class,
],
Publish Config & Migrations
php artisan vendor:publish --provider="Sowork\YAuth\YAuthServiceProvider"
php artisan migrate
(Skip migrations if extending existing tables by calling YAuth::ignoreMigrations() in AppServiceProvider.)
First Use Case Assign a role to a user and check permissions:
// Assign role 'admin' to user with ID 1
YAuth::assignRole(1, 'admin');
// Check if user 1 has 'edit_post' permission
if (YAuth::hasPermission(1, 'edit_post')) {
// Grant access
}
Role-Based Access Control (RBAC)
YAuth::assignRole($userId, $roleName)YAuth::revokeRole($userId, $roleName)YAuth::hasRole($userId, $roleName)Permission Management
YAuth::grantPermission($roleName, $permission)YAuth::revokePermission($roleName, $permission)YAuth::hasPermission($userId, $permission)Multi-Table Permissions
Use YAuth::hasPermission($userId, $permission, $tableName) for table-specific permissions (e.g., posts table).
Middleware Integration Create a middleware to enforce permissions globally:
namespace App\Http\Middleware;
use Closure;
use YAuth\Facades\YAuth;
class CheckPermission
{
public function handle($request, Closure $next, $permission)
{
if (!YAuth::hasPermission($request->user()->id, $permission)) {
abort(403);
}
return $next($request);
}
}
Register in app/Http/Kernel.php:
protected $routeMiddleware = [
'permission' => \App\Http\Middleware\CheckPermission::class,
];
Usage in routes:
Route::get('/admin/posts', function () {
// ...
})->middleware(['auth', 'permission:edit_post']);
Dynamic Permission Checks Useful for conditional UI rendering:
@if(YAuth::hasPermission(auth()->id(), 'manage_users'))
<button class="btn btn-danger">Delete User</button>
@endif
Migration Conflicts
YAuth::ignoreMigrations() in AppServiceProvider before publishing migrations.users and roles tables exist. Customize via config/yauth.php:
'tables' => [
'users' => 'custom_users',
'roles' => 'custom_roles',
],
Permission Caching
YAuthServiceProvider:
YAuth::setCacheDriver('redis');
Case Sensitivity
define('ROLE_ADMIN', 'admin');
YAuth::assignRole($userId, ROLE_ADMIN);
Middleware Overhead
Log Permission Checks Override the facade to log checks:
YAuth::setLogger(function ($message) {
\Log::debug($message);
});
Dump User Permissions Inspect a user’s roles/permissions:
$userId = 1;
$roles = YAuth::getRoles($userId);
$permissions = YAuth::getPermissions($userId);
dd(compact('roles', 'permissions'));
Clear Cached Permissions If using caching, manually clear:
php artisan cache:clear
Custom Permission Logic
Extend the permission resolver by binding a custom resolver in AppServiceProvider:
YAuth::extend(function ($app) {
return new CustomPermissionResolver();
});
Event Listeners Listen for role/permission changes:
YAuth::roleAssigned(function ($userId, $role) {
// Send notification, log, etc.
});
API Integration For APIs, use a trait to avoid middleware repetition:
namespace App\Traits;
use YAuth\Facades\YAuth;
trait ApiPermissionCheck
{
protected function checkPermission($permission)
{
if (!YAuth::hasPermission(auth()->id(), $permission)) {
return response()->json(['error' => 'Unauthorized'], 403);
}
}
}
How can I help you explore Laravel packages today?