andrey-helldar /
laravel-roles
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Helldar\Roles; |
||
| 4 | |||
| 5 | use Helldar\Roles\Console\PermissionCreate; |
||
| 6 | use Helldar\Roles\Console\PermissionDelete; |
||
| 7 | use Helldar\Roles\Console\RoleCreate; |
||
| 8 | use Helldar\Roles\Console\RoleDelete; |
||
| 9 | use Helldar\Roles\Constants\Tables; |
||
| 10 | use Helldar\Roles\Facades\Config; |
||
| 11 | use Helldar\Roles\Models\Permission; |
||
| 12 | use Helldar\Roles\Traits\Searchable; |
||
| 13 | use Illuminate\Contracts\Auth\Authenticatable; |
||
| 14 | use Illuminate\Support\Facades\Blade; |
||
| 15 | use Illuminate\Support\Facades\Cache; |
||
| 16 | use Illuminate\Support\Facades\Gate; |
||
| 17 | use Illuminate\Support\Facades\Schema; |
||
| 18 | |||
| 19 | class ServiceProvider extends \Illuminate\Support\ServiceProvider |
||
| 20 | { |
||
| 21 | use Searchable; |
||
| 22 | |||
| 23 | public function boot() |
||
| 24 | { |
||
| 25 | $this->publishConfig(); |
||
| 26 | $this->publishMigrations(); |
||
| 27 | $this->bootCommands(); |
||
| 28 | |||
| 29 | $this->blade(); |
||
| 30 | $this->can(); |
||
| 31 | } |
||
| 32 | |||
| 33 | public function register() |
||
| 34 | { |
||
| 35 | $this->mergeConfigFrom(__DIR__ . '/../config/roles.php', Config::name()); |
||
| 36 | } |
||
| 37 | |||
| 38 | protected function publishMigrations() |
||
| 39 | { |
||
| 40 | $this->publishes([ |
||
| 41 | __DIR__ . '/../database/migrations' => database_path('migrations'), |
||
| 42 | ], 'migrations'); |
||
| 43 | } |
||
| 44 | |||
| 45 | protected function publishConfig() |
||
| 46 | { |
||
| 47 | $this->publishes([ |
||
| 48 | __DIR__ . '/../config/roles.php' => config_path(Config::filename()), |
||
| 49 | ], 'config'); |
||
| 50 | } |
||
| 51 | |||
| 52 | protected function blade() |
||
| 53 | { |
||
| 54 | if ($this->doesntExistPermissionsTable() || ! Config::useBlade()) { |
||
| 55 | return; |
||
| 56 | } |
||
| 57 | |||
| 58 | /* Role */ |
||
| 59 | Blade::directive('role', function ($role) { |
||
| 60 | return "<?php if(\auth()->check() && \auth()->user()->hasRole($role)): ?>"; |
||
| 61 | }); |
||
| 62 | |||
| 63 | Blade::directive('endrole', function () { |
||
| 64 | return '<?php endif; ?>'; |
||
| 65 | }); |
||
| 66 | |||
| 67 | /* Roles */ |
||
| 68 | Blade::directive('roles', function ($roles) { |
||
| 69 | return "<?php if(\auth()->check() && \auth()->user()->hasRoles($roles)): ?>"; |
||
| 70 | }); |
||
| 71 | |||
| 72 | Blade::directive('endroles', function () { |
||
| 73 | return '<?php endif; ?>'; |
||
| 74 | }); |
||
| 75 | |||
| 76 | /* Permission */ |
||
| 77 | Blade::directive('permission', function ($permission) { |
||
| 78 | return "<?php if(\auth()->check() && \auth()->user()->hasPermission($permission)): ?>"; |
||
| 79 | }); |
||
| 80 | |||
| 81 | Blade::directive('endpermission', function () { |
||
| 82 | return '<?php endif; ?>'; |
||
| 83 | }); |
||
| 84 | |||
| 85 | /* Permissions */ |
||
| 86 | Blade::directive('permissions', function ($permissions) { |
||
| 87 | return "<?php if(\auth()->check() && \auth()->user()->hasPermissions($permissions)): ?>"; |
||
| 88 | }); |
||
| 89 | |||
| 90 | Blade::directive('endpermissions', function () { |
||
| 91 | return '<?php endif; ?>'; |
||
| 92 | }); |
||
| 93 | } |
||
| 94 | |||
| 95 | protected function can() |
||
| 96 | { |
||
| 97 | if ($this->doesntExistPermissionsTable() || ! Config::useCanDirective()) { |
||
| 98 | return; |
||
| 99 | } |
||
| 100 | |||
| 101 | foreach ($this->getPermissions() as $permission) { |
||
| 102 | Gate::define($permission, function (Authenticatable $user) use ($permission) { |
||
| 103 | return $user->hasPermission($permission); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 104 | }); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | protected function bootCommands() |
||
| 109 | { |
||
| 110 | $this->commands([ |
||
| 111 | PermissionCreate::class, |
||
| 112 | PermissionDelete::class, |
||
| 113 | RoleCreate::class, |
||
| 114 | RoleDelete::class, |
||
| 115 | ]); |
||
| 116 | } |
||
| 117 | |||
| 118 | protected function getPermissions(): array |
||
| 119 | { |
||
| 120 | return Cache::remember('permissions-model', $this->ttl(), static function () { |
||
| 121 | return Permission::query() |
||
| 122 | ->get(['slug']) |
||
| 123 | ->pluck('slug') |
||
| 124 | ->toArray(); |
||
| 125 | }); |
||
| 126 | } |
||
| 127 | |||
| 128 | protected function doesntExistPermissionsTable(): bool |
||
| 129 | { |
||
| 130 | return Config::useCache() |
||
| 131 | ? Cache::remember(__FUNCTION__, $this->ttl(), function () { |
||
| 132 | return ! $this->existPermissionsTable(); |
||
| 133 | }) : ! $this->existPermissionsTable(); |
||
| 134 | } |
||
| 135 | |||
| 136 | protected function existPermissionsTable(): bool |
||
| 137 | { |
||
| 138 | return Schema::connection(Config::connection())->hasTable(Tables::PERMISSIONS); |
||
| 139 | } |
||
| 140 | |||
| 141 | protected function ttl(): ?int |
||
| 142 | { |
||
| 143 | return Config::cacheTtl(); |
||
| 144 | } |
||
| 145 | } |
||
| 146 |