Skip to content
This repository was archived by the owner on May 19, 2020. It is now read-only.

Commit 9a01263

Browse files
committed
Improved gate check and added 5.8 support
1 parent ac95418 commit 9a01263

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"email": "shea.lewis89@gmail.com"
99
}],
1010
"require": {
11-
"illuminate/support": "5.7.*"
11+
"illuminate/support": "5.7.*|5.8.*"
1212
},
1313
"autoload": {
1414
"psr-4": {

src/ShinobiServiceProvider.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Caffeinated\Shinobi;
44

5-
use Gate;
6-
use Blade;
5+
use Exception;
6+
use Illuminate\Support\Facades\Gate;
77
use Caffeinated\Shinobi\Models\Role;
8+
use Illuminate\Support\Facades\Blade;
89
use Illuminate\Foundation\Application;
910
use Illuminate\Support\Facades\Schema;
1011
use Illuminate\Support\ServiceProvider;
@@ -53,13 +54,17 @@ public function register()
5354
*/
5455
protected function registerGates()
5556
{
56-
if (Schema::hasTable('permissions')) {
57-
Shinobi::permission()->with('roles')->get()->map(function($permission) {
58-
Gate::define($permission->slug, function($user) use ($permission) {
59-
return $user->hasPermissionTo($permission);
60-
});
61-
});
62-
}
57+
Gate::before(function($user, $permission) {
58+
try {
59+
if (method_exists($user, 'hasPermissionTo')) {
60+
$permission = Shinobi::permission()->where('slug', $permission)->firstOrFail();
61+
62+
return $user->hasPermissionTo($permission) ?: null;
63+
}
64+
} catch (Exception $e) {
65+
//
66+
}
67+
});
6368
}
6469

6570
/**

0 commit comments

Comments
 (0)