3 final class PhabricatorUserPreferences
4 extends PhabricatorUserDAO
6 PhabricatorPolicyInterface
,
7 PhabricatorDestructibleInterface
,
8 PhabricatorApplicationTransactionInterface
{
10 const BUILTIN_GLOBAL_DEFAULT
= 'global';
13 protected $preferences = array();
14 protected $builtinKey;
16 private $user = self
::ATTACHABLE
;
17 private $defaultSettings;
19 protected function getConfiguration() {
21 self
::CONFIG_AUX_PHID
=> true,
22 self
::CONFIG_SERIALIZATION
=> array(
23 'preferences' => self
::SERIALIZATION_JSON
,
25 self
::CONFIG_COLUMN_SCHEMA
=> array(
26 'userPHID' => 'phid?',
27 'builtinKey' => 'text32?',
29 self
::CONFIG_KEY_SCHEMA
=> array(
31 'columns' => array('userPHID'),
34 'key_builtin' => array(
35 'columns' => array('builtinKey'),
39 ) + parent
::getConfiguration();
42 public function generatePHID() {
43 return PhabricatorPHID
::generateNewPHID(
44 PhabricatorUserPreferencesPHIDType
::TYPECONST
);
47 public function getPreference($key, $default = null) {
48 return idx($this->preferences
, $key, $default);
51 public function setPreference($key, $value) {
52 $this->preferences
[$key] = $value;
56 public function unsetPreference($key) {
57 unset($this->preferences
[$key]);
61 public function getDefaultValue($key) {
62 if ($this->defaultSettings
) {
63 return $this->defaultSettings
->getSettingValue($key);
66 $setting = self
::getSettingObject($key);
72 $setting = id(clone $setting)
73 ->setViewer($this->getUser());
75 return $setting->getSettingDefaultValue();
78 public function getSettingValue($key) {
79 if (array_key_exists($key, $this->preferences
)) {
80 return $this->preferences
[$key];
83 return $this->getDefaultValue($key);
86 private static function getSettingObject($key) {
87 $settings = PhabricatorSetting
::getAllSettings();
88 return idx($settings, $key);
91 public function attachDefaultSettings(PhabricatorUserPreferences
$settings) {
92 $this->defaultSettings
= $settings;
96 public function attachUser(PhabricatorUser
$user = null) {
101 public function getUser() {
102 return $this->assertAttached($this->user
);
105 public function hasManagedUser() {
106 $user_phid = $this->getUserPHID();
111 $user = $this->getUser();
112 if ($user->getIsSystemAgent() ||
$user->getIsMailingList()) {
120 * Load or create a preferences object for the given user.
122 * @param PhabricatorUser User to load or create preferences for.
124 public static function loadUserPreferences(PhabricatorUser
$user) {
125 return id(new PhabricatorUserPreferencesQuery())
127 ->withUsers(array($user))
128 ->needSyntheticPreferences(true)
133 * Load or create a global preferences object.
135 * If no global preferences exist, an empty preferences object is returned.
137 * @param PhabricatorUser Viewing user.
139 public static function loadGlobalPreferences(PhabricatorUser
$viewer) {
140 $global = id(new PhabricatorUserPreferencesQuery())
144 self
::BUILTIN_GLOBAL_DEFAULT
,
149 $global = id(new self())
150 ->attachUser(new PhabricatorUser());
156 public function newTransaction($key, $value) {
157 $setting_property = PhabricatorUserPreferencesTransaction
::PROPERTY_SETTING
;
158 $xaction_type = PhabricatorUserPreferencesTransaction
::TYPE_SETTING
;
160 return id(clone $this->getApplicationTransactionTemplate())
161 ->setTransactionType($xaction_type)
162 ->setMetadataValue($setting_property, $key)
163 ->setNewValue($value);
166 public function getEditURI() {
167 if ($this->getUser()) {
168 return '/settings/user/'.$this->getUser()->getUsername().'/';
170 return '/settings/builtin/'.$this->getBuiltinKey().'/';
174 public function getDisplayName() {
175 if ($this->getBuiltinKey()) {
176 return pht('Global Default Settings');
179 return pht('Personal Settings');
182 /* -( PhabricatorPolicyInterface )----------------------------------------- */
185 public function getCapabilities() {
187 PhabricatorPolicyCapability
::CAN_VIEW
,
188 PhabricatorPolicyCapability
::CAN_EDIT
,
192 public function getPolicy($capability) {
193 switch ($capability) {
194 case PhabricatorPolicyCapability
::CAN_VIEW
:
195 $user_phid = $this->getUserPHID();
200 return PhabricatorPolicies
::getMostOpenPolicy();
201 case PhabricatorPolicyCapability
::CAN_EDIT
:
202 if ($this->hasManagedUser()) {
203 return PhabricatorPolicies
::POLICY_ADMIN
;
206 $user_phid = $this->getUserPHID();
211 return PhabricatorPolicies
::POLICY_ADMIN
;
215 public function hasAutomaticCapability($capability, PhabricatorUser
$viewer) {
216 if ($this->hasManagedUser()) {
217 if ($viewer->getIsAdmin()) {
222 switch ($this->getBuiltinKey()) {
223 case self
::BUILTIN_GLOBAL_DEFAULT
:
224 // NOTE: Without this policy exception, the logged-out viewer can not
225 // see global preferences.
232 /* -( PhabricatorDestructibleInterface )----------------------------------- */
235 public function destroyObjectPermanently(
236 PhabricatorDestructionEngine
$engine) {
241 /* -( PhabricatorApplicationTransactionInterface )------------------------- */
244 public function getApplicationTransactionEditor() {
245 return new PhabricatorUserPreferencesEditor();
248 public function getApplicationTransactionTemplate() {
249 return new PhabricatorUserPreferencesTransaction();