Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Yauth Laravel Package

sowork/yauth

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. 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,
    ],
    
  2. 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.)

  3. 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
    }
    

Implementation Patterns

Core Workflows

  1. Role-Based Access Control (RBAC)

    • Assign Roles: YAuth::assignRole($userId, $roleName)
    • Revoke Roles: YAuth::revokeRole($userId, $roleName)
    • Check Role: YAuth::hasRole($userId, $roleName)
  2. Permission Management

    • Grant Permissions: YAuth::grantPermission($roleName, $permission)
    • Revoke Permissions: YAuth::revokePermission($roleName, $permission)
    • Check Permissions: YAuth::hasPermission($userId, $permission)
  3. Multi-Table Permissions Use YAuth::hasPermission($userId, $permission, $tableName) for table-specific permissions (e.g., posts table).

  4. 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']);
    
  5. Dynamic Permission Checks Useful for conditional UI rendering:

    @if(YAuth::hasPermission(auth()->id(), 'manage_users'))
        <button class="btn btn-danger">Delete User</button>
    @endif
    

Gotchas and Tips

Pitfalls

  1. Migration Conflicts

    • If extending existing tables, always call YAuth::ignoreMigrations() in AppServiceProvider before publishing migrations.
    • Default migrations assume users and roles tables exist. Customize via config/yauth.php:
      'tables' => [
          'users' => 'custom_users',
          'roles' => 'custom_roles',
      ],
      
  2. Permission Caching

    • Permissions are not cached by default. For high-traffic apps, implement caching in YAuthServiceProvider:
      YAuth::setCacheDriver('redis');
      
  3. Case Sensitivity

    • Role/permission names are case-sensitive. Use constants or lowercase them consistently:
      define('ROLE_ADMIN', 'admin');
      YAuth::assignRole($userId, ROLE_ADMIN);
      
  4. Middleware Overhead

    • Avoid checking permissions in middleware for every request. Use middleware sparingly (e.g., only for protected routes).

Debugging Tips

  1. Log Permission Checks Override the facade to log checks:

    YAuth::setLogger(function ($message) {
        \Log::debug($message);
    });
    
  2. Dump User Permissions Inspect a user’s roles/permissions:

    $userId = 1;
    $roles = YAuth::getRoles($userId);
    $permissions = YAuth::getPermissions($userId);
    dd(compact('roles', 'permissions'));
    
  3. Clear Cached Permissions If using caching, manually clear:

    php artisan cache:clear
    

Extension Points

  1. Custom Permission Logic Extend the permission resolver by binding a custom resolver in AppServiceProvider:

    YAuth::extend(function ($app) {
        return new CustomPermissionResolver();
    });
    
  2. Event Listeners Listen for role/permission changes:

    YAuth::roleAssigned(function ($userId, $role) {
        // Send notification, log, etc.
    });
    
  3. 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);
            }
        }
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui