@param ContainerInterface $container
@param int                $userId
@param array|null         $scope

@return null|array

@throws ContainerExceptionInterface
@throws NotFoundExceptionInterface
487796
public static function validateScope(ContainerInterface $container, int $userId, array $scope = null): ?array
    {
        // Here is the place you can implement your scope limitation for users. Such as
        // limiting scopes that could be assigned for the user token.
        // It could be role based system or any other system that suits your application.
        //
        // Possible return values:
        // - `null` means no scope changes for the user.
        // - `array` with scope identities. Token issued for the user will be limited to this scope.
        // - authorization exception if you want to stop token issuing process and notify the user
        //   do not have enough rights to issue requested scopes.

        $result = null;
        if ($scope !== null) {
            /** @var UsersApi $usersApi */
            /** @var FactoryInterface $factory */
            $factory  = $container->get(FactoryInterface::class);
            $usersApi = $factory->createApi(UsersApi::class);

            $userScopes    = $usersApi->readScopes($userId);
            $adjustedScope = array_intersect($userScopes, $scope);
            if (count($adjustedScope) !== count($scope)) {
                $result = $adjustedScope;
            }
        }

        return $result;
    }
743219
public function setContainer(ContainerInterface $container = null)
    {
        parent::setContainer($container);

        if ($this->getApplication()) {
            $mediaClasses = $this->getApplication()->getAutoloader()->glob('Media'.DIRECTORY_SEPARATOR.'*');
            foreach ($mediaClasses as $mediaClass) {
                class_exists($mediaClass);
            }
        }
    }
649451
protected function prepare(ContainerInterface $container = null, array $arguments = [])
    {
        $result = [];

        foreach ($arguments as $k => $arg) {
            if (is_array($arg)) {
                $result[$k] = $this->prepare($container, $arg);
                continue;
            } elseif ($arg == '@Container') {
                $result[$k] = $container;
                continue;
            } elseif (strpos($arg, '@') !== 0) {
                $result[$k] = $arg;
                continue;
            }

            $arg = substr($arg, 1);

            if (!$container) {
                throw new ContainerException(sprintf('Unable to resolve dependency for "%s" - missing dependency "%s"', $this->class, $arg));
            }

            $result[$k] = $container->get($arg);
        }

        return $result;
    }
29573
public function register(ContainerInterface $container)
    {
        if (! isset($container['session'])) {
            $container['session'] = function (ContainerInterface $container) {
                return new Session;
            };
        }

        if (! isset($container['router'])) {
            /**
             * This service MUST return a SHARED instance
             * of Mbh\Interfaces\RouterInterface.
             *
             * @param Container $container
             * @return RouterInterface
             */
            $container['router'] = function (ContainerInterface $container) {
                $routerCacheFile = false;
                if (isset($container->get('settings')['routerCacheFile'])) {
                    $routerCacheFile = $container->get('settings')['routerCacheFile'];
                }

                $router = (new Router)->setCacheFile($routerCacheFile);
                if (method_exists($router, 'setContainer')) {
                    $router->setContainer($container);
                }

                if (method_exists($router, 'setCallableResolver')) {
                    $router->setCallableResolver($container['callableResolver']);
                }

                return $router;
            };
        }
    }
785041
public function processUserRoles(UserInterface $user, $userRoles)
    {
        $get = function($entity) {
            /* @var UserInterface $entity */
            return $entity->getId();
        };

        $delete = function($userRole) use ($user) {
            $user->removeUserRole($userRole);
            $this->em->remove($userRole);
        };

        $update = function($userRole, $userRoleData) {
            return $this->updateUserRole($userRole, $userRoleData);
        };

        $add = function($userRole) use ($user) {
            return $this->addUserRole($user, $userRole);
        };

        $entities = $user->getUserRoles();

        $result = $this->processSubEntities(
            $entities,
            $userRoles,
            $get,
            $add,
            $update,
            $delete
        );

        $this->resetIndexOfSubentites($entities);

        return $result;
    }
352851
public function assignRoleToUser($userId, Request $request)
    {
        $roleAssignment = $this->inputDispatcher->parse(
            new Message(
                array('Content-Type' => $request->headers->get('Content-Type')),
                $request->getContent()
            )
        );

        $user = $this->userService->loadUser($userId);
        $role = $this->roleService->loadRole($roleAssignment->roleId);

        try {
            $this->roleService->assignRoleToUser($role, $user, $roleAssignment->limitation);
        } catch (LimitationValidationException $e) {
            throw new BadRequestException($e->getMessage());
        }

        $roleAssignments = $this->roleService->getRoleAssignmentsForUser($user);

        return new Values\RoleAssignmentList($roleAssignments, $user->id);
    }
137201
public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint)
    {
        // The provider does nothing, but is required
        $providerId = sprintf('%s.%s', self::PROVIDER_SWITCH_USER_STATELESS, $id);
        $container->setDefinition(
            $providerId,
            new DefinitionDecorator(self::PROVIDER_SWITCH_USER_STATELESS)
        );

        // The listener does the logic
        $listenerId = sprintf('%s.%s', self::LISTENER_SWITCH_USER_STATELESS, $id);
        $container
            ->setDefinition(
                $listenerId,
                new DefinitionDecorator(self::LISTENER_SWITCH_USER_STATELESS)
            )
            ->replaceArgument(1, new Reference($userProvider))
            ->replaceArgument(3, $id)
            ->replaceArgument(6, $config['parameter'])
            ->replaceArgument(7, $config['role'])
        ;

        return [$providerId, $listenerId, $defaultEntryPoint];
    }
799741
private static function invalidRequestHandler(ContainerInterface $container): InvalidRequestHandlerInterface
    {
        if (!$container->has(InvalidRequestHandlerInterface::class)) {
            return new InvalidRequestHandler();
        }

        return $container->get(InvalidRequestHandlerInterface::class);
    }
1276797
public function load($userid, $apikey, $scope, $name, $args)
    {
        $key = $this->getKey($userid, $apikey, $scope, $name, $args);

        return $this->redis->get($key);
    }
900243
public function prepareHandler(Container $container)
    {
        $handler = $container->get('config')->get('user.handler') ?: NullUserHandler::class;

        $container->bindShared(UserHandlerInterface::class, $handler);
    }
1082199
public function getUser(string $userid)
    {
        $cl = $this->getUserClass();
        $user = new $cl;
        return $user->obtainByUserID($userid);
    }
746205
protected function call($method, $id)
    {
        if (!$this->container) {
            throw new ContainerException('Container not injected');
        }
        if (\method_exists($this->container, $method)) {
            return $this->container->$method($id);
        }
        throw new ContainerException('Container method not found: ' . $method);
    }
58832
public function authorisation(Container $container)
    {
        $auth   = new Authorisation();
        $config = $container->get('config');

        foreach ((array) $config->get('user.policies') as $name => $policy) {
            if ($policy instanceof ClassMeta || (is_string($policy) && class_exists($policy))) {
                $instance = $container->newInstance($policy);

                if (is_subclass_of($instance, PolicyInterface::class)) {
                    $auth->addPolicy($name, $instance);
                } elseif (is_subclass_of($instance, PolicyProviderInterface::class)) {
                    $auth->registerPolicyProvider($instance);
                }
            } elseif ($policy === false) {
                continue;
            } else {
                throw new \InvalidArgumentException(sprintf(
                    'Please register instance of %s or %s',
                    PolicyInterface::class,
                    PolicyProviderInterface::class
                ));
            }
        }

        /** @var EventDispatcher $dispatcher */
        $this->dispatcher->triggerEvent('onLoadAuthorisationPolicies', ['auth' => $auth]);

        return $auth;
    }
427085
public static function can_create_data_deletion_request_for_children(int $userid, int $requesterid = null): bool {
        global $USER;
        $requesterid = $requesterid ?: $USER->id;
        return has_capability('tool/dataprivacy:makedatadeletionrequestsforchildren', \context_user::instance($userid),
            $requesterid);
    }
1331271
private function buildBatch($userId)
    {
        $batch = array();

        while ($method = $this->getQueue()->dequeue(MethodInterface::SERVER_PLATFORM)) {
            $batch[] = $this->getMethodData($method, $userId);
        }

        return $batch;
    }
867160
public function add(ContainerInterface $container, $name = null)
    {
        if ($name !== null) {
            $this->containers[$name] = $container;
        } else {
            array_push($this->containers, $container);
        }
    }
1209465
public function initContainer($container, $key = '')
    {
        if ($container instanceof Container) {
            $this->setContainer($container);
            $this->setLogger(
                $this->getContainer()->offsetGet(LoggerService::SERVICE_ID)->getLogger()
            );

            // @TODO: implemented because of the legacy, we will don't need it when we're using the container in every case.
            if (!empty($key)) {
                try {
                    return $this->getContainer()->offsetGet($key);
                } catch (\InvalidArgumentException $e) {
                }
            }

            return null;
        }

        return $container;
    }