1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Fort\Console\Commands; |
6
|
|
|
|
7
|
|
|
use Carbon\Carbon; |
8
|
|
|
use Illuminate\Console\Command; |
9
|
|
|
use Rinvex\Support\Traits\SeederHelper; |
10
|
|
|
|
11
|
|
|
class SeedCommand extends Command |
12
|
|
|
{ |
13
|
|
|
use SeederHelper; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The name and signature of the console command. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $signature = 'rinvex:seed:fort'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command description. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $description = 'Seed Rinvex Fort Data.'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Execute the console command. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function handle() |
35
|
|
|
{ |
36
|
|
|
$this->warn($this->description); |
37
|
|
|
|
38
|
|
|
if ($this->ensureExistingDatabaseTables('rinvex/fort')) { |
39
|
|
|
$this->seedResources(app('rinvex.fort.ability'), realpath(__DIR__.'/../../../resources/data/abilities.json'), ['name', 'description', 'policy']); |
|
|
|
|
40
|
|
|
$this->seedResources(app('rinvex.fort.role'), realpath(__DIR__.'/../../../resources/data/roles.json'), ['name', 'description'], function () { |
|
|
|
|
41
|
|
|
// Grant abilities to roles |
42
|
|
|
app('rinvex.fort.role')->where('slug', 'admin')->first()->grantAbilities('superadmin', 'global'); |
43
|
|
|
}); |
44
|
|
|
$this->seedUsers(); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Seed default users. |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
protected function seedUsers() |
54
|
|
|
{ |
55
|
|
|
$this->warn('Seeding Users:'); |
56
|
|
|
|
57
|
|
|
$user = [ |
58
|
|
|
'username' => 'Fort', |
59
|
|
|
'email' => '[email protected]', |
60
|
|
|
'email_verified' => true, |
61
|
|
|
'is_active' => true, |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
$user = tap(app('rinvex.fort.user')->firstOrNew($user)->fill([ |
65
|
|
|
'email_verified_at' => Carbon::now(), |
66
|
|
|
'remember_token' => str_random(10), |
67
|
|
|
'password' => $password = str_random(), |
68
|
|
|
]), function ($instance) { |
69
|
|
|
$instance->save(); |
70
|
|
|
}); |
71
|
|
|
|
72
|
|
|
// Assign roles to users |
73
|
|
|
$user->assignRoles('admin'); |
74
|
|
|
|
75
|
|
|
$this->table(['Username', 'Password'], [['username' => $user['username'], 'password' => $password]]); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.