Skip to content
Snippets Groups Projects
Select Git revision
  • a3da1483a9e689846179159355badfec8073dbec
  • main default protected
  • 5.5 protected
  • atlantis
  • 5.3 protected
  • 5.0 protected
  • issue-23
  • issue8-seat-logging-and-export
  • ticket-216
  • tickets-215-216-241-242
10 results

StudipDialog.vue

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;
        }
    }