1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace jeremykenedy\LaravelRoles; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider; |
6
|
|
|
use jeremykenedy\LaravelRoles\App\Http\Middleware\VerifyLevel; |
7
|
|
|
use jeremykenedy\LaravelRoles\App\Http\Middleware\VerifyPermission; |
8
|
|
|
use jeremykenedy\LaravelRoles\App\Http\Middleware\VerifyRole; |
9
|
|
|
use jeremykenedy\LaravelRoles\Database\Seeds\DefaultConnectRelationshipsSeeder; |
10
|
|
|
use jeremykenedy\LaravelRoles\Database\Seeds\DefaultPermissionsTableSeeder; |
11
|
|
|
use jeremykenedy\LaravelRoles\Database\Seeds\DefaultRolesTableSeeder; |
12
|
|
|
use jeremykenedy\LaravelRoles\Database\Seeds\DefaultUsersTableSeeder; |
13
|
|
|
|
14
|
|
|
class RolesServiceProvider extends ServiceProvider |
15
|
|
|
{ |
16
|
|
|
private $_packageTag = 'laravelroles'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Indicates if loading of the provider is deferred. |
20
|
|
|
* |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
protected $defer = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Bootstrap any application services. |
27
|
|
|
* |
28
|
|
|
* @param \Illuminate\Routing\Router $router The router |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public function boot() |
33
|
|
|
{ |
34
|
|
|
$this->app['router']->aliasMiddleware('role', VerifyRole::class); |
35
|
|
|
$this->app['router']->aliasMiddleware('permission', VerifyPermission::class); |
36
|
|
|
$this->app['router']->aliasMiddleware('level', VerifyLevel::class); |
37
|
|
|
if (config('roles.rolesGuiEnabled')) { |
38
|
|
|
$this->loadRoutesFrom(__DIR__.'/routes/web.php'); |
39
|
|
|
} |
40
|
|
|
$this->loadTranslationsFrom(__DIR__.'/resources/lang/', $this->_packageTag); |
41
|
|
|
$this->registerBladeExtensions(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Register any application services. |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function register() |
50
|
|
|
{ |
51
|
|
|
$this->mergeConfigFrom(__DIR__.'/config/roles.php', 'roles'); |
52
|
|
|
$this->loadMigrations(); |
53
|
|
|
if (config('roles.rolesGuiEnabled')) { |
54
|
|
|
$this->loadViewsFrom(__DIR__.'/resources/views/', $this->_packageTag); |
55
|
|
|
} |
56
|
|
|
$this->publishFiles(); |
57
|
|
|
$this->loadSeedsFrom(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function loadMigrations() |
61
|
|
|
{ |
62
|
|
|
if (config('roles.defaultMigrations.enabled')) { |
63
|
|
|
$this->loadMigrationsFrom(__DIR__.'/Database/Migrations'); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Loads a seeds. |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
private function loadSeedsFrom() |
73
|
|
|
{ |
74
|
|
|
if (config('roles.defaultSeeds.PermissionsTableSeeder')) { |
75
|
|
|
$this->app['seed.handler']->register( |
76
|
|
|
DefaultPermissionsTableSeeder::class |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (config('roles.defaultSeeds.RolesTableSeeder')) { |
81
|
|
|
$this->app['seed.handler']->register( |
82
|
|
|
DefaultRolesTableSeeder::class |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (config('roles.defaultSeeds.ConnectRelationshipsSeeder')) { |
87
|
|
|
$this->app['seed.handler']->register( |
88
|
|
|
DefaultConnectRelationshipsSeeder::class |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (config('roles.defaultSeeds.UsersTableSeeder')) { |
93
|
|
|
$this->app['seed.handler']->register( |
94
|
|
|
DefaultUsersTableSeeder::class |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Publish files for package. |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
private function publishFiles() |
105
|
|
|
{ |
106
|
|
|
$publishTag = $this->_packageTag; |
107
|
|
|
|
108
|
|
|
$this->publishes([ |
109
|
|
|
__DIR__.'/config/roles.php' => config_path('roles.php'), |
110
|
|
|
], $publishTag.'-config'); |
111
|
|
|
|
112
|
|
|
$this->publishes([ |
113
|
|
|
__DIR__.'/Database/Migrations' => database_path('migrations'), |
114
|
|
|
], $publishTag.'-migrations'); |
115
|
|
|
|
116
|
|
|
$this->publishes([ |
117
|
|
|
__DIR__.'/Database/Seeds/publish' => database_path('seeds'), |
118
|
|
|
], $publishTag.'-seeds'); |
119
|
|
|
|
120
|
|
|
$this->publishes([ |
121
|
|
|
__DIR__.'/config/roles.php' => config_path('roles.php'), |
122
|
|
|
__DIR__.'/Database/Migrations' => database_path('migrations'), |
123
|
|
|
__DIR__.'/Database/Seeds/publish' => database_path('seeds'), |
124
|
|
|
], $publishTag); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Register Blade extensions. |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
|
protected function registerBladeExtensions() |
133
|
|
|
{ |
134
|
|
|
$blade = $this->app['view']->getEngineResolver()->resolve('blade')->getCompiler(); |
135
|
|
|
|
136
|
|
|
$blade->directive('role', function ($expression) { |
137
|
|
|
return "<?php if (Auth::check() && Auth::user()->hasRole({$expression})): ?>"; |
138
|
|
|
}); |
139
|
|
|
|
140
|
|
|
$blade->directive('endrole', function () { |
141
|
|
|
return '<?php endif; ?>'; |
142
|
|
|
}); |
143
|
|
|
|
144
|
|
|
$blade->directive('permission', function ($expression) { |
145
|
|
|
return "<?php if (Auth::check() && Auth::user()->hasPermission({$expression})): ?>"; |
146
|
|
|
}); |
147
|
|
|
|
148
|
|
|
$blade->directive('endpermission', function () { |
149
|
|
|
return '<?php endif; ?>'; |
150
|
|
|
}); |
151
|
|
|
|
152
|
|
|
$blade->directive('level', function ($expression) { |
153
|
|
|
$level = trim($expression, '()'); |
154
|
|
|
|
155
|
|
|
return "<?php if (Auth::check() && Auth::user()->level() >= {$level}): ?>"; |
156
|
|
|
}); |
157
|
|
|
|
158
|
|
|
$blade->directive('endlevel', function () { |
159
|
|
|
return '<?php endif; ?>'; |
160
|
|
|
}); |
161
|
|
|
|
162
|
|
|
$blade->directive('allowed', function ($expression) { |
163
|
|
|
return "<?php if (Auth::check() && Auth::user()->allowed({$expression})): ?>"; |
164
|
|
|
}); |
165
|
|
|
|
166
|
|
|
$blade->directive('endallowed', function () { |
167
|
|
|
return '<?php endif; ?>'; |
168
|
|
|
}); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|