Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / settings / storage / PhabricatorUserPreferences.php
blob6cceff57de5d45da02852021d467685864e340ed
1 <?php
3 final class PhabricatorUserPreferences
4 extends PhabricatorUserDAO
5 implements
6 PhabricatorPolicyInterface,
7 PhabricatorDestructibleInterface,
8 PhabricatorApplicationTransactionInterface {
10 const BUILTIN_GLOBAL_DEFAULT = 'global';
12 protected $userPHID;
13 protected $preferences = array();
14 protected $builtinKey;
16 private $user = self::ATTACHABLE;
17 private $defaultSettings;
19 protected function getConfiguration() {
20 return array(
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(
30 'key_user' => array(
31 'columns' => array('userPHID'),
32 'unique' => true,
34 'key_builtin' => array(
35 'columns' => array('builtinKey'),
36 'unique' => true,
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;
53 return $this;
56 public function unsetPreference($key) {
57 unset($this->preferences[$key]);
58 return $this;
61 public function getDefaultValue($key) {
62 if ($this->defaultSettings) {
63 return $this->defaultSettings->getSettingValue($key);
66 $setting = self::getSettingObject($key);
68 if (!$setting) {
69 return null;
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;
93 return $this;
96 public function attachUser(PhabricatorUser $user = null) {
97 $this->user = $user;
98 return $this;
101 public function getUser() {
102 return $this->assertAttached($this->user);
105 public function hasManagedUser() {
106 $user_phid = $this->getUserPHID();
107 if (!$user_phid) {
108 return false;
111 $user = $this->getUser();
112 if ($user->getIsSystemAgent() || $user->getIsMailingList()) {
113 return true;
116 return false;
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())
126 ->setViewer($user)
127 ->withUsers(array($user))
128 ->needSyntheticPreferences(true)
129 ->executeOne();
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())
141 ->setViewer($viewer)
142 ->withBuiltinKeys(
143 array(
144 self::BUILTIN_GLOBAL_DEFAULT,
146 ->executeOne();
148 if (!$global) {
149 $global = id(new self())
150 ->attachUser(new PhabricatorUser());
153 return $global;
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().'/';
169 } else {
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() {
186 return array(
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();
196 if ($user_phid) {
197 return $user_phid;
200 return PhabricatorPolicies::getMostOpenPolicy();
201 case PhabricatorPolicyCapability::CAN_EDIT:
202 if ($this->hasManagedUser()) {
203 return PhabricatorPolicies::POLICY_ADMIN;
206 $user_phid = $this->getUserPHID();
207 if ($user_phid) {
208 return $user_phid;
211 return PhabricatorPolicies::POLICY_ADMIN;
215 public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
216 if ($this->hasManagedUser()) {
217 if ($viewer->getIsAdmin()) {
218 return true;
222 $builtin_key = $this->getBuiltinKey();
224 $is_global = ($builtin_key === self::BUILTIN_GLOBAL_DEFAULT);
225 $is_view = ($capability === PhabricatorPolicyCapability::CAN_VIEW);
227 if ($is_global && $is_view) {
228 // NOTE: Without this policy exception, the logged-out viewer can not
229 // see global preferences.
230 return true;
233 return false;
236 /* -( PhabricatorDestructibleInterface )----------------------------------- */
239 public function destroyObjectPermanently(
240 PhabricatorDestructionEngine $engine) {
241 $this->delete();
245 /* -( PhabricatorApplicationTransactionInterface )------------------------- */
248 public function getApplicationTransactionEditor() {
249 return new PhabricatorUserPreferencesEditor();
252 public function getApplicationTransactionTemplate() {
253 return new PhabricatorUserPreferencesTransaction();