1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MultiTenantLaravel\App\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
|
7
|
|
|
class SyncRolesPermissionsFeatures extends Command |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The name and signature of the console command. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $signature = 'tenant:sync'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The console command description. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $description = 'Sync the multi-tenant roles, permissions & features'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Execute the console command. |
25
|
|
|
* |
26
|
|
|
* @return mixed |
27
|
|
|
*/ |
28
|
|
|
public function handle() |
29
|
|
|
{ |
30
|
|
|
// Sync all roles |
31
|
|
|
$this->syncRoles(); |
32
|
|
|
|
33
|
|
|
// Sync all features |
34
|
|
|
$this->syncFeatures(); |
35
|
|
|
|
36
|
|
|
// Sync all permissions |
37
|
|
|
$this->syncPermissions(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Sync the roles for the project. |
42
|
|
|
*/ |
43
|
|
|
private function syncRoles() |
44
|
|
|
{ |
45
|
|
|
// Make sure we have a super user role |
46
|
|
|
config('multi-tenant.role_class')::firstOrCreate(['name' => 'super', 'label' => 'Super']); |
47
|
|
|
|
48
|
|
|
foreach(config('multi-tenant.roles') as $key => $label) { |
49
|
|
|
config('multi-tenant.role_class')::firstOrCreate(['name' => $key, 'label' => $label]); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Sync the features for the project. |
55
|
|
|
*/ |
56
|
|
|
private function syncFeatures() |
57
|
|
|
{ |
58
|
|
|
foreach(config('multi-tenant.features') as $key => $feature) { |
59
|
|
|
config('multi-tenant.feature_class')::firstOrCreate(['name' => $key, 'label' => $feature['label'], 'model' => $feature['model']]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Sync the permissions for the project. |
65
|
|
|
*/ |
66
|
|
|
private function syncPermissions() |
67
|
|
|
{ |
68
|
|
|
// Loop through all the features, then loop through the permission types |
69
|
|
|
foreach (config('multi-tenant.feature_class')::get() as $feature) { |
70
|
|
|
// Loop through the permission types and create a |
71
|
|
|
// permission for each permission type on this feature |
72
|
|
|
foreach (config('multi-tenant.permission_types') as $permission_type) { |
73
|
|
|
config('multi-tenant.permission_class')::firstOrCreate([ |
74
|
|
|
'name' => $feature->name . '_' . $permission_type, |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|