|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
|
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
|
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
|
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
|
8
|
|
|
* @link https://vistart.me/ |
|
9
|
|
|
* @copyright Copyright (c) 2016 - 2017 vistart |
|
10
|
|
|
* @license https://vistart.me/license/ |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace rhosocial\user\forms; |
|
14
|
|
|
|
|
15
|
|
|
use rhosocial\user\User; |
|
16
|
|
|
use rhosocial\user\Profile; |
|
17
|
|
|
use Yii; |
|
18
|
|
|
use yii\base\Model; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @version 1.0 |
|
22
|
|
|
* @author vistart <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class RegisterForm extends Model |
|
25
|
|
|
{ |
|
26
|
|
|
public $nickname; |
|
27
|
|
|
public $username = false; |
|
28
|
|
|
public $password; |
|
29
|
|
|
public $password_repeat; |
|
30
|
|
|
public $first_name; |
|
31
|
|
|
public $last_name; |
|
32
|
|
|
public $gender = 1; |
|
33
|
|
|
public $userClass; |
|
34
|
|
|
public $model = null; |
|
35
|
|
|
public $continue = 0; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @inheritdoc |
|
39
|
|
|
*/ |
|
40
|
|
|
public function attributeLabels() |
|
41
|
|
|
{ |
|
42
|
|
|
return [ |
|
43
|
|
|
'username' => Yii::t('user', 'Username'), |
|
44
|
|
|
'nickname' => Yii::t('user', 'Nickname'), |
|
45
|
|
|
'password' => Yii::t('user', 'Password'), |
|
46
|
|
|
'password_repeat' => Yii::t('user', 'Password Repeat'), |
|
47
|
|
|
'first_name' => Yii::t('user', 'First Name'), |
|
48
|
|
|
'last_name' => Yii::t('user', 'Last Name'), |
|
49
|
|
|
'gender' => Yii::t('user', 'Gender'), |
|
50
|
|
|
'continue' => Yii::t('user', 'Continue to register'), |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @inheritdoc |
|
56
|
|
|
*/ |
|
57
|
|
|
public function attributeHints() |
|
58
|
|
|
{ |
|
59
|
|
|
return [ |
|
60
|
|
|
'continue' => Yii::t('user', 'If you want to register new users consecutively, check this option.'), |
|
61
|
|
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return User |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function getNoInitUser() |
|
68
|
|
|
{ |
|
69
|
|
|
$userClass = $this->userClass; |
|
70
|
|
|
return $userClass::buildNoInitModel(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return Username |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function getNoInitUsername() |
|
77
|
|
|
{ |
|
78
|
|
|
$usernameClass = $this->getNoInitUser()->usernameClass; |
|
79
|
|
|
return $usernameClass::buildNoInitModel(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @inheritdoc |
|
84
|
|
|
*/ |
|
85
|
|
|
public function init() |
|
86
|
|
|
{ |
|
87
|
|
|
if (empty($this->userClass)) { |
|
88
|
|
|
$this->userClass = Yii::$app->user->identityClass; |
|
89
|
|
|
} |
|
90
|
|
|
$noInit = $this->getNoInitUser(); |
|
91
|
|
|
/* @var $noInit User */ |
|
92
|
|
|
if (class_exists($noInit->usernameClass)) { |
|
93
|
|
|
$this->username = ''; |
|
|
|
|
|
|
94
|
|
|
} else { |
|
95
|
|
|
$this->username = false; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @inheritdoc |
|
101
|
|
|
*/ |
|
102
|
|
|
public function rules() |
|
103
|
|
|
{ |
|
104
|
|
|
$rules = [ |
|
105
|
|
|
[['nickname', 'password', 'password_repeat', 'first_name', 'last_name', 'gender'], 'required'], |
|
106
|
|
|
[['nickname'], 'string', 'max' => 32], |
|
107
|
|
|
[['password', 'password_repeat'], 'string', 'min' => 6, 'max' => 32], |
|
108
|
|
|
['password', 'compare'], |
|
109
|
|
|
[['first_name', 'last_name'], 'string', 'max' => 255], |
|
110
|
|
|
['gender', 'in', 'range' => array_keys(Profile::$genders)], |
|
111
|
|
|
['continue', 'integer'], |
|
112
|
|
|
]; |
|
113
|
|
|
if (is_string($this->username)) { |
|
114
|
|
|
$rules = array_merge($rules, [ |
|
115
|
|
|
['username', 'required'], |
|
116
|
|
|
['username', 'string', 'min'=>2, 'max' => 32], |
|
117
|
|
|
['username', 'match', 'not' => true, 'pattern' => '/^\d+$/', 'message' => Yii::t('user', 'The username can not be a pure number.')], |
|
118
|
|
|
['username', 'unique', 'targetClass' => $this->getNoInitUser()->usernameClass, 'targetAttribute' => $this->getNoInitUsername()->contentAttribute, 'message' => Yii::t('user', 'The username has been used.')] |
|
119
|
|
|
]); |
|
120
|
|
|
} |
|
121
|
|
|
return $rules; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Register user with current model. |
|
126
|
|
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
|
|
public function register() |
|
129
|
|
|
{ |
|
130
|
|
|
if ($this->validate()) { |
|
131
|
|
|
$class = $this->userClass; |
|
132
|
|
|
$user = new $class(['password' => $this->password]); |
|
133
|
|
|
/* @var $user User */ |
|
134
|
|
|
$profile = $user->createProfile(['nickname' => $this->nickname, 'first_name' => $this->first_name, 'last_name' => $this->last_name, 'gender' => $this->gender]); |
|
135
|
|
|
$models[] = $profile; |
|
|
|
|
|
|
136
|
|
|
if (is_string($this->username)) { |
|
137
|
|
|
$username = $user->createUsername($this->username); |
|
138
|
|
|
$models[] = $username; |
|
139
|
|
|
} |
|
140
|
|
|
$result = $user->register($models); |
|
141
|
|
|
if ($result == true) { |
|
142
|
|
|
$this->model = $user; |
|
143
|
|
|
} |
|
144
|
|
|
return $result; |
|
145
|
|
|
} |
|
146
|
|
|
return false; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.