| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- namespace app\models;
- use Yii;
- use yii\base\Model;
- class Login extends Model {
- public $usuario;
- public $clave;
- private $_user = false;
- /**
- * @return array the validation rules.
- */
- public function rules() {
- return [
- [['usuario', 'clave'], 'required'],
- ['clave', 'validatePassword'],
- ];
- }
- /**
- * Validates the password.
- * This method serves as the inline validation for password.
- *
- * @param string $attribute the attribute currently being validated
- * @param array $params the additional name-value pairs given in the rule
- */
- public function validatePassword($attribute, $params) {
- if (!$this->hasErrors()) {
- $user = $this->getUser();
- if (!$user || !$user->validatePassword($this->clave)) {
- $this->addError($attribute, 'Incorrect username or password.');
- }
- }
- }
- /**
- * Logs in a user using the provided username and password.
- * @return bool whether the user is logged in successfully
- */
- public function login() {
- if ($this->validate()) {
- return Yii::$app->user->login($this->getUser(), 3600 * 24 * 30);
- }
- return false;
- }
- /**
- * Finds user by [[username]]
- *
- * @return Usuario|null
- */
- public function getUser() {
- if ($this->_user === false) {
- $this->_user = Usuario::findByUsername($this->usuario);
- }
- return $this->_user;
- }
- }
|