Adds the user context for a given user.

@param int $userid
@return $this
768891
public function add_user_context(int $userid) : contextlist {
        $sql = "SELECT DISTINCT ctx.id
                  FROM {context} ctx
                 WHERE ctx.contextlevel = :contextlevel
                   AND ctx.instanceid = :instanceid";
        return $this->add_from_sql($sql, ['contextlevel' => CONTEXT_USER, 'instanceid' => $userid]);
    }
916824
public function removeUser($userId)
    {
        if (true === is_numeric($userId) && true === in_array($userId, $this->users)) {
            foreach (array_keys($this->users, (int)$userId, true) as $key) {
                unset($this->users[$key]);
            }
        }
        return $this;
    }
1049225
protected static function export_user_data_in_category_contexts($userid, array $contexts) {
        $contexts = array_filter($contexts, function($context) {
            return $context->contextlevel == CONTEXT_COURSECAT;
        });
        if (empty($contexts)) {
            return;
        }

        foreach ($contexts as $context) {
            static::export_user_data_frameworks_in_context($userid, $context);
            static::export_user_data_templates_in_context($userid, $context);
        }
    }
413137
public function loadUser($userId)
    {
        $user = $this->userService->loadUser($userId, Language::ALL);

        $userContentInfo = $user->getVersionInfo()->getContentInfo();
        $contentType = $this->contentTypeService->loadContentType($userContentInfo->contentTypeId);

        try {
            $userMainLocation = $this->locationService->loadLocation($userContentInfo->mainLocationId);
            $relations = $this->contentService->loadRelations($user->getVersionInfo());
        } catch (UnauthorizedException $e) {
            // TODO: Hack for special case to allow current logged in user to load him/here self (but not relations)
            if ($user->id == $this->repository->getCurrentUser()->id) {
                $userMainLocation = $this->repository->sudo(
                    function () use ($userContentInfo) {
                        return $this->locationService->loadLocation($userContentInfo->mainLocationId);
                    }
                );
                // user may not have permissions to read related content, for security reasons do not use sudo().
                $relations = array();
            } else {
                throw $e;
            }
        }

        return new Values\CachedValue(
            new Values\RestUser(
                $user,
                $contentType,
                $userContentInfo,
                $userMainLocation,
                $relations
            ),
            array('locationId' => $userContentInfo->mainLocationId)
        );
    }
262338
private function createAuthorization($userId, $instanceId)
    {
        $apiAll = $this->getNavitiaApiAllId();
        $this->call($this->tyrUrl . '/' . $this->version . '/users/' . $userId . '/authorizations/?api_id=' . $apiAll->id . '&instance_id=' . $instanceId, 'POST');

        return true;
    }
152356
public function assignUser($userGroupId, $userId = [])
    {
        $this->groupId = $userGroupId;

        $this->getLogger()->info('Assigning User IDs ' . json_encode($userId) . ' To User Group ID ' . $this->groupId);
        $response = $this->doPost('/group/members/assign/' . $userGroupId, [
            'userId' => $userId
        ]);

        /** @var XiboUserGroup $userGroup */
        $userGroup = $this->hydrate($response);

        return $userGroup;
    }
356542
public static function add_contexts_for_userid(contextlist $contextlist, $userid) {
        list($db, $table) = static::get_database_and_table();
        if (!$db || !$table) {
            return;
        }

        $sql = 'userid = :userid1 OR relateduserid = :userid2 OR realuserid = :userid3';
        $params = ['userid1' => $userid, 'userid2' => $userid, 'userid3' => $userid];
        $contextids = $db->get_fieldset_select($table, 'DISTINCT contextid', $sql, $params);
        if (empty($contextids)) {
            return;
        }

        $sql = implode(' UNION ', array_map(function($id) use ($db) {
            return 'SELECT ' . $id . $db->sql_null_from_clause();
        }, $contextids));
        $contextlist->add_from_sql($sql, []);
    }
385234
public function loadOrGenerateByUser($userId)
    {
        $referenceClass = $this->referenceClass;
        $reference = new $referenceClass();
        $reference->setId(WorkflowRightInterface::NODE);

        $contentTypes = $this->contentTypeRepository->findAllNotDeletedInLastVersion();
        foreach ($contentTypes as $key => $contentType) {
            if (!$contentType->isDefiningStatusable()) {
                unset($contentTypes[$key]);
            }
        }
        $contentTypes[] = $reference;

        $workflowRight = $this->workflowRightRepository->findOneByUserId($userId);

        if (null === $workflowRight) {
            $workflowRightClass = $this->workflowRightClass;
            $workflowRight = new $workflowRightClass();
            $workflowRight->setUserId($userId);
        }

        return $this->authorizationWorkflowRightManager->cleanAuthorization($contentTypes, $workflowRight);
    }
416853
public static function get_contexts_for_userid(int $userid) : contextlist {
        $sql = "SELECT id
                  FROM {context}
                 WHERE instanceid = :userid
                       AND contextlevel = :contextlevel";

        $contextlist = new contextlist();
        $contextlist->set_component('tool_dataprivacy');
        $contextlist->add_from_sql($sql, ['userid' => $userid, 'contextlevel' => CONTEXT_USER]);
        return $contextlist;
    }
586815
public function loadPoliciesByUserId($userId)
    {
        $groupIds = $this->fetchUserGroups($userId);
        $groupIds[] = $userId;

        return $this->loadRolesForContentObjects($groupIds);
    }
219223
public function createPushPackageForUser($userId)
    {
        $packageDir = sprintf('/%s/pushPackage%s.%s', sys_get_temp_dir(), time(), $userId);
        $package = $this->createPackage($packageDir, $userId);

        $this->generatePackage($package);

        return $package;
    }
1082199
public function getUser(string $userid)
    {
        $cl = $this->getUserClass();
        $user = new $cl;
        return $user->obtainByUserID($userid);
    }
1361373
public static function add_contexts_for_userid(contextlist $contextlist, $userid) {
        $sql = "
            SELECT l.contextid
              FROM {logstore_standard_log} l
             WHERE l.userid = :userid1
                OR l.relateduserid = :userid2
                OR l.realuserid = :userid3";
        $contextlist->add_from_sql($sql, [
            'userid1' => $userid,
            'userid2' => $userid,
            'userid3' => $userid,
        ]);
    }
37661
public function setCurrent($userid)
	{
		$this->debug->groupCollapsed(__METHOD__, $userid);
		Session::start();
		$user = $this->get($userid);
		// initialize session array
		ArrayUtil::path($_SESSION, $this->cfg['sessionPath'], array());
		// create link to session array
		// $this->user = &ArrayUtil::path($_SESSION, $this->cfg['sessionPath']);
        $path = $this->cfg['sessionPath'];
        if (!\is_array($path)) {
            $path = \array_filter(\preg_split('#[\./]#', $path), 'strlen');
        }
        $this->user =& $_SESSION;
        foreach ($path as $k) {
        	if (!isset($this->user[$k])) {
        		$this->user[$k] = array();
        	}
        	$this->user =& $this->user[$k];
        }
		// load session array
		$this->user = $user;
		$this->user['_ts_cur'] = time();
		$this->userid = $this->user['userid'];
		$this->update(array('lastactivity'=>'NOW()'));
		$this->debug->groupEnd();
		return;
	}
324407
public function getUser($userId)
    {
        $response = $this->client->get(sprintf('/v2/user/%s', $userId));

        return new User($response);
    }
1068672
public function addUser(User $user)
    {
        if (!$this->users->contains($user)) {
            $this->users->add($user);
        }

        return $this;
    }
952868
private function setUserRole($userId)
    {
        if (is_multisite()) {

            //Init sites if not done
            if (is_null($this->sites)) {
                $this->initSites();
            }

            //Get role (superadmin should always be administrator)
            if (is_super_admin($userId)) {
                $role = "administrator";
            } else {
                $role = $this->defaultRole;
            }

            //Bulk add (or just this site)
            if (defined('AD_BULK_IMPORT_PROPAGATE') && AD_BULK_IMPORT_PROPAGATE === true) {
                foreach ($this->sites as $site) {
                    if (is_user_member_of_blog($userId, $site->blog_id) === true) {
                        continue;
                    }
                    add_user_to_blog($site->blog_id, $userId, $role);
                }
            } elseif (is_user_member_of_blog($userId, get_current_blog_id()) !== true) {
                add_user_to_blog(get_current_blog_id(), $userId, $role);
            }
        } else {
            if (isset(get_userdata($userId)->roles) && !empty(get_userdata($userId)->roles)) {
                return false;
            }
            wp_update_user(array('ID' => $userId, 'role' => $this->defaultRole));
        }
    }