<?php /* * A Controller is dedicated to process a request * its responsabilities are: * - analyses the action to be done * - analyses the parameters * - act on the model objects to perform the action * - process the data * - call the view and passes it the data * - return the response */ abstract class Controller { protected $name; protected $request; public function __construct($name, $request) { $this->request = $request; $this->name = $name; } abstract public function processRequest(); public function execute() { // Filtering with IP blacklist $ip_adress=$this->request->getIpAddr(); if(Blacklist::isBannedIP($ip_adress)){ $message = json_encode(["message" => "Your IP is banned!"]); return Response::unauthorizedResponse($message); }; $response = $this->processRequest(); if (empty($response)) { // $response = Response::serverErrorResponse("error processing request in ". self::class); // Oh my PHP! $response = Response::serverErrorResponse("error processing request in ". static::class); } return $response; } /** * 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, $perm='user&admin'){ // 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; switch ($perm) { case 'user&admin': if (($data->id != $id) && ($data->role != 2)) { $message = json_encode(["message" => "You don't have access to this ressource."]); return Response::unauthorizedResponse($message); } case 'admin': if (($data->role != 2)) { $message = json_encode(["message" => "You are not admin."]); return Response::unauthorizedResponse($message); } case 'validtoken': break; } $message = json_encode(["message" => "Authentified."]); return Response::okResponse($message); } }