UserController.class.php 3.77 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
        }
Zohten's avatar
Zohten committed
41 42
        $message = json_encode(["message" => "unsupported parameters or method in users"]);
        return Response::errorResponse($message);
Zohten's avatar
Zohten committed
43 44
    }

45 46 47 48 49 50 51
    /**
    * 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
52 53 54
        // Token phase
        $verifyArray = $this->request->verifyJwtToken();
        if ($verifyArray['message']!=="Valid token.") {
55 56
            $message = json_encode($verifyArray['error']);
            return Response::unauthorizedResponse($message);
Zohten's avatar
Zohten committed
57 58 59 60 61 62 63
        }
        // 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);
        }
64 65 66

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

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

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

    /**
Zohten's avatar
Zohten committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    * (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!']);
        $response = Response::okResponse(json_encode($message));
        return $response;
    }

    /**
    * (PUT) Update a specific user in USER table based on id
Zohten's avatar
Zohten committed
110
    *
111
    * @param    array    $array    array containing id + fields to modify
Zohten's avatar
Zohten committed
112 113
    * @return    Response
    */
Zohten's avatar
Zohten committed
114 115
    protected function updateUser($array)
    {
Zohten's avatar
Zohten committed
116
        // Auth with token phase
117
        if($this->authUser($array['id'])->getCode()!=200){
Zohten's avatar
Zohten committed
118
            return authError;
Zohten's avatar
Zohten committed
119
        }
120

Zohten's avatar
Zohten committed
121
        // Update phase
122
        User::updateUser($array);
Zohten's avatar
Zohten committed
123

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

Zohten's avatar
Zohten committed
127
        return $response;
Zohten's avatar
Zohten committed
128
    }
Zohten's avatar
Zohten committed
129
}