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 Yii; |
17
|
|
|
use yii\base\Model; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class UsernameForm |
21
|
|
|
* @package rhosocial\user\forms |
22
|
|
|
* @version 1.0 |
23
|
|
|
* @author vistart <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class UsernameForm extends Model |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var User |
29
|
|
|
*/ |
30
|
|
|
public $user; |
31
|
|
|
public $username; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
|
|
public function init() |
37
|
|
|
{ |
38
|
|
|
if (!$this->user) { |
39
|
|
|
$this->user = Yii::$app->user->identity; |
40
|
|
|
} |
41
|
|
|
$username = $this->user->getUsername()->one(); |
42
|
|
|
if (!$username) { |
43
|
|
|
$this->username = ''; |
44
|
|
|
} else { |
45
|
|
|
$this->username = (string)$username; |
46
|
|
|
} |
47
|
|
|
parent::init(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
|
|
protected function getNoInitUsername() |
54
|
|
|
{ |
55
|
|
|
$class = $this->user->usernameClass; |
56
|
|
|
return $class::buildNoInitModel(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public function rules() |
63
|
|
|
{ |
64
|
|
|
return [ |
65
|
|
|
['username', 'required'], |
66
|
|
|
['username', 'string', 'max' => 32, 'min' => '2'], |
67
|
|
|
['username', 'match', 'not' => true, 'pattern' => '/^\d+$/', 'message' => Yii::t('user', 'The username can not be a pure number.')], |
68
|
|
|
['username', 'match', 'pattern' => '/^\w{2,32}$/'], |
69
|
|
|
['username', 'unique', 'targetClass' => $this->user->usernameClass, 'targetAttribute' => $this->getNoInitUsername()->contentAttribute, 'when' => function ($model, $attribute) { |
70
|
|
|
/* @var $model static */ |
71
|
|
|
$username = $model->user->getUsername()->one(); |
|
|
|
|
72
|
|
|
if (!$username) { |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
return $model->$attribute != (string)$username; |
76
|
|
|
}], |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Change username. |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function changeUsername() |
85
|
|
|
{ |
86
|
|
|
if ($this->validate()) { |
87
|
|
|
$username = $this->user->createUsername($this->username); |
88
|
|
|
if (!$username) { |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
return $username->save(); |
92
|
|
|
} |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public function attributeLabels() |
100
|
|
|
{ |
101
|
|
|
return [ |
102
|
|
|
'username' => Yii::t('user', 'Username'), |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritdoc |
108
|
|
|
*/ |
109
|
|
|
public function attributeHints() |
110
|
|
|
{ |
111
|
|
|
return [ |
112
|
|
|
'username' => Yii::t('user', 'Specify a username to provide convenience for login.'), |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.