|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* The admin controller |
|
5
|
|
|
* |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
7
|
|
|
* @author Omar El Gabry <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
class AdminController extends Controller { |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* A method that will be triggered before calling action method. |
|
15
|
|
|
* Any changes here will reflect then on Controller::triggerComponents() method |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
public function beforeAction(){ |
|
19
|
|
|
|
|
20
|
|
|
parent::beforeAction(); |
|
21
|
|
|
|
|
22
|
|
|
$action = $this->request->param('action'); |
|
23
|
|
|
$actions = ['getUsers', 'updateUserInfo', 'deleteUser']; |
|
24
|
|
|
|
|
25
|
|
|
// define the action methods that needs to be triggered only through POST & Ajax request. |
|
26
|
|
|
$this->Security->requireAjax($actions); |
|
|
|
|
|
|
27
|
|
|
$this->Security->requirePost($actions); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
// You need to explicitly define the form fields that you expect to be returned in POST request, |
|
30
|
|
|
// if form field wasn't defined, this will detected as form tampering attempt. |
|
31
|
|
|
switch($action){ |
|
32
|
|
|
case "getUsers": |
|
33
|
|
|
$this->Security->config("form", [ 'fields' => ['name', 'email', 'role', 'page']]); |
|
|
|
|
|
|
34
|
|
|
break; |
|
35
|
|
|
case "updateUserInfo": |
|
36
|
|
|
$this->Security->config("form", [ 'fields' => ['user_id', 'name', 'password', 'role']]); |
|
|
|
|
|
|
37
|
|
|
break; |
|
38
|
|
|
case "deleteUser": |
|
39
|
|
|
$this->Security->config("form", [ 'fields' => ['user_id']]); |
|
|
|
|
|
|
40
|
|
|
break; |
|
41
|
|
|
case "updateBackup": |
|
42
|
|
|
case "restoreBackup": |
|
43
|
|
|
$this->Security->config("validateCsrfToken", true); |
|
|
|
|
|
|
44
|
|
|
break; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* show all users |
|
50
|
|
|
* |
|
51
|
|
|
*/ |
|
52
|
|
|
public function users(){ |
|
53
|
|
|
|
|
54
|
|
|
Config::setJsConfig('curPage', "users"); |
|
55
|
|
|
$this->view->renderWithLayouts(Config::get('VIEWS_PATH') . "layout/default/", Config::get('ADMIN_VIEWS_PATH') . 'users/index.php'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* get users by name, email & role |
|
60
|
|
|
* |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getUsers(){ |
|
63
|
|
|
|
|
64
|
|
|
$name = $this->request->data("name"); |
|
65
|
|
|
$email = $this->request->data("email"); |
|
66
|
|
|
$role = $this->request->data("role"); |
|
67
|
|
|
$pageNum = $this->request->data("page"); |
|
68
|
|
|
|
|
69
|
|
|
$usersData = $this->admin->getUsers($name, $email, $role, $pageNum); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
if(!$usersData){ |
|
72
|
|
|
$this->view->renderErrors($this->admin->errors()); |
|
|
|
|
|
|
73
|
|
|
} else{ |
|
74
|
|
|
|
|
75
|
|
|
$usersHTML = $this->view->render(Config::get('ADMIN_VIEWS_PATH') . 'users/users.php', array("users" => $usersData["users"])); |
|
76
|
|
|
$paginationHTML = $this->view->render(Config::get('VIEWS_PATH') . 'pagination/default.php', array("pagination" => $usersData["pagination"])); |
|
77
|
|
|
$this->view->renderJson(array("data" => ["users" => $usersHTML, "pagination" => $paginationHTML])); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* view a user |
|
83
|
|
|
* |
|
84
|
|
|
* @param integer|string $userId |
|
85
|
|
|
*/ |
|
86
|
|
|
public function viewUser($userId = 0){ |
|
87
|
|
|
|
|
88
|
|
|
$userId = Encryption::decryptId($userId); |
|
89
|
|
|
|
|
90
|
|
|
if(!$this->user->exists($userId)){ |
|
|
|
|
|
|
91
|
|
|
return $this->error(404); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
Config::setJsConfig('curPage', "users"); |
|
95
|
|
|
Config::setJsConfig('userId', Encryption::encryptId($userId)); |
|
96
|
|
|
|
|
97
|
|
|
$this->view->renderWithLayouts(Config::get('VIEWS_PATH') . "layout/default/", Config::get('ADMIN_VIEWS_PATH') . 'users/viewUser.php', array("userId" => $userId)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* update user profile info(name, password, role) |
|
102
|
|
|
* |
|
103
|
|
|
*/ |
|
104
|
|
|
public function updateUserInfo(){ |
|
105
|
|
|
|
|
106
|
|
|
$userId = Encryption::decryptId($this->request->data("user_id")); |
|
107
|
|
|
$name = $this->request->data("name"); |
|
108
|
|
|
$password = $this->request->data("password"); |
|
109
|
|
|
$role = $this->request->data("role"); |
|
110
|
|
|
|
|
111
|
|
|
if(!$this->user->exists($userId)){ |
|
|
|
|
|
|
112
|
|
|
return $this->error(404); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$result = $this->admin->updateUserInfo($userId, Session::getUserId(), $name, $password, $role); |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
if(!$result){ |
|
118
|
|
|
$this->view->renderErrors($this->admin->errors()); |
|
|
|
|
|
|
119
|
|
|
}else{ |
|
120
|
|
|
$this->view->renderSuccess("Profile has been updated."); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* delete a user |
|
126
|
|
|
* |
|
127
|
|
|
*/ |
|
128
|
|
View Code Duplication |
public function deleteUser(){ |
|
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
$userId = Encryption::decryptIdWithDash($this->request->data("user_id")); |
|
131
|
|
|
|
|
132
|
|
|
if(!$this->user->exists($userId)){ |
|
|
|
|
|
|
133
|
|
|
return $this->error(404); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$this->admin->deleteUser(Session::getUserId(), $userId); |
|
|
|
|
|
|
137
|
|
|
$this->view->renderJson(array("success" => true)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* view backups if exist |
|
142
|
|
|
* |
|
143
|
|
|
*/ |
|
144
|
|
|
public function backups(){ |
|
145
|
|
|
|
|
146
|
|
|
Config::setJsConfig('curPage', "backups"); |
|
147
|
|
|
$this->view->renderWithLayouts(Config::get('VIEWS_PATH') . "layout/default/", Config::get('ADMIN_VIEWS_PATH') . 'backups.php'); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* update backup |
|
152
|
|
|
* |
|
153
|
|
|
*/ |
|
154
|
|
|
public function updateBackup(){ |
|
155
|
|
|
|
|
156
|
|
|
$this->admin->updateBackup(); |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
Session::set('backup-success', "Backup has been updated"); |
|
159
|
|
|
return $this->redirector->root("Admin/Backups"); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* restore backup |
|
164
|
|
|
* |
|
165
|
|
|
*/ |
|
166
|
|
|
public function restoreBackup(){ |
|
167
|
|
|
|
|
168
|
|
|
$result = $this->admin->restoreBackup(); |
|
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
if(!$result){ |
|
171
|
|
|
Session::set('backup-errors', $this->admin->errors()); |
|
|
|
|
|
|
172
|
|
|
return $this->redirector->root("Admin/Backups"); |
|
173
|
|
|
}else{ |
|
174
|
|
|
Session::set('backup-success', "Backup has been restored successfully"); |
|
175
|
|
|
return $this->redirector->root("Admin/Backups"); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Is user authorized for admin controller & requested action method? |
|
181
|
|
|
* |
|
182
|
|
|
* @return bool |
|
183
|
|
|
*/ |
|
184
|
|
|
public function isAuthorized(){ |
|
185
|
|
|
|
|
186
|
|
|
$role = Session::getUserRole(); |
|
187
|
|
|
if(isset($role) && $role === "admin"){ |
|
188
|
|
|
return true; |
|
189
|
|
|
} |
|
190
|
|
|
return false; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
} |
|
194
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.