<?php

class SinglesignonController extends Controller {

    public $layout = "";

    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('login', 'error', 'logout'),
                'users' => array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions' => array('Index'),
                'expression' => '$user->isMember()',
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    // Uncomment the following methods and override them if needed

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function actionIndex() {
        $hr_admin_portals = [5];
        $portaId = Yii::app()->request->getParam('id');
        $user_id = Yii::app()->user->id;
        $model = MasPortal::model()->findByPk($portaId);
        if (in_array($portaId, $hr_admin_portals)) {
            $userModel = User::model()->findByPk($user_id);
            $emp_id = $userModel->employee_id;
            $condition = "";
            if ($portaId == 5) {
                /*
                 *
                 *
                 * For Level = 1 FOR HR ADMIN
                 *
                 */
                $condition = " hr_connected_employee_id = {$emp_id}";
                $condition .= " AND level = 1 ";
            } else {
                /*
                 * in other case LEVEL
                 * */
                $condition = " hr_connected_employee_id  = {$emp_id}";
                $condition .= " AND level = 2 ";
            }

            $userModel = User::model()->find(["condition" => $condition]);
            $user_id = $userModel->id;
        }
//        $condition = "portal_id=" . $portaId . " AND user_id=" . $user_id;
//        $userportal = UserPortal::model()->find(["condition" => $condition]);
//        if ($userportal == null || $userportal->isActive != 'Y') {
//            throw new CHttpException(403, "Permission denied. You have no permission to access this portal.");
//            die();
//        }
        $hr_db_main = GeneralSettings::getValue("NIA_MAIN_DB");
        $sql = "SELECT " . $model->identity_field . " FROM $hr_db_main.erp_user WHERE id = {$user_id}";
//        echo $sql;
        $emp_id = Yii::app()->db->createCommand($sql)->queryScalar();
//        echo " emp_id = " . $emp_id;
//        die();
        $ssoHelper = new SsoHelper($emp_id, $portaId, $model->identity_field);
        $result = $ssoHelper->getToken();
//        print_r($result);
//        die();
        if ($result["status"] == "success" && !empty($emp_id)) {
            $model = MasPortal::model()->findByPk($portaId);
            $url = $model->return_url . SsoHelper::LOGIN_ENDPOINT . "&u=$emp_id&token=" . $result['data']["token"] . "&t=" . $model->identity_field;
//            echo $url;
            $this->redirect($url);
        } else {

            throw new CHttpException(403, 'Permission denied');
        }
    }

}
