|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Terranet\Administrator\Console; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
|
|
|
|
|
7
|
|
|
use Illuminate\Contracts\Hashing\Hasher; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
|
|
|
|
|
9
|
|
|
use Terranet\Administrator\Traits\SessionGuardHelper; |
|
10
|
|
|
|
|
11
|
|
|
class AdministratorCreateCommand extends Command |
|
12
|
|
|
{ |
|
13
|
|
|
use SessionGuardHelper; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The name and signature of the console command. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $signature = 'administrator:create'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The console command description. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $description = 'Create new administrator user.'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Hasher |
|
31
|
|
|
*/ |
|
32
|
|
|
private $hasher; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Create a new command instance. |
|
36
|
|
|
* |
|
37
|
|
|
* @param Hasher $hasher |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(Hasher $hasher) |
|
40
|
|
|
{ |
|
41
|
|
|
parent::__construct(); |
|
42
|
|
|
|
|
43
|
|
|
$this->hasher = $hasher; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Execute the console command. |
|
48
|
|
|
*/ |
|
49
|
|
|
public function handle() |
|
50
|
|
|
{ |
|
51
|
|
|
$name = $this->ask('Name', 'Administrator'); |
|
52
|
|
|
$email = $this->ask('Email', '[email protected]'); |
|
53
|
|
|
$password = $this->ask('Password', 'secret'); |
|
54
|
|
|
|
|
55
|
|
|
$data = $this->prepareData($name, $email, $password); |
|
56
|
|
|
|
|
57
|
|
|
$this->tryUpdatingRole($this->createUserInstance($data)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Prepare administrator's data. |
|
62
|
|
|
* |
|
63
|
|
|
* @param $name |
|
64
|
|
|
* @param $email |
|
65
|
|
|
* @param $password |
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function prepareData($name, $email, $password) |
|
70
|
|
|
{ |
|
71
|
|
|
$data = compact('name', 'email', 'password'); |
|
72
|
|
|
|
|
73
|
|
|
if (app('scaffold.config')->get('manage_passwords')) { |
|
|
|
|
|
|
74
|
|
|
$data['password'] = $this->hasher->make($data['password']); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $data; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Try to update user's role to admin if column 'role' exists. |
|
82
|
|
|
* |
|
83
|
|
|
* @param $instance |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function tryUpdatingRole($instance) |
|
88
|
|
|
{ |
|
89
|
|
|
try { |
|
90
|
|
|
return $instance->forceFill(['role' => 'admin'])->save(); |
|
91
|
|
|
} catch (Exception $e) { |
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Insert new user into database. |
|
98
|
|
|
* |
|
99
|
|
|
* @param $data |
|
100
|
|
|
* |
|
101
|
|
|
* @throws Exception |
|
102
|
|
|
* |
|
103
|
|
|
* @return Model |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function createUserInstance($data) |
|
106
|
|
|
{ |
|
107
|
|
|
$config = app('scaffold.config'); |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
if (!$model = $this->fetchModel($config)) { |
|
110
|
|
|
throw new Exception('Could not find a model to create user.'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
try { |
|
114
|
|
|
$model::unguard(); |
|
115
|
|
|
|
|
116
|
|
|
return (new $model())->create($this->dataWithId($data)); |
|
117
|
|
|
} catch (Exception $e) { |
|
118
|
|
|
return (new $model())->create($data); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Merge Administrator's data with an ID. |
|
124
|
|
|
* |
|
125
|
|
|
* @param $data |
|
126
|
|
|
* |
|
127
|
|
|
* @return array |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function dataWithId($data) |
|
130
|
|
|
{ |
|
131
|
|
|
return array_merge(['id' => 1], $data); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths