This repository was archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserManagementAction.php
More file actions
373 lines (315 loc) · 11 KB
/
UserManagementAction.php
File metadata and controls
373 lines (315 loc) · 11 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
/*
SIKOPIS - Sistem Komputerisasi Data Pelaporan Inventory SPBU
===================================
Author: Virga Ramada
Version: 1.0
URL: http://www.sikopis.net/
Copyright And License Information
=================================
Copyright (c) 2007 Virga Ramada
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
require_once 'classes/UserManager.class.php';
require_once 'vo/UserRole.class.php';
require_once 'classes/TemplateEngine.class.php';
require_once 'classes/LogManager.class.php';
require_once 'classes/PaginateIt.class.php';
require_once 'classes/UserRoleEnum.class.php';
require_once 'utils/Validate.php';
//define("IS_DEBUG", false);
//define("IS_CACHING", false);
if (empty ($_REQUEST['PHPSESSID'])) {
$tpl_engine = TemplateEngine::getEngine();
//$tpl_engine->caching = IS_CACHING;
//$tpl_engine->debugging = IS_DEBUG;
$tpl_engine->assign("errors", array ("session_expired" => "Anda harus login terlebih dahulu"));
$tpl_engine->display('login.tpl');
} else {
session_id($_REQUEST['PHPSESSID']);
session_start();
validateSession();
}
function validateSession() {
$method_type = @ $_REQUEST["method"];
if (empty($_SESSION["station_id"])) {
$tpl_engine = TemplateEngine::getEngine();
//$tpl_engine->caching = IS_CACHING;
//$tpl_engine->debugging = IS_DEBUG;
$tpl_engine->assign("errors", array ("station_id" => "SPBU tidak dikenal!"));
$tpl_engine->display('login.tpl');
}
else if (!empty ($_SESSION['user_role']) && $_SESSION['user_role'] != UserRoleEnum::GAS_STATION_OWNER && $_SESSION['user_role'] != UserRoleEnum::GAS_STATION_ADMINISTRATOR) {
$tpl_engine = TemplateEngine::getEngine();
//$tpl_engine->caching = IS_CACHING;
//$tpl_engine->debugging = IS_DEBUG;
$tpl_engine->assign("errors", array ("user_role" => "Anda tidak punya wewenang untuk merubah/menambah user"));
$tpl_engine->display('login.tpl');
} else if (empty ($_SESSION['user_role'])) {
$tpl_engine = TemplateEngine::getEngine();
//$tpl_engine->caching = IS_CACHING;
//$tpl_engine->debugging = IS_DEBUG;
$tpl_engine->assign("errors", array ("user_role" => "Anda tidak memiliki role yang memadai untuk mengakses"));
$tpl_engine->display('login.tpl');
}
else {
if (empty ($method_type)) {
prepare();
}
if ($method_type == 'create') {
create();
}
if ($method_type == 'update') {
update();
}
if ($method_type == 'delete') {
delete();
}
if ($method_type == 'edit') {
edit();
}
if ($method_type == 'deleteAll') {
deleteAll();
}
if ($method_type == 'activateAccount') {
activateAccount();
}
if ($method_type == 'passivateAccount') {
passivateAccount();
}
}
}
function edit() {
try {
$user_id = $_REQUEST["user_id"];
$tpl_engine = TemplateEngine::getEngine();
// $tpl_engine->caching = IS_CACHING;
// $tpl_engine->debugging = IS_DEBUG;
$user_mgr = new UserManager();
$vo = $user_mgr->findByPrimaryKey($user_id);
if ( $vo != NULL) {
$tpl_engine->assign("user", $vo);
}
$result = findAll($tpl_engine);
$tpl_engine->assign("user_list", $result);
$tpl_engine->display('user_mgt.tpl');
} catch (Exception $e) {
die($e);
}
}
function prepare() {
$tpl_engine = TemplateEngine::getEngine();
//$tpl_engine->caching = IS_CACHING;
//$tpl_engine->debugging = IS_DEBUG;
$result = findAll($tpl_engine);
$tpl_engine->assign("user_list", $result);
$tpl_engine->display('user_mgt.tpl');
}
function create() {
try {
$tpl_engine = TemplateEngine::getEngine();
//$tpl_engine->caching = IS_CACHING;
// $tpl_engine->debugging = IS_DEBUG;
$errors = validate();
if (empty ($errors)) {
$_vo = new UserRole();
$_vo->setUsername($_POST["user_name"]);
$_vo->setUserPassword($_POST["user_password"]);
$_vo->setUserRole($_POST["user_role"]);
$_vo->setFirstName($_POST["first_name"]);
$_vo->setLastName($_POST["last_name"]);
$_vo->setEmailAddress($_POST["email_address"]);
$_vo->setStationId($_SESSION["station_id"]);
$_vo->setAccountActivated("true");
$user_mgr = new UserManager();
$user_exist = $user_mgr->findByUserName($_POST["user_name"],NULL, NULL);
if (!empty ($user_exist)) {
$errors = array ("user_name" => "Login user '".$_POST["user_name"]."' sudah terpakai. Silahkan pilih login yang lain.");
$tpl_engine->assign("errors", $errors);
} else {
$user_mgr->create($_vo);
}
} else {
$tpl_engine->assign("errors", $errors);
}
$result = findAll($tpl_engine);
$tpl_engine->assign("user_list", $result);
$tpl_engine->display('user_mgt.tpl');
} catch (Exception $e) {
die($e);
}
}
function update() {
try {
$user_id = $_POST["user_id"];
$tpl_engine = TemplateEngine::getEngine();
//$tpl_engine->caching = IS_CACHING;
//$tpl_engine->debugging = IS_DEBUG;
$errors = validate(true);
if (empty ($errors)) {
$_vo = new UserRole();
$_vo->setUsername($_POST["user_name"]);
$_vo->setUserRole($_POST["user_role"]);
$_vo->setFirstName($_POST["first_name"]);
$_vo->setLastName($_POST["last_name"]);
$_vo->setUserId($user_id);
$_vo->setEmailAddress($_POST["email_address"]);
$user_mgr = new UserManager();
$user_mgr->update($_vo);
} else {
$tpl_engine->assign("errors", $errors);
}
$result = findAll($tpl_engine);
$tpl_engine->assign("user_list", $result);
$tpl_engine->display('user_mgt.tpl');
} catch (Exception $e) {
die($e);
}
}
function activateAccount() {
try {
$user_id = $_REQUEST["user_id"];
$tpl_engine = TemplateEngine::getEngine();
//$tpl_engine->caching = IS_CACHING;
// $tpl_engine->debugging = IS_DEBUG;
if (empty ($user_id)) {
$errors = array ("user_id" => "Silahkan isi User id dengan benar");
}
if (empty ($errors)) {
$user_mgr = new UserManager();
$user_mgr->activateAccount($user_id, $_SESSION["station_id"]);
} else {
$tpl_engine->assign("errors", $errors);
}
$result = findAll($tpl_engine);
$tpl_engine->assign("user_list", $result);
$tpl_engine->display('user_mgt.tpl');
} catch (Exception $e) {
die($e);
}
}
function passivateAccount() {
try {
$user_id = $_REQUEST["user_id"];
$tpl_engine = TemplateEngine::getEngine();
//$tpl_engine->caching = IS_CACHING;
// $tpl_engine->debugging = IS_DEBUG;
if (empty ($user_id)) {
$errors = array ("user_id" => "Silahkan isi User id dengan benar");
}
if (empty ($errors)) {
$user_mgr = new UserManager();
$user_mgr->passivateAccount($user_id, $_SESSION["station_id"]);
} else {
$tpl_engine->assign("errors", $errors);
}
$result = findAll($tpl_engine);
$tpl_engine->assign("user_list", $result);
$tpl_engine->display('user_mgt.tpl');
} catch (Exception $e) {
die($e);
}
}
function delete() {
try {
$user_id = $_REQUEST["user_id"];
$tpl_engine = TemplateEngine::getEngine();
// $tpl_engine->caching = IS_CACHING;
// $tpl_engine->debugging = IS_DEBUG;
$user_mgr = new UserManager();
$user_mgr->delete($user_id);
$result = findAll($tpl_engine);
$tpl_engine->assign("user_list", $result);
$tpl_engine->display('user_mgt.tpl');
} catch (Exception $e) {
die($e);
}
}
function deleteAll() {
try {
$tpl_engine = TemplateEngine::getEngine();
//$tpl_engine->caching = IS_CACHING;
//$tpl_engine->debugging = IS_DEBUG;
$user_mgr = new UserManager();
$user_mgr->deleteAll($_SESSION["station_id"]);
$result = findAll($tpl_engine);
$tpl_engine->assign("user_list", $result);
$tpl_engine->display('user_mgt.tpl');
} catch (Exception $e) {
die($e);
}
}
function findAll($tpl_engine = NULL) {
$pageNumber = @ $_REQUEST['page'];
$user_mgr = new UserManager();
$totalData = (int) $user_mgr->countAllNoSuperuserAndOwner($_SESSION["station_id"]);
if (empty ($pageNumber)) {
$pageNumber = 1;
}
$itemsPerPage = 10;
$PaginateIt = new PaginateIt();
$PaginateIt->SetItemsPerPage($itemsPerPage);
$PaginateIt->SetItemCount($totalData);
$pageIndex = ((int) $pageNumber * $itemsPerPage - $itemsPerPage);
if ($pageIndex > $totalData) {
$pageIndex = 0;
}
$result = $user_mgr->findAllNoSuperuserAndOwner($pageIndex, $itemsPerPage, $_SESSION["station_id"]);
if (!empty ($tpl_engine)) {
$tpl_engine->assign("roles", UserRoleEnum::names());
$tpl_engine->assign("active_roles", UserRoleEnum::allNames());
$tpl_engine->assign("paginatedUserList", $PaginateIt->GetPageLinks());
}
return $result;
}
function validate($is_update=false) {
$result = array ();
if (empty ($_POST["user_role"])) {
$result = array ("user_role" => "Silahkan isi Tipe user dengan benar");
} else if (strlen($_POST["user_role"]) > 20) {
$result = array ("user_role" => "Jenis User tidak boleh lebih dari 20 karakter");
}
if (empty ($_POST["user_name"])) {
$result = array ("user_name" => "Silahkan isi Login yang dipakai dengan benar");
} else if (strlen($_POST["user_name"]) > 20) {
$result = array ("user_name" => "Login yang dipakai tidak boleh lebih dari 20 karakter");
}
if (!$is_update) {
if (empty ($_POST["user_password"])) {
$result = array ("user_password" => "Silahkan isi kata sandi dengan benar");
} else if (strlen($_POST["user_password"]) < 6 || strlen($_POST["user_password"]) > 20) {
$result = array ("user_password" => "Kata sandi harus terdiri dari 6 - 20 karakter");
}
if (empty ($_POST["user_password2"])) {
$result = array ("user_password2" => "Silahkan isi verifikasi kata sandi dengan benar");
} else if ($_POST["user_password2"] != $_POST["user_password"]) {
$result = array ("user_password2" => "Verifikasi kata sandi tidak valid");
}
}
if (empty ($_POST["first_name"])) {
$result = array ("first_name" => "Silahkan isi nama depan dengan benar");
} else if (strlen($_POST["first_name"]) > 30) {
$result = array ("first_name" => "Nama depan tidak boleh lebih dari 30 karakter");
}
if (!empty ($_POST["last_name"]) && strlen($_POST["last_name"]) > 30) {
$result = array ("last_name" => "Nama belakang tidak boleh lebih dari 30 karakter");
}
if (empty ($_POST["email_address"])) {
$result = array ("email_address" => "Silahkan isi e-mail dengan benar");
} else if (!Validate::email($_POST["email_address"])) {
$result = array ("email_address" => "E-mail tidak valid");
}
return $result;
}
?>