Your IP : 216.73.216.79


Current Path : /var/www/html/learn/common/models/
Upload File :
Current File : /var/www/html/learn/common/models/LoginForm.php

<?php

namespace common\models;

use Yii;
use yii\base\Model;

/**
 * Login form
 */
class LoginForm extends Model {

    public $username;
    public $password;
    public $rememberMe = true;
    private $_user;

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            // username and password are both required
            [['username', 'password'], 'required'],
            // rememberMe must be a boolean value
            ['rememberMe', 'boolean'],
            // password is validated by validatePassword()
            ['password', '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->password)) {
                $this->addError($attribute, Yii::t('app', 'Incorrect username or password.'));
            }
        }
    }

    /**
     * Logs in a user using the provided username and password.
     *
     * @return boolean whether the user is logged in successfully
     */
    public function login() {
        $user = $this->getUser();
        if ($this->validate()) {
            // Update last login timestamp
            //$user->last_login = time();
            //$user->last_login_attempts = 0;
            //$user->login_attempts = 0;
            //$user->save();
            return Yii::$app->user->login($user, $this->rememberMe ? 3600 * 24 * 30 : 0);
        } else {
            if ($user !== null) {
                // Record login attempts and last login attempts for login throttling
                //$user->last_login_attempts = time();
                //$user->login_attempts = intval($user->login_attempts) + 1;
                //$user->save();
            }
            return false;
        }
    }

    /**
     * Logs in a user using the provided username and password.
     *
     * @return boolean whether the user is logged in successfully
     */
    public function loginFrontend() {
        $user = $this->getUser();
        if ($this->validate()) {
            $role = $user->staff->roles;
            $rolearr = [];

            array_walk($role, function($v) use (&$rolearr) {
                $rolearr[] = $v->role_code;
            });

            if (!in_array('FNT', $rolearr)) {
                $this->addError('password', 'Incorrect username or password.');
                return false;
            }

            return Yii::$app->user->login($user, $this->rememberMe ? 3600 * 24 * 30 : 0);
        } else {
            if ($user !== null) {
                // Record login attempts and last login attempts for login throttling
                //$user->last_login_attempts = time();
                //$user->login_attempts = intval($user->login_attempts) + 1;
                //$user->save();
            }
            return false;
        }
    }

    /**
     * Finds user by [[username]]
     *
     * @return User|null
     */
    protected function getUser() {
        if ($this->_user === null) {
            $this->_user = User::findByUsername($this->username);
        }

        return $this->_user;
    }

}