Commit 5f9b4bc7 authored by Zohten's avatar Zohten

more comments

parent ac16a6ca
...@@ -22,14 +22,17 @@ class UserController extends Controller ...@@ -22,14 +22,17 @@ class UserController extends Controller
$httpMethod=$this->request->getHttpMethod(); $httpMethod=$this->request->getHttpMethod();
$uriParams=$this->request->getUriParams(); $uriParams=$this->request->getUriParams();
switch($httpMethod) { switch ($httpMethod) {
case 'GET': case 'GET':
// If there is a uriParams, it is the /user/{id} endpoint
if ($uriParams) { if ($uriParams) {
return $this->getUser($uriParams[0]); return $this->getUser($uriParams[0]);
} }
// Else, it is the /user endpoint
return $this->getAllUsers(); return $this->getAllUsers();
break; break;
case 'PUT': case 'PUT':
// If there is a uriParams, it is the /user/{id} endpoint
if ($uriParams) { if ($uriParams) {
$body = $this->request->getData(); $body = $this->request->getData();
return $this->updateUser(array_merge($body, ['id'=>$uriParams[0]])); return $this->updateUser(array_merge($body, ['id'=>$uriParams[0]]));
...@@ -71,6 +74,7 @@ class UserController extends Controller ...@@ -71,6 +74,7 @@ class UserController extends Controller
*/ */
protected function updateUser($array) protected function updateUser($array)
{ {
// Token phase
try { try {
$jwt_token = $this->request->getJwtToken(); $jwt_token = $this->request->getJwtToken();
$decodedJWT = JWT::decode($jwt_token, JWT_BACKEND_KEY, array('HS256')); $decodedJWT = JWT::decode($jwt_token, JWT_BACKEND_KEY, array('HS256'));
...@@ -84,10 +88,13 @@ class UserController extends Controller ...@@ -84,10 +88,13 @@ class UserController extends Controller
return Response::unauthorizedResponse($jsonResult); return Response::unauthorizedResponse($jsonResult);
} }
// Auth phase
if ($decodedJWT->data->id != $array['id']) { if ($decodedJWT->data->id != $array['id']) {
$message = json_encode(["message" => "You don't have access to this account."]); $message = json_encode(["message" => "You don't have access to this account."]);
return Response::unauthorizedResponse($message); return Response::unauthorizedResponse($message);
} }
// Update phase
User::updateUser($array); User::updateUser($array);
$message = json_encode(["message" => 'User succesfully updated !']); $message = json_encode(["message" => 'User succesfully updated !']);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment