From 1bfc97e36a153a0ade660f7e402be4f8a1940192 Mon Sep 17 00:00:00 2001 From: Dennis Kiptugen Date: Fri, 18 Nov 2022 12:35:37 +0300 Subject: [PATCH] modified: README.md added additional documentation modified: composer.json contributors modified: src/Commands/PermissionsGenerate.php modified: src/Exceptions/UnauthorizedException.php fixed typo modified: src/LaravelPermissionServiceProvider.php added blade directive modified: src/Middleware/AuthRoles.php new file: src/Models/PermissionRole.php created new model modified: src/Traits/HasRoles.php added relationship --- .DS_Store | Bin 0 -> 6148 bytes README.md | 16 ++++++++++++++++ composer.json | 4 ++++ src/Commands/PermissionsGenerate.php | 6 ++++-- src/Exceptions/UnauthorizedException.php | 2 +- src/LaravelPermissionServiceProvider.php | 8 ++++++++ src/Middleware/AuthRoles.php | 7 +++++-- src/Models/PermissionRole.php | 21 +++++++++++++++++++++ src/Traits/HasRoles.php | 17 +++++++++++------ 9 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 .DS_Store create mode 100644 src/Models/PermissionRole.php diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..69464a264be9ca71277466c5382c25030aae3c73 GIT binary patch literal 6148 zcmeHK%}T>S5Z-O8O({YSiXIod7EDEG#Y>3w1&ruHr6#6mFlI}VnnNk%sxRc5_&m<+ zZotx>MeGdhe)GGV{UH0p7~|e7>N93D#w=)v9F;YK?zN$YNk-&2Mm`M_8G`j0PEG8u z1AcpxlQ=IN=ZiO*t*!00)wa6Uz5gsrzX*z1?giN`T31pgVXX(@bv!A@ z_U^e%iy%%XnJS2*38dWJ#Azf;PtMXPSGB$lSRJb~w)f}r{&3I}1830_^V4IeCx(NQ z#iC>F9Uh%sj$e}3RK9CAIgqVn$6yT~pqRzH`B|FC^a(t5cAZ5?3=jjv05P!P448w# z>TWm>w0vTK7+7Th_XhzD(KT3UR9gphczwor3lRl$d`ln-gRa3!BSb*BE(O%3+&nS3 zE(gCbd9J}qqb_G$%?#t1nd`?3SF?j(sC33%jnoqZ#K1BGO>NqE{$Ic^v-Xj{TtXHx zKn(md26$`ak33kEIa^njhi9#T_5ck9^D0z8Kwr89fPwqSfePxlKppa2gOx@c1^ucV PkS+p>5bB75Utr)H1(r!w literal 0 HcmV?d00001 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'); + } }