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']]); |
|
|
|
|
29
|
|
|
break; |
30
|
|
|
case "delete": |
31
|
|
|
// If you want to disable validation for form tampering |
32
|
|
|
// $this->Security->config("validateForm", false); |
|
|
|
|
33
|
|
|
$this->Security->config("form", [ 'fields' => ['todo_id']]); |
|
|
|
|
34
|
|
|
break; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function index(){ |
39
|
|
|
|
40
|
|
|
// display todo list |
41
|
|
|
$this->view->renderWithLayouts(Config::get('VIEWS_PATH') . "layout/todo/", Config::get('VIEWS_PATH') . 'todo/index.php'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
View Code Duplication |
public function create(){ |
|
|
|
|
45
|
|
|
|
46
|
|
|
$content = $this->request->data("content"); |
47
|
|
|
$todo = $this->todo->create(Session::getUserId(), $content); |
|
|
|
|
48
|
|
|
|
49
|
|
|
if(!$todo){ |
50
|
|
|
|
51
|
|
|
// in case of normal post request |
52
|
|
|
Session::set('errors', $this->todo->errors()); |
|
|
|
|
53
|
|
|
return $this->redirector->root("Todo"); |
54
|
|
|
|
55
|
|
|
// in case of ajax |
56
|
|
|
// $this->view->renderErrors($this->todo->errors()); |
|
|
|
|
57
|
|
|
|
58
|
|
|
}else{ |
59
|
|
|
|
60
|
|
|
// in case of normal post request |
61
|
|
|
Session::set('success', "Todo has been created"); |
62
|
|
|
return $this->redirector->root("Todo"); |
63
|
|
|
|
64
|
|
|
// in case of ajax |
65
|
|
|
// $this->view->renderJson(array("success" => "Todo has been created")); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function delete(){ |
70
|
|
|
|
71
|
|
|
$todoId = Encryption::decryptIdWithDash($this->request->data("todo_id")); |
72
|
|
|
$this->todo->delete($todoId); |
|
|
|
|
73
|
|
|
|
74
|
|
|
// in case of normal post request |
75
|
|
|
Session::set('success', "Todo has been deleted"); |
76
|
|
|
return $this->redirector->root("Todo"); |
77
|
|
|
|
78
|
|
|
// in case of ajax |
79
|
|
|
// $this->view->renderJson(array("success" => "Todo has been deleted")); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function isAuthorized(){ |
83
|
|
|
|
84
|
|
|
$action = $this->request->param('action'); |
85
|
|
|
$role = Session::getUserRole(); |
86
|
|
|
$resource = "todo"; |
87
|
|
|
|
88
|
|
|
// only for admins |
89
|
|
|
Permission::allow('admin', $resource, ['*']); |
90
|
|
|
|
91
|
|
|
// only for normal users |
92
|
|
|
Permission::allow('user', $resource, ['delete'], 'owner'); |
93
|
|
|
|
94
|
|
|
$todoId = $this->request->data("todo_id"); |
95
|
|
|
|
96
|
|
|
if(!empty($todoId)){ |
97
|
|
|
$todoId = Encryption::decryptIdWithDash($todoId); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$config = [ |
101
|
|
|
"user_id" => Session::getUserId(), |
102
|
|
|
"table" => "todo", |
103
|
|
|
"id" => $todoId]; |
104
|
|
|
|
105
|
|
|
return Permission::check($role, $resource, $action, $config); |
106
|
|
|
} |
107
|
|
|
} |
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.