1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Peto16\User; |
4
|
|
|
|
5
|
|
|
use \Anax\DI\InjectionAwareInterface; |
6
|
|
|
use \Anax\DI\InjectionAwareTrait; |
7
|
|
|
use \Peto16\User\HTMLForm\UserLoginForm; |
8
|
|
|
use \Peto16\User\HTMLForm\CreateUserForm; |
9
|
|
|
use \Peto16\User\HTMLForm\UpdateUserForm; |
10
|
|
|
use \Peto16\User\HTMLForm\DeleteUserForm; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Controller for Login |
14
|
|
|
*/ |
15
|
|
|
class UserController implements InjectionAwareInterface |
16
|
|
|
{ |
17
|
|
|
use InjectionAwareTrait; |
18
|
|
|
|
19
|
|
|
private $session; |
20
|
|
|
private $userService; |
21
|
|
|
private $response; |
22
|
|
|
private $view; |
23
|
|
|
private $pageRender; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Initiate the controller. |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
8 |
|
public function init() |
32
|
|
|
{ |
33
|
8 |
|
$this->userService = $this->di->get("userService"); |
34
|
8 |
|
$this->session = $this->di->get("session"); |
35
|
8 |
|
$this->response = $this->di->get("response"); |
36
|
8 |
|
$this->view = $this->di->get("view"); |
37
|
8 |
|
$this->pageRender = $this->di->get("pageRender"); |
38
|
8 |
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Login-page |
44
|
|
|
* |
45
|
|
|
* @throws Exception |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
1 |
|
public function getPostLogin() |
50
|
|
|
{ |
51
|
1 |
|
if ($this->userService->checkLoggedin()) { |
52
|
1 |
|
$this->response->redirect(""); |
53
|
1 |
|
} |
54
|
|
|
|
55
|
1 |
|
$title = "Administration - Login"; |
56
|
1 |
|
$form = new UserLoginForm($this->di); |
57
|
|
|
|
58
|
1 |
|
$form->check(); |
59
|
|
|
|
60
|
|
|
$data = [ |
61
|
1 |
|
"form" => $form->getHTML(), |
62
|
1 |
|
]; |
63
|
|
|
|
64
|
1 |
|
$this->view->add("user/login", $data); |
65
|
|
|
|
66
|
1 |
|
$this->pageRender->renderPage(["title" => $title]); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Create user page. |
73
|
|
|
* |
74
|
|
|
* @throws Exception |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
1 |
|
public function getPostCreateUser() |
79
|
|
|
{ |
80
|
1 |
|
$title = "Skapa användare"; |
81
|
1 |
|
$form = new CreateUserForm($this->di); |
82
|
|
|
|
83
|
1 |
|
$form->check(); |
84
|
|
|
|
85
|
|
|
$data = [ |
86
|
1 |
|
"content" => $form->getHTML(), |
87
|
1 |
|
]; |
88
|
|
|
|
89
|
1 |
|
$this->view->add("default2/article", $data); |
90
|
|
|
|
91
|
1 |
|
$this->pageRender->renderPage(["title" => $title]); |
92
|
1 |
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Uppdatera användare. |
98
|
|
|
* |
99
|
|
|
* @param integer $id User id. |
100
|
|
|
* |
101
|
|
|
* @throws Exception |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
1 |
|
public function getPostUpdateUser($id) |
106
|
|
|
{ |
107
|
1 |
|
$loggedInUser = $this->userService->getCurrentLoggedInUser(); |
108
|
|
|
|
109
|
1 |
|
if (!$loggedInUser) { |
110
|
1 |
|
$this->response->redirect("login"); |
111
|
1 |
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
if ($loggedInUser->id != $id) { |
115
|
1 |
|
if (!$loggedInUser->administrator) { |
116
|
1 |
|
$this->response->redirect(""); |
117
|
1 |
|
} |
118
|
1 |
|
} |
119
|
|
|
|
120
|
1 |
|
$title = "Uppdatera användaren"; |
121
|
1 |
|
$form = new UpdateUserForm($this->di, $id); |
122
|
|
|
|
123
|
1 |
|
$form->check(); |
124
|
|
|
|
125
|
|
|
$data = [ |
126
|
1 |
|
"content" => $form->getHTML(), |
127
|
1 |
|
]; |
128
|
|
|
|
129
|
1 |
|
$this->view->add("default2/article", $data); |
130
|
|
|
|
131
|
1 |
|
$this->pageRender->renderPage(["title" => $title]); |
132
|
1 |
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Handler with form to delete an item. |
138
|
|
|
* |
139
|
|
|
* @return void |
140
|
|
|
*/ |
141
|
1 |
|
public function getPostDeleteUser($id) |
142
|
|
|
{ |
143
|
1 |
|
$loggedInUser = $this->userService->getCurrentLoggedInUser(); |
144
|
|
|
|
145
|
1 |
|
if (!$loggedInUser) { |
146
|
1 |
|
$this->response->redirect("login"); |
147
|
1 |
|
return false; |
148
|
|
|
} |
149
|
1 |
|
if (!$loggedInUser->administrator) { |
150
|
1 |
|
$this->response->redirect("login"); |
151
|
1 |
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
$title = "Radera en användare"; |
155
|
1 |
|
$form = new DeleteUserForm($this->di, $id); |
156
|
|
|
|
157
|
1 |
|
$form->check(); |
158
|
|
|
|
159
|
|
|
$data = [ |
160
|
1 |
|
"content" => $form->getHTML(), |
161
|
1 |
|
]; |
162
|
|
|
|
163
|
1 |
|
$this->view->add("default2/article", $data); |
164
|
|
|
|
165
|
1 |
|
$this->pageRender->renderPage(["title" => $title]); |
166
|
1 |
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Logout user. |
172
|
|
|
* |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
1 |
|
public function logout() |
176
|
|
|
{ |
177
|
1 |
|
$this->session->delete("user"); |
178
|
1 |
|
$this->response->redirect("user/login"); |
179
|
1 |
|
} |
180
|
|
|
} |
181
|
|
|
|