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