UserController.class.php 2.47 KB
Newer Older
quentin.vrel's avatar
quentin.vrel committed
1
<?php
quentin.vrel's avatar
quentin.vrel committed
2 3 4 5 6
include_once __ROOT_DIR . '/libs/php-jwt/src/BeforeValidException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/ExpiredException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/SignatureInvalidException.php';
include_once __ROOT_DIR . '/libs/php-jwt/src/JWT.php';
use \Firebase\JWT\JWT;
quentin.vrel's avatar
quentin.vrel committed
7

Zohten's avatar
Zohten committed
8 9 10 11
class UserController extends Controller
{
    public function __construct($name, $request)
    {
quentin.vrel's avatar
quentin.vrel committed
12 13 14 15 16 17 18 19 20
        parent::__construct($name, $request);
    }

    // ==============
    // Actions
    // ==============

    public function processRequest()
    {
Zohten's avatar
Zohten committed
21
        switch ($this->request->getHttpMethod()) {
quentin.vrel's avatar
quentin.vrel committed
22
            case 'GET':
Zohten's avatar
Zohten committed
23
                if ($this->request->getUriParams()) {
quentin.vrel's avatar
quentin.vrel committed
24
                    return $this->getUser($this->request->getUriParams()[0]);
Zohten's avatar
Zohten committed
25
                }
quentin.vrel's avatar
quentin.vrel committed
26 27 28 29

                return $this->getAllUsers();
                break;
            case 'PUT':
Zohten's avatar
Zohten committed
30 31 32
                if ($this->request->getUriParams()) {
                    return $this->updateUser(array_merge($this->request->getData(), ['id'=>$this->request->getUriParams()[0]]));
                }
quentin.vrel's avatar
quentin.vrel committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46
                break;
        }
        return Response::errorResponse("unsupported parameters or method in users");
    }

    protected function getAllUsers()
    {
        $users = User::getList();
        $response = Response::okResponse(json_encode($users));
        //var_dump($json);die;
        // TODO
        return $response;
    }

Zohten's avatar
Zohten committed
47 48
    protected function getUser($id)
    {
quentin.vrel's avatar
quentin.vrel committed
49 50 51 52
        $user = User::getRow($id);
        $response = Response::okResponse(json_encode($user));
        return $response;
    }
Zohten's avatar
Zohten committed
53 54
    protected function updateUser($array)
    {
quentin.vrel's avatar
quentin.vrel committed
55
        try {
quentin.vrel's avatar
quentin.vrel committed
56
            //var_dump($array);die;
quentin.vrel's avatar
quentin.vrel committed
57 58 59 60
            $jwt_token = $this->request->getJwtToken();
            // echo "jwt = $jwt_token";
            $decodedJWT = JWT::decode($jwt_token, JWT_BACKEND_KEY, array('HS256'));
            
Zohten's avatar
Zohten committed
61
            if ($decodedJWT->data->id != $array['id']) {
quentin.vrel's avatar
quentin.vrel committed
62
                throw new Exception("You don't have access to this account.", 1);
Zohten's avatar
Zohten committed
63
            }
quentin.vrel's avatar
quentin.vrel committed
64 65
            
            User::updateUser($array);
Zohten's avatar
Zohten committed
66
        } catch (Exception $e) {
quentin.vrel's avatar
quentin.vrel committed
67 68 69 70 71 72 73
            header('WWW-Authenticate: Bearer realm="'.JWT_ISSUER.'"');
   
            $jsonResult =  json_encode(array(
                "message" => "Access denied.",
                "error" => $e->getMessage()
            ));
            return Response::unauthorizedResponse($jsonResult);
Zohten's avatar
Zohten committed
74 75 76
        }
        $response = Response::okResponse('User succesfully updated !');
        return $response;
quentin.vrel's avatar
quentin.vrel committed
77
    }
Zohten's avatar
Zohten committed
78
}