Skip to content

Commit

Permalink
Don't fetch the logged-in user to get their preferred language
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Feb 22, 2018
1 parent 6aecd1f commit 4873162
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Added
- Added the `getenv()` Twig function. ([#2471](https://github.com/craftcms/cms/pull/2471))
- Added `craft\services\Users::getUserPreference()`.

### Changed
- Element query classes can now specify the default `orderBy` value by overriding `craft\elements\db\ElementQuery::defaultOrderBy`.
Expand All @@ -16,6 +17,7 @@
- Fixed a bug where `craft\helpers\ChartHelper::getRunChartDataFromQuery()` was overriding the query’s `SELECT` clause.
- Fixed a bug where Single sections were showing the currently logged-in user as their author.
- Fixed a bug where element queries for entries within Structure sections weren’t getting ordered in the Structure-defined order by default in some cases.
- Fixed a bug where `yii\web\User::getIdentity()` would return `null` when called from a plugin’s `init()` method. ([#2473](https://github.com/craftcms/cms/issues/2473))

## 3.0.0-RC11 - 2018-02-20

Expand Down
17 changes: 7 additions & 10 deletions src/base/ApplicationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use craft\db\Connection;
use craft\db\MigrationManager;
use craft\db\Query;
use craft\elements\User;
use craft\errors\DbConnectException;
use craft\errors\WrongEditionException;
use craft\events\EditionChangeEvent;
Expand Down Expand Up @@ -1186,15 +1185,13 @@ private function _getUserLanguage(): string
{
/** @var WebApplication|ConsoleApplication $this */
// If the user is logged in *and* has a primary language set, use that
try {
/** @var User|null $user */
$user = $this->getUser()->getIdentity();
} catch (\Exception $e) {
$user = null;
}

if ($user && ($preferredLanguage = $user->getPreferredLanguage()) !== null) {
return $preferredLanguage;
if ($this instanceof WebApplication) {
// Don't actually try to fetch the user, as plugins haven't been loaded yet.
$session = $this->getSession();
$id = $session->getHasSessionId() || $session->getIsActive() ? $session->get($this->getUser()->idParam) : null;
if ($id && ($language = $this->getUsers()->getUserPreference($id, 'language')) !== null) {
return $language;
}
}

// Fall back on the default CP language, if there is one, otherwise the browser language
Expand Down
14 changes: 14 additions & 0 deletions src/services/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,20 @@ public function saveUserPreferences(User $user, array $preferences)
->execute();
}

/**
* Returns one of a user’s preferences by its key.
*
* @param int|null $userId The user’s ID
* @param string $key The preference’s key
* @param mixed $default The default value, if the preference hasn’t been set
* @return mixed The user’s preference
*/
public function getUserPreference(int $userId = null, string $key, $default = null)
{
$preferences = $this->getUserPreferences($userId);
return $preferences[$key] ?? $default;
}

/**
* Sends a new account activation email for a user, regardless of their status.
* A new verification code will generated for the user overwriting any existing one.
Expand Down

0 comments on commit 4873162

Please sign in to comment.