public function testGetUser()
 {
     // failure
     $model = new LoginForm();
     $model->username = '******';
     $this->assertNull($model->getUser());
     // success
     $model = new LoginForm();
     $model->username = $this->users['user1']['username'];
     $this->assertInstanceOf('app\\models\\User', $model->getUser());
 }
Esempio n. 2
0
 public function actionLogin()
 {
     $model = new LoginForm();
     $data = Yii::$app->request->post();
     $model->load($data, '');
     if ($model->login()) {
         $user = $model->getUser();
         return ['token' => $user->token];
     } else {
         Yii::$app->response->statusCode = 401;
         return ['errors' => $this->filterErrors($model->getErrors())];
     }
 }
Esempio n. 3
0
 public function actionLogin()
 {
     if (!Yii::$app->user->isGuest) {
         $user = User::findOne(Yii::$app->user->id);
         return json_encode(['success' => true, 'data' => $user->attributes]);
     }
     $login = new LoginForm();
     $login->email = Yii::$app->request->post('email');
     $login->password = Yii::$app->request->post('password');
     if ($login->login()) {
         $user = $login->getUser();
         return json_encode(['success' => true, 'data' => $user->attributes]);
     } else {
         if ($login->hasErrors()) {
             return json_encode(['success' => false, 'data' => $login->getErrors()]);
         }
     }
 }
 public function actionLogin()
 {
     if (!Yii::$app->user->isGuest) {
         return $this->afterLogin();
     }
     $model = new LoginForm();
     if ($model->load(Yii::$app->request->post()) && $model->login()) {
         $user = $model->getUser();
         if ($user->status != User::STATUS_ACTIVE) {
             Yii::$app->user->logout();
             Yii::$app->session->setFlash('error', 'Your account is not active.');
             return $this->refresh();
         }
         if ($user->role >= User::ROLE_STAFF) {
             return $this->redirect(['/admin']);
         } else {
             Yii::$app->user->logout();
             Yii::$app->session->setFlash('error', 'You do not have permission.');
             return $this->refresh();
         }
     } else {
         return $this->render('login', ['model' => $model]);
     }
 }
 /**
  * Send email link to access reset password
  * @return type mixed
  */
 public function actionReset()
 {
     if (!\Yii::$app->user->isGuest) {
         return $this->goHome();
     }
     $model = new LoginForm();
     //if request is post
     if ($model->load(Yii::$app->request->post())) {
         if ($user = $model->getUser()) {
             //Found user
             \Yii::$app->notification->sendMail($user->identity, 4, ['{{name}}' => $user->profile->first_name, '{{link}}' => Html::a('This URL', Yii::$app->urlManager->createAbsoluteUrl(['admin/changesecurity/', 'token' => base64_encode($user->access_token)]))]);
             Yii::$app->session->setFlash('success', 'We have sent an email with the reset link! Please check it out.');
             $model = new LoginForm();
         } else {
             //No user exists
             Yii::$app->session->setFlash('error', 'You are not a member!');
         }
     }
     return $this->render('reset', ['model' => $model]);
 }
 /**
  * Send email link to access reset password
  * @return void mixed
  */
 public function actionReset()
 {
     if (!\Yii::$app->user->isGuest) {
         return $this->goHome();
     }
     $model = new LoginForm();
     //if request is post
     if ($model->load(Yii::$app->request->post())) {
         if ($user = $model->getUser()) {
             //Found user
             if ($user->level == 3) {
                 $params = ['{{name}}' => $user->parent->profile->first_name, '{{child}}' => $user->profile->label . ' "' . $user->fullname . '"', '{{link}}' => Html::a('Click', Yii::$app->urlManager->createAbsoluteUrl(['user/family/', 'id' => $user->id]))];
                 \Yii::$app->notification->sendMail($user->parent->identity, 5, $params);
                 Yii::$app->session->setFlash('success', 'We have sent an email to your parent! Please ask to reset your password.');
             } else {
                 $params = ['{{name}}' => $user->profile->first_name, '{{link}}' => Html::a('This URL', Yii::$app->urlManager->createAbsoluteUrl(['user/changesecurity/', 'token' => base64_encode($user->access_token)]))];
                 \Yii::$app->notification->sendMail($user->identity, 4, $params);
                 Yii::$app->session->setFlash('success', 'We have sent an email with the reset link! Please check it out.');
             }
             $model = new LoginForm();
         } else {
             //No user exists
             Yii::$app->session->setFlash('error', 'You are not a member!');
         }
     }
     return $this->render('reset', ['model' => $model]);
 }