/** * Displays the login page * Handles processing of the login form and forgot password form */ public function actionLogin() { $UserLogin = new UserLogin(); $this->layout = false; // redirect if user already logged in //if (!Yii::app()->user->isGuest) { // $this->redirect($this->createUrl('/site/dashboard')); //} // process login form submission if (isset($_POST['UserLogin'])) { if (isset($_POST['UserLogin']['LoginEmail'])) { // user login // set model scenario $UserLogin->scenario = 'formLogin'; // collect user input data $UserLogin->attributes = $_POST['UserLogin']; // validate user input and redirect to the dashboard if ($UserLogin->validate() && $UserLogin->authenticate()) { // redirect to relevant dashboard based on role $authRoles = Yii::app()->authManager->getRoles(Yii::app()->user->id); foreach (array_keys($authRoles) as $authRole) { switch ($authRole) { case 'superAdministrator': $this->redirect($this->createUrl('admin/index')); break; case 'testimonialReviewer': case 'testimonialApprover': $this->redirect($this->createUrl('testimonial/dashboard')); break; } } // do redirect if (UserLogin::model()->findByPk(Yii::app()->user->id)->IsPasswordReset) { // change password form $this->redirect($this->createUrl('user/profilepasswordupdate')); } else { // default dashboard // echo ":TEST:"; // echo(UserLogin::model()->findByPk(Yii::app()->user->id)->AgreetoTerms); //die(':TEST'); $this->redirect($this->createUrl('company/index')); } } } elseif (isset($_POST['UserLogin']['ResetEmail'])) { // password reset // email of account for password reset $resetEmail = $_POST['UserLogin']['ResetEmail']; // check for record matching entered email if (!($UserLogin = UserLogin::model()->findByAttributes(array('LoginEmail' => $resetEmail)))) { $UserLogin = new UserLogin(); } // set model scenario $UserLogin->scenario = 'formReset'; // generate a new password $newPassword = $UserLogin->generatePassword(); // set reset attributes $UserLogin->UserPassword = md5($newPassword); $UserLogin->ResetEmail = $resetEmail; $UserLogin->IsPasswordReset = 1; // save password change if ($UserLogin->save()) { // build message with temporary password $mail = new YiiMailer(); $mail->clearLayout(); // no layout, plain text email $mail->setFrom(Yii::app()->params['adminEmail'], Yii::app()->params['adminName']); $mail->setTo($UserLogin->ResetEmail); $mail->setSubject('ITR | Password Reset'); $mail->setBody('Your password has been reset, you may now log in with the following temporary password: '******'success', 'An email with a temporary password has been sent to your email address.'); } else { // set failure message for user Yii::app()->user->setFlash('failure', 'There was an error when trying to send your temporary password. Please try again in a few minutes or contact web support.'); } // reload view, clears post variables $this->redirect($this->createUrl('user/login')); } // clear generated password so it doesn't display on the login form $UserLogin->UserPassword = ''; } } // display the login form $this->render('login', array('UserLogin' => $UserLogin)); }