예제 #1
0
 /**
  * Loads the user model for the logged in user.
  *
  * @throws \nordsoftware\yii_account\exceptions\Exception if the user is a guest.
  * @return \nordsoftware\yii_account\models\ar\Account the model.
  */
 public function loadAccount()
 {
     if ($this->isGuest) {
         throw new Exception("Trying to load model for guest user.");
     }
     if (!isset($this->_model)) {
         $modelClass = Helper::getModule()->getClassName(Module::CLASS_ACCOUNT);
         $this->_model = \CActiveRecord::model($modelClass)->findByPk($this->id);
     }
     return $this->_model;
 }
예제 #2
0
 public static function createEntry($accountId, $success)
 {
     $modelClass = Helper::getModule()->getClassName(Module::CLASS_LOGIN_HISTORY);
     /** @var \nordsoftware\yii_account\models\ar\AccountLoginHistory $model */
     $model = new $modelClass();
     $model->accountId = $accountId;
     $model->success = $success;
     $model->numFailedAttempts = !$success ? $model->resolveNumFailedAttempts() : 0;
     if (!$model->save()) {
         throw new Exception('Failed to save login history entry.');
     }
 }
예제 #3
0
 /**
  * Creates a password history entry.
  *
  * @param int $accountId account id.
  * @param string $salt password salt.
  * @param string $password hashed password.
  * @throws \nordsoftware\yii_account\exceptions\Exception if the history entry cannot be saved.
  */
 public function createHistoryEntry($accountId, $salt, $password)
 {
     $modelClass = Helper::getModule()->getClassName(Module::CLASS_PASSWORD_HISTORY);
     /** @var \nordsoftware\yii_account\models\ar\AccountPasswordHistory $model */
     $model = new $modelClass();
     $model->accountId = $accountId;
     $model->salt = $salt;
     $model->password = $password;
     if (!$model->save()) {
         throw new Exception('Failed to save password history entry.');
     }
 }
예제 #4
0
 /**
  * Creates a new account with the given username and password.
  *
  * @param string $username
  * @param string $password
  * @throws \nordsoftware\yii_account\exceptions\Exception
  */
 public function actionCreate($username, $password)
 {
     $modelClass = Helper::getModule()->getClassName(Module::CLASS_ACCOUNT);
     /** @var \nordsoftware\yii_account\models\ar\Account $account */
     $account = new $modelClass();
     $account->username = $username;
     $account->password = $password;
     if (!$account->save(false)) {
         throw new Exception("Failed to create account.");
     }
     echo "Account {$username}:{$password} created.\n";
 }
예제 #5
0
 /**
  * @param string $email
  * @return \nordsoftware\yii_account\models\ar\Account
  * @throws \nordsoftware\yii_account\exceptions\Exception
  */
 public function loadModel($email)
 {
     $modelClass = Helper::getModule()->getClassName(Module::CLASS_ACCOUNT);
     return \CActiveRecord::model($modelClass)->findByAttributes(array('email' => $email));
 }
예제 #6
0
 /**
  * @return \nordsoftware\yii_account\models\ar\Account|\YiiPassword\Behavior
  */
 protected function loadAccount()
 {
     $modelClass = Helper::getModule()->getClassName(Module::CLASS_ACCOUNT);
     return \CActiveRecord::model($modelClass)->find(array('condition' => 'username=:username OR email=:email', 'params' => array(':username' => strtolower($this->username), ':email' => $this->username)));
 }