Completed
Push — master ( b0b47a...abbd86 )
by Arman
15s queued 11s
created

BaseAuth::towStepVerification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 16
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.0.0
13
 */
14
15
namespace Quantum\Libraries\Auth;
16
17
use Quantum\Libraries\Mailer\Mailer;
18
19
/**
20
 * Trait AuthTools
21
 * @package Quantum\Libraries\Auth
22
 */
23
abstract class BaseAuth
24
{
25
26
    /**
27
     * User
28
     * @return mixed|null
29
     */
30
    protected abstract function user();
31
32
    /**
33
     * Check
34
     * @return bool
35
     */
36
    public function check()
37
    {
38
        return !is_null($this->user());
39
    }
40
41
    /**
42
     * Check Verification
43
     * @return bool
44
     */
45
    public function checkVerification()
46
    {
47
        if (isset($this->user()->verification_code) && !empty($this->user()->verification_code)){
48
            return true;
49
        }
50
        return false;
51
    }
52
53
    /**
54
     * Sign Up
55
     * @param array $userData
56
     * @param array|null $customData
57
     * @return mixed
58
     */
59
    public function signup(Mailer $mailer, $userData, $customData = null)
60
    {
61
        $activationToken = $this->generateToken();
62
63
        $userData[$this->keys['passwordKey']] = $this->hasher->hash($userData[$this->keys['passwordKey']]);
64
        $userData[$this->keys['activationTokenKey']] = $activationToken;
65
66
        $user = $this->authService->add($userData);
67
68
        $body = [
69
            'user' => $user,
70
            'activationToken' => $activationToken
71
        ];
72
73
        if ($customData) {
74
            $body = array_merge($body, $customData);
75
        }
76
77
        $this->sendMail($mailer, $user, $body);
78
79
        return $user;
80
    }
81
82
    /**
83
     * Activate
84
     * @param string $token
85
     */
86
    public function activate($token)
87
    {
88
        $this->authService->update(
89
                $this->keys['activationTokenKey'],
90
                $token, [$this->keys['activationTokenKey'] => '']
91
        );
92
    }
93
94
    /**
95
     * Forget
96
     * @param Mailer $mailer
97
     * @param string $email
98
     * @param string $template
99
     * @return string
100
     */
101
    public function forget(Mailer $mailer, $email)
102
    {
103
        $user = $this->authService->get($this->keys['usernameKey'], $email);
104
105
        $resetToken = $this->generateToken();
106
107
        $this->authService->update(
108
                $this->keys['usernameKey'],
109
                $email,
110
                [$this->keys['resetTokenKey'] => $resetToken]
111
        );
112
113
        $body = [
114
            'user' => $user,
115
            'resetToken' => $resetToken
116
        ];
117
118
        $this->sendMail($mailer, $user, $body);
119
120
        return $resetToken;
121
    }
122
123
    /**
124
     * Reset
125
     * @param string $token
126
     * @param string $password
127
     */
128
    public function reset($token, $password)
129
    {
130
        $user = $this->authService->get($this->keys['resetTokenKey'], $token);
131
132
        if (!$this->isActivated($user)) {
133
            $this->activate($token);
134
        }
135
136
        $this->authService->update(
137
                $this->keys['resetTokenKey'],
138
                $token,
139
                [$this->keys['passwordKey'] => $this->hasher->hash($password), $this->keys['resetTokenKey'] => '']
140
        );
141
    }
142
143
    /**
144
     * Filter Fields
145
     * @param array $user
146
     * @return mixed
147
     */
148
    protected function filterFields(array $user)
149
    {
150
        if (count($this->authService->getVisibleFields())) {
151
            foreach ($user as $key => $value) {
152
                if (!in_array($key, $this->authService->getVisibleFields())) {
153
                    unset($user[$key]);
154
                }
155
            }
156
        }
157
158
        return $user;
159
    }
160
161
    /**
162
     * Generate Token
163
     * @return string
164
     */
165
    protected function generateToken()
166
    {
167
        return base64_encode($this->hasher->hash(env('APP_KEY')));
168
    }
169
170
    /**
171
     * Is user account activated
172
     * @param mixed $user
173
     * @return bool
174
     */
175
    protected function isActivated($user)
176
    {
177
        return empty($user[$this->keys['activationTokenKey']]) ? true : false;
178
    }
179
180
    /**
181
     * Send email
182
     * @param Mailer $mailer
183
     * @param array $user
184
     * @param array $body
185
     */
186
    protected function sendMail(Mailer $mailer, array $user, array $body)
187
    {
188
        $fullName = (isset($user['firstname']) && isset($user['lastname'])) ? $user['firstname'] . ' ' . $user['lastname'] : '';
189
190
        $mailer->setFrom(config()->get('app_email'), config()->get('app_name'))
191
                ->setAddress($user[$this->keys['usernameKey']], $fullName)
192
                ->setBody($body)
193
                ->send();
194
    }
195
196
    /**
197
     * Tow Step Verification
198
     * @param array $user
199
     * @param Mailer $mailer
200
     * @return array $user
201
     */
202
203
    protected function towStepVerification($mailer, $user)
204
    {
205
        $body = [
206
            'user' => $user,
207
            'code' => random_number(6)
208
        ];
209
210
        $this->authService->update($this->keys['usernameKey'], $user[$this->keys['usernameKey']], [
211
            $this->keys['verificationCode'] => $body['code']
212
        ]);
213
214
        $user['verification_code'] = $body['code'];
215
216
        $this->sendMail($mailer, $user, $body);
217
218
        return $user;
219
    }
220
}
221