1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace inblank\activeuser\models\forms; |
4
|
|
|
|
5
|
|
|
use inblank\activeuser\traits\CommonTrait; |
6
|
|
|
use yii; |
7
|
|
|
use yii\base\Model; |
8
|
|
|
|
9
|
|
|
class RestoreForm extends Model |
10
|
|
|
{ |
11
|
|
|
use CommonTrait; |
12
|
|
|
|
13
|
|
|
const SCENARIO_PASSWORD = 'password'; |
14
|
|
|
const SCENARIO_EMAIL = 'email'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string user email for restore password |
18
|
|
|
*/ |
19
|
|
|
public $email; |
20
|
|
|
/** |
21
|
|
|
* @var string new password |
22
|
|
|
*/ |
23
|
|
|
public $password; |
24
|
|
|
|
25
|
|
|
/** @inheritdoc */ |
26
|
|
|
public function attributeLabels() |
27
|
|
|
{ |
28
|
|
|
return [ |
29
|
|
|
'email' => Yii::t('activeuser_general', 'Email'), |
30
|
|
|
'password' => Yii::t('activeuser_general', 'Password'), |
31
|
|
|
]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
|
|
public function rules() |
38
|
|
|
{ |
39
|
|
|
return [ |
40
|
|
|
['email', 'required', 'on' => self::SCENARIO_EMAIL], |
41
|
|
|
['email', 'email', 'skipOnEmpty' => true], |
42
|
|
|
|
43
|
|
|
['password', 'required', 'on' => self::SCENARIO_PASSWORD], |
44
|
|
|
['password', 'string', 'length' => [6, 20], 'skipOnEmpty' => true], |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Restore |
50
|
|
|
* @return bool |
51
|
|
|
* @throws yii\base\InvalidConfigException |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function restore() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
if (!$this->validate()) { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
/** @var \inblank\activeuser\models\User $user */ |
59
|
|
|
$user = Yii::createObject(self::di('User'))->findOne(['email' => $this->email]); |
|
|
|
|
60
|
|
|
if ($user) { |
61
|
|
|
$user->restore(); |
62
|
|
|
} |
63
|
|
|
return true; |
64
|
|
|
} |
65
|
|
|
/** |
66
|
|
|
* Change password |
67
|
|
|
* @return bool |
68
|
|
|
* @throws yii\base\InvalidConfigException |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function changePassword() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
if (!$this->validate()) { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
/** @var \inblank\activeuser\models\User $user */ |
76
|
|
|
$user = Yii::createObject(self::di('User'))->findOne(['email' => $this->email]); |
|
|
|
|
77
|
|
|
if ($user) { |
78
|
|
|
$user->password = $this->password; |
79
|
|
|
$user->newPassword(); |
80
|
|
|
} |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.