UserController.class.php 4.52 KB
Newer Older
Zohten's avatar
Zohten committed
1 2
<?php

Zohten's avatar
Zohten committed
3 4 5 6
class UserController extends Controller
{
    public function __construct($name, $request)
    {
Zohten's avatar
Zohten committed
7 8 9
        parent::__construct($name, $request);
    }

Zohten's avatar
Zohten committed
10 11 12 13 14
    /**
    * Process incoming request for the /user endpoint
    *
    * @return    Response
    */
Zohten's avatar
Zohten committed
15 16
    public function processRequest()
    {
17 18 19
        $httpMethod=$this->request->getHttpMethod();
        $uriParams=$this->request->getUriParams();

Zohten's avatar
Zohten committed
20
        switch ($httpMethod) {
Zohten's avatar
Zohten committed
21
            case 'GET':
Zohten's avatar
Zohten committed
22
                // If there is a uriParams, it is the /user/{id} endpoint
23 24
                if ($uriParams) {
                    return $this->getUser($uriParams[0]);
Zohten's avatar
Zohten committed
25
                }
Zohten's avatar
Zohten committed
26
                // Else, it is the /user endpoint
Zohten's avatar
Zohten committed
27 28 29
                return $this->getAllUsers();
                break;
            case 'PUT':
Zohten's avatar
Zohten committed
30
                // If there is a uriParams, it is the /user/{id} endpoint
31
                if ($uriParams) {
Zohten's avatar
Zohten committed
32 33
                    $body = $this->request->getData();
                    return $this->updateUser(array_merge($body, ['id'=>$uriParams[0]]));
Zohten's avatar
Zohten committed
34
                }
Zohten's avatar
Zohten committed
35
                break;
Zohten's avatar
Zohten committed
36 37 38 39
            case 'POST':
                $body = $this->request->getData();
                return $this->addUser($body);
                break;
Zohten's avatar
Zohten committed
40 41 42 43 44
            case 'DELETE':
                if ($uriParams) {
                    return $this->deleteUser($uriParams[0]);
                }
                break;
Zohten's avatar
Zohten committed
45
        }
Zohten's avatar
Zohten committed
46 47
        $message = json_encode(["message" => "unsupported parameters or method in users"]);
        return Response::errorResponse($message);
Zohten's avatar
Zohten committed
48 49
    }

50 51 52 53 54 55 56
    /**
    * 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){
Zohten's avatar
Zohten committed
57 58 59
        // Token phase
        $verifyArray = $this->request->verifyJwtToken();
        if ($verifyArray['message']!=="Valid token.") {
60 61
            $message = json_encode($verifyArray['error']);
            return Response::unauthorizedResponse($message);
Zohten's avatar
Zohten committed
62 63 64 65 66 67 68
        }
        // Auth phase
        $data = $verifyArray['decodedJWT']->data;
        if (($data->id != $id) && ($data->role != 2)) {
            $message = json_encode(["message" => "You don't have access to this account."]);
            return Response::unauthorizedResponse($message);
        }
69 70 71

        $message = json_encode(["message" => "Authentified."]);
        return Response::okResponse($message);
Zohten's avatar
Zohten committed
72 73
    }

Zohten's avatar
Zohten committed
74
    /**
Zohten's avatar
Zohten committed
75
    * (GET) Get all users in USER table
Zohten's avatar
Zohten committed
76 77 78
    *
    * @return    Response
    */
Zohten's avatar
Zohten committed
79 80 81
    protected function getAllUsers()
    {
        $users = User::getList();
82
        $response = Response::okResponse(json_encode($users, JSON_PRETTY_PRINT));
Zohten's avatar
Zohten committed
83 84 85
        return $response;
    }

Zohten's avatar
Zohten committed
86
    /**
Zohten's avatar
Zohten committed
87
    * (GET) Get a specific user in USER table based on id
Zohten's avatar
Zohten committed
88
    *
89
    * @param    int    $id    id of the User
Zohten's avatar
Zohten committed
90 91
    * @return    Response
    */
Zohten's avatar
Zohten committed
92 93
    protected function getUser($id)
    {
Zohten's avatar
Zohten committed
94 95 96 97
        $user = User::getRow($id);
        $response = Response::okResponse(json_encode($user));
        return $response;
    }
Zohten's avatar
Zohten committed
98 99

    /**
Zohten's avatar
Zohten committed
100 101 102 103 104 105 106 107 108
    * (POST) Add a specific user in USER table
    *
    * @param    array    $array    array containing
    * @return    Response
    */
    protected function addUser($array)
    {
        User::addRow($array);
        $message = json_encode(["message" => 'User succesfully added!']);
Zohten's avatar
Zohten committed
109
        $response = Response::okResponse($message);
Zohten's avatar
Zohten committed
110 111 112 113 114
        return $response;
    }

    /**
    * (PUT) Update a specific user in USER table based on id
Zohten's avatar
Zohten committed
115
    *
116
    * @param    array    $array    array containing id + fields to modify
Zohten's avatar
Zohten committed
117 118
    * @return    Response
    */
Zohten's avatar
Zohten committed
119 120
    protected function updateUser($array)
    {
Zohten's avatar
Zohten committed
121
        // Auth with token phase
Zohten's avatar
Zohten committed
122 123 124
        $authResponse = $this->authUser($id);
        if($authResponse->getCode()!=200){
            return $authResponse;
Zohten's avatar
Zohten committed
125
        }
126

Zohten's avatar
Zohten committed
127
        // Update phase
128
        User::updateUser($array);
Zohten's avatar
Zohten committed
129

Zohten's avatar
Zohten committed
130
        $message = json_encode(["message" => 'User succesfully updated!']);
Zohten's avatar
Zohten committed
131 132
        $response = Response::okResponse($message);

Zohten's avatar
Zohten committed
133
        return $response;
Zohten's avatar
Zohten committed
134
    }
Zohten's avatar
Zohten committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

    /**
    * (DELETE) Delete a specific user in USER table based on id
    *
    * @param    int    $id    id of the User
    * @return    Response
    */
    protected function deleteUser($id)
    {
        // Auth with token phase
        $authResponse = $this->authUser($id);
        if($authResponse->getCode()!=200){
            return $authResponse;
        }

        // Update phase
        User::deleteRow($id);

        $message = json_encode(["message" => 'User succesfully deleted!']);
        $response = Response::okResponse($message);

        return $response;
    }
Zohten's avatar
Zohten committed
158
}