Commit e88babfd33806c7bb8b6d4534eb6b6b4baf602ba
1 parent
bd24a9e8
v0.0.2-stable
Showing
5 changed files
with
414 additions
and
0 deletions
Show diff stats
| 1 | +<?php | |
| 2 | + namespace common\models; | |
| 3 | + | |
| 4 | + use artbox\core\models\User; | |
| 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 | + | |
| 17 | + private $_user; | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * @inheritdoc | |
| 21 | + */ | |
| 22 | + public function rules() | |
| 23 | + { | |
| 24 | + return [ | |
| 25 | + // username and password are both required | |
| 26 | + [ | |
| 27 | + [ | |
| 28 | + 'username', | |
| 29 | + 'password', | |
| 30 | + ], | |
| 31 | + 'required', | |
| 32 | + ], | |
| 33 | + // rememberMe must be a boolean value | |
| 34 | + [ | |
| 35 | + 'rememberMe', | |
| 36 | + 'boolean', | |
| 37 | + ], | |
| 38 | + // password is validated by validatePassword() | |
| 39 | + [ | |
| 40 | + 'password', | |
| 41 | + 'validatePassword', | |
| 42 | + ], | |
| 43 | + ]; | |
| 44 | + } | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * Validates the password. | |
| 48 | + * This method serves as the inline validation for password. | |
| 49 | + * | |
| 50 | + * @param string $attribute the attribute currently being validated | |
| 51 | + * @param array $params the additional name-value pairs given in the rule | |
| 52 | + */ | |
| 53 | + public function validatePassword($attribute, $params) | |
| 54 | + { | |
| 55 | + if (!$this->hasErrors()) { | |
| 56 | + $user = $this->getUser(); | |
| 57 | + if (!$user || !$user->validatePassword($this->password)) { | |
| 58 | + $this->addError($attribute, 'Incorrect username or password.'); | |
| 59 | + } | |
| 60 | + } | |
| 61 | + } | |
| 62 | + | |
| 63 | + /** | |
| 64 | + * Logs in a user using the provided username and password. | |
| 65 | + * | |
| 66 | + * @return bool whether the user is logged in successfully | |
| 67 | + */ | |
| 68 | + public function login() | |
| 69 | + { | |
| 70 | + if ($this->validate()) { | |
| 71 | + return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); | |
| 72 | + } else { | |
| 73 | + return false; | |
| 74 | + } | |
| 75 | + } | |
| 76 | + | |
| 77 | + /** | |
| 78 | + * Finds user by [[username]] | |
| 79 | + * | |
| 80 | + * @return User|null | |
| 81 | + */ | |
| 82 | + protected function getUser() | |
| 83 | + { | |
| 84 | + if ($this->_user === null) { | |
| 85 | + $this->_user = User::findByUsername($this->username); | |
| 86 | + } | |
| 87 | + | |
| 88 | + return $this->_user; | |
| 89 | + } | |
| 90 | + } | ... | ... |
| 1 | +<?php | |
| 2 | + | |
| 3 | + namespace frontend\models; | |
| 4 | + | |
| 5 | + use Yii; | |
| 6 | + use yii\base\Model; | |
| 7 | + | |
| 8 | + /** | |
| 9 | + * ContactForm is the model behind the contact form. | |
| 10 | + */ | |
| 11 | + class ContactForm extends Model | |
| 12 | + { | |
| 13 | + public $name; | |
| 14 | + public $email; | |
| 15 | + public $subject; | |
| 16 | + public $body; | |
| 17 | + public $verifyCode; | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * @inheritdoc | |
| 21 | + */ | |
| 22 | + public function rules() | |
| 23 | + { | |
| 24 | + return [ | |
| 25 | + // name, email, subject and body are required | |
| 26 | + [ | |
| 27 | + [ | |
| 28 | + 'name', | |
| 29 | + 'email', | |
| 30 | + 'subject', | |
| 31 | + 'body', | |
| 32 | + ], | |
| 33 | + 'required', | |
| 34 | + ], | |
| 35 | + // email has to be a valid email address | |
| 36 | + [ | |
| 37 | + 'email', | |
| 38 | + 'email', | |
| 39 | + ], | |
| 40 | + // verifyCode needs to be entered correctly | |
| 41 | + [ | |
| 42 | + 'verifyCode', | |
| 43 | + 'captcha', | |
| 44 | + ], | |
| 45 | + ]; | |
| 46 | + } | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * @inheritdoc | |
| 50 | + */ | |
| 51 | + public function attributeLabels() | |
| 52 | + { | |
| 53 | + return [ | |
| 54 | + 'verifyCode' => 'Verification Code', | |
| 55 | + ]; | |
| 56 | + } | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * Sends an email to the specified email address using the information collected by this model. | |
| 60 | + * | |
| 61 | + * @param string $email the target email address | |
| 62 | + * | |
| 63 | + * @return bool whether the email was sent | |
| 64 | + */ | |
| 65 | + public function sendEmail($email) | |
| 66 | + { | |
| 67 | + return Yii::$app->mailer->compose() | |
| 68 | + ->setTo($email) | |
| 69 | + ->setFrom([ $this->email => $this->name ]) | |
| 70 | + ->setSubject($this->subject) | |
| 71 | + ->setTextBody($this->body) | |
| 72 | + ->send(); | |
| 73 | + } | |
| 74 | + } | ... | ... |
| 1 | +<?php | |
| 2 | + namespace frontend\models; | |
| 3 | + | |
| 4 | + use Yii; | |
| 5 | + use yii\base\Model; | |
| 6 | + use common\models\User; | |
| 7 | + | |
| 8 | + /** | |
| 9 | + * Password reset request form | |
| 10 | + */ | |
| 11 | + class PasswordResetRequestForm extends Model | |
| 12 | + { | |
| 13 | + public $email; | |
| 14 | + | |
| 15 | + /** | |
| 16 | + * @inheritdoc | |
| 17 | + */ | |
| 18 | + public function rules() | |
| 19 | + { | |
| 20 | + return [ | |
| 21 | + [ | |
| 22 | + 'email', | |
| 23 | + 'trim', | |
| 24 | + ], | |
| 25 | + [ | |
| 26 | + 'email', | |
| 27 | + 'required', | |
| 28 | + ], | |
| 29 | + [ | |
| 30 | + 'email', | |
| 31 | + 'email', | |
| 32 | + ], | |
| 33 | + [ | |
| 34 | + 'email', | |
| 35 | + 'exist', | |
| 36 | + 'targetClass' => '\common\models\User', | |
| 37 | + 'filter' => [ 'status' => User::STATUS_ACTIVE ], | |
| 38 | + 'message' => 'There is no user with this email address.', | |
| 39 | + ], | |
| 40 | + ]; | |
| 41 | + } | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * Sends an email with a link, for resetting the password. | |
| 45 | + * | |
| 46 | + * @return bool whether the email was send | |
| 47 | + */ | |
| 48 | + public function sendEmail() | |
| 49 | + { | |
| 50 | + /* @var $user User */ | |
| 51 | + $user = User::findOne( | |
| 52 | + [ | |
| 53 | + 'status' => User::STATUS_ACTIVE, | |
| 54 | + 'email' => $this->email, | |
| 55 | + ] | |
| 56 | + ); | |
| 57 | + | |
| 58 | + if (!$user) { | |
| 59 | + return false; | |
| 60 | + } | |
| 61 | + | |
| 62 | + if (!User::isPasswordResetTokenValid($user->password_reset_token)) { | |
| 63 | + $user->generatePasswordResetToken(); | |
| 64 | + if (!$user->save()) { | |
| 65 | + return false; | |
| 66 | + } | |
| 67 | + } | |
| 68 | + | |
| 69 | + return Yii::$app->mailer->compose( | |
| 70 | + [ | |
| 71 | + 'html' => 'passwordResetToken-html', | |
| 72 | + 'text' => 'passwordResetToken-text', | |
| 73 | + ], | |
| 74 | + [ 'user' => $user ] | |
| 75 | + ) | |
| 76 | + ->setFrom([ Yii::$app->params[ 'supportEmail' ] => Yii::$app->name . ' robot' ]) | |
| 77 | + ->setTo($this->email) | |
| 78 | + ->setSubject('Password reset for ' . Yii::$app->name) | |
| 79 | + ->send(); | |
| 80 | + } | |
| 81 | + } | ... | ... |
| 1 | +<?php | |
| 2 | + namespace frontend\models; | |
| 3 | + | |
| 4 | + use yii\base\Model; | |
| 5 | + use yii\base\InvalidParamException; | |
| 6 | + use common\models\User; | |
| 7 | + | |
| 8 | + /** | |
| 9 | + * Password reset form | |
| 10 | + */ | |
| 11 | + class ResetPasswordForm extends Model | |
| 12 | + { | |
| 13 | + public $password; | |
| 14 | + | |
| 15 | + /** | |
| 16 | + * @var \common\models\User | |
| 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 = User::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 | + } | ... | ... |
| 1 | +<?php | |
| 2 | + namespace frontend\models; | |
| 3 | + | |
| 4 | + use yii\base\Model; | |
| 5 | + use common\models\User; | |
| 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' => '\common\models\User', | |
| 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' => '\common\models\User', | |
| 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 User|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 User(); | |
| 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 | + } | ... | ... |