aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/InstituteMemberships/ByUserIndex.php
blob: c7612e1b14a844a59413244f53e320c59ced2700 (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
<?php

namespace JsonApi\Routes\InstituteMemberships;

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

class ByUserIndex extends JsonApiController
{
    protected $allowedIncludePaths = ['user', 'institute'];

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

    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function __invoke(Request $request, Response $response, $args)
    {
        if (!$user = \User::find($args['id'])) {
            throw new RecordNotFoundException();
        }

        if (!Authority::canShowUser($this->getUser($request), $user)) {
            throw new AuthorizationFailedException();
        }

        $institutes = $user->institute_memberships;
        if (!$GLOBALS['perm']->have_profile_perm('user', $user->id)) {
            $institutes = $institutes->filter(fn($membership) => $membership->inst_perms !== 'user');
        }
        $total = count($institutes);
        list($offset, $limit) = $this->getOffsetAndLimit();

        return $this->getPaginatedContentResponse($institutes->limit($offset, $limit), $total);
    }
}