1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Microboard\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Facades\Validator; |
7
|
|
|
use Facades\Microboard\Factory; |
8
|
|
|
use Microboard\Models\Role; |
9
|
|
|
use Microboard\Models\User; |
10
|
|
|
|
11
|
|
|
class InstallCommand extends Command |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The name and signature of the console command. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $signature = 'microboard:install'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The console command description. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $description = 'It\'s publish the package assets, and migrate the database.'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The created admin. |
29
|
|
|
* |
30
|
|
|
* @var User |
31
|
|
|
*/ |
32
|
|
|
private $admin = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Execute the console command. |
36
|
|
|
* |
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
|
|
public function handle() |
40
|
|
|
{ |
41
|
|
|
$this->output->progressStart(5); |
42
|
|
|
$this->output->newLine(); |
43
|
|
|
|
44
|
|
|
// create storage link first |
45
|
|
|
$this->call('storage:link'); |
46
|
|
|
$this->output->progressAdvance(); |
47
|
|
|
$this->output->newLine(); |
48
|
|
|
|
49
|
|
|
// Publish the assets (config, routes file, css and js files, database migrations) |
50
|
|
|
$this->publishAssets(); |
51
|
|
|
$this->output->progressAdvance(); |
52
|
|
|
$this->output->newLine(); |
53
|
|
|
|
54
|
|
|
// Migrate the database |
55
|
|
|
$this->migrateDatabase(); |
56
|
|
|
$this->output->progressAdvance(); |
57
|
|
|
$this->output->newLine(); |
58
|
|
|
|
59
|
|
|
// Create admin |
60
|
|
|
if ($this->confirm('Create new admin?', false)) { |
61
|
|
|
$this->askToCreateAdmin(); |
62
|
|
|
} |
63
|
|
|
$this->output->progressAdvance(); |
64
|
|
|
$this->output->newLine(); |
65
|
|
|
|
66
|
|
|
// Create Permissions and roles |
67
|
|
|
$this->createRolesAndPermissions(); |
68
|
|
|
$this->output->progressAdvance(); |
69
|
|
|
$this->output->newLine(); |
70
|
|
|
|
71
|
|
|
$this->info('All done.'); |
72
|
|
|
$this->info('Do something awesome!'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function publishAssets() |
76
|
|
|
{ |
77
|
|
|
$this->call('vendor:publish', [ |
78
|
|
|
'--provider' => 'Microboard\\Providers\\MicroboardServiceProvider' |
79
|
|
|
]); |
80
|
|
|
$this->call('vendor:publish', [ |
81
|
|
|
'--provider' => 'Microboard\\Providers\\ViewServiceProvider' |
82
|
|
|
]); |
83
|
|
|
$this->info('All assets has been published'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function migrateDatabase() |
87
|
|
|
{ |
88
|
|
|
$this->call('migrate'); |
89
|
|
|
$this->info('Database has been migrated'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function askToCreateAdmin() |
93
|
|
|
{ |
94
|
|
|
$name = $this->ask('What is the admin name?'); |
95
|
|
|
$email = $this->ask('What is email?'); |
96
|
|
|
$password = $this->secret('What is password?'); |
97
|
|
|
$passwordConfirmation = $this->secret('Please confirm the password'); |
98
|
|
|
|
99
|
|
|
$validator = Validator::make([ |
100
|
|
|
'name' => $name, |
101
|
|
|
'email' => $email, |
102
|
|
|
'password' => $password, |
103
|
|
|
'password_confirmation' => $passwordConfirmation |
104
|
|
|
], [ |
105
|
|
|
'name' => ['required', 'string', 'min:2', 'max:200'], |
106
|
|
|
'email' => ['required', 'string', 'email', 'unique:users,email'], |
107
|
|
|
'password' => ['required', 'string', 'min:4', 'max:200', 'confirmed'], |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
if ($validator->fails()) { |
111
|
|
|
$this->error('Admin not created, See error messages below'); |
112
|
|
|
$errors = []; |
113
|
|
|
|
114
|
|
|
foreach (['name', 'email', 'password'] as $field) { |
115
|
|
|
if ($validator->errors()->has($field)) { |
116
|
|
|
$errors[] = [ucfirst($field), $validator->errors()->first($field)]; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$this->output->table(['Field', 'Message'], $errors); |
121
|
|
|
|
122
|
|
|
return $this->askToCreateAdmin(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$this->admin = User::create([ |
126
|
|
|
'name' => $name, |
127
|
|
|
'email' => $email, |
128
|
|
|
'password' => bcrypt($password) |
129
|
|
|
]); |
130
|
|
|
|
131
|
|
|
$this->info('Admin creates successfully'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private function createRolesAndPermissions() |
135
|
|
|
{ |
136
|
|
|
$adminRole = Role::updateOrCreate(['name' => 'admin', 'display_name' => 'Administrator']); |
137
|
|
|
Role::updateOrCreate(['name' => 'user', 'display_name' => 'Normal user']); |
138
|
|
|
|
139
|
|
|
$this->info('Default roles has created/updated successfully'); |
140
|
|
|
|
141
|
|
|
Factory::createResourcesPermissionsFor($adminRole, [ |
142
|
|
|
'dashboard' => ['viewAny'], |
143
|
|
|
'users' => [], |
144
|
|
|
'roles' => [] |
145
|
|
|
]); |
146
|
|
|
|
147
|
|
|
$this->info('Default permissions has created/updated successfully'); |
148
|
|
|
|
149
|
|
|
if ($this->admin) { |
150
|
|
|
$adminRole->users()->save($this->admin); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|