UserController.class.php 4.26 KB
Newer Older
raphael.peim's avatar
raphael.peim committed
1 2 3 4 5 6 7 8 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
<?php
    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;

    class UserController extends Controller {
        public function __construct($name, $request) {
            parent::__construct($name, $request);
        }

        // ==============
        // Actions
        // ==============
        public function processRequest() {
            switch ($this->request->getHttpMethod()) {
                case 'POST':
                    $post = json_decode(file_get_contents("php://input"));
                    return $this->createUser($post);
                break;
                case 'GET':
                    if (empty($this->request->getUriParameters()))
                        return $this->getAllUsers();
                    else 
                        return $this->getUserById($this->request->getUriParameters()[0]);
                break;
                case 'PUT':
                    $put = json_decode(file_get_contents("php://input"));
                    $id = $this->request->getUriParameters()[0];
                    return $this->updateUser($put, $id);
                break;
                case 'DELETE':
                    $id = $this->request->getUriParameters()[0];
                    return $this->deleteUser($id);
                break;
            }
            return Response::errorResponse("unsupported parameters or method in users");
        }

        protected function createUser($post) {
            if (isset($post->firstname)
                && isset($post->lastname)
                && isset($post->login)
                && isset($post->email)
                && isset($post->password)
                && isset($post->role)) {

                User::create($post);
                $response = Response::okResponse("Utilisateur ajouté");
            }
            else {
                // $response = Response::notFoundResponse("Aucun utilisateur ajouté");
                $response = Response::notFoundResponse(var_dump($post));
            }  

            return $response;
        }

        protected function getAllUsers() {
            $users = User::getList();

            if (!empty($users))
                $response = Response::okResponse(json_encode($users));
            else
                $response = Response::notFoundResponse("Aucune réponse");

            return $response;
        }

        protected function getUserById($id) {
            $user = User::getWithId($id);

            if (!empty($user))
                $response = Response::okResponse(json_encode($user));
            else
                $response = Response::notFoundResponse("Aucune réponse");

            return $response;
        }

        protected function updateUser($put, $id) {
            $user = User::getWithId($id);

raphael.peim's avatar
raphael.peim committed
85 86 87 88
            if (isset($put->email)) {
                if (!empty($user)) {
                    $jwt_token = $this->request->getJwtToken();
                    $jwt = JWT::decode($jwt_token, JWT_BACKEND_KEY, array('HS256'));
raphael.peim's avatar
raphael.peim committed
89
                
raphael.peim's avatar
raphael.peim committed
90 91 92 93 94 95 96 97
                    if ($jwt->data->id == $id) {
                        User::update($put, $id);
                        $response = Response::okResponse("Utilisateur modifié");
                        return $response;
                    }
                    else {
                        return Response::unauthorizedResponse("Modification non autorisée");
                    }
raphael.peim's avatar
raphael.peim committed
98 99
                }
                else {
raphael.peim's avatar
raphael.peim committed
100
                    return Response::notFoundResponse("Aucun utilisateur trouvé");
raphael.peim's avatar
raphael.peim committed
101 102 103
                }
            }
            else {
raphael.peim's avatar
raphael.peim committed
104
                return Response::notFoundResponse("Mauvais paramètres");
raphael.peim's avatar
raphael.peim committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
            }  
        }

        protected function deleteUser($id) {
            $user = User::getWithId($id);

            if (!empty($user)) {
                User::delete($id);
                $response = Response::okResponse("Utilisateur supprimé");
            }
            else {
                $response = Response::notFoundResponse("Aucun utilisateur supprimé");
            }  

            return $response;
        }
    }
?>