diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..69464a2 Binary files /dev/null and b/.DS_Store differ diff --git a/README.md b/README.md index ca7125c..2b684af 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,17 @@ After that, you need to run the migration files: ``` $ php artisan migrate ``` +On User Model add +```php +use Amir\Traits\HasRoles; +class User extends Authenticatable +{ + use HasRoles; + + + +``` ### How to authorize user This package adds a `role_id` to the `users` table. Roles are stored in the `roles` table. You can assign a role to a user in your administrator panel or by creating a seed file. @@ -111,6 +121,12 @@ Also, you can use these options in combination: ``` $ php artisan permissions:clear --roles admin --tables permission_role ``` +On routes you can now +``` +@can_access('home.index') + Home +@endcan_access +``` ## About diff --git a/composer.json b/composer.json index 8eb4573..5f7584b 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,10 @@ { "name": "Amir Yousefi", "email": "amiryousefi.it@gmail.com" + }, + { + "name": "Dennis K Kiptugen", + "email": "dennis.kiptoo@caydeesoft.com" } ], "extra": { diff --git a/src/Commands/PermissionsGenerate.php b/src/Commands/PermissionsGenerate.php index 7acd870..0452d9e 100644 --- a/src/Commands/PermissionsGenerate.php +++ b/src/Commands/PermissionsGenerate.php @@ -52,8 +52,10 @@ public function handle() foreach ($routes as $route){ $action = $route->getActionname(); - if ($action == "Closure") { - continue; + if ($action == "Closure") + { + continue; + } } $name = $route->getName(); diff --git a/src/Exceptions/UnauthorizedException.php b/src/Exceptions/UnauthorizedException.php index d689a2f..04b0940 100644 --- a/src/Exceptions/UnauthorizedException.php +++ b/src/Exceptions/UnauthorizedException.php @@ -9,7 +9,7 @@ class UnauthorizedException extends HttpException public static function noPermission() { - return new static(403, 'User don\'t have permission', null, []); + return new static(403, 'User doesn\'t have permission', null, []); } } diff --git a/src/LaravelPermissionServiceProvider.php b/src/LaravelPermissionServiceProvider.php index 7ed6c49..ba525e5 100644 --- a/src/LaravelPermissionServiceProvider.php +++ b/src/LaravelPermissionServiceProvider.php @@ -3,6 +3,7 @@ use Amir\Permission\Middleware\AuthRoles; use Illuminate\Support\ServiceProvider; use Illuminate\Routing\Router; +use Illuminate\Support\Facades\Auth; class LaravelPermissionServiceProvider extends ServiceProvider { public function boot(Router $router) @@ -17,6 +18,13 @@ public function boot(Router $router) } $router->aliasMiddleware('auth.role', AuthRoles::class); + Blade::directive('can_access', function ($expression) { + return "<?php if ({Auth::user()->permission->contains('name',$expression)}) : ?>"; + }); + + Blade::directive('endcan_access', function ($expression) { + return '<?php endif; ?>'; + }); } public function register() { diff --git a/src/Middleware/AuthRoles.php b/src/Middleware/AuthRoles.php index c661912..7796e9e 100644 --- a/src/Middleware/AuthRoles.php +++ b/src/Middleware/AuthRoles.php @@ -29,7 +29,7 @@ public function handle($request, Closure $next) throw_if(!auth($authGuard)->check(), UnauthenticatedException::notLoggedIn()); - $action = $request->route()->getActionname(); + /*$action = $request->route()->getActionname(); $name = $request->route()->getActionname(); $role_id = auth($authGuard)->user()->role_id; @@ -39,7 +39,10 @@ public function handle($request, Closure $next) $query->orWhere('action', $action); })->whereHas('roles', function ($query) use($role_id){ $query->where('id',$role_id); - })->first(); + })->first();*/ + $action = $request->route()->getActionname(); + $name = $request->route()->getName(); + $permission = auth($authGuard)->user()->permission->where('name',$name)->where('action',$action); throw_if(is_null($permission), UnauthorizedException::noPermission()); diff --git a/src/Models/PermissionRole.php b/src/Models/PermissionRole.php new file mode 100644 index 0000000..f71ed4b --- /dev/null +++ b/src/Models/PermissionRole.php @@ -0,0 +1,21 @@ +belongsTo(Role::class); + } + public function permission() + { + return $this->belongsTo(Permission::class) ; + } + } diff --git a/src/Traits/HasRoles.php b/src/Traits/HasRoles.php index a1783be..0de738f 100644 --- a/src/Traits/HasRoles.php +++ b/src/Traits/HasRoles.php @@ -7,11 +7,16 @@ trait HasRoles { public function role() - { - return $this->belongsTo(Role::class); - } + { + return $this->belongsTo(Role::class); + } - public function getRoleNameAttribute(){ - return $this->role()->first()->name ?? null ; - } + public function getRoleNameAttribute() + { + return $this->role()->first()->name ?? null ; + } + public function permission() + { + return $this->hasManyThrough(Permission::class,Permission_Role::class,'role_id','id','role_id','permission_id'); + } }