stripUsernameRegexp = $config->get('strip_username_regexp'); } /** * {@inheritdoc} */ public function getName() { return $this->name; } /** * {@inheritdoc} */ public function setName($name) { $this->name = $name; return $this; } /** * Get the remote user from environment or $_SERVER, if any * * @param string $variable The name of the variable where to read the user from * * @return string|null */ public static function getRemoteUser($variable = 'REMOTE_USER') { $username = getenv($variable); if (! empty($username)) { return $username; } if (array_key_exists($variable, $_SERVER) && ! empty($_SERVER[$variable])) { return $_SERVER[$variable]; } } /** * Get the remote user information from environment or $_SERVER, if any * * @return array Contains always two entries, the username and origin which may both set to null. */ public static function getRemoteUserInformation() { foreach (static::$remoteUserEnvvars as $envVar) { $username = static::getRemoteUser($envVar); if ($username !== null) { return array($username, $envVar); } } return array(null, null); } /** * {@inheritdoc} */ public function authenticate(User $user, $password = null) { list($username, $field) = static::getRemoteUserInformation(); if ($username !== null) { $user->setExternalUserInformation($username, $field); if ($this->stripUsernameRegexp) { $stripped = @preg_replace($this->stripUsernameRegexp, '', $username); if ($stripped === false) { Logger::error('Failed to strip external username. The configured regular expression is invalid.'); return false; } $username = $stripped; } $user->setUsername($username); return true; } return false; } }