Your IP : 216.73.216.79


Current Path : /var/www/html/akept/backend/controllers/
Upload File :
Current File : /var/www/html/akept/backend/controllers/SiteController.php

<?php

namespace backend\controllers;

use Yii;
//use backend\modules\mimin\components\AccessControl;
use yii\filters\AccessControl;
use common\models\User;
use yii\web\Controller;
use common\models\LoginForm;
use backend\models\PasswordResetRequestForm;
use backend\models\User as UserValidate;
use yii\filters\VerbFilter;
use backend\modules\audittrail\models\AuditTrailEntry;

//use yii\web\AssetBundle;
/**
 * Site controller
 */
class SiteController extends Controller {

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['logout', 'index', 'twofa'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                    [
                        'actions' => ['stub', 'login', 'twofa'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                //    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions() {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionIndex() {


        /**
         * if module dashboard has been setting to specific module  
         * config in backend/params 
         * */
        if ($this->loadDashboard()) {
            return $this->loadDashboard();
        }

        return $this->render('index');
    }

    public function actionLogin() {

        $this->layout = 'empty';

        if (!Yii::$app->user->isGuest) {
            $this->goBack();
        }
        $model = new LoginForm();
        $modelUser = new User();
        $modelForgotPass = new PasswordResetRequestForm;
        $modelValidate = new UserValidate();

        if ($model->load(Yii::$app->request->post()) && $model->login()) {


            return $this->goBack();
        } else {

            return $this->render('login', [
                        'model' => $model,
                        'modelUser' => $modelUser,
                        'modelForgotPass' => $modelForgotPass,
                        'validate' => $modelValidate,
            ]);
        }
    }

    public function actionLogout() {

        $modellog = new AuditTrailEntry();
        $modellog->model_type = 'logout';
        $modellog->happened_at = time();
        $modellog->foreign_pk = '{"id":' . Yii::$app->user->identity->id . '}';
        $modellog->user_id = Yii::$app->user->identity->id;
        $modellog->type = 'logout';
        $modellog->data = NULL;
        $modellog->save(false);

        Yii::$app->user->logout();

        return $this->goHome();
    }

    public function actionRequestPasswordReset() {

        $model = new PasswordResetRequestForm();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
                Yii::$app->session->setFlash('success', Yii::t('app', 'Sila periksa emel anda untuk tindakan seterusnya.'));
                return $this->goHome();
            }
            return $this->refresh();
        } else {

            return $this->render('requestPasswordResetToken', [
                        'model' => $model,
            ]);
        }
    }

    public function actionForgotPassword() {

        if (isset($_POST['email'])) {
            $mailTo = $_POST['email'];

            $checkUser = User::find()->where("email='$mailTo'")->one();
            if (count($checkUser) > 0) {
                Yii::$app->mailer->compose('email')
                        ->setFrom(Yii::$app->params['adminEmail'])
                        ->setTo($mailTo) //replace email sini untuk testing
                        ->setSubject('Admin - Terlupa kata laluan')
                        ->setHtmlBody('Kata Laluan baru: 12345qwer')
                        ->send();

                $checkUser->password_hash = Yii::$app->security->generatePasswordHash("12345qwer");
                $checkUser->update();
                return "BERJAYA\nAn emel telah dihantar.";
            } else {
                return "GAGAL\nEmel ini tidak wujud di dalam sistemxxxxx";
            }
        }
    }

    public function actionStub() {
        // return $this->render('stub');
    }

    public function actionTwofa($token) {

        $this->layout = false;

        return $this->render('login2fa', compact('token'));
    }

    private function loadDashboard() {

        if (!empty(Yii::$app->user->identity->id)) {

            $statuslogin = 1;
            $modelCheck = AuditTrailEntry::find()->where(['=', 'model_type', 'login'])->andwhere(['=', 'user_id', Yii::$app->user->identity->id])->orderby(['id' => SORT_DESC])->one();
            if (!empty($modelCheck)) {
                if (date("Y-m-d", $modelCheck->happened_at) == date('Y-m-d')) {
                    $statuslogin = 0;
                } else {
                    $statuslogin = 1;
                }
            }
            if ($statuslogin == 1) {
                $modellog = new AuditTrailEntry();
                $modellog->model_type = 'login';
                $modellog->happened_at = time();
                $modellog->foreign_pk = '{"id":' . Yii::$app->user->identity->id . '}';
                $modellog->user_id = Yii::$app->user->identity->id;
                $modellog->type = 'login';
                $modellog->data = NULL;
                $modellog->save(false);
            }
        }

        $config = \Yii::$app->params['dashboardModule'];

        if (!empty($config)) {
            return \Yii::$app->runAction($config['route'], $config['param']);
        } else {
            return false;
        }
    }

    public function actionFrameChatbot() {
        return $this->render('iframe-chatbot');
    }
}