Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\modules\auth\controllers;
- use Yii;
- use yii\db\Query;
- use yii\web\Controller;
- use yii\helpers\ArrayHelper;
- use app\mylibs\myrbac;
- use yii\filters\AccessControl;
- use app\repomodels\repoSuccession;
- class DefaultController extends Controller
- {
- public $layout = '@app/modules/layouts/main-admin';
- public $lisfungsiakses = array();
- public function behaviors()
- {
- $myrbac = new myrbac();
- $module = Yii::$app->controller->module->id;
- $controller = Yii::$app->controller->id;
- $controllerid = 'app/modules/' . $module . '/controllers/' . ucfirst($controller) . 'Controller.php';
- $listfungsi = $myrbac->newvalidation($module, $controllerid);
- if ($listfungsi == null) {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- //'only' => $listfungsi,
- 'rules' => [
- [
- 'allow' => false,
- 'roles' => ['@'],
- ],
- ],
- ],
- ];
- } else {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- //'only' => $listfungsi,
- 'rules' => [
- [
- 'actions' => $listfungsi,
- 'allow' => true,
- 'roles' => ['@'],
- ],
- [
- 'allow' => false,
- 'roles' => ['?']
- ]
- ],
- ],
- ];
- }
- }
- public function actionIndex() {
- $query = (new Query())
- ->select(['users.username','role.coderole','users.id','role.id_role'])
- ->from('users')
- ->join('join','role','role.id_role = users.role')
- ->orderBy(['users.username'=>SORT_ASC]);
- $data['attruser'] = $query->createCommand()->queryAll();
- $data['addlink'] = "/auth/default/adduser";
- $data['editlink'] = "/auth/default/edituser";
- $data['deletelink'] = "/auth/default/deleteuser";
- return $this->render('index',$data);
- }
- public function actionAdduser($id = null , $id_role = null) {
- $identity = Yii::$app->user->identity;
- $reposuc = new repoSuccession();
- $getAuth = $reposuc->getTauth()->andWhere(['T_AUTH.PERNR'=>$identity->username]);
- $getUnit = $reposuc->getUnit()->andWhere(['in','T_ROLE_TO_DIVISI.ROLE',$getAuth]);
- $getUnit2 = $reposuc->getUnit()->andWhere(['in','T_ROLE_TO_DIVISI.ROLE',$getAuth])->all();
- // $query = "select * from T_ROLE_TO_DIVISI where ROLE like concat('%-', '".implode("','", $getUnit2)."')";
- // $create_query = \Yii::$app->db->createCommand($query);
- // $result = $create_query->getRawSql();
- $cariuser = \app\models\Users::findOne(['id'=>$id]);
- $data['model'] = new \app\models\Users();
- $identity = Yii::$app->user->identity;
- $find = $identity->findAuth($identity->username);
- $array_unit = [];
- for($i=0; $i<count($getUnit2); $i++){
- array_push($array_unit, $getUnit2[$i]['CDDIV']);
- }
- $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-%')";
- $create_query2 = \Yii::$app->db->createCommand($query2);
- $result2 = $create_query2->queryAll();
- $array_unit2 =[];
- for ($j=0; $j < count($result2); $j++) {
- if(strpos(($result2[$j]['ROLE']), 'ADM-HCP') === false){
- array_push($array_unit2, $result2[$j]['ROLE']);
- }else{
- array_push($array_unit2, $result2[$j]['ROLE'].' - '.$result2[$j]['CDDIV'] );
- }
- }
- $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%')";
- $create_query3 = \Yii::$app->db->createCommand($query3);
- $result3 = $create_query3->queryAll();
- $array_unit3 =[];
- for ($j=0; $j < count($result3); $j++) {
- array_push($array_unit3, $result3[$j]['ROLE']);
- }
- $cek_hcd = strpos(("ADM-".$getUnit2[0]['CDDIV']), 'ADM-HCD');
- $cek_hcp = strpos(("ADM-".$getUnit2[0]['CDDIV']), 'ADM-HCP');
- if(in_array("ADM-".$getUnit2[0]['CDDIV'], $array_unit) && $cek_hcd === false && $cek_hcp === false ){
- $data['adm'] = true;
- $key = array_search("KTU-".$getUnit2[0]['CDDIV'], $array_unit);
- $data['listrole'] = $array_unit[$key];
- }elseif($cek_hcd !== false){
- $data['adm'] = false;
- $data['listrole'] = $array_unit2;
- }elseif($cek_hcp === false) {
- $data['adm'] = false;
- $data['listrole'] = $array_unit3;
- }else{
- $data['adm'] = false;
- $data['listrole'] = $array_unit;
- }
- $username = \app\models\CvPersonal::find()->where(['unit'=> $getUnit])->all();
- if($username == null){
- $username = \app\models\CvPersonal::find()->all();
- }else{
- $username = \app\models\CvPersonal::find()->where(['unit'=> $getUnit])->all();
- }
- $data['listusername'] = $username;
- $data['access'] = ['LDAP' => 'LDAP', 'REGISTER' => 'REGISTER'];
- if ($_POST) {
- if (empty($id)) {
- $role = $_POST['ROLE'];
- // $FT = \app\models\FTTRPT0001::find()->where('N_NIK=:nik',[':nik'=>$role['username']])->one();
- // if (empty($FT)) {
- // Yii::$app->session->setFlash('warning', "Sorry NIK undefined");
- // return $this->redirect(['/auth/default/index']);
- // } else {
- $cek_adm = \app\models\Users::find()->where('username=:nik',[':nik'=>$role['username']])->one();
- $role_name = \app\models\Role::find()->where('id_role=:role',[':role'=>$role['role_id']])->one();
- if (empty($cek_adm)) {
- $new_user = new \app\models\Users();
- $new_user->username=$role['username'];
- $new_user->password=SHA1($role['password']);
- $new_user->email=$role['email'];
- $new_user->role=$role['role_id'];
- $new_user->accessToken=($role['accessToken'] == 'LDAP') ? 'NIK': 'MANUAL';
- $new_user->created_at= date('Y-m-d H:i:s');
- $new_user->updated_at = date('Y-m-d H:i:s',strtotime('9999-12-31 00:00:00'));
- $new_user->buscd = $role_name->coderole;
- if ($new_user->save()) {
- Yii::$app->session->setFlash('success', "Success Save");
- } else {
- Yii::$app->session->setFlash('warning', "Sorry NIK undefined");
- }
- } else {
- $new_user = new \app\models\Users();
- $new_user->username=$role['username'];
- $new_user->password=SHA1($role['password']);
- $new_user->email=$role['email'];
- $new_user->role=$role['role_id'];
- $new_user->accessToken=($role['accessToken'] == 'LDAP') ? 'NIK': 'MANUAL';
- $new_user->created_at= date('Y-m-d H:i:s');
- $new_user->updated_at = date('Y-m-d H:i:s',strtotime('9999-12-31 00:00:00'));
- $new_user->buscd = $role_name->coderole;
- if ($new_user->save()) {
- $new_auth = new \app\models\TAuth();
- $new_auth->BEGDA = date('Y-m-d');
- $new_auth->ENDDA = date('Y-m-d', strtotime('9999-12-31'));
- $new_auth->BUSCD = 'TLKM';
- $new_auth->APLID = 'A009';
- $new_auth->ROLE = $role['role_id'];
- $new_auth->PERNR = $role['username'];
- $new_auth->RULE = '';
- $new_auth->DATA = '';
- $new_auth->CRTDT = date('Y-m-d');
- $new_auth->USRDT = Yii::$app->user->identity->username;
- $new_auth->save();
- $qryfn = (new Query())
- ->select(['JENCD'])
- ->from('v_role_to_divisi')->where(['coderole' => $role_name->coderole])->groupBy(['JENCD']);
- $qryfngroup = (new Query())
- ->from('role_fungsi_group')
- ->where(['in', 'JENCD', $qryfn])->all();
- foreach ($qryfngroup as $qg):
- $new_fungsi = new \app\models\RoleFungsiAkses();
- $new_fungsi->role_fungsi_id = $qg['id_fungsi'];
- $new_fungsi->role_id =$role['role_id'];
- $new_fungsi->save();
- $cek_adm->updated_at = $new_user->created_at;
- $cek_adm->save();
- endforeach;
- Yii::$app->session->setFlash('success', "Success Save");
- } else {
- Yii::$app->session->setFlash('warning', "Sorry NIK undefined");
- }
- }
- // }
- return $this->redirect(['/auth/default/index']);
- }
- }
- return $this->renderAjax('_roleuser', $data);
- }
- public function actionEdituser($id) {
- $model = \app\models\Users::find()->where(['id' => $id])->asArray()->one();
- $listrole = \app\models\Role::find()->all();
- $access = ['LDAP' => 'LDAP', 'REGISTER' => 'REGISTER'];
- return $this->renderAjax('_roleuser', ['model' => $model, 'listrole'=> $listrole, 'access' => $access]);
- // $find_user->username = 'tes';
- // $find_user->email = 'tes';
- // $find_user->password = 'tes';
- // $find_user->accessToken = 'tes';
- // $find_user->role = 'tes';
- // die(var_dump($find_user));
- // if($find_user->save()){
- // return $this->redirect(['auth/default/index']);
- // }else{
- // return $this->renderAjax('_roleuser', $find_user);
- // }
- }
- public function actionDeleteuser($id) {
- $find_user = \app\models\Users::find()->where(['id' => $id])->one();
- $find_user->delete();
- return $this->redirect(['default/index']);
- }
- public function actionIndexRole(){
- $sql = "select * from role";
- $query = \Yii::$app->db->createCommand($sql);
- $result = $query->queryAll();
- return $this->render('index_role', [
- 'result' => $result
- ]);
- }
- public function actionAddusertorole($id){
- $obj_role = \app\models\Role::findOne(['id_role'=>$id]);
- $listuser = \app\models\Users::find()->all();
- return $this->renderAjax('listuser', ['listuser' => $listuser]);
- }
- public function actionCreaterole(){
- $model = new \app\models\Role();
- return $this->renderAjax('_createrole', ['model' => $model]);
- }
- public function actionSavecreate(){
- if(
- isset($_POST['controller']) &&
- isset($_POST['name'])
- ){
- $name = $_POST['controller'];
- $coderole = $_POST['name'];
- $model = new \app\models\Role();
- $model->coderole = $coderole;
- $model->name = $name;
- $model->save();
- }
- }
- public function actionFunctionUser(){
- $data['listrole'] = \app\models\Role::find()->all();
- return $this->renderAjax('_chooserole');
- }
- public function actionSetting($id){
- $obj_role = \app\models\Role::find()->where(['id_role' => $id])->one();
- $obj_menu = \app\models\RoleMenu::find()->all();
- $obj_controller = \app\models\RoleController::find()->all();
- $obj_function = \app\models\RoleFungsi::find()->all();
- $menu = \yii\helpers\ArrayHelper::map($obj_menu, 'menu_name', 'menu_name');
- $controller = \yii\helpers\ArrayHelper::map($obj_controller, 'controller_name', 'controller_name');
- $function = \yii\helpers\ArrayHelper::map($obj_function, 'function_name', 'function_name');
- return $this->renderAjax('setting', ['menu' => $menu, 'controller' => $controller, 'function' => $function, 'obj_role' => $obj_role]);
- }
- public function actionSavesetting(){
- if(
- isset($_POST['id_role']) &&
- isset($_POST['menu']) &&
- isset($_POST['controller']) &&
- isset($_POST['fungsi'])
- ){
- $id = $_POST['id_role'];
- $menu = $_POST['menu'];
- $controller = $_POST['controller'];
- $fungsi = $_POST['fungsi'];
- $model = \app\models\Role::find()->where(['id_role' => $id])->one();
- if($model != null){
- return [
- 'success' => true,
- 'message' => $model
- ];
- }else{
- return [
- 'success' => false,
- 'message' => $model->errors
- ];
- }
- }
- }
- public static function getControllers(){
- $files = scandir(\Yii::getAlias('@app') . DIRECTORY_SEPARATOR . 'modules');
- $output = [];
- for ($i=2;$i<count($files);$i++){
- $output[] = [
- 'value' => ucwords($files[$i]),
- 'title' =>ucwords($files[$i]),
- ];
- }
- return $output;
- }
- public function actionGetactions($c){
- $class = new \ReflectionClass('app\modules\\'.$c.'\controllers\DefaultController');
- $method = $class->getMethods(\ReflectionMethod::IS_PUBLIC);
- $output = [];
- foreach ($method as $v){
- if (substr($v->name, 0, 6) == 'action'){
- $output[] = (substr($v->name, 6));
- }
- }
- foreach ($output as $v){
- if ($v !== 's'){
- echo '<option value="'.$v.'">'.$v.'</option>';
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement