/** * @param Request $request * @return Request */ protected function logIn(Request $request) { $header = $request->getHeaderLine('authorization'); $parts = explode(';', $header); $actor = new Guest(); if (isset($parts[0]) && starts_with($parts[0], $this->prefix)) { $token = substr($parts[0], strlen($this->prefix)); if (($accessToken = AccessToken::find($token)) && $accessToken->isValid()) { $actor = $accessToken->user; $actor->updateLastSeen()->save(); } elseif (isset($parts[1]) && ($apiKey = ApiKey::valid($token))) { $userParts = explode('=', trim($parts[1])); if (isset($userParts[0]) && $userParts[0] === 'userId') { $actor = User::find($userParts[1]); } } } if ($actor->exists) { $locale = $actor->getPreference('locale'); } else { $locale = array_get($request->getCookieParams(), 'locale'); } if ($locale && $this->locales->hasLocale($locale)) { $this->locales->setLocale($locale); } return $request->withAttribute('actor', $actor ?: new Guest()); }
/** * {@inheritdoc} */ public function __invoke(Request $request, Response $response, callable $out = null) { $actor = $request->getAttribute('actor'); if ($actor->exists) { $locale = $actor->getPreference('locale'); } else { $locale = array_get($request->getCookieParams(), 'locale'); } if ($locale && $this->locales->hasLocale($locale)) { $this->locales->setLocale($locale); } return $out ? $out($request, $response) : $response; }
/** * Set the application's actor instance according to the request token. * * @param Request $request * @return Request */ protected function logIn(Request $request) { $actor = new Guest(); if ($token = $this->getToken($request)) { if (!$token->isValid()) { // TODO: https://github.com/flarum/core/issues/253 } elseif ($actor = $token->user) { $actor->updateLastSeen()->save(); } } if ($actor->exists) { $locale = $actor->getPreference('locale'); } else { $locale = array_get($request->getCookieParams(), 'locale'); } if ($locale && $this->locales->hasLocale($locale)) { $this->locales->setLocale($locale); } return $request->withAttribute('actor', $actor); }
/** * Get the name of the locale to use. * * @param User $actor * @param Request $request * @return string */ protected function getLocale(User $actor, Request $request) { if ($actor->exists) { $locale = $actor->getPreference('locale'); } else { $locale = array_get($request->getCookieParams(), 'locale'); } if (!$locale || !$this->locales->hasLocale($locale)) { return $this->settings->get('default_locale', 'en'); } return $locale; }