Issues (426)

src/Commands/CreateSuperAdmin.php (1 issue)

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