UserController.class.php 4.21 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
<?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':
raphael.peim's avatar
raphael.peim committed
19 20
                    $post = json_decode(file_get_contents("php://input"));
                    return $this->createUser($post);
raphael.peim's avatar
raphael.peim committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
                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) {
raphael.peim's avatar
raphael.peim committed
42 43 44 45 46 47 48
            if (isset($post->firstname)
                && isset($post->lastname)
                && isset($post->login)
                && isset($post->email)
                && isset($post->password)
                && isset($post->role)) {

raphael.peim's avatar
raphael.peim committed
49 50 51 52
                User::create($post);
                $response = Response::okResponse("Utilisateur ajouté");
            }
            else {
raphael.peim's avatar
raphael.peim committed
53
                $response = Response::errorInParametersResponse("Mauvais paramètres");
raphael.peim's avatar
raphael.peim committed
54 55 56 57 58 59 60 61 62 63 64
            }  

            return $response;
        }

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

            if (!empty($users))
                $response = Response::okResponse(json_encode($users));
            else
raphael.peim's avatar
raphael.peim committed
65
                $response = Response::notFoundResponse("Aucun utilisateur trouvé");
raphael.peim's avatar
raphael.peim committed
66 67 68 69 70 71 72 73 74 75

            return $response;
        }

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

            if (!empty($user))
                $response = Response::okResponse(json_encode($user));
            else
raphael.peim's avatar
raphael.peim committed
76
                $response = Response::notFoundResponse("Aucun utilisateur trouvé");
raphael.peim's avatar
raphael.peim committed
77 78 79 80 81 82 83

            return $response;
        }

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

raphael.peim's avatar
raphael.peim committed
84 85 86 87
            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
88
                
raphael.peim's avatar
raphael.peim committed
89 90 91 92 93 94 95 96
                    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
97 98
                }
                else {
raphael.peim's avatar
raphael.peim committed
99
                    return Response::notFoundResponse("Aucun utilisateur trouvé");
raphael.peim's avatar
raphael.peim committed
100 101 102
                }
            }
            else {
raphael.peim's avatar
raphael.peim committed
103
                return Response::errorInParametersResponse("Mauvais paramètres");
raphael.peim's avatar
raphael.peim committed
104
            }  
raphael.peim's avatar
raphael.peim committed
105 106 107 108 109 110 111 112 113 114
        }

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

            if (!empty($user)) {
                User::delete($id);
                $response = Response::okResponse("Utilisateur supprimé");
            }
            else {
raphael.peim's avatar
raphael.peim committed
115
                $response = Response::notFoundResponse("Aucun utilisateur trouvé");
raphael.peim's avatar
raphael.peim committed
116 117 118 119 120 121
            }  

            return $response;
        }
    }
?>