| Current Path : /var/www/html/chatbot/backend/models/ |
| Current File : /var/www/html/chatbot/backend/models/LoginForm.php |
<?php
namespace backend\models;
use yii\base\Model;
use Yii;
/**
* Signup form
*/
class LoginForm extends Model
{
public $username;
public $password;
public $rememberMe = true;
private $_user;
/**
* @inheritdoc
*/
public function rules()
{
return [
['username', 'filter', 'filter' => 'trim'],
['username', 'required', 'message' => 'Nama Pengguna tidak boleh kosong'],
['username', 'string', 'min' => 2, 'max' => 255],
['password', 'required', 'message' => 'Kata Laluan tidak boleh kosong'],
['password', 'string', 'min' => 6],
];
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return [
'username' => Yii::t('app','Nama Pengguna'),
'password' => Yii::t('app','Kata Laluan'),
];
}
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;
}
}
public function iframe()
{
$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->iframe($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 = \common\models\User::findByUsername($this->username);
}
return $this->_user;
}
}