|
1
|
|
|
<?php namespace Arcanesoft\Auth\Seeds; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanesoft\Auth\Models\Permission; |
|
4
|
|
|
use Arcanesoft\Auth\Models\Role; |
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class RolesSeeder |
|
10
|
|
|
* |
|
11
|
|
|
* @package Arcanesoft\Auth\Seeds |
|
12
|
|
|
* @author ARCANEDEV <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class RolesSeeder extends Seeder |
|
15
|
|
|
{ |
|
16
|
|
|
/* ----------------------------------------------------------------- |
|
17
|
|
|
| Main Methods |
|
18
|
|
|
| ----------------------------------------------------------------- |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Seed roles. |
|
23
|
|
|
* |
|
24
|
|
|
* @param array $roles |
|
25
|
|
|
*/ |
|
26
|
|
|
public function seed(array $roles) |
|
27
|
|
|
{ |
|
28
|
|
|
$roles = $this->prepareRoles($roles); |
|
29
|
|
|
|
|
30
|
|
|
Role::insert($roles); |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
$this->syncAdminRole(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/* ----------------------------------------------------------------- |
|
36
|
|
|
| Other Methods |
|
37
|
|
|
| ----------------------------------------------------------------- |
|
38
|
|
|
*/ |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Prepare roles to seed. |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $roles |
|
44
|
|
|
* |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function prepareRoles(array $roles) |
|
48
|
|
|
{ |
|
49
|
|
|
$now = Carbon::now(); |
|
50
|
|
|
|
|
51
|
|
|
foreach ($roles as $key => $role) { |
|
52
|
|
|
$roles[$key]['slug'] = $this->slugify($role['name']); |
|
53
|
|
|
$roles[$key]['is_active'] = isset($role['is_active']) ? $role['is_active'] : true; |
|
54
|
|
|
$roles[$key]['is_locked'] = isset($role['is_locked']) ? $role['is_locked'] : true; |
|
55
|
|
|
$roles[$key]['created_at'] = $now; |
|
56
|
|
|
$roles[$key]['updated_at'] = $now; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $roles; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Sync the admin role with all permissions. |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function syncAdminRole() |
|
66
|
|
|
{ |
|
67
|
|
|
/** @var \Arcanesoft\Auth\Models\Role $admin */ |
|
68
|
|
|
$admin = Role::admin()->first(); |
|
69
|
|
|
$admin->permissions()->sync( |
|
70
|
|
|
Permission::all()->pluck('id')->toArray() |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Slugify the value. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $value |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function slugify($value) |
|
82
|
|
|
{ |
|
83
|
|
|
return Str::slug($value, config('arcanesoft.auth.roles.slug-separator', '-')); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Sync the roles. |
|
88
|
|
|
* |
|
89
|
|
|
* @param array $roles |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function syncRoles(array $roles) |
|
92
|
|
|
{ |
|
93
|
|
|
/** @var \Illuminate\Database\Eloquent\Collection $permissions */ |
|
94
|
|
|
$permissions = Permission::all(); |
|
95
|
|
|
|
|
96
|
|
|
foreach ($roles as $roleSlug => $permissionSlug) { |
|
97
|
|
|
/** @var \Arcanesoft\Auth\Models\Role $role */ |
|
98
|
|
|
$role = Role::where('slug', $roleSlug)->first(); |
|
99
|
|
|
$filtered = $permissions->filter(function (Permission $permission) use ($permissionSlug) { |
|
100
|
|
|
return Str::startsWith($permission->slug, $permissionSlug); |
|
101
|
|
|
}); |
|
102
|
|
|
|
|
103
|
|
|
$role->permissions()->sync( |
|
104
|
|
|
$filtered->pluck('id')->toArray() |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.