UserController.class.php 4.67 KB
Newer Older
1 2
<?php

3 4 5 6 7 8
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;

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
class UserController extends Controller {

    public function __construct($name, $request) {
        parent::__construct($name, $request);
    }

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

    public function processRequest()
    {
         switch ($this->request->getHttpMethod()) {
            case 'GET':
                $id = $this->request->getURIParams()[0];
                return $this->getUser($id);
                break;
            
            case 'POST':
                $data = json_decode(file_get_contents("php://input"),TRUE);
                return $this->createUser($data);
                break;

            case 'PUT':
                $id = $this->request->getURIParams()[0];
                $data = json_decode(file_get_contents("php://input"),TRUE);
                return $this->updateUser($id,$data);
                break;

            case 'DELETE':
                $id = $this->request->getURIParams()[0];
                return $this->deleteUser($id);
                break;

        }
        return Response::errorResponse("unsupported parameters or method in user");
    }

    protected function getUser($id)
    {
        $user = User::getUserById($id);
thibaut-felten's avatar
thibaut-felten committed
50 51 52 53 54
        if($user == Array()){
            $response = Response::errorInParametersResponse("User not found");
        }else{
            $response = new Response(200,json_encode($user));
        }
55 56 57 58
        return $response;
    }

    protected function deleteUser($id){
thibaut-felten's avatar
thibaut-felten committed
59 60 61 62 63 64 65
        $user = User::getUserById($id);
        if($user == Array()){
            $response = Response::errorInParametersResponse("User not found");
        }else{
            User::deleteUser($id);
            $response = Response::okResponse("User deleted");
        }
66 67 68
        return $response;
    }

69 70 71
    protected function updateUser($id,$data){
        try {
            $jwt_token = $this->request->getJwtToken();
72

73
            $decodedJWT = JWT::decode($jwt_token, JWT_BACKEND_KEY, array('HS256'));
74

75 76 77 78 79
            $userValues = User::getUserById($id);
            $userValues=($userValues[0]);
            if($userValues == []){
                $response = Response::errorResponse("User not found");
                return $response;
80
            }else{
81 82 83 84 85
                if(array_key_exists('USER_LOGIN',$data)){
                    $login = $data['USER_LOGIN'];
                }else{
                    $login = $userValues->USER_LOGIN;
                }
86

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
                if(array_key_exists('USER_EMAIL',$data)){
                    $email = $data['USER_EMAIL'];
                }else{
                    $email = $userValues->USER_EMAIL;
                }

                if(array_key_exists('USER_LASTNAME',$data)){
                    $lastname = $data['USER_LASTNAME'];
                }else{
                    $lastname = $userValues->USER_LASTNAME;
                }

                if(array_key_exists('USER_FIRSTNAME',$data)){
                    $firstname = $data['USER_FIRSTNAME'];
                }else{
                    $firstname = $userValues->USER_FIRSTNAME;
                }

                $user = User::updateUser(array("id" => $id, "login" => $login,"email" => $email,"lastname" => $lastname,"firstname" => $firstname));
                $response = new Response(200,json_encode($user));
                return $response;
108 109
            }

110 111 112 113 114 115 116 117
        } catch (Exception $e){
            header('WWW-Authenticate: Bearer realm="'.JWT_ISSUER.'"');

            $jsonResult =  json_encode(array(
                "message" => "Access denied.",
                "error" => $e->getMessage()
            ));
            return Response::unauthorizedResponse($jsonResult);
118
        }
119
        
120 121 122 123
    }

    protected function createUser($data)
    {
thibaut-felten's avatar
thibaut-felten committed
124 125 126 127 128 129 130
        if(array_key_exists("USER_LOGIN", $data) && array_key_exists("USER_PASSWORD", $data) && array_key_exists("USER_ROLE", $data) && array_key_exists("USER_EMAIL", $data) && array_key_exists("USER_LASTNAME", $data) && array_key_exists("USER_FIRSTNAME", $data)){
            $user = User::createUser(array( "login" => $data['USER_LOGIN'], "password" => $data['USER_PASSWORD'], "role" => $data['USER_ROLE'], "email" => $data['USER_EMAIL'],"lastname" => $data['USER_LASTNAME'],"firstname" => $data['USER_FIRSTNAME']));
            $response = new Response(200,json_encode($user));
        }else{
            $response = Response::errorInParametersResponse("Parameters missing");
        }
        
131 132 133
        return $response;
    }
}