Skip to content
Snippets Groups Projects
Select Git revision
  • e6a11cb96f7a0259554580369f41b2caf259c650
  • main default protected
  • step-3263
  • feature/plugins-cli
  • feature/vite
  • step-2484-peerreview
  • biest/issue-5051
  • tests/simplify-jsonapi-tests
  • fix/typo-in-1a70031
  • feature/broadcasting
  • database-seeders-and-factories
  • feature/peer-review-2
  • feature-feedback-jsonapi
  • feature/peerreview
  • feature/balloon-plus
  • feature/stock-images-unsplash
  • tic-2588
  • 5.0
  • 5.2
  • biest/unlock-blocks
  • biest-1514
21 results

StructuralElementsCopy.php

Blame
  • Forked from Stud.IP / Stud.IP
    Source project has a limited visibility.
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    UsersIndex.php 2.20 KiB
    <?php
    
    namespace JsonApi\Routes\Users;
    
    use JsonApi\Errors\AuthorizationFailedException;
    use JsonApi\Errors\BadRequestException;
    use JsonApi\JsonApiController;
    use JsonApi\Schemas\User as UserSchema;
    use Psr\Http\Message\ResponseInterface as Response;
    use Psr\Http\Message\ServerRequestInterface as Request;
    
    class UsersIndex extends JsonApiController
    {
        protected $allowedFilteringParameters = ['search'];
        protected $allowedIncludePaths = [
            UserSchema::REL_ACTIVITYSTREAM,
            UserSchema::REL_CONTACTS,
            UserSchema::REL_COURSES,
            UserSchema::REL_COURSE_MEMBERSHIPS,
            UserSchema::REL_EVENTS,
            UserSchema::REL_INSTITUTE_MEMBERSHIPS,
            UserSchema::REL_SCHEDULE,
        ];
        protected $allowedPagingParameters = ['offset', 'limit'];
    
        /**
         * @SuppressWarnings(PHPMD.UnusedFormalParameter)
         */
        public function __invoke(Request $request, Response $response, $args): Response
        {
            if (!Authority::canIndexUsers($this->getUser($request))) {
                throw new AuthorizationFailedException();
            }
    
            $this->validateFilters();
            $filters = $this->getFilters();
    
            list($offset, $limit) = $this->getOffsetAndLimit();
            $partSQL = \GlobalSearchUsers::getSQL($filters['search'], [], $limit + $offset);
            $total = (int) \DBManager::get()->fetchColumn('SELECT FOUND_ROWS() as found_rows');
            $users = \User::findMany(
                array_map(function ($array) {
                    return $array['user_id'];
                }, \DBManager::get()->fetchAll($partSQL))
            );
    
            return $this->getPaginatedContentResponse($users, $total);
        }
    
        private function validateFilters(): void
        {
            $filtering = $this->getQueryParameters()->getFilteringParameters() ?? [];
    
            if (array_key_exists('search', $filtering)) {
                if (mb_strlen(trim($filtering['search'])) < 3) {
                    throw new BadRequestException('Filter `search` should be at least 3 characters long.');
                }
            }
        }
    
        private function getFilters()
        {
            $filtering = $this->getQueryParameters()->getFilteringParameters() ?? [];
    
            $filters['search'] = $filtering['search'] ?? '%%%';
    
            return $filters;
        }
    }