|
1
|
|
|
<?php namespace Arcanesoft\Auth\Seeds; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanesoft\Auth\Bases\Seeder; |
|
4
|
|
|
use Arcanesoft\Auth\Models\Permission; |
|
5
|
|
|
use Arcanesoft\Auth\Models\Role; |
|
6
|
|
|
use Carbon\Carbon; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class RolesSeeder |
|
11
|
|
|
* |
|
12
|
|
|
* @package Arcanesoft\Auth\Seeds |
|
13
|
|
|
* @author ARCANEDEV <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class RolesSeeder extends Seeder |
|
16
|
|
|
{ |
|
17
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
18
|
|
|
| Main Functions |
|
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 Functions |
|
37
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
38
|
|
|
*/ |
|
39
|
|
|
/** |
|
40
|
|
|
* Prepare roles to seed. |
|
41
|
|
|
* |
|
42
|
|
|
* @param array $roles |
|
43
|
|
|
* |
|
44
|
|
|
* @return array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function prepareRoles(array $roles) |
|
47
|
|
|
{ |
|
48
|
|
|
$now = Carbon::now(); |
|
49
|
|
|
|
|
50
|
|
|
foreach ($roles as $key => $role) { |
|
51
|
|
|
$roles[$key]['slug'] = $this->slugify($role['name']); |
|
52
|
|
|
$roles[$key]['is_active'] = isset($role['is_active']) ? $role['is_active'] : true; |
|
53
|
|
|
$roles[$key]['is_locked'] = isset($role['is_locked']) ? $role['is_locked'] : true; |
|
54
|
|
|
$roles[$key]['created_at'] = $now; |
|
55
|
|
|
$roles[$key]['updated_at'] = $now; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $roles; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Sync the admin role with all permissions. |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function syncAdminRole() |
|
65
|
|
|
{ |
|
66
|
|
|
/** @var \Arcanesoft\Auth\Models\Role $admin */ |
|
67
|
|
|
$admin = Role::admin()->first(); |
|
68
|
|
|
$admin->permissions()->sync( |
|
69
|
|
|
Permission::all()->pluck('id')->toArray() |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Slugify the value. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $value |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function slugify($value) |
|
81
|
|
|
{ |
|
82
|
|
|
return Str::slug($value, config('arcanesoft.auth.roles.slug-separator', '-')); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|