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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?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('create_dtm'),
new Map('retries'),
new Map('email'),
new Map('tokenjwt'),
new Map('lifetime'),
new Map('update_date'),
new Map('accessToken'),
new Map('nim'),
new Map('kaprodi'),
new Map('status'),
new Map('prodi'),
new Map('koordinator'),
new Map('nopeg')
),
parent::CASE_SENSITIVE
);
}
/**
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, $postgree = false){
/* load access data */
$acc = $this->_storage->getAccess($id, $postgree);
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;
}
public function identify($data, $status){
$result = new Result();
$result = $this->_storage->checkIdentify($data, $status);
return $result;
}
public function identifymhs($data, $status){
$result = new Result();
$result = $this->_storage->checkIdentifymhs($data, $status);
return $result;
}
public function saveidentify($data){
$result = new Result();
$result = $this->_storage->saveidentify($data);
return $result;
}
public function savemhs($data, $user, $status){
$result = new Result();
$result = $this->_storage->savemhs($data, $user, $status);
return $result;
}
public function savedosen($data){
$result = new Result();
$result = $this->_storage->savedosen($data);
// print_r('as');die;
return $result;
}
public function updatepasspegawai($data, $username){
$result = new Result();
$result = $this->_storage->updatepasspegawai($data, $username);
// print_r('as');die;
return $result;
}
public function updatepassmhs($data, $username){
$result = new Result();
$result = $this->_storage->updatepassmhs($data, $username);
// print_r('as');die;
return $result;
}
public function authenticatechangeaccount($credential, $data = array(), $retriesMode = self::RETRIES_FALSE){
$result = new Result();
// print_r('die;');die;
$retries = 0;
/* loaded */
if ($this->id && $this->_loaded) {
/* jika user aktif */
if (($this->status == self::STATUS_ACTIVE)) {
if($this->password == $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;
}
}