Commit c616c474c7544ef42438a498b9587b1fea10b564
1 parent
83e905ff
Artbox great prepairings
Showing
4 changed files
with
355 additions
and
0 deletions
Show diff stats
1 | +<?php | ||
2 | + | ||
3 | + namespace artbox\order\models; | ||
4 | + | ||
5 | + use Yii; | ||
6 | + use yii\base\Model; | ||
7 | + | ||
8 | + /** | ||
9 | + * Login form | ||
10 | + */ | ||
11 | + class LoginForm extends Model | ||
12 | + { | ||
13 | + public $username; | ||
14 | + public $password; | ||
15 | + public $rememberMe = true; | ||
16 | + public $returnUrl; | ||
17 | + | ||
18 | + private $_user; | ||
19 | + | ||
20 | + /** | ||
21 | + * @inheritdoc | ||
22 | + */ | ||
23 | + public function rules() | ||
24 | + { | ||
25 | + return [ | ||
26 | + // username and password are both required | ||
27 | + [ | ||
28 | + [ | ||
29 | + 'username', | ||
30 | + 'password', | ||
31 | + ], | ||
32 | + 'required', | ||
33 | + ], | ||
34 | + [ | ||
35 | + [ | ||
36 | + 'returnUrl', | ||
37 | + ], | ||
38 | + 'string', | ||
39 | + ], | ||
40 | + // rememberMe must be a boolean value | ||
41 | + [ | ||
42 | + 'rememberMe', | ||
43 | + 'boolean', | ||
44 | + ], | ||
45 | + // password is validated by validatePassword() | ||
46 | + [ | ||
47 | + 'password', | ||
48 | + 'validatePassword', | ||
49 | + ], | ||
50 | + ]; | ||
51 | + } | ||
52 | + | ||
53 | + /** | ||
54 | + * Validates the password. | ||
55 | + * This method serves as the inline validation for password. | ||
56 | + * | ||
57 | + * @param string $attribute the attribute currently being validated | ||
58 | + * @param array $params the additional name-value pairs given in the rule | ||
59 | + */ | ||
60 | + public function validatePassword($attribute, $params) | ||
61 | + { | ||
62 | + if (!$this->hasErrors()) { | ||
63 | + $user = $this->getUser(); | ||
64 | + if (!$user || !$user->validatePassword($this->password)) { | ||
65 | + $this->addError($attribute, 'Incorrect username or password.'); | ||
66 | + } | ||
67 | + } | ||
68 | + } | ||
69 | + | ||
70 | + /** | ||
71 | + * Logs in a user using the provided username and password. | ||
72 | + * | ||
73 | + * @return bool whether the user is logged in successfully | ||
74 | + */ | ||
75 | + public function login() | ||
76 | + { | ||
77 | + if ($this->validate()) { | ||
78 | + return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); | ||
79 | + } else { | ||
80 | + return false; | ||
81 | + } | ||
82 | + } | ||
83 | + | ||
84 | + /** | ||
85 | + * Finds user by [[username]] | ||
86 | + * | ||
87 | + * @return Customer|null | ||
88 | + */ | ||
89 | + protected function getUser() | ||
90 | + { | ||
91 | + if ($this->_user === null) { | ||
92 | + $this->_user = Customer::findByUsername($this->username); | ||
93 | + } | ||
94 | + | ||
95 | + return $this->_user; | ||
96 | + } | ||
97 | + | ||
98 | + /** | ||
99 | + * @inheritdoc | ||
100 | + */ | ||
101 | + public function attributeLabels() | ||
102 | + { | ||
103 | + return [ | ||
104 | + 'username' => Yii::t('app', 'Логин'), | ||
105 | + 'password' => Yii::t('app', 'Пароль'), | ||
106 | + 'rememberMe' => Yii::t('app', 'Запомнить'), | ||
107 | + ]; | ||
108 | + } | ||
109 | + } |
1 | +<?php | ||
2 | + | ||
3 | + namespace artbox\order\models; | ||
4 | + | ||
5 | + use Yii; | ||
6 | + use yii\base\Model; | ||
7 | + | ||
8 | + /** | ||
9 | + * Password reset request form | ||
10 | + */ | ||
11 | + class PasswordResetRequestForm extends Model | ||
12 | + { | ||
13 | + public $email; | ||
14 | + /** | ||
15 | + * @inheritdoc | ||
16 | + */ | ||
17 | + public function rules() | ||
18 | + { | ||
19 | + return [ | ||
20 | + [ | ||
21 | + 'email', | ||
22 | + 'trim', | ||
23 | + ], | ||
24 | + [ | ||
25 | + 'email', | ||
26 | + 'required', | ||
27 | + ], | ||
28 | + [ | ||
29 | + 'email', | ||
30 | + 'email', | ||
31 | + ], | ||
32 | + [ | ||
33 | + 'email', | ||
34 | + 'exist', | ||
35 | + 'targetClass' => '\artbox\order\models\Customer', | ||
36 | + 'filter' => [ 'status' => Customer::STATUS_ACTIVE ], | ||
37 | + 'message' => 'There is no user with this email address.', | ||
38 | + ], | ||
39 | + ]; | ||
40 | + } | ||
41 | + /** | ||
42 | + * Sends an email with a link, for resetting the password. | ||
43 | + * | ||
44 | + * @return bool whether the email was send | ||
45 | + */ | ||
46 | + public function sendEmail() | ||
47 | + { | ||
48 | + /* @var $user Customer */ | ||
49 | + $user = Customer::findOne( | ||
50 | + [ | ||
51 | + 'status' => Customer::STATUS_ACTIVE, | ||
52 | + 'email' => $this->email, | ||
53 | + ] | ||
54 | + ); | ||
55 | + if (!$user) { | ||
56 | + return false; | ||
57 | + } | ||
58 | + | ||
59 | + if (!Customer::isPasswordResetTokenValid($user->password_reset_token)) { | ||
60 | + $user->generatePasswordResetToken(); | ||
61 | + if (!$user->save()) { | ||
62 | + return false; | ||
63 | + } | ||
64 | + } | ||
65 | + return Yii::$app->mailer->compose( | ||
66 | + [ | ||
67 | + 'html' => 'passwordResetToken-html', | ||
68 | + 'text' => 'passwordResetToken-text', | ||
69 | + ], | ||
70 | + [ 'user' => $user ] | ||
71 | + ) | ||
72 | + ->setFrom([ Yii::$app->params[ 'supportEmail' ] => Yii::$app->name . ' robot' ]) | ||
73 | + ->setTo($this->email) | ||
74 | + ->setSubject('Password reset for ' . Yii::$app->name) | ||
75 | + ->send(); | ||
76 | + } | ||
77 | + } | ||
0 | \ No newline at end of file | 78 | \ No newline at end of file |
1 | +<?php | ||
2 | + | ||
3 | + namespace artbox\order\models; | ||
4 | + | ||
5 | + use yii\base\Model; | ||
6 | + use yii\base\InvalidParamException; | ||
7 | + | ||
8 | + /** | ||
9 | + * Password reset form | ||
10 | + */ | ||
11 | + class ResetPasswordForm extends Model | ||
12 | + { | ||
13 | + public $password; | ||
14 | + | ||
15 | + /** | ||
16 | + * @var \artbox\order\models\Customer | ||
17 | + */ | ||
18 | + private $_user; | ||
19 | + | ||
20 | + /** | ||
21 | + * Creates a form model given a token. | ||
22 | + * | ||
23 | + * @param string $token | ||
24 | + * @param array $config name-value pairs that will be used to initialize the object properties | ||
25 | + * | ||
26 | + * @throws \yii\base\InvalidParamException if token is empty or not valid | ||
27 | + */ | ||
28 | + public function __construct($token, $config = []) | ||
29 | + { | ||
30 | + if (empty($token) || !is_string($token)) { | ||
31 | + throw new InvalidParamException('Password reset token cannot be blank.'); | ||
32 | + } | ||
33 | + $this->_user = Customer::findByPasswordResetToken($token); | ||
34 | + if (!$this->_user) { | ||
35 | + throw new InvalidParamException('Wrong password reset token.'); | ||
36 | + } | ||
37 | + parent::__construct($config); | ||
38 | + } | ||
39 | + | ||
40 | + /** | ||
41 | + * @inheritdoc | ||
42 | + */ | ||
43 | + public function rules() | ||
44 | + { | ||
45 | + return [ | ||
46 | + [ | ||
47 | + 'password', | ||
48 | + 'required', | ||
49 | + ], | ||
50 | + [ | ||
51 | + 'password', | ||
52 | + 'string', | ||
53 | + 'min' => 6, | ||
54 | + ], | ||
55 | + ]; | ||
56 | + } | ||
57 | + | ||
58 | + /** | ||
59 | + * Resets password. | ||
60 | + * | ||
61 | + * @return bool if password was reset. | ||
62 | + */ | ||
63 | + public function resetPassword() | ||
64 | + { | ||
65 | + $user = $this->_user; | ||
66 | + $user->setPassword($this->password); | ||
67 | + $user->removePasswordResetToken(); | ||
68 | + | ||
69 | + return $user->save(false); | ||
70 | + } | ||
71 | + } | ||
0 | \ No newline at end of file | 72 | \ No newline at end of file |
1 | +<?php | ||
2 | + | ||
3 | + namespace artbox\order\models; | ||
4 | + | ||
5 | + use yii\base\Model; | ||
6 | + | ||
7 | + /** | ||
8 | + * Signup form | ||
9 | + */ | ||
10 | + class SignupForm extends Model | ||
11 | + { | ||
12 | + public $username; | ||
13 | + public $email; | ||
14 | + public $password; | ||
15 | + | ||
16 | + /** | ||
17 | + * @inheritdoc | ||
18 | + */ | ||
19 | + public function rules() | ||
20 | + { | ||
21 | + return [ | ||
22 | + [ | ||
23 | + 'username', | ||
24 | + 'trim', | ||
25 | + ], | ||
26 | + [ | ||
27 | + 'username', | ||
28 | + 'required', | ||
29 | + ], | ||
30 | + [ | ||
31 | + 'username', | ||
32 | + 'unique', | ||
33 | + 'targetClass' => '\artbox\order\models\Customer', | ||
34 | + 'message' => 'This username has already been taken.', | ||
35 | + ], | ||
36 | + [ | ||
37 | + 'username', | ||
38 | + 'string', | ||
39 | + 'min' => 2, | ||
40 | + 'max' => 255, | ||
41 | + ], | ||
42 | + | ||
43 | + [ | ||
44 | + 'email', | ||
45 | + 'trim', | ||
46 | + ], | ||
47 | + [ | ||
48 | + 'email', | ||
49 | + 'required', | ||
50 | + ], | ||
51 | + [ | ||
52 | + 'email', | ||
53 | + 'email', | ||
54 | + ], | ||
55 | + [ | ||
56 | + 'email', | ||
57 | + 'string', | ||
58 | + 'max' => 255, | ||
59 | + ], | ||
60 | + [ | ||
61 | + 'email', | ||
62 | + 'unique', | ||
63 | + 'targetClass' => '\artbox\order\models\Customer', | ||
64 | + 'message' => 'This email address has already been taken.', | ||
65 | + ], | ||
66 | + | ||
67 | + [ | ||
68 | + 'password', | ||
69 | + 'required', | ||
70 | + ], | ||
71 | + [ | ||
72 | + 'password', | ||
73 | + 'string', | ||
74 | + 'min' => 6, | ||
75 | + ], | ||
76 | + ]; | ||
77 | + } | ||
78 | + | ||
79 | + /** | ||
80 | + * Signs user up. | ||
81 | + * | ||
82 | + * @return \artbox\order\models\Customer|null the saved model or null if saving fails | ||
83 | + */ | ||
84 | + public function signup() | ||
85 | + { | ||
86 | + if (!$this->validate()) { | ||
87 | + return null; | ||
88 | + } | ||
89 | + | ||
90 | + $user = new Customer(); | ||
91 | + $user->username = $this->username; | ||
92 | + $user->email = $this->email; | ||
93 | + $user->setPassword($this->password); | ||
94 | + $user->generateAuthKey(); | ||
95 | + | ||
96 | + return $user->save() ? $user : null; | ||
97 | + } | ||
98 | + } | ||
0 | \ No newline at end of file | 99 | \ No newline at end of file |