You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					59 lines
				
				1.2 KiB
			
		
		
			
		
	
	
					59 lines
				
				1.2 KiB
			| 
											13 years ago
										 | <?php
 | ||
|  | 
 | ||
|  | namespace app\models;
 | ||
|  | 
 | ||
| 
											13 years ago
										 | use Yii;
 | ||
| 
											13 years ago
										 | use yii\base\Model;
 | ||
|  | 
 | ||
|  | /**
 | ||
| 
											13 years ago
										 |  * LoginForm is the model behind the login form.
 | ||
| 
											13 years ago
										 |  */
 | ||
|  | class LoginForm extends Model
 | ||
|  | {
 | ||
|  | 	public $username;
 | ||
|  | 	public $password;
 | ||
| 
											13 years ago
										 | 	public $rememberMe = true;
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 	/**
 | ||
|  | 	 * @return array the validation rules.
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function rules()
 | ||
|  | 	{
 | ||
|  | 		return array(
 | ||
| 
											13 years ago
										 | 			// username and password are both required
 | ||
|  | 			array('username, password', 'required'),
 | ||
|  | 			// password is validated by validatePassword()
 | ||
| 
											13 years ago
										 | 			array('password', 'validatePassword'),
 | ||
| 
											13 years ago
										 | 			// rememberMe must be a boolean value
 | ||
| 
											13 years ago
										 | 			array('rememberMe', 'boolean'),
 | ||
| 
											13 years ago
										 | 		);
 | ||
|  | 	}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 	/**
 | ||
|  | 	 * Validates the password.
 | ||
|  | 	 * This method serves as the inline validation for password.
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function validatePassword()
 | ||
|  | 	{
 | ||
|  | 		$user = User::findByUsername($this->username);
 | ||
| 
											13 years ago
										 | 		if (!$user || !$user->validatePassword($this->password)) {
 | ||
| 
											13 years ago
										 | 			$this->addError('password', 'Incorrect username or password.');
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 	/**
 | ||
|  | 	 * Logs in a user using the provided username and password.
 | ||
|  | 	 * @return boolean whether the user is logged in successfully
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function login()
 | ||
|  | 	{
 | ||
|  | 		if ($this->validate()) {
 | ||
|  | 			$user = User::findByUsername($this->username);
 | ||
| 
											13 years ago
										 | 			Yii::$app->user->login($user, $this->rememberMe ? 3600*24*30 : 0);
 | ||
| 
											13 years ago
										 | 			return true;
 | ||
|  | 		} else {
 | ||
|  | 			return false;
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
| 
											13 years ago
										 | }
 |