Esempio n. 1
0
 /**
  * Finds user by [[username]]
  *
  * @return User|null
  */
 public function getUser()
 {
     if ($this->_user === false) {
         $this->_user = User::findByUsername($this->username);
     }
     return $this->_user;
 }
Esempio n. 2
0
 public function getUpadatedByUserName()
 {
     $account = User::find()->where(['id' => $this->id])->one();
     if ($account) {
         return $account->username;
     }
 }
Esempio n. 3
0
 public function getLatestUsers($numOfUsers)
 {
     $users = User::find()->where(['status' => User::STATUS_ACTIVE])->orderBy(['created_at' => SORT_DESC])->limit($numOfUsers)->all();
     if ($users) {
         return $users;
     } else {
         return null;
     }
 }
 public function SaveNewPassword()
 {
     $user = User::findOne($this->id);
     $user->password_hash = Yii::$app->security->generatePasswordHash($this->new_password);
     if ($user->save()) {
         return true;
     } else {
         return false;
     }
 }
 /**
  * Creates a form model given a token.
  *
  * @param  string                          $token
  * @param  array                           $config name-value pairs that will be used to initialize the object properties
  * @throws \yii\base\InvalidParamException if token is empty or not valid
  */
 public function __construct($token, $config = [])
 {
     if (empty($token) || !is_string($token)) {
         throw new InvalidParamException('Password reset token cannot be blank.');
     }
     $this->_user = User::findByPasswordResetToken($token);
     if (!$this->_user) {
         throw new InvalidParamException('Wrong password reset token.');
     } else {
         parent::__construct($config);
     }
 }
Esempio n. 6
0
 /**
  * Creates a form model given a token.
  *
  * @param  string                          $token
  * @param  array                           $config name-value pairs that will be used to initialize the object properties
  * @throws \yii\base\InvalidParamException if token is empty or not valid
  */
 public function __construct($token, $config = [])
 {
     if (empty($token) || !is_string($token)) {
         throw new InvalidParamException('Activate token cannot be blank.');
     }
     $this->user = User::findByActivateToken($token);
     if (!$this->user) {
         throw new InvalidParamException('Wrong activate token.');
     } else {
         parent::__construct($config);
     }
 }
 public function actionBackendLogin()
 {
     if (!\Yii::$app->user->isGuest) {
         return $this->goHome();
     }
     $model = new LoginForm();
     if ($model->load(Yii::$app->request->post()) && $model->login()) {
         $user = User::findIdentity(Yii::$app->user->identity->id);
         $user->touch('last_visited');
         return $this->goBack();
     } else {
         return $this->render('backend-login', ['model' => $model]);
     }
 }
Esempio n. 8
0
 public function sendEmail()
 {
     /* @var $user User */
     $user = User::findOne(['status' => User::STATUS_ACTIVE, 'email' => $this->email]);
     if ($user) {
         if (!User::isActivateTokenValid($user->password_reset_token)) {
             $user->generateActivateToken();
         }
         if ($user->save()) {
             $mailer = \Yii::$app->mailer;
             $mailer->viewPath = $this->viewPath;
             $mailer->getView()->theme = \Yii::$app->view->theme;
             return $mailer->compose(['html' => 'ActivateToken-html', 'text' => 'ActivateToken-text'], ['user' => $user])->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'])->setTo($this->email)->setSubject('Activate Account for ' . \Yii::$app->name)->send();
         }
     }
     return false;
 }
 public function onAuthSuccess($client)
 {
     $attributes = $client->getUserAttributes();
     /* @var $auth Auth */
     $auth = Auth::find()->where(['source' => $client->getId(), 'source_id' => $attributes['id']])->one();
     if (Yii::$app->user->isGuest) {
         if ($auth) {
             // login
             $user = User::findOne(['id' => $auth->user_id]);
             Yii::$app->user->login($user);
         } else {
             // signup
             if (isset($attributes['email']) && User::find()->where(['email' => $attributes['email']])->exists()) {
                 Yii::$app->getSession()->setFlash('error', [Yii::t('app', "User with the same email as in {client} account already exists but isn't linked to it. Login using email first to link it.", ['client' => $client->getTitle()])]);
             } else {
                 $password = Yii::$app->security->generateRandomString(6);
                 $user = new User(['username' => $attributes['name'], 'email' => $attributes['email'], 'password' => $password]);
                 $user->generateAuthKey();
                 $user->generatePasswordResetToken();
                 $transaction = $user->getDb()->beginTransaction();
                 if ($user->save()) {
                     $authManager = Yii::$app->authManager;
                     $authorRole = $authManager->getRole('User');
                     $authManager->assign($authorRole, $user->id);
                     $auth = new Auth(['user_id' => $user->id, 'source' => $client->getId(), 'source_id' => (string) $attributes['id']]);
                     if ($auth->save()) {
                         $transaction->commit();
                         Yii::$app->user->login($user);
                     } else {
                         print_r($auth->getErrors());
                     }
                 } else {
                     print_r($user->getErrors());
                 }
             }
         }
     } else {
         // user already logged in
         if (!$auth) {
             // add auth provider
             $auth = new Auth(['user_id' => Yii::$app->user->id, 'source' => $client->getId(), 'source_id' => $attributes['id']]);
             $auth->save();
         }
     }
 }
Esempio n. 10
0
 public function actionSetPassword($id)
 {
     $model = new SetPasswordForm();
     $model->id = $id;
     if ($user = User::findOne($id)) {
         if ($model->load(Yii::$app->request->post()) && $model->SaveNewPassword()) {
             Yii::$app->getSession()->setFlash('success', 'new password set');
             return $this->redirect(['view', 'id' => $id]);
         } else {
             return $this->render('set-password', ['model' => $model]);
         }
     }
 }
 protected function findModelChangePassword($id)
 {
     if (($model = User::findOne($id)) !== null) {
         $new_model = new ChangePasswordForm();
         $new_model->username = $model->username;
         $new_model->email = $model->email;
         return $new_model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }