-
Notifications
You must be signed in to change notification settings - Fork 554
/
CreateSuperAdmin.php
151 lines (132 loc) · 3.8 KB
/
CreateSuperAdmin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
namespace A17\Twill\Commands;
use Illuminate\Config\Repository as Config;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Factory as ValidatorFactory;
use Carbon\Carbon;
class CreateSuperAdmin extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'twill:superadmin {email?} {password?}';
/**
* The console command description.
*
* @var string
*/
protected $description = "Create the superadmin account";
/**
* @var ValidatorFactory
*/
protected $validatorFactory;
/**
* @var Config
*/
protected $config;
public function __construct(ValidatorFactory $validatorFactory, Config $config)
{
parent::__construct();
$this->validatorFactory = $validatorFactory;
$this->config = $config;
}
/**
* Create super admin account.
*
* @return void
*/
public function handle()
{
$this->info("Let's create a superadmin account!");
$email = $this->setEmail();
$password = $this->setPassword();
$userModel = twillModel('user');
$user = new $userModel();
$user->fill([
'name' => "Admin",
'email' => $email,
'published' => true,
]);
if (config('twill.enabled.permissions-management')) {
$user->is_superadmin = true;
} else {
$user->role = 'SUPERADMIN';
}
$user->registered_at = Carbon::now();
$user->password = Hash::make($password);
if ($user->save()) {
$this->info('Your account has been created');
return;
}
$this->error('Failed creating user. Things you can check: Database permissions, run migrations');
}
/**
* Prompt user to enter email and validate it.
*
* @return string $email
*/
private function setEmail()
{
if (filled($email = $this->argument('email'))) {
return $email;
}
$email = $this->ask('Enter an email');
if ($this->validateEmail($email)) {
return $email;
} else {
$this->error("Your email is not valid");
return $this->setEmail();
}
}
/**
* Prompt user to enter password, confirm and validate it.
*
* @return string $password
*/
private function setPassword()
{
if (filled($password = $this->argument('password'))) {
return $password;
}
$password = $this->secret('Enter a password');
if ($this->validatePassword($password)) {
$confirmPassword = $this->secret('Confirm the password');
if ($password === $confirmPassword) {
return $password;
} else {
$this->error('Password does not match the confirm password');
return $this->setPassword();
}
} else {
$this->error("Your password is not valid, at least 6 characters");
return $this->setPassword();
}
}
/**
* Determine if the email address given valid.
*
* @param string $email
* @return boolean
*/
private function validateEmail($email)
{
return $this->validatorFactory->make(['email' => $email], [
'email' => 'required|email|max:255|unique:' . $this->config->get('twill.users_table'),
])->passes();
}
/**
* Determine if the password given valid.
*
* @param string $password
* @return boolean
*/
private function validatePassword($password)
{
return $this->validatorFactory->make(['password' => $password], [
'password' => 'required|min:6',
])->passes();
}
}