Advertisement
Guest User

Untitled

a guest
Mar 26th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.39 KB | None | 0 0
  1. <?php
  2. namespace app\modules\auth\controllers;
  3. use Yii;
  4. use yii\db\Query;
  5. use yii\web\Controller;
  6. use yii\helpers\ArrayHelper;
  7. use app\mylibs\myrbac;
  8. use yii\filters\AccessControl;
  9. use app\repomodels\repoSuccession;
  10. class DefaultController extends Controller
  11. {
  12.     public $layout = '@app/modules/layouts/main-admin';
  13.     public $lisfungsiakses = array();
  14.    
  15.     public function behaviors()
  16.     {
  17.         $myrbac = new myrbac();
  18.         $module = Yii::$app->controller->module->id;
  19.         $controller = Yii::$app->controller->id;
  20.         $controllerid = 'app/modules/' . $module . '/controllers/' . ucfirst($controller) . 'Controller.php';
  21.         $listfungsi = $myrbac->newvalidation($module, $controllerid);
  22.         if ($listfungsi == null) {
  23.             return [
  24.                 'access' => [
  25.                     'class' => AccessControl::className(),
  26.                     //'only' => $listfungsi,
  27.                     'rules' => [
  28.                         [
  29.                             'allow' => false,
  30.                             'roles' => ['@'],
  31.                         ],
  32.                     ],
  33.                 ],
  34.             ];
  35.         } else {
  36.             return [
  37.                 'access' => [
  38.                     'class' => AccessControl::className(),
  39.                     //'only' => $listfungsi,
  40.                     'rules' => [
  41.                         [
  42.                             'actions' => $listfungsi,
  43.                             'allow' => true,
  44.                             'roles' => ['@'],
  45.                         ],
  46.  
  47.                         [
  48.                             'allow' => false,
  49.                             'roles' => ['?']
  50.                         ]
  51.                     ],
  52.                 ],
  53.             ];
  54.         }
  55.     }
  56.  
  57.     public function actionIndex() {
  58.         $query = (new Query())
  59.                 ->select(['users.username','role.coderole','users.id','role.id_role'])
  60.                 ->from('users')
  61.                 ->join('join','role','role.id_role = users.role')
  62.                 ->orderBy(['users.username'=>SORT_ASC]);
  63.         $data['attruser'] = $query->createCommand()->queryAll();
  64.         $data['addlink'] = "/auth/default/adduser";
  65.         $data['editlink'] = "/auth/default/edituser";
  66.         $data['deletelink'] = "/auth/default/deleteuser";
  67.         return $this->render('index',$data);
  68.     }
  69.  
  70.     public function actionAdduser($id = null , $id_role = null) {
  71.         $identity = Yii::$app->user->identity;
  72.         $reposuc = new repoSuccession();
  73.         $getAuth = $reposuc->getTauth()->andWhere(['T_AUTH.PERNR'=>$identity->username]);
  74.         $getUnit = $reposuc->getUnit()->andWhere(['in','T_ROLE_TO_DIVISI.ROLE',$getAuth]);
  75.         $getUnit2 = $reposuc->getUnit()->andWhere(['in','T_ROLE_TO_DIVISI.ROLE',$getAuth])->all();
  76.  
  77.         // $query = "select * from T_ROLE_TO_DIVISI where ROLE like concat('%-', '".implode("','", $getUnit2)."')";
  78.   //       $create_query = \Yii::$app->db->createCommand($query);
  79.   //       $result = $create_query->getRawSql();
  80.  
  81.         $cariuser = \app\models\Users::findOne(['id'=>$id]);
  82.         $data['model'] = new \app\models\Users();
  83.         $identity = Yii::$app->user->identity;
  84.         $find = $identity->findAuth($identity->username);
  85.         $array_unit = [];
  86.         for($i=0; $i<count($getUnit2); $i++){
  87.             array_push($array_unit, $getUnit2[$i]['CDDIV']);
  88.         }
  89.  
  90.         $query2 = "select * from T_ROLE_TO_DIVISI where ROLE not like concat('ADM-HCD') and ROLE not like concat('KTU-%') and ROLE not like concat('MAN-%')";
  91.         $create_query2 = \Yii::$app->db->createCommand($query2);
  92.         $result2 = $create_query2->queryAll();
  93.         $array_unit2 =[];
  94.         for ($j=0; $j < count($result2); $j++) {
  95.             if(strpos(($result2[$j]['ROLE']), 'ADM-HCP') === false){
  96.                 array_push($array_unit2, $result2[$j]['ROLE']);                            
  97.             }else{
  98.                 array_push($array_unit2, $result2[$j]['ROLE'].' - '.$result2[$j]['CDDIV'] );
  99.             }
  100.         }
  101.  
  102.         $query3 = "select * from T_ROLE_TO_DIVISI where CDDIV IN ('".implode("','", $array_unit)."') and ROLE not like concat('KTU-%') and ROLE not like concat('ADM-HCP%')";
  103.         $create_query3 = \Yii::$app->db->createCommand($query3);
  104.         $result3 = $create_query3->queryAll();
  105.         $array_unit3 =[];
  106.         for ($j=0; $j < count($result3); $j++) {
  107.             array_push($array_unit3, $result3[$j]['ROLE']);
  108.         }
  109.  
  110.         $cek_hcd = strpos(("ADM-".$getUnit2[0]['CDDIV']), 'ADM-HCD');
  111.         $cek_hcp = strpos(("ADM-".$getUnit2[0]['CDDIV']), 'ADM-HCP');
  112.  
  113.         if(in_array("ADM-".$getUnit2[0]['CDDIV'], $array_unit) && $cek_hcd === false && $cek_hcp === false ){
  114.             $data['adm'] = true;
  115.             $key = array_search("KTU-".$getUnit2[0]['CDDIV'], $array_unit);
  116.             $data['listrole'] = $array_unit[$key];
  117.         }elseif($cek_hcd !== false){
  118.             $data['adm'] = false;
  119.             $data['listrole'] = $array_unit2;
  120.         }elseif($cek_hcp === false) {
  121.             $data['adm'] = false;
  122.             $data['listrole'] = $array_unit3;
  123.         }else{
  124.             $data['adm'] = false;
  125.             $data['listrole'] = $array_unit;
  126.         }
  127.        
  128.         $username = \app\models\CvPersonal::find()->where(['unit'=> $getUnit])->all();
  129.         if($username == null){
  130.             $username = \app\models\CvPersonal::find()->all();
  131.         }else{
  132.             $username = \app\models\CvPersonal::find()->where(['unit'=> $getUnit])->all();
  133.         }
  134.  
  135.         $data['listusername'] = $username;
  136.         $data['access'] = ['LDAP' => 'LDAP', 'REGISTER' => 'REGISTER'];
  137.         if ($_POST) {
  138.             if (empty($id)) {
  139.                 $role = $_POST['ROLE'];
  140. //              $FT = \app\models\FTTRPT0001::find()->where('N_NIK=:nik',[':nik'=>$role['username']])->one();
  141. //              if (empty($FT)) {
  142. //                  Yii::$app->session->setFlash('warning', "Sorry NIK undefined");
  143. //                  return $this->redirect(['/auth/default/index']);
  144. //              } else {
  145.                     $cek_adm = \app\models\Users::find()->where('username=:nik',[':nik'=>$role['username']])->one();
  146.                     $role_name = \app\models\Role::find()->where('id_role=:role',[':role'=>$role['role_id']])->one();
  147.                     if (empty($cek_adm)) {
  148.                         $new_user = new \app\models\Users();
  149.                         $new_user->username=$role['username'];
  150.                         $new_user->password=SHA1($role['password']);
  151.                         $new_user->email=$role['email'];
  152.                         $new_user->role=$role['role_id'];
  153.                         $new_user->accessToken=($role['accessToken'] == 'LDAP') ? 'NIK': 'MANUAL';
  154.                         $new_user->created_at= date('Y-m-d H:i:s');
  155.                         $new_user->updated_at = date('Y-m-d H:i:s',strtotime('9999-12-31 00:00:00'));
  156.                         $new_user->buscd = $role_name->coderole;
  157.                         if ($new_user->save()) {
  158.                             Yii::$app->session->setFlash('success', "Success Save");
  159.                         } else {
  160.                             Yii::$app->session->setFlash('warning', "Sorry NIK undefined");
  161.                         }
  162.                     } else {
  163.                         $new_user = new \app\models\Users();
  164.                         $new_user->username=$role['username'];
  165.                         $new_user->password=SHA1($role['password']);
  166.                         $new_user->email=$role['email'];
  167.                         $new_user->role=$role['role_id'];
  168.                         $new_user->accessToken=($role['accessToken'] == 'LDAP') ? 'NIK': 'MANUAL';
  169.                         $new_user->created_at= date('Y-m-d H:i:s');
  170.                         $new_user->updated_at = date('Y-m-d H:i:s',strtotime('9999-12-31 00:00:00'));
  171.                         $new_user->buscd = $role_name->coderole;
  172.                         if ($new_user->save()) {
  173.                             $new_auth = new \app\models\TAuth();
  174.                             $new_auth->BEGDA = date('Y-m-d');
  175.                             $new_auth->ENDDA = date('Y-m-d', strtotime('9999-12-31'));
  176.                             $new_auth->BUSCD = 'TLKM';
  177.                             $new_auth->APLID = 'A009';
  178.                             $new_auth->ROLE = $role['role_id'];
  179.                             $new_auth->PERNR = $role['username'];
  180.                             $new_auth->RULE = '';
  181.                             $new_auth->DATA = '';
  182.                             $new_auth->CRTDT = date('Y-m-d');
  183.                             $new_auth->USRDT = Yii::$app->user->identity->username;
  184.                             $new_auth->save();
  185.  
  186.                             $qryfn = (new Query())
  187.                                 ->select(['JENCD'])
  188.                                 ->from('v_role_to_divisi')->where(['coderole' => $role_name->coderole])->groupBy(['JENCD']);
  189.                             $qryfngroup = (new Query())
  190.                                 ->from('role_fungsi_group')
  191.                                 ->where(['in', 'JENCD', $qryfn])->all();
  192.                             foreach ($qryfngroup as $qg):
  193.                                 $new_fungsi = new \app\models\RoleFungsiAkses();
  194.                                 $new_fungsi->role_fungsi_id = $qg['id_fungsi'];
  195.                                 $new_fungsi->role_id =$role['role_id'];
  196.                                 $new_fungsi->save();
  197.                                 $cek_adm->updated_at = $new_user->created_at;
  198.                                 $cek_adm->save();
  199.                             endforeach;
  200.                             Yii::$app->session->setFlash('success', "Success Save");
  201.                         } else {
  202.                             Yii::$app->session->setFlash('warning', "Sorry NIK undefined");
  203.                         }
  204.                     }
  205. //              }
  206.                 return $this->redirect(['/auth/default/index']);
  207.             }
  208.            
  209.  
  210.         }
  211.         return $this->renderAjax('_roleuser', $data);
  212.  
  213.     }
  214.     public function actionEdituser($id) {
  215.         $model = \app\models\Users::find()->where(['id' => $id])->asArray()->one();
  216.         $listrole = \app\models\Role::find()->all();
  217.         $access = ['LDAP' => 'LDAP', 'REGISTER' => 'REGISTER'];
  218.         return $this->renderAjax('_roleuser', ['model' => $model, 'listrole'=> $listrole, 'access' => $access]);
  219.         // $find_user->username = 'tes';
  220.         // $find_user->email = 'tes';
  221.         // $find_user->password = 'tes';
  222.         // $find_user->accessToken = 'tes';
  223.         // $find_user->role = 'tes';
  224.         // die(var_dump($find_user));
  225.         // if($find_user->save()){
  226.         //  return $this->redirect(['auth/default/index']);
  227.         // }else{
  228.         //  return $this->renderAjax('_roleuser', $find_user);         
  229.         // }   
  230.     }
  231.     public function actionDeleteuser($id) {
  232.         $find_user = \app\models\Users::find()->where(['id' => $id])->one();
  233.         $find_user->delete();
  234.         return $this->redirect(['default/index']);
  235.     }
  236.  
  237.     public function actionIndexRole(){
  238.         $sql = "select * from role";
  239.         $query = \Yii::$app->db->createCommand($sql);
  240.         $result = $query->queryAll();
  241.         return $this->render('index_role', [
  242.             'result' => $result
  243.         ]);
  244.     }
  245.  
  246.     public function actionAddusertorole($id){
  247.         $obj_role = \app\models\Role::findOne(['id_role'=>$id]);
  248.         $listuser = \app\models\Users::find()->all();
  249.         return $this->renderAjax('listuser', ['listuser' => $listuser]);
  250.     }
  251.  
  252.     public function actionCreaterole(){
  253.         $model = new \app\models\Role();
  254.         return $this->renderAjax('_createrole', ['model' => $model]);
  255.     }
  256.  
  257.     public function actionSavecreate(){
  258.         if(
  259.             isset($_POST['controller']) &&
  260.             isset($_POST['name'])
  261.         ){
  262.             $name = $_POST['controller'];
  263.             $coderole = $_POST['name'];
  264.  
  265.             $model = new \app\models\Role();
  266.             $model->coderole = $coderole;
  267.             $model->name = $name;
  268.             $model->save();
  269.         }
  270.     }
  271.  
  272.     public function actionFunctionUser(){
  273.         $data['listrole'] = \app\models\Role::find()->all();
  274.         return $this->renderAjax('_chooserole');
  275.     }
  276.  
  277.     public function actionSetting($id){
  278.         $obj_role = \app\models\Role::find()->where(['id_role' => $id])->one();
  279.         $obj_menu = \app\models\RoleMenu::find()->all();
  280.         $obj_controller = \app\models\RoleController::find()->all();
  281.         $obj_function = \app\models\RoleFungsi::find()->all();
  282.        
  283.         $menu = \yii\helpers\ArrayHelper::map($obj_menu, 'menu_name', 'menu_name');
  284.         $controller = \yii\helpers\ArrayHelper::map($obj_controller, 'controller_name', 'controller_name');
  285.         $function = \yii\helpers\ArrayHelper::map($obj_function, 'function_name', 'function_name');
  286.        
  287.         return $this->renderAjax('setting', ['menu' => $menu, 'controller' => $controller, 'function' => $function, 'obj_role' => $obj_role]);
  288.     }
  289.  
  290.     public function actionSavesetting(){
  291.         if(
  292.             isset($_POST['id_role']) &&
  293.             isset($_POST['menu']) &&
  294.             isset($_POST['controller']) &&
  295.             isset($_POST['fungsi'])
  296.         ){
  297.             $id = $_POST['id_role'];
  298.             $menu = $_POST['menu'];
  299.             $controller = $_POST['controller'];
  300.             $fungsi = $_POST['fungsi'];
  301.             $model = \app\models\Role::find()->where(['id_role' => $id])->one();
  302.  
  303.             if($model != null){
  304.                 return [
  305.                     'success' => true,
  306.                     'message' => $model
  307.                 ];
  308.             }else{
  309.                 return [
  310.                     'success' => false,
  311.                     'message' => $model->errors
  312.                 ];
  313.             }
  314.         }
  315.     }
  316.  
  317.     public static function getControllers(){
  318.         $files = scandir(\Yii::getAlias('@app') . DIRECTORY_SEPARATOR . 'modules');
  319.         $output = [];
  320.         for ($i=2;$i<count($files);$i++){
  321.                 $output[] = [
  322.                   'value' => ucwords($files[$i]),
  323.                   'title' =>ucwords($files[$i]),
  324.                 ];
  325.         }
  326.         return $output;
  327.     }
  328.    
  329.     public function actionGetactions($c){    
  330.         $class = new \ReflectionClass('app\modules\\'.$c.'\controllers\DefaultController');
  331.         $method = $class->getMethods(\ReflectionMethod::IS_PUBLIC);
  332.         $output = [];
  333.         foreach ($method as $v){
  334.             if (substr($v->name, 0, 6) == 'action'){
  335.                 $output[] = (substr($v->name, 6));
  336.             }
  337.         }
  338.  
  339.         foreach ($output as $v){
  340.             if ($v !== 's'){
  341.                 echo '<option value="'.$v.'">'.$v.'</option>';
  342.             }
  343.         }
  344.     }
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement