Skip to content

Commit 07a2500

Browse files
authored
Merge pull request #9 from asiaCoder/master
multi roles on routes
2 parents c50e3c7 + 724f4d4 commit 07a2500

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ You can put your routes for a role in a `Route` group like this:
3737
Route::group([
3838
'middleware' => 'auth.role',
3939
'prefix' => ...,
40-
'role' => 'admin',
40+
'role' => ['admin', 'customer'],
4141
...
4242
],function (){
4343
...

src/Commands/PermissionsGenerate.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,38 @@ public function __construct()
4141
public function handle()
4242
{
4343
$options = $this->options();
44-
if ($options['fresh']){
44+
45+
if ($options['fresh']) {
4546
Permission::query()->delete();
4647
Role::query()->delete();
4748
}
49+
4850
$routes = Route::getRoutes()->getRoutes();
4951

5052
foreach ($routes as $route){
5153
$action = $route->getActionname();
52-
if($action == "Closure"){
54+
55+
if ($action == "Closure") {
5356
continue;
5457
}
55-
$name = $route->getName();
5658

59+
$name = $route->getName();
5760
$permission = Permission::firstOrCreate(['action'=>$action, 'name'=>$name]);
5861

59-
if(key_exists('role', $route->action)){
60-
$role = $route->action['role'];
61-
$role = Role::firstOrCreate(['name'=> $role]);
62+
if (key_exists('role', $route->action)) {
63+
$roles = $route->action['role'];
64+
65+
if (is_array($roles)) {
66+
foreach ($roles as $role) {
67+
$role = Role::firstOrCreate(['name'=> $role]);
68+
69+
$role->permissions()->syncWithoutDetaching($permission->id);
70+
}
71+
} else {
72+
$role = Role::firstOrCreate(['name'=> $roles]);
6273

63-
$role->permissions()->syncWithoutDetaching($permission->id);
74+
$role->permissions()->syncWithoutDetaching($permission->id);
75+
}
6476
}
6577
}
6678
}

0 commit comments

Comments
 (0)