prat
(Yurfriend19)
September 18, 2013, 7:22am
1
I have a column in my database which indicates if user should be disabled from login or not since I don’t want to delete it from database as user login might be enabled in future. If accessLevel is 4 then user login should be disabled else he should able to login. I have implemented this in following way
public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->username,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$record=User::model()->findByAttributes(array('accountID'=>$this->username));
if($record->accessLevel == 4)
return false;
else
{
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);
return true;
}
}
else
return false;
}
So my first question is that if I am doing this in correct way and 2nd question how should I display a particular message to user like "your login has been disabled. contact administrator"?
Keith
(Kburton)
September 18, 2013, 7:58am
2
I’ve done this by adding my own error codes to the UserIdentity class:
class UserIdentity extends CUserIdentity
{
private $passwordVerifier;
private $user;
const ERROR_ACCOUNT_NOT_ACTIVE = 101;
const ERROR_TOO_MANY_FAILED_LOGINS = 102;
public function __construct($username, $passwordVerifier)
{
$this->passwordVerifier = $passwordVerifier;
// Leave password blank as it isn't used in verification
parent::__construct($username, '');
}
/**
* Authenticates a user.
* @return boolean whether authentication succeeds.
*/
public function authenticate()
{
$this->user = User::model()->findByAttributes(array('Email'=>$this->username));
if ($this->user === null)
$this->errorCode = self::ERROR_USERNAME_INVALID;
else if (!$this->user->isActive())
$this->errorCode = self::ERROR_ACCOUNT_NOT_ACTIVE;
else if ($this->user->tooManyFailedLogins())
$this->errorCode = self::ERROR_TOO_MANY_FAILED_LOGINS;
else if ($this->passwordVerifier->getHash() !== $this->user->Password)
$this->errorCode = self::ERROR_PASSWORD_INVALID;
else
$this->errorCode = self::ERROR_NONE;
return !$this->errorCode;
}
Ankit_Modi
(Ankit Modi)
September 18, 2013, 7:58am
3
prat:
I have a column in my database which indicates if user should be disabled from login or not since I don’t want to delete it from database as user login might be enabled in future. If accessLevel is 4 then user login should be disabled else he should able to login. I have implemented this in following way
public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->username,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$record=User::model()->findByAttributes(array('accountID'=>$this->username));
if($record->accessLevel == 4)
return false;
else
{
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);
return true;
}
}
else
return false;
}
So my first question is that if I am doing this in correct way and 2nd question how should I display a particular message to user like "your login has been disabled. contact administrator"?
i think it’s correct way and if you want to display the message using flash
prat
(Yurfriend19)
September 18, 2013, 9:34am
4
thanks for the replies. I will using flash to display message.