1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-10 00:42:41 +01:00

Fix construction of default settings for users with no settings at all

Summary:
Ref T11098. Users with at least one setting set correctly fall back to the defaults, but users with no settings at all currently do not.

Make them fall back to global defaults properly.

Test Plan:
  - Set global defaults to some non-default setting.
  - Completely delete a user's settings.
  - `bin/cache purge --purge-all` or `--purge-user`.
  - View settings as the user.
    - Before change: showed hard-coded defaults instead of global defaults until you save anything.
    - After change: properly shows global defaults from the start.

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T11098

Differential Revision: https://secure.phabricator.com/D16112
This commit is contained in:
epriestley 2016-06-13 15:19:51 -07:00
parent 65634781b4
commit d68b2cc0e4
2 changed files with 38 additions and 4 deletions

View file

@ -31,19 +31,39 @@ final class PhabricatorUserPreferencesCacheType
->setViewer($viewer) ->setViewer($viewer)
->withUserPHIDs($user_phids) ->withUserPHIDs($user_phids)
->execute(); ->execute();
$preferences = mpull($preferences, null, 'getUserPHID');
// If some users don't have settings of their own yet, we need to load
// the global default settings to generate caches for them.
if (count($preferences) < count($user_phids)) {
$global = id(new PhabricatorUserPreferencesQuery())
->setViewer($viewer)
->withBuiltinKeys(
array(
PhabricatorUserPreferences::BUILTIN_GLOBAL_DEFAULT,
))
->executeOne();
} else {
$global = null;
}
$all_settings = PhabricatorSetting::getAllSettings(); $all_settings = PhabricatorSetting::getAllSettings();
$settings = array(); $settings = array();
foreach ($preferences as $preference) { foreach ($users as $user_phid => $user) {
$user_phid = $preference->getUserPHID(); $preference = idx($preferences, $user_phid, $global);
if (!$preference) {
continue;
}
foreach ($all_settings as $key => $setting) { foreach ($all_settings as $key => $setting) {
$value = $preference->getSettingValue($key); $value = $preference->getSettingValue($key);
// As an optimization, we omit the value from the cache if it is // As an optimization, we omit the value from the cache if it is
// exactly the same as the hardcoded default. // exactly the same as the hardcoded default.
$default_value = id(clone $setting) $default_value = id(clone $setting)
->setViewer($users[$user_phid]) ->setViewer($user)
->getSettingDefaultValue(); ->getSettingDefaultValue();
if ($value === $default_value) { if ($value === $default_value) {
continue; continue;

View file

@ -130,9 +130,23 @@ final class PhabricatorUserPreferences
return $preferences; return $preferences;
} }
return id(new self()) $preferences = id(new self())
->setUserPHID($user->getPHID()) ->setUserPHID($user->getPHID())
->attachUser($user); ->attachUser($user);
$global = id(new PhabricatorUserPreferencesQuery())
->setViewer($user)
->withBuiltinKeys(
array(
self::BUILTIN_GLOBAL_DEFAULT,
))
->executeOne();
if ($global) {
$preferences->attachDefaultSettings($global);
}
return $preferences;
} }
public function newTransaction($key, $value) { public function newTransaction($key, $value) {