1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Todo Controller |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
7
|
|
|
* @author Omar El Gabry <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
class TodoController extends Controller{ |
|
|
|
|
11
|
|
|
|
12
|
|
|
// override this method to perform any logic before calling action method as explained above |
13
|
|
|
public function beforeAction(){ |
14
|
|
|
|
15
|
|
|
parent::beforeAction(); |
16
|
|
|
|
17
|
|
|
// define the actions in this Controller |
18
|
|
|
$action = $this->request->param('action'); |
19
|
|
|
|
20
|
|
|
// restrict the request to action methods |
21
|
|
|
// $this->Security->requireAjax(['create', 'delete']); |
|
|
|
|
22
|
|
|
$this->Security->requirePost(['create', 'delete']); |
|
|
|
|
23
|
|
|
|
24
|
|
|
// define the expected form fields for every action if exist |
25
|
|
View Code Duplication |
switch($action){ |
|
|
|
|
26
|
|
|
case "create": |
27
|
|
|
// you can exclude form fields if you don't care if they were sent with form fields or not |
28
|
|
|
$this->Security->config("form", [ 'fields' => ['content'], 'exclude' => ['submit']]); |
|
|
|
|
29
|
|
|
break; |
30
|
|
|
case "delete": |
31
|
|
|
// if you don't want to validate the CSRF Token, then assign 'validateCsrfToken' to false |
32
|
|
|
// $this->Security->config("validateCsrfToken", false); |
|
|
|
|
33
|
|
|
$this->Security->config("form", [ 'fields' => ['todo_id'], 'exclude' => ['submit']]); |
|
|
|
|
34
|
|
|
break; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function index(){ |
39
|
|
|
|
40
|
|
|
$html = $this->view->renderWithLayouts(Config::get('VIEWS_PATH') . "layout/todo/", Config::get('VIEWS_PATH') . 'todo/index.php'); |
41
|
|
|
|
42
|
|
|
// display todo list |
43
|
|
|
echo $html; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function create(){ |
47
|
|
|
|
48
|
|
|
$content = $this->request->data("content"); |
49
|
|
|
$todo = $this->todo->create(Session::getUserId(), $content); |
|
|
|
|
50
|
|
|
|
51
|
|
|
if(!$todo){ |
52
|
|
|
|
53
|
|
|
// in case of normal post request |
54
|
|
|
Session::set('error', $this->todo->errors()); |
|
|
|
|
55
|
|
|
Redirector::to(PUBLIC_ROOT . "Todo"); |
56
|
|
|
|
57
|
|
|
// in case of ajax |
58
|
|
|
// echo $this->view->renderErrors($this->todo->errors()); |
|
|
|
|
59
|
|
|
|
60
|
|
|
}else{ |
61
|
|
|
|
62
|
|
|
// in case of normal post request |
63
|
|
|
Session::set('success', "Todo has been created"); |
64
|
|
|
Redirector::to(PUBLIC_ROOT . "Todo"); |
65
|
|
|
|
66
|
|
|
// in case of ajax |
67
|
|
|
// echo $this->view->JSONEncode(array("success" => "Todo has been created")); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function delete(){ |
72
|
|
|
|
73
|
|
|
$todoId = Encryption::decryptIdWithDash($this->request->data("todo_id")); |
74
|
|
|
$this->todo->delete($todoId); |
|
|
|
|
75
|
|
|
|
76
|
|
|
// in case of normal post request |
77
|
|
|
Session::set('success', "Todo has been deleted"); |
78
|
|
|
Redirector::to(PUBLIC_ROOT . "Todo"); |
79
|
|
|
|
80
|
|
|
// in case of ajax |
81
|
|
|
// echo $this->view->JSONEncode(array("success" => "Todo has been deleted")); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function isAuthorized(){ |
85
|
|
|
|
86
|
|
|
$action = $this->request->param('action'); |
87
|
|
|
$role = Session::getUserRole(); |
88
|
|
|
$resource = "todo"; |
89
|
|
|
|
90
|
|
|
// only for admins |
91
|
|
|
Permission::allow('admin', $resource, ['*']); |
92
|
|
|
|
93
|
|
|
// only for normal users |
94
|
|
|
Permission::allow('user', $resource, ['delete'], 'owner'); |
95
|
|
|
|
96
|
|
|
$todoId = $this->request->data("todo_id"); |
97
|
|
|
|
98
|
|
|
if(!empty($todoId)){ |
99
|
|
|
$todoId = Encryption::decryptIdWithDash($todoId); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$config = [ |
103
|
|
|
"user_id" => Session::getUserId(), |
104
|
|
|
"table" => "todo", |
105
|
|
|
"id" => $todoId]; |
106
|
|
|
|
107
|
|
|
return Permission::check($role, $resource, $action, $config); |
108
|
|
|
} |
109
|
|
|
} |
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.