UserController.class.php 3.22 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 36
                break;
        }
Zohten's avatar
Zohten committed
37 38
        $message = json_encode(["message" => "unsupported parameters or method in users"]);
        return Response::errorResponse($message);
Zohten's avatar
Zohten committed
39 40
    }

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

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

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

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

    /**
    * Update a specific user in USER table based on id
    *
93
    * @param    array    $array    array containing id + fields to modify
Zohten's avatar
Zohten committed
94 95
    * @return    Response
    */
Zohten's avatar
Zohten committed
96 97
    protected function updateUser($array)
    {
Zohten's avatar
Zohten committed
98
        // Auth with token phase
99
        if($this->authUser($array['id'])->getCode()!=200){
Zohten's avatar
Zohten committed
100
            return authError;
Zohten's avatar
Zohten committed
101
        }
102

Zohten's avatar
Zohten committed
103
        // Update phase
104
        User::updateUser($array);
Zohten's avatar
Zohten committed
105 106 107 108

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

Zohten's avatar
Zohten committed
109
        return $response;
Zohten's avatar
Zohten committed
110
    }
Zohten's avatar
Zohten committed
111
}