User.php 4.48 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
<?php

namespace Khansia\Access;

use Khansia\Generic\Result;
use Khansia\Generic\Objects\Map;

/* extends dari mapper agar bisa mapping properti class ke tabel */
class User extends \Khansia\Generic\Objects\Mapper {

    const STATUS_ACTIVE     = 10;
    const STATUS_NOTACTIVE  = 20;
    const STATUS_BLOCKED    = 30;

    const CODE_AUTH_INVALID = 31;
    const CODE_AUTH_SUSPEND = 32;
    const CODE_AUTH_LOCKED  = 33;
    const CODE_AUTH_FAILED  = 34;

    const RETRIES_TRUE      = TRUE;
    const RETRIES_FALSE     = FALSE;

    protected $_storage;
    protected $_config;
    protected $_loaded = false;

    public function __construct(User\Storage\Skeleton $storage) {

        /* simpan user storage */
        $this->_storage = $storage;

        /* load config */
        //$this->_config = $this->_storage->fetchConfig('USER');
        /*
          map properti class ke tabel
          sehingga nama field di tabel dapat diwakili oleh property class
        */
        parent::__construct(
            array(),
            array(
                new Map('iduser','id'),
                new Map('username'),
                new Map('password'),
                new Map('name'),
                new Map('role'),
                new Map('status'),
                new Map('deviceid'),
                new Map('token'),
                new Map('create_dtm'),
                new Map('retries'),
            ),
            parent::CASE_INSENSITIVE
        );
    }

    /**
      save
    */
    public function save($update = false) {

      /* simpan via storage */
      $result = $this->_storage->save($this, $update);
        if ($result->code == 0) {
            $this->id = $result->data;
        }
        return $result;
    }

    public function load($id, $mode = User\Storage::LOADBY_ID){
       
        /* load dari storage */
        if ($data = $this->_storage->load($id, $mode)) {

            /* load sukses, set data properti class dari hasil query */
            $this->push($data);            

            /* set loaded */
            $this->_loaded = true;
            return true;

        } else {

            $this->_loaded = false;

        }
    }

    public function loadAccess($id){

        /* load access data */
        $acc = $this->_storage->getAccess($id);
        return $acc;

    }

    public function authenticate($credential, $data = array(), $retriesMode = self::RETRIES_FALSE){
        $result = new Result();  
        
        $retries = 0;

        /* loaded */
        if ($this->id && $this->_loaded) {

            /* jika user aktif */
            if (($this->status == self::STATUS_ACTIVE)) {
                
                if($this->password == md5($credential)){
                    $authenticated = true;
                }else{
                    $authenticated = false;
                }

                if ($authenticated) {

                    $this->retries = 'NULL';
                    $this->save(true);
                    
                    /* QA: auth success */
                    $result->code = $result::CODE_SUCCESS;
                    $result->info = 'user_auth_success';
                }else{
                    
                    /* cek user jika gagal password sebanyak 3x */
                    if($retriesMode == self::RETRIES_TRUE){

                        if($this->retries == 'NULL'){
                            $ret_data = 0;
                        }else{
                            $ret_data = (int) $this->retries;
                        }

                        if($ret_data < 3){
                            $this->retries = $ret_data + 1;
                        }                        
                            
                        if((int) $this->retries == 3){ // jika sudah mencapai 3x then
                            $this->status = self::STATUS_BLOCKED;
                        }

                        $this->save(true);
                    }
                    /* QA: fail invalid passwd */
                    $result->code = self::CODE_AUTH_FAILED;
                    $result->info = 'user_auth_failed';
                }

            }else{
                /* QA: user is locked */
                $result->code = self::CODE_AUTH_LOCKED;
                $result->info = 'user_auth_locked';
            }
        }else{

            /* QA: user not loaded */
            $result->code = self::CODE_AUTH_INVALID;
            $result->info = 'user_auth_invalid';
        }

        /* return result */
        return $result;
        
    }

}