aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Events/UserEventsIndex.php
blob: 192d279bb45c4425c580c980559b99002394dfc2 (plain)
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
<?php

namespace JsonApi\Routes\Events;

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use JsonApi\JsonApiController;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\RecordNotFoundException;

class UserEventsIndex extends JsonApiController
{
    protected $allowedFilteringParameters = ['timestamp'];

    protected $allowedIncludePaths = ['owner'];

    protected $allowedPagingParameters = ['offset', 'limit'];

    public function __invoke(Request $request, Response $response, $args)
    {
        if (!$observedUser = \User::find($args['id'])) {
            throw new RecordNotFoundException();
        }

        $user = $this->getUser($request);

        if ($user->id !== $observedUser->id) {
            // absichtlich keine AuthorizationFailedException
            // damit unsichtbare Nutzer nicht ermittelt werden können
            throw new RecordNotFoundException();
        }

        $filtering = $this->getQueryParameters()->getFilteringParameters();
        if (isset($filtering['timestamp'])) {
            $start = max(0, (int) $filtering['timestamp']);
        } else {
            $start = (new \DateTime())->modify('midnight')->getTimestamp();
        }
        $end = strtotime('+2 weeks', $start);

        //See the RecordNotFoundException above: This route only lets user
        //retrieve the dates of their own calendar. So no permission check
        //is needed.
        $start_dt = new \DateTime();
        $start_dt->setTimestamp($start);
        $end_dt = new \DateTime();
        $end_dt->setTimestamp($end);
        $list = \CalendarDateAssignment::getEvents($start_dt, $end_dt, $user->id);
        list($offset, $limit) = $this->getOffsetAndLimit();

        return $this->getPaginatedContentResponse(
            array_slice(array_values($list), $offset, $limit),
            count($list)
        );
    }
}