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
<?php
class UserController extends Controller
{
public function __construct($name, $request)
{
parent::__construct($name, $request);
}
/**
* Process incoming request for the /user endpoint
*
* @return Response
*/
public function processRequest()
{
$httpMethod=$this->request->getHttpMethod();
$uriParams=$this->request->getUriParams();
switch ($httpMethod) {
case 'GET':
// If there is a uriParams, it is the /user/{id} endpoint
if ($uriParams) {
return $this->getUser($uriParams[0]);
}
// Else, it is the /user endpoint
return $this->getAllUsers();
break;
case 'PUT':
// If there is a uriParams, it is the /user/{id} endpoint
if ($uriParams) {
$body = $this->request->getData();
return $this->updateUser(array_merge($body, ['id'=>$uriParams[0]]));
}
break;
case 'POST':
$body = $this->request->getData();
return $this->addUser($body);
break;
case 'DELETE':
if ($uriParams) {
return $this->deleteUser($uriParams[0]);
}
break;
}
$message = json_encode(["message" => "unsupported parameters or method in users"]);
return Response::errorResponse($message);
}
/**
* Authentificate a user if he has the same id as the one in token, bypassed by admin
*
* @param int $id id of the User
* @return Response
*/
public function authUser($id){
// Token phase
$verifyArray = $this->request->verifyJwtToken();
if ($verifyArray['message']!=="Valid token.") {
$message = json_encode($verifyArray['error']);
return Response::unauthorizedResponse($message);
}
// Auth phase
$data = $verifyArray['decodedJWT']->data;
if (($data->id != $id) && ($data->role != 2)) {
$message = json_encode(["message" => "You don't have access to this account."]);
return Response::unauthorizedResponse($message);
}
$message = json_encode(["message" => "Authentified."]);
return Response::okResponse($message);
}
/**
* (GET) Get all users in USER table
*
* @return Response
*/
protected function getAllUsers()
{
$users = User::getList();
$response = Response::okResponse(json_encode($users, JSON_PRETTY_PRINT));
return $response;
}
/**
* (GET) Get a specific user in USER table based on id
*
* @param int $id id of the User
* @return Response
*/
protected function getUser($id)
{
$user = User::getRow($id);
$response = Response::okResponse(json_encode($user));
return $response;
}
/**
* (POST) Add a specific user in USER table
*
* @param array $array array containing
* @return Response
*/
protected function addUser($array)
{
// Check if mendatory fields are filed
if (!isset($array['login']) || !isset($array['pwd']) || !isset($array['mail'])) {
$message = json_encode(["message" => 'login, pwd and mail fields are mandatory']);
return new Response(422, $message);
}
// Check if mail is valid
if (!filter_var($array['mail'], FILTER_VALIDATE_EMAIL)) {
$message = json_encode(["message" => 'Email is not valid']);
return new Response(422, $message);
}
// Check if login/pseudo is already used
if (User::checkLogin($array['login'])) {
$message = json_encode(["message" => 'This pseudo is already used']);
return new Response(422, $message);
}
// Fill facultative field
if (!isset($array['avatar'])){
$array['avatar'] = '';
}
if (!isset($array['lastname'])){
$array['lastname'] = '';
}
if (!isset($array['firstname'])){
$array['firstname'] = '';
}
// Create row
User::addRow($array);
$message = json_encode(["message" => 'User succesfully added!']);
$response = Response::createdResponse($message);
return $response;
}
/**
* (PUT) Update a specific user in USER table based on id
*
* @param array $array array containing id + fields to modify
* @return Response
*/
protected function updateUser($array)
{
// Auth with token phase
$authResponse = $this->authUser($array['id']);
if($authResponse->getCode()!=200){
return $authResponse;
}
// Update phase
User::updateUser($array);
$message = json_encode(["message" => 'User succesfully updated!']);
$response = Response::okResponse($message);
return $response;
}
/**
* (DELETE) Delete a specific user in USER table based on id
*
* @param int $id id of the User
* @return Response
*/
protected function deleteUser($id)
{
// Auth with token phase
$authResponse = $this->authUser($id);
if($authResponse->getCode()!=200){
return $authResponse;
}
// Update phase
User::deleteRow($id);
$message = json_encode(["message" => 'User succesfully deleted!']);
$response = Response::okResponse($message);
return $response;
}
}