|
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\Facades\Config; |
|
10
|
|
|
use Helldar\Roles\Models\Permission; |
|
11
|
|
|
use Helldar\Roles\Traits\Searchable; |
|
12
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
|
13
|
|
|
use Illuminate\Support\Facades\Blade; |
|
14
|
|
|
use Illuminate\Support\Facades\Gate; |
|
15
|
|
|
|
|
16
|
|
|
class ServiceProvider extends \Illuminate\Support\ServiceProvider |
|
17
|
|
|
{ |
|
18
|
|
|
use Searchable; |
|
19
|
|
|
|
|
20
|
|
|
public function boot() |
|
21
|
|
|
{ |
|
22
|
|
|
$this->publishConfig(); |
|
23
|
|
|
$this->publishMigrations(); |
|
24
|
|
|
$this->bootCommands(); |
|
25
|
|
|
|
|
26
|
|
|
$this->blade(); |
|
27
|
|
|
$this->can(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function register() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->mergeConfigFrom(__DIR__ . '/../config/roles.php', Config::name()); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function publishMigrations() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->publishes([ |
|
38
|
|
|
__DIR__ . '/../database/migrations' => database_path('migrations'), |
|
39
|
|
|
], 'migrations'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function publishConfig() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->publishes([ |
|
45
|
|
|
__DIR__ . '/../config/roles.php' => config_path(Config::filename()), |
|
46
|
|
|
], 'config'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function blade() |
|
50
|
|
|
{ |
|
51
|
|
|
if (! Config::useBlade()) { |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/* Role */ |
|
56
|
|
|
Blade::directive('role', function ($role) { |
|
57
|
|
|
return "<?php if(\auth()->check() && \auth()->user()->hasRole($role)) { ?>"; |
|
58
|
|
|
}); |
|
59
|
|
|
|
|
60
|
|
|
Blade::directive('endrole', function () { |
|
61
|
|
|
return '<?php } ?>'; |
|
62
|
|
|
}); |
|
63
|
|
|
|
|
64
|
|
|
/* Roles */ |
|
65
|
|
|
Blade::directive('role', function ($roles) { |
|
66
|
|
|
return "<?php if(\auth()->check() && \auth()->user()->hasRoles($roles)) { ?>"; |
|
67
|
|
|
}); |
|
68
|
|
|
|
|
69
|
|
|
Blade::directive('endroles', function () { |
|
70
|
|
|
return '<?php } ?>'; |
|
71
|
|
|
}); |
|
72
|
|
|
|
|
73
|
|
|
/* Permission */ |
|
74
|
|
|
Blade::directive('permission', function ($permission) { |
|
75
|
|
|
return "<?php if(\auth()->check() && \auth()->user()->hasPermission($permission)) ?>"; |
|
76
|
|
|
}); |
|
77
|
|
|
|
|
78
|
|
|
Blade::directive('endpermission', function () { |
|
79
|
|
|
return '<?php } ?>'; |
|
80
|
|
|
}); |
|
81
|
|
|
|
|
82
|
|
|
/* Permissions */ |
|
83
|
|
|
Blade::directive('permissions', function ($permissions) { |
|
84
|
|
|
return "<?php if(\auth()->check() && \auth()->user()->hasPermissions($permissions)) ?>"; |
|
85
|
|
|
}); |
|
86
|
|
|
|
|
87
|
|
|
Blade::directive('endpermissions', function () { |
|
88
|
|
|
return '<?php } ?>'; |
|
89
|
|
|
}); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function can() |
|
93
|
|
|
{ |
|
94
|
|
|
if (! Config::useCanDirective()) { |
|
95
|
|
|
return; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
Permission::query() |
|
99
|
|
|
->get(['slug']) |
|
100
|
|
|
->each(function (Permission $permission) { |
|
101
|
|
|
Gate::define($permission->slug, function (Authenticatable $user) use ($permission) { |
|
102
|
|
|
return $user->hasPermission($permission); |
|
|
|
|
|
|
103
|
|
|
}); |
|
104
|
|
|
}); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
protected function bootCommands() |
|
108
|
|
|
{ |
|
109
|
|
|
$this->commands([ |
|
110
|
|
|
PermissionCreate::class, |
|
111
|
|
|
PermissionDelete::class, |
|
112
|
|
|
RoleCreate::class, |
|
113
|
|
|
RoleDelete::class, |
|
114
|
|
|
]); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|