Completed
Push — master ( 884021...8237b9 )
by Charles
05:43
created

Registration::verifyUsernameOrEmail()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
3
namespace yrc\api\forms;
4
5
use Base32\Base32;
6
use Yii;
7
8
/**
9
 * @class Registration
10
 * Registration form
11
 */
12
abstract class Registration extends \yii\base\Model
13
{
14
    /**
15
     * The email
16
     * @var string $email
17
     */
18
    public $email;
19
20
    /**
21
     * The username
22
     * @var string $username
23
     */
24
    public $username;
25
26
    /**
27
     * The password
28
     * @var string $password
29
     */
30
    public $password;
31
32
    /**
33
     * The password repeated
34
     * @var string $password_verify
35
     */
36
    public $password_verify;
37
38
    /**
39
     * Validation rules
40
     * @return array
41
     */
42
    public function rules()
43
    {
44
        return [
45
            [['email', 'password', 'password_verify', 'username'], 'required'],
46
            [['email'], 'email'],
47
            [['email'], 'verifyUsernameOrEmail'],
48
            [['username'], 'verifyUsernameOrEmail'],
49
            [['password', 'password_verify'], 'string', 'length' => [8, 100]],
50
            [['username'], 'string', 'length' => [1, 100]],
51
            [['password_verify'], 'compare', 'compareAttribute' => 'password']
52
        ];
53
    }
54
55
    /**
56
     * Verifies the username
57
     * @param string $attribute
58
     * @param array $params
59
     */
60
    public function verifyUsernameOrEmail($attribute, $params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
        if (!$this->hasErrors()) {
63
            $user = new Yii::$app->yrc->userClass;
64
            $user->$attribute = $this->$attribute;
65
66
            $user->validate([$attribute]);
67
68
            if ($user->hasErrors($attribute)) {
69
                $this->addError($attribute, $user->getErrors($attribute));
70
            }
71
        }
72
    }
73
74
    /**
75
     * Registers a new user
76
     * @return boolean
77
     */
78
    public function register()
79
    {
80
        if ($this->validate()) {
81
            $user = new Yii::$app->yrc->userClass;
82
            
83
            $user->attributes = [
84
                'email'             => $this->email,
85
                'username'          => $this->username,
86
                'password'          => $this->password,
87
                'otp_enabled'       => 0,
88
                'otp_secret'        => '',
89
            ];
90
        
91
            if ($user->save()) {
92
                // Create an activation token for the user, and store it in the cache
93
                $token = Base32::encode(\random_bytes(64));
94
                
95
                Yii::$app->cache->set(hash('sha256', $token . '_activation_token'), [
96
                    'id' => $user->id
97
                ]);
98
99
                return Yii::$app->yrc->sendEmail('activate', Yii::t('app', 'Activate your account'), $user->email, ['token' => $token]);
100
            }
101
        }
102
103
        return false;
104
    }
105
}