| Current Path : /var/www/html/hr/controllers/ |
| Current File : /var/www/html/hr/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;
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() {
return $this->render('index');
}
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(5, 'login', '');
return $this->redirect(['dashboard/index']);
}
return $this->render('login', ['model' => $model,]);
}
public function actionLogout() {
$Log = new Log();
$Log->insertlog(6, '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;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>';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$send_email = mail($email_post, 'Reset Password', $email_content, $headers);
// if (Yii::$app->mailer->compose()
// ->setFrom(Yii::$app->params['senderEmail'])
// ->setTo($email_post)
// ->setSubject('Reset Password')
// ->setHtmlBody($email_content)
// ->send())
if ($send_email) {
$msg = '<div class="alert alert-success mt-3" role="alert">
<div class="alert-text">Password success to 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]);
}
}