Commit 0aaf1c25 authored by Zohten's avatar Zohten

added code getter for responses

parent 92de9aa7
...@@ -69,4 +69,8 @@ class Response ...@@ -69,4 +69,8 @@ class Response
echo $this->body; echo $this->body;
exit; // do we keep that? exit; // do we keep that?
} }
public function getCode(){
return $this->code;
}
} }
...@@ -38,11 +38,18 @@ class UserController extends Controller ...@@ -38,11 +38,18 @@ class UserController extends Controller
return Response::errorResponse($message); return Response::errorResponse($message);
} }
public function authUser($id, $allowAdmin=True){ /**
* 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 // Token phase
$verifyArray = $this->request->verifyJwtToken(); $verifyArray = $this->request->verifyJwtToken();
if ($verifyArray['message']!=="Valid token.") { if ($verifyArray['message']!=="Valid token.") {
return Response::unauthorizedResponse($jsonResult); $message = json_encode($verifyArray['error']);
return Response::unauthorizedResponse($message);
} }
// Auth phase // Auth phase
$data = $verifyArray['decodedJWT']->data; $data = $verifyArray['decodedJWT']->data;
...@@ -50,10 +57,13 @@ class UserController extends Controller ...@@ -50,10 +57,13 @@ class UserController extends Controller
$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);
} }
$message = json_encode(["message" => "Authentified."]);
return Response::okResponse($message);
} }
/** /**
* Get all users in USER table * GET all users in USER table
* *
* @return Response * @return Response
*/ */
...@@ -65,8 +75,9 @@ class UserController extends Controller ...@@ -65,8 +75,9 @@ class UserController extends Controller
} }
/** /**
* Get a specific user in USER table based on id * GET a specific user in USER table based on id
* *
* @param int $id id of the User
* @return Response * @return Response
*/ */
protected function getUser($id) protected function getUser($id)
...@@ -79,15 +90,16 @@ class UserController extends Controller ...@@ -79,15 +90,16 @@ class UserController extends Controller
/** /**
* Update a specific user in USER table based on id * Update a specific user in USER table based on id
* *
* @param array $array array containing id + fields to modify
* @return Response * @return Response
*/ */
protected function updateUser($array) protected function updateUser($array)
{ {
// Auth with token phase // Auth with token phase
$authError = $this->authUser($array['id']); if($this->authUser($array['id'])->getCode()!=200){
if($authError){
return authError; return authError;
} }
// Update phase // Update phase
User::updateUser($array); User::updateUser($array);
......
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