UserController.php 6.47 KB
Newer Older
Indra Raja's avatar
Indra Raja committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
<?php
/**
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Application\Controller;

use Application\Master;
use Zend\View\Model\ViewModel;

class UserController extends \Application\Master\GlobalActionController
{
    public function __construct($headScript)
    {
        $this->headScript = $headScript;
    }

    public function indexAction() {
        $result = new Result(0, 403, 'invalid_controller');
        header('Content-Type: application/json');
        return $this->redirect()->toRoute('login');
    }

    public function loginAction(){
        try {
            
            $view = new ViewModel();

            $session        = $this->getSession();
            $token_keamanan = md5(uniqid(mt_rand(), true));
            
            error_log('$token_keamanan: ' . $token_keamanan);
            
            if($session) {
                $session->put(null, array('token_keamanan' => $token_keamanan));
                $message = $session->get('message');
            } else {
                $message = 'Tidak dapat mengakses database. Mohon ulangi beberapa saat lagi.';
            }

            $view->setVariables(array(
                'message'           => $message,
                'token_keamanan'    => $token_keamanan
            ));

            //$view->setTerminal(true);
            
            $this->layout("layout/layoutUmum");
            
            return $view;
        } catch (Exception $ex) {
            error_log($ex->getMessage());
            return $this->redirect()->toRoute('login');
        }
    }

    public function authenticateAction(){

        try {
            $uri     = $this->getRequest()->getUri();
            $baseurl = sprintf('//%s', $uri->getHost());

            $post    = $this->getRequest()->getPost();
            $session = $this->getSession();
        
            // if (strlen('' . $post['token_keamanan']) <= 0 || $post['token_keamanan'] <> $session->get('token_keamanan')) {
            //     $message = htmlspecialchars('Token login tidak sesuai. Mohon ulangi.', ENT_QUOTES, 'UTF-8');
            //     $session->put(null, array('message' => $message));
            //     return $this->redirect()->toRoute('login');
            // }

            $username = $post['username'];
            $password = $post['passwd'];

            $storage = \Khansia\Access\User\Storage::factory($this->getDb(), $this->getConfig());
            $user    =  new \Khansia\Access\User($storage);
                
            if($user->load($username,  \Khansia\Access\User\Storage::LOADBY_CODE)){ // sukses load then

                $authResult = $user->authenticate($password, null, \Khansia\Access\User::RETRIES_TRUE);

                if($authResult->code == $authResult::CODE_SUCCESS) {

                    $session->owner($user->id);
                    
                    /* get access role */
                    $access = $user->loadAccess($user->id);
                    
                    $accessArray = array();
                    
                    foreach($access->data as $data=> $val){

                        if($val['access_status'] == 'TRUE'){
                            $newStat = true;
                        }else{
                            $newStat = false;
                        }
                        $accessArray[$val['access_code']] = $newStat;
                    
                    }

                    $session->put(null, array(
                        'baseurl'           => $baseurl,
                        'user_id'           => $user->id,
                        'usernamed'         => $user->username,
                        'passwd'            => $user->password,
                        'name'              => $user->name,
                        'role'              => $user->role,
                        'status'            => $user->status,
                        'deviceid'          => $user->deviceid,
                        'token'             => $user->token,
                        'retries'           => $user->retries,
                        'create_dtm'        => $user->create_dtm,
                        'access'            => $accessArray,
                        'role_code'         => $access->data[0]['role_code'],
                        
                    ));

                    $session->flush();

                    /* direct data */
                    return $this->redirect()->toRoute('dashboard');
                }else{

                    switch($authResult->code) {
                        case \Khansia\Access\User::CODE_AUTH_INVALID:
                            $authMessage = 'User tidak valid';
                            break;
                        case \Khansia\Access\User::CODE_AUTH_SUSPEND:
                            $authMessage = 'User ditangguhkan';
                            break;
                        case \Khansia\Access\User::CODE_AUTH_LOCKED:
                            $authMessage = 'User tidak aktif';
                            break;
                        case \Khansia\Access\User::CODE_AUTH_FAILED:
                            $authMessage = 'Password tidak sesuai';
                            break;
                    }

                    $message = htmlspecialchars($authMessage, ENT_QUOTES, 'UTF-8');

                    $session->put(null, array('message' => $message));
                    
                    return $this->redirect()->toRoute('login');
                }
            }else{
                
                $session = $this->getSession();
                
                $session->put(null, array('message' => "incorrect username or passowrd "));
    
                return $this->redirect()->toRoute('login');
            }

        } catch (\Exception $ex) {
            $session = $this->getSession();
            $message = htmlspecialchars($ex->getMessage(), ENT_QUOTES, 'UTF-8');
            
            $session->put(null, array('message' => $message));

            return $this->redirect()->toRoute('login');
        }        
    }

    public function logoutAction() {
        try {
            $session = $this->getSession();
            $session->stop();
            
            return $this->redirect()->toRoute('login');
        } catch (\Exception $ex) {
            return $this->redirect()->toRoute('login');
        }
    }


}