| Current Path : /var/www/html/helpdesk/controllers/ |
| Current File : /var/www/html/helpdesk/controllers/SiteController.php |
<?php
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\Log;
use app\models\User;
use yii\helpers\Url;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
class SiteController extends Controller {
public $layout = "main_original";
public function beforeAction($action) {
if (parent::beforeAction($action)) {
if ($action->id == 'error') {
$this->layout = 'error';
}
return true;
}
}
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
public function actions() {
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}
public function actionIndex() {
$this->layout = 'public';
return $this->render('public');
}
public function actionLogin() {
$this->layout = 'login';
if (!Yii::$app->user->isGuest) {
return $this->redirect(['dashboard/index']);
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
$Log = new Log();
$Log->insertlog('login', 'login', '');
if (Yii::$app->user->identity->role == 'GD' || Yii::$app->user->identity->role == 'ME' || Yii::$app->user->identity->role == 'CW' || Yii::$app->user->identity->role == 'WD')
return $this->redirect(['tasks/index', 'TasksSearch[task_status]' => ['pending', 'on-going']]);
else
return $this->redirect(['dashboard/index']);
}
return $this->render('login', ['model' => $model,]);
}
public function actionLogout() {
$Log = new Log();
$Log->insertlog('logout', 'logout', '');
Yii::$app->user->logout();
return $this->goHome();
}
public function actionForgot() {
$msg = '';
$this->layout = 'login';
if (Yii::$app->request->post()) {
$email_post = Yii::$app->request->post('email');
//find inside user
if (($model = User::findOne(['email' => $email_post])) !== null) {
//reset password
$pwd = Yii::$app->security->generateRandomString(8);
$model->pwd = \Yii::$app->security->generatePasswordHash($pwd);
if ($model->save(false)) {
//send email
$email_content = '<html>
<body>
<div style="font-family: \'Century Gothic\', CenturyGothic, AppleGothic, sans-serif; font-size: 14px;background-color:rgb(234,234,234);padding:2em 0;">
<div style="width:70%;background-color:white;margin:0 auto;padding:5em 5em 5em 5em;border:1px solid #ccc;">
<p>Hi ' . $model->name . ',</p>
<p>We\'ve received your request to reset your password. You can access the system now using below details :</p>
<p>URL: <a href="' . Url::base(true) . '" target="_blank">' . Url::base(true) . '</a></p>
<p>Email: ' . $email_post . '</p>
<p>Password: ' . $pwd . '</p>
<p>- Admin -</p>
</div>
</div>
</body>
</html>';
if (Yii::$app->mailer->compose()
->setFrom(Yii::$app->params['senderEmail'])
->setTo($email_post)
->setSubject('Reset Password')
->setHtmlBody($email_content)
->send()) {
$msg = $pwd.'<div class="alert alert-success mt-3" role="alert">
<div class="alert-text">Your password has been reset. <br>Please check your email for your new password</div>
</div>';
}
} else {
//print_r($model->getErrors());
$msg = '<div class="alert alert-danger mt-3" role="alert">
<div class="alert-text">Failed to reset password. <br>Please try again.</div>
</div>';
}
} else {
$msg = '<div class="alert alert-danger mt-3" role="alert">
<div class="alert-text">' . $email_post . ' not found. <br>Please make sure the email address are correct.</div>
</div>';
}
}
return $this->render('forgot', ['msg' => $msg]);
}
public function actionRegister() {
$this->layout = 'login';
$msg = '';
$model = new User();
$model->scenario = 'register';
$model->uuid = Uuid::uuid4();
$model->status = 1;
$model->auth_key = \Yii::$app->security->generateRandomString();
$model->dept = 'dept07';
$model->role = 'USER';
$model->created_dt = date("Y-m-d H:i:s");
$model->created_by = $model->id;
$model->updated_dt = date("Y-m-d H:i:s");
$model->updated_by = $model->id;
if ($model->load(Yii::$app->request->post())) {
$model->pwd = \Yii::$app->security->generatePasswordHash($model->pwd);
if ($model->save()) {
$msg = '<div class="alert alert-success mt-3" role="alert">
<div class="alert-text">Account successfully registered.
<br>Please <a href="index.php?r=site/login">log in</a> using email address & password registered</div>
</div>';
}
}
return $this->render('register', ['model' => $model, 'msg' => $msg]);
}
public function actionFaq() {
$this->layout = 'public';
return $this->render('faq');
}
public function actionKnowledge() {
$this->layout = 'public';
return $this->render('knowledge');
}
}