|
1
|
|
|
<?php namespace Lanin\Laravel\SetupWizard\Commands\Steps; |
|
2
|
|
|
|
|
3
|
|
|
class CreateUser extends AbstractStep |
|
4
|
|
|
{ |
|
5
|
|
|
/** |
|
6
|
|
|
* Return command prompt text. |
|
7
|
|
|
* |
|
8
|
|
|
* @return string |
|
9
|
|
|
*/ |
|
10
|
2 |
|
public function prompt() |
|
11
|
|
|
{ |
|
12
|
2 |
|
return 'Create new user?'; |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Prepare step data. |
|
17
|
|
|
* |
|
18
|
|
|
* @return mixed |
|
19
|
|
|
*/ |
|
20
|
2 |
|
public function prepare() |
|
21
|
|
|
{ |
|
22
|
2 |
|
$result = []; |
|
23
|
2 |
|
foreach (config('setup.create_user.fields') as $column => $title) |
|
24
|
|
|
{ |
|
25
|
2 |
|
$result[$column] = $this->command->ask($title); |
|
26
|
|
|
|
|
27
|
2 |
|
if ($column == config('setup.create_user.password_field')) |
|
28
|
2 |
|
{ |
|
29
|
2 |
|
$result[$column] = bcrypt($result[$column]); |
|
30
|
2 |
|
} |
|
31
|
2 |
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
$result['__table'] = $this->getTable(config('setup.create_user.table')); |
|
34
|
|
|
|
|
35
|
2 |
|
return $result; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Preview results. |
|
40
|
|
|
* |
|
41
|
|
|
* @param mixed $results |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function preview($results) |
|
45
|
|
|
{ |
|
46
|
2 |
|
$table = $results['__table']; |
|
47
|
2 |
|
unset($results['__table']); |
|
48
|
|
|
|
|
49
|
2 |
|
$this->command->info('I will insert this values into <comment>' . $table . '</comment> table'); |
|
50
|
2 |
|
$this->command->table(['column', 'value'], $this->arrayToTable($results)); |
|
51
|
2 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Finish step. |
|
55
|
|
|
* |
|
56
|
|
|
* @param mixed $results |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
2 |
|
public function finish($results) |
|
60
|
|
|
{ |
|
61
|
2 |
|
$table = $results['__table']; |
|
62
|
2 |
|
unset($results['__table']); |
|
63
|
|
|
|
|
64
|
2 |
|
return \DB::table($table)->insert($results); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get users table name. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $table |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
6 |
|
protected function getTable($table = '') |
|
74
|
|
|
{ |
|
75
|
6 |
|
if (empty($table)) |
|
76
|
6 |
|
{ |
|
77
|
4 |
|
switch (config('auth.driver')) |
|
78
|
|
|
{ |
|
79
|
4 |
|
case 'eloquent': |
|
80
|
2 |
|
$table = $this->getTableByModelClass(config('auth.model')); |
|
81
|
2 |
|
break; |
|
82
|
2 |
|
case 'database': |
|
83
|
2 |
|
default: |
|
84
|
2 |
|
$table = config('auth.table'); |
|
85
|
2 |
|
break; |
|
86
|
4 |
|
} |
|
87
|
4 |
|
} |
|
88
|
|
|
|
|
89
|
6 |
|
return $table; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Resolve user's model and get associated table. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $model |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
2 |
|
protected function getTableByModelClass($model) |
|
99
|
|
|
{ |
|
100
|
2 |
|
$model = new $model(); |
|
101
|
2 |
|
$table = $model->getTable(); |
|
102
|
|
|
|
|
103
|
2 |
|
return $table; |
|
104
|
|
|
} |
|
105
|
|
|
} |