Changeset View
Changeset View
Standalone View
Standalone View
src/applications/people/storage/PhabricatorUser.php
| Show First 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | final class PhabricatorUser | ||||
| private $alternateCSRFString = self::ATTACHABLE; | private $alternateCSRFString = self::ATTACHABLE; | ||||
| private $session = self::ATTACHABLE; | private $session = self::ATTACHABLE; | ||||
| private $rawCacheData = array(); | private $rawCacheData = array(); | ||||
| private $usableCacheData = array(); | private $usableCacheData = array(); | ||||
| private $authorities = array(); | private $authorities = array(); | ||||
| private $handlePool; | private $handlePool; | ||||
| private $csrfSalt; | private $csrfSalt; | ||||
| private $timezoneOverride; | |||||
| private $settingCacheKeys = array(); | |||||
| private $settingCache = array(); | |||||
| protected function readField($field) { | protected function readField($field) { | ||||
| switch ($field) { | switch ($field) { | ||||
| // Make sure these return booleans. | // Make sure these return booleans. | ||||
| case 'isAdmin': | case 'isAdmin': | ||||
| return (bool)$this->isAdmin; | return (bool)$this->isAdmin; | ||||
| case 'isDisabled': | case 'isDisabled': | ||||
| return (bool)$this->isDisabled; | return (bool)$this->isDisabled; | ||||
| ▲ Show 20 Lines • Show All 398 Lines • ▼ Show 20 Lines | return $this->loadOneRelative( | ||||
| '(isPrimary = 1)'); | '(isPrimary = 1)'); | ||||
| } | } | ||||
| /* -( Settings )----------------------------------------------------------- */ | /* -( Settings )----------------------------------------------------------- */ | ||||
| public function getUserSetting($key) { | public function getUserSetting($key) { | ||||
| // NOTE: We store available keys and cached values separately to make it | |||||
| // faster to check for `null` in the cache, which is common. | |||||
| if (isset($this->settingCacheKeys[$key])) { | |||||
| return $this->settingCache[$key]; | |||||
| } | |||||
| $settings_key = PhabricatorUserPreferencesCacheType::KEY_PREFERENCES; | $settings_key = PhabricatorUserPreferencesCacheType::KEY_PREFERENCES; | ||||
| $settings = $this->requireCacheData($settings_key); | $settings = $this->requireCacheData($settings_key); | ||||
| $defaults = PhabricatorSetting::getAllEnabledSettings($this); | |||||
| if (array_key_exists($key, $settings)) { | if (array_key_exists($key, $settings)) { | ||||
| return $settings[$key]; | $value = $settings[$key]; | ||||
| // Make sure the value is valid before we return it. This makes things | |||||
| // more robust when options are changed or removed. | |||||
| if (isset($defaults[$key])) { | |||||
| try { | |||||
| id(clone $defaults[$key]) | |||||
| ->setViewer($this) | |||||
| ->assertValidValue($value); | |||||
| $this->settingCacheKeys[$key] = true; | |||||
| $this->settingCache[$key] = $value; | |||||
| return $value; | |||||
| } catch (Exception $ex) { | |||||
| // Fall through below and return the default value. | |||||
| } | |||||
| } | |||||
| } | } | ||||
| $defaults = PhabricatorSetting::getAllEnabledSettings($this); | |||||
| if (isset($defaults[$key])) { | if (isset($defaults[$key])) { | ||||
| return $defaults[$key]->getSettingDefaultValue(); | $value = id(clone $defaults[$key]) | ||||
| ->setViewer($this) | |||||
| ->getSettingDefaultValue(); | |||||
| } else { | |||||
| $value = null; | |||||
| } | } | ||||
| return null; | $this->settingCacheKeys[$key] = true; | ||||
| $this->settingCache[$key] = $value; | |||||
| return $value; | |||||
| } | } | ||||
| /** | /** | ||||
| * Test if a given setting is set to a particular value. | * Test if a given setting is set to a particular value. | ||||
| * | * | ||||
| * @param const Setting key. | * @param const Setting key. | ||||
| * @param wild Value to compare. | * @param wild Value to compare. | ||||
| * @return bool True if the setting has the specified value. | * @return bool True if the setting has the specified value. | ||||
| * @task settings | * @task settings | ||||
| */ | */ | ||||
| public function compareUserSetting($key, $value) { | public function compareUserSetting($key, $value) { | ||||
| $actual = $this->getUserSetting($key); | $actual = $this->getUserSetting($key); | ||||
| return ($actual == $value); | return ($actual == $value); | ||||
| } | } | ||||
| /** | |||||
| * @task settings | |||||
| */ | |||||
| public function clearUserSettingCache() { | |||||
| $this->settingCacheKeys = array(); | |||||
| $this->settingCache = array(); | |||||
| $settings_key = PhabricatorUserPreferencesCacheType::KEY_PREFERENCES; | |||||
| $this->clearCacheData($settings_key); | |||||
| return $this; | |||||
| } | |||||
| public function getTranslation() { | public function getTranslation() { | ||||
| return $this->getUserSetting(PhabricatorTranslationSetting::SETTINGKEY); | return $this->getUserSetting(PhabricatorTranslationSetting::SETTINGKEY); | ||||
| } | } | ||||
| public function getTimezoneIdentifier() { | public function getTimezoneIdentifier() { | ||||
| if ($this->timezoneOverride) { | |||||
| return $this->timezoneOverride; | |||||
| } | |||||
| return $this->getUserSetting(PhabricatorTimezoneSetting::SETTINGKEY); | return $this->getUserSetting(PhabricatorTimezoneSetting::SETTINGKEY); | ||||
| } | } | ||||
| /** | /** | ||||
| * Override the user's timezone identifier. | * Override the user's timezone identifier. | ||||
| * | * | ||||
| * This is primarily useful for unit tests. | * This is primarily useful for unit tests. | ||||
| * | * | ||||
| * @param string New timezone identifier. | * @param string New timezone identifier. | ||||
| * @return this | * @return this | ||||
| * @task settings | * @task settings | ||||
| */ | */ | ||||
| public function overrideTimezoneIdentifier($identifier) { | public function overrideTimezoneIdentifier($identifier) { | ||||
| $this->timezoneOverride = $identifier; | $timezone_key = PhabricatorTimezoneSetting::SETTINGKEY; | ||||
| $this->settingCacheKeys[$timezone_key] = true; | |||||
| $this->settingCache[$timezone_key] = $identifier; | |||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getSex() { | public function getSex() { | ||||
| return $this->getUserSetting(PhabricatorPronounSetting::SETTINGKEY); | return $this->getUserSetting(PhabricatorPronounSetting::SETTINGKEY); | ||||
| } | } | ||||
| public function loadPreferences() { | public function loadPreferences() { | ||||
| Show All 28 Lines | public function loadPreferences() { | ||||
| return $preferences; | return $preferences; | ||||
| } | } | ||||
| public function loadEditorLink( | public function loadEditorLink( | ||||
| $path, | $path, | ||||
| $line, | $line, | ||||
| PhabricatorRepository $repository = null) { | PhabricatorRepository $repository = null) { | ||||
| $editor = $this->loadPreferences()->getPreference( | $editor = $this->getUserSetting(PhabricatorEditorSetting::SETTINGKEY); | ||||
| PhabricatorUserPreferences::PREFERENCE_EDITOR); | |||||
| if (is_array($path)) { | if (is_array($path)) { | ||||
| $multiedit = $this->loadPreferences()->getPreference( | $multi_key = PhabricatorEditorMultipleSetting::SETTINGKEY; | ||||
| PhabricatorUserPreferences::PREFERENCE_MULTIEDIT); | $multiedit = $this->getUserSetting($multi_key); | ||||
| switch ($multiedit) { | switch ($multiedit) { | ||||
| case '': | case PhabricatorEditorMultipleSetting::VALUE_SPACES: | ||||
| $path = implode(' ', $path); | $path = implode(' ', $path); | ||||
| break; | break; | ||||
| case 'disable': | case PhabricatorEditorMultipleSetting::VALUE_SINGLE: | ||||
| default: | |||||
| return null; | return null; | ||||
| } | } | ||||
| } | } | ||||
| if (!strlen($editor)) { | if (!strlen($editor)) { | ||||
| return null; | return null; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 243 Lines • ▼ Show 20 Lines | public function formatShortDateTime($when, $now = null) { | ||||
| if ($when->format('Y') !== $now->format('Y')) { | if ($when->format('Y') !== $now->format('Y')) { | ||||
| // Different year, so show "Feb 31 2075". | // Different year, so show "Feb 31 2075". | ||||
| $format = 'M j Y'; | $format = 'M j Y'; | ||||
| } else if ($when->format('Ymd') !== $now->format('Ymd')) { | } else if ($when->format('Ymd') !== $now->format('Ymd')) { | ||||
| // Same year but different month and day, so show "Feb 31". | // Same year but different month and day, so show "Feb 31". | ||||
| $format = 'M j'; | $format = 'M j'; | ||||
| } else { | } else { | ||||
| // Same year, month and day so show a time of day. | // Same year, month and day so show a time of day. | ||||
| $pref_time = PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT; | $pref_time = PhabricatorTimeFormatSetting::SETTINGKEY; | ||||
| $format = $this->getPreference($pref_time); | $format = $this->getUserSetting($pref_time); | ||||
| } | } | ||||
| return $when->format($format); | return $when->format($format); | ||||
| } | } | ||||
| public function getPreference($key) { | |||||
| $preferences = $this->loadPreferences(); | |||||
| // TODO: After T4103 and T7707 this should eventually be pushed down the | |||||
| // stack into modular preference definitions and role profiles. This is | |||||
| // just fixing T8601 and mildly anticipating those changes. | |||||
epriestley: This change is pretty much what this comment was anticipating. | |||||
| $value = $preferences->getPreference($key); | |||||
| $allowed_values = null; | |||||
| switch ($key) { | |||||
| case PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT: | |||||
| $allowed_values = array( | |||||
| 'g:i A', | |||||
| 'H:i', | |||||
| ); | |||||
| break; | |||||
| case PhabricatorUserPreferences::PREFERENCE_DATE_FORMAT: | |||||
| $allowed_values = array( | |||||
| 'Y-m-d', | |||||
| 'n/j/Y', | |||||
| 'd-m-Y', | |||||
| ); | |||||
| break; | |||||
| } | |||||
| if ($allowed_values !== null) { | |||||
| $allowed_values = array_fuse($allowed_values); | |||||
| if (empty($allowed_values[$value])) { | |||||
| $value = head($allowed_values); | |||||
| } | |||||
| } | |||||
| return $value; | |||||
| } | |||||
| public function __toString() { | public function __toString() { | ||||
| return $this->getUsername(); | return $this->getUsername(); | ||||
| } | } | ||||
| public static function loadOneWithEmailAddress($address) { | public static function loadOneWithEmailAddress($address) { | ||||
| $email = id(new PhabricatorUserEmail())->loadOneWhere( | $email = id(new PhabricatorUserEmail())->loadOneWhere( | ||||
| 'address = %s', | 'address = %s', | ||||
| $address); | $address); | ||||
| ▲ Show 20 Lines • Show All 682 Lines • Show Last 20 Lines | |||||
This change is pretty much what this comment was anticipating.