PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/applications/transactions/editor/PhabricatorApplicationTransactionEditor.php

http://github.com/facebook/phabricator
PHP | 5406 lines | 3683 code | 971 blank | 752 comment | 427 complexity | 420a5bc5efdd5e47135045a109caa51f MD5 | raw file
Possible License(s): JSON, MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause, LGPL-2.0, MIT, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. *
  4. * Publishing and Managing State
  5. * ======
  6. *
  7. * After applying changes, the Editor queues a worker to publish mail, feed,
  8. * and notifications, and to perform other background work like updating search
  9. * indexes. This allows it to do this work without impacting performance for
  10. * users.
  11. *
  12. * When work is moved to the daemons, the Editor state is serialized by
  13. * @{method:getWorkerState}, then reloaded in a daemon process by
  14. * @{method:loadWorkerState}. **This is fragile.**
  15. *
  16. * State is not persisted into the daemons by default, because we can not send
  17. * arbitrary objects into the queue. This means the default behavior of any
  18. * state properties is to reset to their defaults without warning prior to
  19. * publishing.
  20. *
  21. * The easiest way to avoid this is to keep Editors stateless: the overwhelming
  22. * majority of Editors can be written statelessly. If you need to maintain
  23. * state, you can either:
  24. *
  25. * - not require state to exist during publishing; or
  26. * - pass state to the daemons by implementing @{method:getCustomWorkerState}
  27. * and @{method:loadCustomWorkerState}.
  28. *
  29. * This architecture isn't ideal, and we may eventually split this class into
  30. * "Editor" and "Publisher" parts to make it more robust. See T6367 for some
  31. * discussion and context.
  32. *
  33. * @task mail Sending Mail
  34. * @task feed Publishing Feed Stories
  35. * @task search Search Index
  36. * @task files Integration with Files
  37. * @task workers Managing Workers
  38. */
  39. abstract class PhabricatorApplicationTransactionEditor
  40. extends PhabricatorEditor {
  41. private $contentSource;
  42. private $object;
  43. private $xactions;
  44. private $isNewObject;
  45. private $mentionedPHIDs;
  46. private $continueOnNoEffect;
  47. private $continueOnMissingFields;
  48. private $raiseWarnings;
  49. private $parentMessageID;
  50. private $heraldAdapter;
  51. private $heraldTranscript;
  52. private $subscribers;
  53. private $unmentionablePHIDMap = array();
  54. private $transactionGroupID;
  55. private $applicationEmail;
  56. private $isPreview;
  57. private $isHeraldEditor;
  58. private $isInverseEdgeEditor;
  59. private $actingAsPHID;
  60. private $heraldEmailPHIDs = array();
  61. private $heraldForcedEmailPHIDs = array();
  62. private $heraldHeader;
  63. private $mailToPHIDs = array();
  64. private $mailCCPHIDs = array();
  65. private $feedNotifyPHIDs = array();
  66. private $feedRelatedPHIDs = array();
  67. private $feedShouldPublish = false;
  68. private $mailShouldSend = false;
  69. private $modularTypes;
  70. private $silent;
  71. private $mustEncrypt = array();
  72. private $stampTemplates = array();
  73. private $mailStamps = array();
  74. private $oldTo = array();
  75. private $oldCC = array();
  76. private $mailRemovedPHIDs = array();
  77. private $mailUnexpandablePHIDs = array();
  78. private $mailMutedPHIDs = array();
  79. private $webhookMap = array();
  80. private $transactionQueue = array();
  81. private $sendHistory = false;
  82. private $shouldRequireMFA = false;
  83. private $hasRequiredMFA = false;
  84. private $request;
  85. private $cancelURI;
  86. private $extensions;
  87. private $parentEditor;
  88. private $subEditors = array();
  89. private $publishableObject;
  90. private $publishableTransactions;
  91. const STORAGE_ENCODING_BINARY = 'binary';
  92. /**
  93. * Get the class name for the application this editor is a part of.
  94. *
  95. * Uninstalling the application will disable the editor.
  96. *
  97. * @return string Editor's application class name.
  98. */
  99. abstract public function getEditorApplicationClass();
  100. /**
  101. * Get a description of the objects this editor edits, like "Differential
  102. * Revisions".
  103. *
  104. * @return string Human readable description of edited objects.
  105. */
  106. abstract public function getEditorObjectsDescription();
  107. public function setActingAsPHID($acting_as_phid) {
  108. $this->actingAsPHID = $acting_as_phid;
  109. return $this;
  110. }
  111. public function getActingAsPHID() {
  112. if ($this->actingAsPHID) {
  113. return $this->actingAsPHID;
  114. }
  115. return $this->getActor()->getPHID();
  116. }
  117. /**
  118. * When the editor tries to apply transactions that have no effect, should
  119. * it raise an exception (default) or drop them and continue?
  120. *
  121. * Generally, you will set this flag for edits coming from "Edit" interfaces,
  122. * and leave it cleared for edits coming from "Comment" interfaces, so the
  123. * user will get a useful error if they try to submit a comment that does
  124. * nothing (e.g., empty comment with a status change that has already been
  125. * performed by another user).
  126. *
  127. * @param bool True to drop transactions without effect and continue.
  128. * @return this
  129. */
  130. public function setContinueOnNoEffect($continue) {
  131. $this->continueOnNoEffect = $continue;
  132. return $this;
  133. }
  134. public function getContinueOnNoEffect() {
  135. return $this->continueOnNoEffect;
  136. }
  137. /**
  138. * When the editor tries to apply transactions which don't populate all of
  139. * an object's required fields, should it raise an exception (default) or
  140. * drop them and continue?
  141. *
  142. * For example, if a user adds a new required custom field (like "Severity")
  143. * to a task, all existing tasks won't have it populated. When users
  144. * manually edit existing tasks, it's usually desirable to have them provide
  145. * a severity. However, other operations (like batch editing just the
  146. * owner of a task) will fail by default.
  147. *
  148. * By setting this flag for edit operations which apply to specific fields
  149. * (like the priority, batch, and merge editors in Maniphest), these
  150. * operations can continue to function even if an object is outdated.
  151. *
  152. * @param bool True to continue when transactions don't completely satisfy
  153. * all required fields.
  154. * @return this
  155. */
  156. public function setContinueOnMissingFields($continue_on_missing_fields) {
  157. $this->continueOnMissingFields = $continue_on_missing_fields;
  158. return $this;
  159. }
  160. public function getContinueOnMissingFields() {
  161. return $this->continueOnMissingFields;
  162. }
  163. /**
  164. * Not strictly necessary, but reply handlers ideally set this value to
  165. * make email threading work better.
  166. */
  167. public function setParentMessageID($parent_message_id) {
  168. $this->parentMessageID = $parent_message_id;
  169. return $this;
  170. }
  171. public function getParentMessageID() {
  172. return $this->parentMessageID;
  173. }
  174. public function getIsNewObject() {
  175. return $this->isNewObject;
  176. }
  177. public function getMentionedPHIDs() {
  178. return $this->mentionedPHIDs;
  179. }
  180. public function setIsPreview($is_preview) {
  181. $this->isPreview = $is_preview;
  182. return $this;
  183. }
  184. public function getIsPreview() {
  185. return $this->isPreview;
  186. }
  187. public function setIsSilent($silent) {
  188. $this->silent = $silent;
  189. return $this;
  190. }
  191. public function getIsSilent() {
  192. return $this->silent;
  193. }
  194. public function getMustEncrypt() {
  195. return $this->mustEncrypt;
  196. }
  197. public function getHeraldRuleMonograms() {
  198. // Convert the stored "<123>, <456>" string into a list: "H123", "H456".
  199. $list = $this->heraldHeader;
  200. $list = preg_split('/[, ]+/', $list);
  201. foreach ($list as $key => $item) {
  202. $item = trim($item, '<>');
  203. if (!is_numeric($item)) {
  204. unset($list[$key]);
  205. continue;
  206. }
  207. $list[$key] = 'H'.$item;
  208. }
  209. return $list;
  210. }
  211. public function setIsInverseEdgeEditor($is_inverse_edge_editor) {
  212. $this->isInverseEdgeEditor = $is_inverse_edge_editor;
  213. return $this;
  214. }
  215. public function getIsInverseEdgeEditor() {
  216. return $this->isInverseEdgeEditor;
  217. }
  218. public function setIsHeraldEditor($is_herald_editor) {
  219. $this->isHeraldEditor = $is_herald_editor;
  220. return $this;
  221. }
  222. public function getIsHeraldEditor() {
  223. return $this->isHeraldEditor;
  224. }
  225. public function addUnmentionablePHIDs(array $phids) {
  226. foreach ($phids as $phid) {
  227. $this->unmentionablePHIDMap[$phid] = true;
  228. }
  229. return $this;
  230. }
  231. private function getUnmentionablePHIDMap() {
  232. return $this->unmentionablePHIDMap;
  233. }
  234. protected function shouldEnableMentions(
  235. PhabricatorLiskDAO $object,
  236. array $xactions) {
  237. return true;
  238. }
  239. public function setApplicationEmail(
  240. PhabricatorMetaMTAApplicationEmail $email) {
  241. $this->applicationEmail = $email;
  242. return $this;
  243. }
  244. public function getApplicationEmail() {
  245. return $this->applicationEmail;
  246. }
  247. public function setRaiseWarnings($raise_warnings) {
  248. $this->raiseWarnings = $raise_warnings;
  249. return $this;
  250. }
  251. public function getRaiseWarnings() {
  252. return $this->raiseWarnings;
  253. }
  254. public function setShouldRequireMFA($should_require_mfa) {
  255. if ($this->hasRequiredMFA) {
  256. throw new Exception(
  257. pht(
  258. 'Call to setShouldRequireMFA() is too late: this Editor has already '.
  259. 'checked for MFA requirements.'));
  260. }
  261. $this->shouldRequireMFA = $should_require_mfa;
  262. return $this;
  263. }
  264. public function getShouldRequireMFA() {
  265. return $this->shouldRequireMFA;
  266. }
  267. public function getTransactionTypesForObject($object) {
  268. $old = $this->object;
  269. try {
  270. $this->object = $object;
  271. $result = $this->getTransactionTypes();
  272. $this->object = $old;
  273. } catch (Exception $ex) {
  274. $this->object = $old;
  275. throw $ex;
  276. }
  277. return $result;
  278. }
  279. public function getTransactionTypes() {
  280. $types = array();
  281. $types[] = PhabricatorTransactions::TYPE_CREATE;
  282. $types[] = PhabricatorTransactions::TYPE_HISTORY;
  283. if ($this->object instanceof PhabricatorEditEngineSubtypeInterface) {
  284. $types[] = PhabricatorTransactions::TYPE_SUBTYPE;
  285. }
  286. if ($this->object instanceof PhabricatorSubscribableInterface) {
  287. $types[] = PhabricatorTransactions::TYPE_SUBSCRIBERS;
  288. }
  289. if ($this->object instanceof PhabricatorCustomFieldInterface) {
  290. $types[] = PhabricatorTransactions::TYPE_CUSTOMFIELD;
  291. }
  292. if ($this->object instanceof PhabricatorTokenReceiverInterface) {
  293. $types[] = PhabricatorTransactions::TYPE_TOKEN;
  294. }
  295. if ($this->object instanceof PhabricatorProjectInterface ||
  296. $this->object instanceof PhabricatorMentionableInterface) {
  297. $types[] = PhabricatorTransactions::TYPE_EDGE;
  298. }
  299. if ($this->object instanceof PhabricatorSpacesInterface) {
  300. $types[] = PhabricatorTransactions::TYPE_SPACE;
  301. }
  302. $types[] = PhabricatorTransactions::TYPE_MFA;
  303. $template = $this->object->getApplicationTransactionTemplate();
  304. if ($template instanceof PhabricatorModularTransaction) {
  305. $xtypes = $template->newModularTransactionTypes();
  306. foreach ($xtypes as $xtype) {
  307. $types[] = $xtype->getTransactionTypeConstant();
  308. }
  309. }
  310. if ($template) {
  311. $comment = $template->getApplicationTransactionCommentObject();
  312. if ($comment) {
  313. $types[] = PhabricatorTransactions::TYPE_COMMENT;
  314. }
  315. }
  316. return $types;
  317. }
  318. private function adjustTransactionValues(
  319. PhabricatorLiskDAO $object,
  320. PhabricatorApplicationTransaction $xaction) {
  321. if ($xaction->shouldGenerateOldValue()) {
  322. $old = $this->getTransactionOldValue($object, $xaction);
  323. $xaction->setOldValue($old);
  324. }
  325. $new = $this->getTransactionNewValue($object, $xaction);
  326. $xaction->setNewValue($new);
  327. }
  328. private function getTransactionOldValue(
  329. PhabricatorLiskDAO $object,
  330. PhabricatorApplicationTransaction $xaction) {
  331. $type = $xaction->getTransactionType();
  332. $xtype = $this->getModularTransactionType($type);
  333. if ($xtype) {
  334. $xtype = clone $xtype;
  335. $xtype->setStorage($xaction);
  336. return $xtype->generateOldValue($object);
  337. }
  338. switch ($type) {
  339. case PhabricatorTransactions::TYPE_CREATE:
  340. case PhabricatorTransactions::TYPE_HISTORY:
  341. return null;
  342. case PhabricatorTransactions::TYPE_SUBTYPE:
  343. return $object->getEditEngineSubtype();
  344. case PhabricatorTransactions::TYPE_MFA:
  345. return null;
  346. case PhabricatorTransactions::TYPE_SUBSCRIBERS:
  347. return array_values($this->subscribers);
  348. case PhabricatorTransactions::TYPE_VIEW_POLICY:
  349. if ($this->getIsNewObject()) {
  350. return null;
  351. }
  352. return $object->getViewPolicy();
  353. case PhabricatorTransactions::TYPE_EDIT_POLICY:
  354. if ($this->getIsNewObject()) {
  355. return null;
  356. }
  357. return $object->getEditPolicy();
  358. case PhabricatorTransactions::TYPE_JOIN_POLICY:
  359. if ($this->getIsNewObject()) {
  360. return null;
  361. }
  362. return $object->getJoinPolicy();
  363. case PhabricatorTransactions::TYPE_SPACE:
  364. if ($this->getIsNewObject()) {
  365. return null;
  366. }
  367. $space_phid = $object->getSpacePHID();
  368. if ($space_phid === null) {
  369. $default_space = PhabricatorSpacesNamespaceQuery::getDefaultSpace();
  370. if ($default_space) {
  371. $space_phid = $default_space->getPHID();
  372. }
  373. }
  374. return $space_phid;
  375. case PhabricatorTransactions::TYPE_EDGE:
  376. $edge_type = $xaction->getMetadataValue('edge:type');
  377. if (!$edge_type) {
  378. throw new Exception(
  379. pht(
  380. "Edge transaction has no '%s'!",
  381. 'edge:type'));
  382. }
  383. // See T13082. If this is an inverse edit, the parent editor has
  384. // already populated the transaction values correctly.
  385. if ($this->getIsInverseEdgeEditor()) {
  386. return $xaction->getOldValue();
  387. }
  388. $old_edges = array();
  389. if ($object->getPHID()) {
  390. $edge_src = $object->getPHID();
  391. $old_edges = id(new PhabricatorEdgeQuery())
  392. ->withSourcePHIDs(array($edge_src))
  393. ->withEdgeTypes(array($edge_type))
  394. ->needEdgeData(true)
  395. ->execute();
  396. $old_edges = $old_edges[$edge_src][$edge_type];
  397. }
  398. return $old_edges;
  399. case PhabricatorTransactions::TYPE_CUSTOMFIELD:
  400. // NOTE: Custom fields have their old value pre-populated when they are
  401. // built by PhabricatorCustomFieldList.
  402. return $xaction->getOldValue();
  403. case PhabricatorTransactions::TYPE_COMMENT:
  404. return null;
  405. default:
  406. return $this->getCustomTransactionOldValue($object, $xaction);
  407. }
  408. }
  409. private function getTransactionNewValue(
  410. PhabricatorLiskDAO $object,
  411. PhabricatorApplicationTransaction $xaction) {
  412. $type = $xaction->getTransactionType();
  413. $xtype = $this->getModularTransactionType($type);
  414. if ($xtype) {
  415. $xtype = clone $xtype;
  416. $xtype->setStorage($xaction);
  417. return $xtype->generateNewValue($object, $xaction->getNewValue());
  418. }
  419. switch ($type) {
  420. case PhabricatorTransactions::TYPE_CREATE:
  421. return null;
  422. case PhabricatorTransactions::TYPE_SUBSCRIBERS:
  423. return $this->getPHIDTransactionNewValue($xaction);
  424. case PhabricatorTransactions::TYPE_VIEW_POLICY:
  425. case PhabricatorTransactions::TYPE_EDIT_POLICY:
  426. case PhabricatorTransactions::TYPE_JOIN_POLICY:
  427. case PhabricatorTransactions::TYPE_TOKEN:
  428. case PhabricatorTransactions::TYPE_INLINESTATE:
  429. case PhabricatorTransactions::TYPE_SUBTYPE:
  430. case PhabricatorTransactions::TYPE_HISTORY:
  431. return $xaction->getNewValue();
  432. case PhabricatorTransactions::TYPE_MFA:
  433. return true;
  434. case PhabricatorTransactions::TYPE_SPACE:
  435. $space_phid = $xaction->getNewValue();
  436. if (!strlen($space_phid)) {
  437. // If an install has no Spaces or the Spaces controls are not visible
  438. // to the viewer, we might end up with the empty string here instead
  439. // of a strict `null`, because some controller just used `getStr()`
  440. // to read the space PHID from the request.
  441. // Just make this work like callers might reasonably expect so we
  442. // don't need to handle this specially in every EditController.
  443. return $this->getActor()->getDefaultSpacePHID();
  444. } else {
  445. return $space_phid;
  446. }
  447. case PhabricatorTransactions::TYPE_EDGE:
  448. // See T13082. If this is an inverse edit, the parent editor has
  449. // already populated appropriate transaction values.
  450. if ($this->getIsInverseEdgeEditor()) {
  451. return $xaction->getNewValue();
  452. }
  453. $new_value = $this->getEdgeTransactionNewValue($xaction);
  454. $edge_type = $xaction->getMetadataValue('edge:type');
  455. $type_project = PhabricatorProjectObjectHasProjectEdgeType::EDGECONST;
  456. if ($edge_type == $type_project) {
  457. $new_value = $this->applyProjectConflictRules($new_value);
  458. }
  459. return $new_value;
  460. case PhabricatorTransactions::TYPE_CUSTOMFIELD:
  461. $field = $this->getCustomFieldForTransaction($object, $xaction);
  462. return $field->getNewValueFromApplicationTransactions($xaction);
  463. case PhabricatorTransactions::TYPE_COMMENT:
  464. return null;
  465. default:
  466. return $this->getCustomTransactionNewValue($object, $xaction);
  467. }
  468. }
  469. protected function getCustomTransactionOldValue(
  470. PhabricatorLiskDAO $object,
  471. PhabricatorApplicationTransaction $xaction) {
  472. throw new Exception(pht('Capability not supported!'));
  473. }
  474. protected function getCustomTransactionNewValue(
  475. PhabricatorLiskDAO $object,
  476. PhabricatorApplicationTransaction $xaction) {
  477. throw new Exception(pht('Capability not supported!'));
  478. }
  479. protected function transactionHasEffect(
  480. PhabricatorLiskDAO $object,
  481. PhabricatorApplicationTransaction $xaction) {
  482. switch ($xaction->getTransactionType()) {
  483. case PhabricatorTransactions::TYPE_CREATE:
  484. case PhabricatorTransactions::TYPE_HISTORY:
  485. return true;
  486. case PhabricatorTransactions::TYPE_CUSTOMFIELD:
  487. $field = $this->getCustomFieldForTransaction($object, $xaction);
  488. return $field->getApplicationTransactionHasEffect($xaction);
  489. case PhabricatorTransactions::TYPE_EDGE:
  490. // A straight value comparison here doesn't always get the right
  491. // result, because newly added edges aren't fully populated. Instead,
  492. // compare the changes in a more granular way.
  493. $old = $xaction->getOldValue();
  494. $new = $xaction->getNewValue();
  495. $old_dst = array_keys($old);
  496. $new_dst = array_keys($new);
  497. // NOTE: For now, we don't consider edge reordering to be a change.
  498. // We have very few order-dependent edges and effectively no order
  499. // oriented UI. This might change in the future.
  500. sort($old_dst);
  501. sort($new_dst);
  502. if ($old_dst !== $new_dst) {
  503. // We've added or removed edges, so this transaction definitely
  504. // has an effect.
  505. return true;
  506. }
  507. // We haven't added or removed edges, but we might have changed
  508. // edge data.
  509. foreach ($old as $key => $old_value) {
  510. $new_value = $new[$key];
  511. if ($old_value['data'] !== $new_value['data']) {
  512. return true;
  513. }
  514. }
  515. return false;
  516. }
  517. $type = $xaction->getTransactionType();
  518. $xtype = $this->getModularTransactionType($type);
  519. if ($xtype) {
  520. return $xtype->getTransactionHasEffect(
  521. $object,
  522. $xaction->getOldValue(),
  523. $xaction->getNewValue());
  524. }
  525. if ($xaction->hasComment()) {
  526. return true;
  527. }
  528. return ($xaction->getOldValue() !== $xaction->getNewValue());
  529. }
  530. protected function shouldApplyInitialEffects(
  531. PhabricatorLiskDAO $object,
  532. array $xactions) {
  533. return false;
  534. }
  535. protected function applyInitialEffects(
  536. PhabricatorLiskDAO $object,
  537. array $xactions) {
  538. throw new PhutilMethodNotImplementedException();
  539. }
  540. private function applyInternalEffects(
  541. PhabricatorLiskDAO $object,
  542. PhabricatorApplicationTransaction $xaction) {
  543. $type = $xaction->getTransactionType();
  544. $xtype = $this->getModularTransactionType($type);
  545. if ($xtype) {
  546. $xtype = clone $xtype;
  547. $xtype->setStorage($xaction);
  548. return $xtype->applyInternalEffects($object, $xaction->getNewValue());
  549. }
  550. switch ($type) {
  551. case PhabricatorTransactions::TYPE_CUSTOMFIELD:
  552. $field = $this->getCustomFieldForTransaction($object, $xaction);
  553. return $field->applyApplicationTransactionInternalEffects($xaction);
  554. case PhabricatorTransactions::TYPE_CREATE:
  555. case PhabricatorTransactions::TYPE_HISTORY:
  556. case PhabricatorTransactions::TYPE_SUBTYPE:
  557. case PhabricatorTransactions::TYPE_MFA:
  558. case PhabricatorTransactions::TYPE_TOKEN:
  559. case PhabricatorTransactions::TYPE_VIEW_POLICY:
  560. case PhabricatorTransactions::TYPE_EDIT_POLICY:
  561. case PhabricatorTransactions::TYPE_JOIN_POLICY:
  562. case PhabricatorTransactions::TYPE_SUBSCRIBERS:
  563. case PhabricatorTransactions::TYPE_INLINESTATE:
  564. case PhabricatorTransactions::TYPE_EDGE:
  565. case PhabricatorTransactions::TYPE_SPACE:
  566. case PhabricatorTransactions::TYPE_COMMENT:
  567. return $this->applyBuiltinInternalTransaction($object, $xaction);
  568. }
  569. return $this->applyCustomInternalTransaction($object, $xaction);
  570. }
  571. private function applyExternalEffects(
  572. PhabricatorLiskDAO $object,
  573. PhabricatorApplicationTransaction $xaction) {
  574. $type = $xaction->getTransactionType();
  575. $xtype = $this->getModularTransactionType($type);
  576. if ($xtype) {
  577. $xtype = clone $xtype;
  578. $xtype->setStorage($xaction);
  579. return $xtype->applyExternalEffects($object, $xaction->getNewValue());
  580. }
  581. switch ($type) {
  582. case PhabricatorTransactions::TYPE_SUBSCRIBERS:
  583. $subeditor = id(new PhabricatorSubscriptionsEditor())
  584. ->setObject($object)
  585. ->setActor($this->requireActor());
  586. $old_map = array_fuse($xaction->getOldValue());
  587. $new_map = array_fuse($xaction->getNewValue());
  588. $subeditor->unsubscribe(
  589. array_keys(
  590. array_diff_key($old_map, $new_map)));
  591. $subeditor->subscribeExplicit(
  592. array_keys(
  593. array_diff_key($new_map, $old_map)));
  594. $subeditor->save();
  595. // for the rest of these edits, subscribers should include those just
  596. // added as well as those just removed.
  597. $subscribers = array_unique(array_merge(
  598. $this->subscribers,
  599. $xaction->getOldValue(),
  600. $xaction->getNewValue()));
  601. $this->subscribers = $subscribers;
  602. return $this->applyBuiltinExternalTransaction($object, $xaction);
  603. case PhabricatorTransactions::TYPE_CUSTOMFIELD:
  604. $field = $this->getCustomFieldForTransaction($object, $xaction);
  605. return $field->applyApplicationTransactionExternalEffects($xaction);
  606. case PhabricatorTransactions::TYPE_CREATE:
  607. case PhabricatorTransactions::TYPE_HISTORY:
  608. case PhabricatorTransactions::TYPE_SUBTYPE:
  609. case PhabricatorTransactions::TYPE_MFA:
  610. case PhabricatorTransactions::TYPE_EDGE:
  611. case PhabricatorTransactions::TYPE_TOKEN:
  612. case PhabricatorTransactions::TYPE_VIEW_POLICY:
  613. case PhabricatorTransactions::TYPE_EDIT_POLICY:
  614. case PhabricatorTransactions::TYPE_JOIN_POLICY:
  615. case PhabricatorTransactions::TYPE_INLINESTATE:
  616. case PhabricatorTransactions::TYPE_SPACE:
  617. case PhabricatorTransactions::TYPE_COMMENT:
  618. return $this->applyBuiltinExternalTransaction($object, $xaction);
  619. }
  620. return $this->applyCustomExternalTransaction($object, $xaction);
  621. }
  622. protected function applyCustomInternalTransaction(
  623. PhabricatorLiskDAO $object,
  624. PhabricatorApplicationTransaction $xaction) {
  625. $type = $xaction->getTransactionType();
  626. throw new Exception(
  627. pht(
  628. "Transaction type '%s' is missing an internal apply implementation!",
  629. $type));
  630. }
  631. protected function applyCustomExternalTransaction(
  632. PhabricatorLiskDAO $object,
  633. PhabricatorApplicationTransaction $xaction) {
  634. $type = $xaction->getTransactionType();
  635. throw new Exception(
  636. pht(
  637. "Transaction type '%s' is missing an external apply implementation!",
  638. $type));
  639. }
  640. /**
  641. * @{class:PhabricatorTransactions} provides many built-in transactions
  642. * which should not require much - if any - code in specific applications.
  643. *
  644. * This method is a hook for the exceedingly-rare cases where you may need
  645. * to do **additional** work for built-in transactions. Developers should
  646. * extend this method, making sure to return the parent implementation
  647. * regardless of handling any transactions.
  648. *
  649. * See also @{method:applyBuiltinExternalTransaction}.
  650. */
  651. protected function applyBuiltinInternalTransaction(
  652. PhabricatorLiskDAO $object,
  653. PhabricatorApplicationTransaction $xaction) {
  654. switch ($xaction->getTransactionType()) {
  655. case PhabricatorTransactions::TYPE_VIEW_POLICY:
  656. $object->setViewPolicy($xaction->getNewValue());
  657. break;
  658. case PhabricatorTransactions::TYPE_EDIT_POLICY:
  659. $object->setEditPolicy($xaction->getNewValue());
  660. break;
  661. case PhabricatorTransactions::TYPE_JOIN_POLICY:
  662. $object->setJoinPolicy($xaction->getNewValue());
  663. break;
  664. case PhabricatorTransactions::TYPE_SPACE:
  665. $object->setSpacePHID($xaction->getNewValue());
  666. break;
  667. case PhabricatorTransactions::TYPE_SUBTYPE:
  668. $object->setEditEngineSubtype($xaction->getNewValue());
  669. break;
  670. }
  671. }
  672. /**
  673. * See @{method::applyBuiltinInternalTransaction}.
  674. */
  675. protected function applyBuiltinExternalTransaction(
  676. PhabricatorLiskDAO $object,
  677. PhabricatorApplicationTransaction $xaction) {
  678. switch ($xaction->getTransactionType()) {
  679. case PhabricatorTransactions::TYPE_EDGE:
  680. if ($this->getIsInverseEdgeEditor()) {
  681. // If we're writing an inverse edge transaction, don't actually
  682. // do anything. The initiating editor on the other side of the
  683. // transaction will take care of the edge writes.
  684. break;
  685. }
  686. $old = $xaction->getOldValue();
  687. $new = $xaction->getNewValue();
  688. $src = $object->getPHID();
  689. $const = $xaction->getMetadataValue('edge:type');
  690. foreach ($new as $dst_phid => $edge) {
  691. $new[$dst_phid]['src'] = $src;
  692. }
  693. $editor = new PhabricatorEdgeEditor();
  694. foreach ($old as $dst_phid => $edge) {
  695. if (!empty($new[$dst_phid])) {
  696. if ($old[$dst_phid]['data'] === $new[$dst_phid]['data']) {
  697. continue;
  698. }
  699. }
  700. $editor->removeEdge($src, $const, $dst_phid);
  701. }
  702. foreach ($new as $dst_phid => $edge) {
  703. if (!empty($old[$dst_phid])) {
  704. if ($old[$dst_phid]['data'] === $new[$dst_phid]['data']) {
  705. continue;
  706. }
  707. }
  708. $data = array(
  709. 'data' => $edge['data'],
  710. );
  711. $editor->addEdge($src, $const, $dst_phid, $data);
  712. }
  713. $editor->save();
  714. $this->updateWorkboardColumns($object, $const, $old, $new);
  715. break;
  716. case PhabricatorTransactions::TYPE_VIEW_POLICY:
  717. case PhabricatorTransactions::TYPE_SPACE:
  718. $this->scrambleFileSecrets($object);
  719. break;
  720. case PhabricatorTransactions::TYPE_HISTORY:
  721. $this->sendHistory = true;
  722. break;
  723. }
  724. }
  725. /**
  726. * Fill in a transaction's common values, like author and content source.
  727. */
  728. protected function populateTransaction(
  729. PhabricatorLiskDAO $object,
  730. PhabricatorApplicationTransaction $xaction) {
  731. $actor = $this->getActor();
  732. // TODO: This needs to be more sophisticated once we have meta-policies.
  733. $xaction->setViewPolicy(PhabricatorPolicies::POLICY_PUBLIC);
  734. if ($actor->isOmnipotent()) {
  735. $xaction->setEditPolicy(PhabricatorPolicies::POLICY_NOONE);
  736. } else {
  737. $xaction->setEditPolicy($this->getActingAsPHID());
  738. }
  739. // If the transaction already has an explicit author PHID, allow it to
  740. // stand. This is used by applications like Owners that hook into the
  741. // post-apply change pipeline.
  742. if (!$xaction->getAuthorPHID()) {
  743. $xaction->setAuthorPHID($this->getActingAsPHID());
  744. }
  745. $xaction->setContentSource($this->getContentSource());
  746. $xaction->attachViewer($actor);
  747. $xaction->attachObject($object);
  748. if ($object->getPHID()) {
  749. $xaction->setObjectPHID($object->getPHID());
  750. }
  751. if ($this->getIsSilent()) {
  752. $xaction->setIsSilentTransaction(true);
  753. }
  754. return $xaction;
  755. }
  756. protected function didApplyInternalEffects(
  757. PhabricatorLiskDAO $object,
  758. array $xactions) {
  759. return $xactions;
  760. }
  761. protected function applyFinalEffects(
  762. PhabricatorLiskDAO $object,
  763. array $xactions) {
  764. return $xactions;
  765. }
  766. final protected function didCommitTransactions(
  767. PhabricatorLiskDAO $object,
  768. array $xactions) {
  769. foreach ($xactions as $xaction) {
  770. $type = $xaction->getTransactionType();
  771. // See T13082. When we're writing edges that imply corresponding inverse
  772. // transactions, apply those inverse transactions now. We have to wait
  773. // until the object we're editing (with this editor) has committed its
  774. // transactions to do this. If we don't, the inverse editor may race,
  775. // build a mail before we actually commit this object, and render "alice
  776. // added an edge: Unknown Object".
  777. if ($type === PhabricatorTransactions::TYPE_EDGE) {
  778. // Don't do anything if we're already an inverse edge editor.
  779. if ($this->getIsInverseEdgeEditor()) {
  780. continue;
  781. }
  782. $edge_const = $xaction->getMetadataValue('edge:type');
  783. $edge_type = PhabricatorEdgeType::getByConstant($edge_const);
  784. if ($edge_type->shouldWriteInverseTransactions()) {
  785. $this->applyInverseEdgeTransactions(
  786. $object,
  787. $xaction,
  788. $edge_type->getInverseEdgeConstant());
  789. }
  790. continue;
  791. }
  792. $xtype = $this->getModularTransactionType($type);
  793. if (!$xtype) {
  794. continue;
  795. }
  796. $xtype = clone $xtype;
  797. $xtype->setStorage($xaction);
  798. $xtype->didCommitTransaction($object, $xaction->getNewValue());
  799. }
  800. }
  801. public function setContentSource(PhabricatorContentSource $content_source) {
  802. $this->contentSource = $content_source;
  803. return $this;
  804. }
  805. public function setContentSourceFromRequest(AphrontRequest $request) {
  806. $this->setRequest($request);
  807. return $this->setContentSource(
  808. PhabricatorContentSource::newFromRequest($request));
  809. }
  810. public function getContentSource() {
  811. return $this->contentSource;
  812. }
  813. public function setRequest(AphrontRequest $request) {
  814. $this->request = $request;
  815. return $this;
  816. }
  817. public function getRequest() {
  818. return $this->request;
  819. }
  820. public function setCancelURI($cancel_uri) {
  821. $this->cancelURI = $cancel_uri;
  822. return $this;
  823. }
  824. public function getCancelURI() {
  825. return $this->cancelURI;
  826. }
  827. protected function getTransactionGroupID() {
  828. if ($this->transactionGroupID === null) {
  829. $this->transactionGroupID = Filesystem::readRandomCharacters(32);
  830. }
  831. return $this->transactionGroupID;
  832. }
  833. final public function applyTransactions(
  834. PhabricatorLiskDAO $object,
  835. array $xactions) {
  836. $is_new = ($object->getID() === null);
  837. $this->isNewObject = $is_new;
  838. $is_preview = $this->getIsPreview();
  839. $read_locking = false;
  840. $transaction_open = false;
  841. // If we're attempting to apply transactions, lock and reload the object
  842. // before we go anywhere. If we don't do this at the very beginning, we
  843. // may be looking at an older version of the object when we populate and
  844. // filter the transactions. See PHI1165 for an example.
  845. if (!$is_preview) {
  846. if (!$is_new) {
  847. $this->buildOldRecipientLists($object, $xactions);
  848. $object->openTransaction();
  849. $transaction_open = true;
  850. $object->beginReadLocking();
  851. $read_locking = true;
  852. $object->reload();
  853. }
  854. }
  855. try {
  856. $this->object = $object;
  857. $this->xactions = $xactions;
  858. $this->validateEditParameters($object, $xactions);
  859. $xactions = $this->newMFATransactions($object, $xactions);
  860. $actor = $this->requireActor();
  861. // NOTE: Some transaction expansion requires that the edited object be
  862. // attached.
  863. foreach ($xactions as $xaction) {
  864. $xaction->attachObject($object);
  865. $xaction->attachViewer($actor);
  866. }
  867. $xactions = $this->expandTransactions($object, $xactions);
  868. $xactions = $this->expandSupportTransactions($object, $xactions);
  869. $xactions = $this->combineTransactions($xactions);
  870. foreach ($xactions as $xaction) {
  871. $xaction = $this->populateTransaction($object, $xaction);
  872. }
  873. if (!$is_preview) {
  874. $errors = array();
  875. $type_map = mgroup($xactions, 'getTransactionType');
  876. foreach ($this->getTransactionTypes() as $type) {
  877. $type_xactions = idx($type_map, $type, array());
  878. $errors[] = $this->validateTransaction(
  879. $object,
  880. $type,
  881. $type_xactions);
  882. }
  883. $errors[] = $this->validateAllTransactions($object, $xactions);
  884. $errors[] = $this->validateTransactionsWithExtensions(
  885. $object,
  886. $xactions);
  887. $errors = array_mergev($errors);
  888. $continue_on_missing = $this->getContinueOnMissingFields();
  889. foreach ($errors as $key => $error) {
  890. if ($continue_on_missing && $error->getIsMissingFieldError()) {
  891. unset($errors[$key]);
  892. }
  893. }
  894. if ($errors) {
  895. throw new PhabricatorApplicationTransactionValidationException(
  896. $errors);
  897. }
  898. if ($this->raiseWarnings) {
  899. $warnings = array();
  900. foreach ($xactions as $xaction) {
  901. if ($this->hasWarnings($object, $xaction)) {
  902. $warnings[] = $xaction;
  903. }
  904. }
  905. if ($warnings) {
  906. throw new PhabricatorApplicationTransactionWarningException(
  907. $warnings);
  908. }
  909. }
  910. }
  911. foreach ($xactions as $xaction) {
  912. $this->adjustTransactionValues($object, $xaction);
  913. }
  914. // Now that we've merged and combined transactions, check for required
  915. // capabilities. Note that we're doing this before filtering
  916. // transactions: if you try to apply an edit which you do not have
  917. // permission to apply, we want to give you a permissions error even
  918. // if the edit would have no effect.
  919. $this->applyCapabilityChecks($object, $xactions);
  920. $xactions = $this->filterTransactions($object, $xactions);
  921. if (!$is_preview) {
  922. $this->hasRequiredMFA = true;
  923. if ($this->getShouldRequireMFA()) {
  924. $this->requireMFA($object, $xactions);
  925. }
  926. if ($this->shouldApplyInitialEffects($object, $xactions)) {
  927. if (!$transaction_open) {
  928. $object->openTransaction();
  929. $transaction_open = true;
  930. }
  931. }
  932. }
  933. if ($this->shouldApplyInitialEffects($object, $xactions)) {
  934. $this->applyInitialEffects($object, $xactions);
  935. }
  936. // TODO: Once everything is on EditEngine, just use getIsNewObject() to
  937. // figure this out instead.
  938. $mark_as_create = false;
  939. $create_type = PhabricatorTransactions::TYPE_CREATE;
  940. foreach ($xactions as $xaction) {
  941. if ($xaction->getTransactionType() == $create_type) {
  942. $mark_as_create = true;
  943. }
  944. }
  945. if ($mark_as_create) {
  946. foreach ($xactions as $xaction) {
  947. $xaction->setIsCreateTransaction(true);
  948. }
  949. }
  950. $xactions = $this->sortTransactions($xactions);
  951. $file_phids = $this->extractFilePHIDs($object, $xactions);
  952. if ($is_preview) {
  953. $this->loadHandles($xactions);
  954. return $xactions;
  955. }
  956. $comment_editor = id(new PhabricatorApplicationTransactionCommentEditor())
  957. ->setActor($actor)
  958. ->setActingAsPHID($this->getActingAsPHID())
  959. ->setContentSource($this->getContentSource())
  960. ->setIsNewComment(true);
  961. if (!$transaction_open) {
  962. $object->openTransaction();
  963. $transaction_open = true;
  964. }
  965. // We can technically test any object for CAN_INTERACT, but we can
  966. // run into some issues in doing so (for example, in project unit tests).
  967. // For now, only test for CAN_INTERACT if the object is explicitly a
  968. // lockable object.
  969. $was_locked = false;
  970. if ($object instanceof PhabricatorEditEngineLockableInterface) {
  971. $was_locked = !PhabricatorPolicyFilter::canInteract($actor, $object);
  972. }
  973. foreach ($xactions as $xaction) {
  974. $this->applyInternalEffects($object, $xaction);
  975. }
  976. $xactions = $this->didApplyInternalEffects($object, $xactions);
  977. try {
  978. $object->save();
  979. } catch (AphrontDuplicateKeyQueryException $ex) {
  980. // This callback has an opportunity to throw a better exception,
  981. // so execution may end here.
  982. $this->didCatchDuplicateKeyException($object, $xactions, $ex);
  983. throw $ex;
  984. }
  985. $group_id = $this->getTransactionGroupID();
  986. foreach ($xactions as $xaction) {
  987. if ($was_locked) {
  988. $is_override = $this->isLockOverrideTransaction($xaction);
  989. if ($is_override) {
  990. $xaction->setIsLockOverrideTransaction(true);
  991. }
  992. }
  993. $xaction->setObjectPHID($object->getPHID());
  994. $xaction->setTransactionGroupID($group_id);
  995. if ($xaction->getComment()) {
  996. $xaction->setPHID($xaction->generatePHID());
  997. $comment_editor->applyEdit($xaction, $xaction->getComment());
  998. } else {
  999. // TODO: This is a transitional hack to let us migrate edge
  1000. // transactions to a more efficient storage format. For now, we're
  1001. // going to write a new slim format to the database but keep the old
  1002. // bulky format on the objects so we don't have to upgrade all the
  1003. // edit logic to the new format yet. See T13051.
  1004. $edge_type = PhabricatorTransactions::TYPE_EDGE;
  1005. if ($xaction->getTransactionType() == $edge_type) {
  1006. $bulky_old = $xaction->getOldValue();
  1007. $bulky_new = $xaction->getNewValue();
  1008. $record = PhabricatorEdgeChangeRecord::newFromTransaction($xaction);
  1009. $slim_old = $record->getModernOldEdgeTransactionData();
  1010. $slim_new = $record->getModernNewEdgeTransactionData();
  1011. $xaction->setOldValue($slim_old);
  1012. $xaction->setNewValue($slim_new);
  1013. $xaction->save();
  1014. $xaction->setOldValue($bulky_old);
  1015. $xaction->setNewValue($bulky_new);
  1016. } else {
  1017. $xaction->save();
  1018. }
  1019. }
  1020. }
  1021. if ($file_phids) {
  1022. $this->attachFiles($object, $file_phids);
  1023. }
  1024. foreach ($xactions as $xaction) {
  1025. $this->applyExternalEffects($object, $xaction);
  1026. }
  1027. $xactions = $this->applyFinalEffects($object, $xactions);
  1028. if ($read_locking) {
  1029. $object->endReadLocking();
  1030. $read_locking = false;
  1031. }
  1032. if ($transaction_open) {
  1033. $object->saveTransaction();
  1034. $transaction_open = false;
  1035. }
  1036. $this->didCommitTransactions($object, $xactions);
  1037. } catch (Exception $ex) {
  1038. if ($read_locking) {
  1039. $object->endReadLocking();
  1040. $read_locking = false;
  1041. }
  1042. if ($transaction_open) {
  1043. $object->killTransaction();
  1044. $transaction_open = false;
  1045. }
  1046. throw $ex;
  1047. }
  1048. // If we need to perform cache engine updates, execute them now.
  1049. id(new PhabricatorCacheEngine())
  1050. ->updateObject($object);
  1051. // Now that we've completely applied the core transaction set, try to apply
  1052. // Herald rules. Herald rules are allowed to either take direct actions on
  1053. // the database (like writing flags), or take indirect actions (like saving
  1054. // some targets for CC when we generate mail a little later), or return
  1055. // transactions which we'll apply normally using another Editor.
  1056. // First, check if *this* is a sub-editor which is itself applying Herald
  1057. // rules: if it is, stop working and return so we don't descend into
  1058. // madness.
  1059. // Otherwise, we're not a Herald editor, so process Herald rules (possibly
  1060. // using a Herald editor to apply resulting transactions) and then send out
  1061. // mail, notifications, and feed updates about everything.
  1062. if ($this->getIsHeraldEditor()) {
  1063. // We are the Herald editor, so stop work here and return the updated
  1064. // transactions.
  1065. return $xactions;
  1066. } else if ($this->getIsInverseEdgeEditor()) {
  1067. // Do not run Herald if we're just recording that this object was
  1068. // mentioned elsewhere. This tends to create Herald side effects which
  1069. // feel arbitrary, and can really slow down edits which mention a large
  1070. // number of other objects. See T13114.
  1071. } else if ($this->shouldApplyHeraldRules($object, $xactions)) {
  1072. // We are not the Herald editor, so try to apply Herald rules.
  1073. $herald_xactions = $this->applyHeraldRules($object, $xactions);
  1074. if ($herald_xactions) {
  1075. $xscript_id = $this->getHeraldTranscript()->getID();
  1076. foreach ($herald_xactions as $herald_xaction) {
  1077. // Don't set a transcript ID if this is a transaction from another
  1078. // application or source, like Owners.
  1079. if ($herald_xaction->getAuthorPHID()) {
  1080. continue;
  1081. }
  1082. $herald_xaction->setMetadataValue('herald:transcriptID', $xscript_id);
  1083. }
  1084. // NOTE: We're acting as the omnipotent user because rules deal with
  1085. // their own policy issues. We use a synthetic author PHID (the
  1086. // Herald application) as the author of record, so that transactions
  1087. // will render in a reasonable way ("Herald assigned this task ...").
  1088. $herald_actor = PhabricatorUser::getOmnipotentUser();
  1089. $herald_phid = id(new PhabricatorHeraldApplication())->getPHID();
  1090. // TODO: It would be nice to give transactions a more specific source
  1091. // which points at the rule which generated them. You can figure this
  1092. // out from transcripts, but it would be cleaner if you didn't have to.
  1093. $herald_source = PhabricatorContentSource::newForSource(
  1094. PhabricatorHeraldContentSource::SOURCECONST);
  1095. $herald_editor = $this->newEditorCopy()
  1096. ->setContinueOnNoEffect(true)
  1097. ->setContinueOnMissingFields(true)
  1098. ->setIsHeraldEditor(true)
  1099. ->setActor($herald_actor)
  1100. ->setActingAsPHID($herald_phid)
  1101. ->setContentSource($herald_source);
  1102. $herald_xactions = $herald_editor->applyTransactions(
  1103. $object,
  1104. $herald_xactions);
  1105. // Merge the new transactions into the transaction list: we want to
  1106. // send email and publish feed stories about them, too.
  1107. $xactions = array_merge($xactions, $herald_xactions);
  1108. }
  1109. // If Herald did not generate transactions, we may still need to handle
  1110. // "Send an Email" rules.
  1111. $adapter = $this->getHeraldAdapter();
  1112. $this->heraldEmailPHIDs = $adapter->getEmailPHIDs();
  1113. $this->heraldForcedEmailPHIDs = $adapter->getForcedEmailPHIDs();
  1114. $this->webhookMap = $adapter->getWebhookMap();
  1115. }
  1116. $xactions = $this->didApplyTransactions($object, $xactions);
  1117. if ($object instanceof PhabricatorCustomFieldInterface) {
  1118. // Maybe this makes more sense to move into the search index itself? For
  1119. // now I'm putting it here since I think we might end up with things that
  1120. // need it to be up to date once the next page loads, but if we don't go
  1121. // there we could move it into search once search moves to the daemons.
  1122. // It now happens in the search indexer as well, but the search indexer is
  1123. // always daemonized, so the logic above still potentially holds. We could
  1124. // possibly get rid of this. The major motivation for putting it in the
  1125. // indexer was to enable reindexing to work.
  1126. $fields = PhabricatorCustomField::getObjectFields(
  1127. $object,
  1128. PhabricatorCustomField::ROLE_APPLICATIONSEARCH);
  1129. $fields->readFieldsFromStorage($object);
  1130. $fields->rebuildIndexes($object);
  1131. }
  1132. $herald_xscript = $this->getHeraldTranscript();
  1133. if ($herald_xscript) {
  1134. $herald_header = $herald_xscript->getXHeraldRulesHeader();
  1135. $herald_header = HeraldTranscript::saveXHeraldRulesHeader(
  1136. $object->getPHID(),
  1137. $herald_header);
  1138. } else {
  1139. $herald_header = HeraldTranscript::loadXHeraldRulesHeader(
  1140. $object->getPHID());
  1141. }
  1142. $this->heraldHeader = $herald_header;
  1143. // See PHI1134. If we're a subeditor, we don't publish information about
  1144. // the edit yet. Our parent editor still needs to finish applying
  1145. // transactions and execute Herald, which may change the information we
  1146. // publish.
  1147. // For example, Herald actions may change the parent object's title or
  1148. // visibility, or Herald may apply rules like "Must Encrypt" that affect
  1149. // email.
  1150. // Once the parent finishes work, it will queue its own publish step and
  1151. // then queue publish steps for its children.
  1152. $this->publishableObject = $object;
  1153. $this->publishableTransactions = $xactions;
  1154. if (!$this->parentEditor) {
  1155. $this->queuePublishing();
  1156. }
  1157. return $xactions;
  1158. }
  1159. final private function queuePublishing() {
  1160. $object = $this->publishableObject;
  1161. $xactions = $this->publishableTransactions;
  1162. if (!$object) {
  1163. throw new Exception(
  1164. pht(
  1165. 'Editor method "queuePublishing()" was called, but no publishable '.
  1166. 'object is present. This Editor is not ready to publish.'));
  1167. }
  1168. // We're going to compute some of the data we'll use to publish these
  1169. // transactions here, before queueing a worker.
  1170. //
  1171. // Primarily, this is more correct: we want to publish the object as it
  1172. // exists right now. The worker may not execute for some time, and we want
  1173. // to use the current To/CC list, not respect any changes which may occur
  1174. // between now and when the worker executes.
  1175. //
  1176. // As a secondary benefit, this tends to reduce the amount of state that
  1177. // Editors need to pass into workers.
  1178. $object = $this->willPublish($object, $xactions);
  1179. if (!$this->getIsSilent()) {
  1180. if ($this->shouldSendMail($object, $xactions)) {
  1181. $this->mailShouldSend = true;
  1182. $this->mailToPHIDs = $this->getMailTo($object);
  1183. $this->mailCCPHIDs = $this->getMailCC($object);
  1184. $this->mailUnexpandablePHIDs = $this->newMailUnexpandablePHIDs($object);
  1185. // Add any recipients who were previously on the notification list
  1186. // but were removed by this change.
  1187. $this->applyOldRecipientLists();
  1188. if ($object instanceof PhabricatorSubscribableInterface) {
  1189. $this->mailMutedPHIDs = PhabricatorEdgeQuery::loadDestinationPHIDs(
  1190. $object->getPHID(),
  1191. PhabricatorMutedByEdgeType::EDGECONST);
  1192. } else {
  1193. $this->mailMutedPHIDs = array();
  1194. }
  1195. $mail_xactions = $this->getTransactionsForMail($object, $xactions);
  1196. $stamps = $this->newMailStamps($object, $xactions);
  1197. foreach ($stamps as $stamp) {
  1198. $this->mailStamps[] = $stamp->toDictionary();
  1199. }
  1200. }
  1201. if ($this->shouldPublishFeedStory($object, $xactions)) {
  1202. $this->feedShouldPublish = true;
  1203. $this->feedRelatedPHIDs = $this->getFeedRelatedPHIDs(
  1204. $object,
  1205. $xactions);
  1206. $this->feedNotifyPHIDs = $this->getFeedNotifyPHIDs(
  1207. $object,
  1208. $xactions);
  1209. }
  1210. }
  1211. PhabricatorWorker::scheduleTask(
  1212. 'PhabricatorApplicationTransactionPublishWorker',
  1213. array(
  1214. 'objectPHID' => $object->getPHID(),
  1215. 'actorPHID' => $this->getActingAsPHID(),
  1216. 'xactionPHIDs' => mpull($xactions, 'getPHID'),
  1217. 'state' => $this->getWorkerState(),
  1218. ),
  1219. array(
  1220. 'objectPHID' => $object->getPHID(),
  1221. 'priority' => PhabricatorWorker::PRIORITY_ALERTS,
  1222. ));
  1223. foreach ($this->subEditors as $sub_editor) {
  1224. $sub_editor->queuePublishing();
  1225. }
  1226. $this->flushTransactionQueue($object);
  1227. }
  1228. protected function didCatchDuplicateKeyException(
  1229. PhabricatorLiskDAO $object,
  1230. array $xactions,
  1231. Exception $ex) {
  1232. return;
  1233. }
  1234. public function publishTransactions(
  1235. PhabricatorLiskDAO $object,
  1236. array $xactions) {
  1237. $this->object = $object;
  1238. $this->xactions = $xactions;
  1239. // Hook for edges or other properties that may need (re-)loading
  1240. $object = $this->willPublish($object, $xactions);
  1241. // The object might have changed, so reassign it.
  1242. $this->object = $object;
  1243. $messages = array();
  1244. if ($this->mailShouldSend) {
  1245. $messages = $this->buildMail($object, $xactions);
  1246. }
  1247. if ($this->supportsSearch()) {
  1248. PhabricatorSearchWorker::queueDocumentForIndexing(
  1249. $object->getPHID(),
  1250. array(
  1251. 'transactionPHIDs' => mpull($xactions, 'getPHID'),
  1252. ));
  1253. }
  1254. if ($this->feedShouldPublish) {
  1255. $mailed = array();
  1256. foreach ($messages as $mail) {
  1257. foreach ($mail->buildRecipientList() as $phid) {
  1258. $mailed[$phid] = $phid;
  1259. }
  1260. }
  1261. $this->publishFeedStory($object, $xactions, $mailed);
  1262. }
  1263. if ($this->sendHistory) {
  1264. $history_mail = $this->buildHistoryMail($object);
  1265. if ($history_mail) {
  1266. $messages[] = $history_mail;
  1267. }
  1268. }
  1269. foreach ($this->newAuxiliaryMail($object, $xactions) as $message) {
  1270. $messages[] = $message;
  1271. }
  1272. // NOTE: This actually sends the mail. We do this last to reduce the chance
  1273. // that we send some mail, hit an exception, then send the mail again when
  1274. // retrying.
  1275. foreach ($messages as $mail) {
  1276. $mail->save();
  1277. }
  1278. $this->queueWebhooks($object, $xactions);
  1279. return $xactions;
  1280. }
  1281. protected function didApplyTransactions($object, array $xactions) {
  1282. // Hook for subclasses.
  1283. return $xactions;
  1284. }
  1285. private function loadHandles(array $xactions) {
  1286. $phids = array();
  1287. foreach ($xactions as $key => $xaction) {
  1288. $phids[$key] = $xaction->getRequiredHandlePHIDs();
  1289. }
  1290. $handles = array();
  1291. $merged = array_mergev($phids);
  1292. if ($merged) {
  1293. $handles = id(new PhabricatorHandleQuery())
  1294. ->setViewer($this->requireActor())
  1295. ->withPHIDs($merged)
  1296. ->execute();
  1297. }
  1298. foreach ($xactions as $key => $xaction) {
  1299. $xaction->setHandles(array_select_keys($handles, $phids[$key]));
  1300. }
  1301. }
  1302. private function loadSubscribers(PhabricatorLiskDAO $object) {
  1303. if ($object->getPHID() &&
  1304. ($object instanceof PhabricatorSubscribableInterface)) {
  1305. $subs = PhabricatorSubscribersQuery::loadSubscribersForPHID(
  1306. $object->getPHID());
  1307. $this->subscribers = array_fuse($subs);
  1308. } else {
  1309. $this->subscribers = array();
  1310. }
  1311. }
  1312. private function validateEditParameters(
  1313. PhabricatorLiskDAO $object,
  1314. array $xactions) {
  1315. if (!$this->getContentSource()) {
  1316. throw new PhutilInvalidStateException('setContentSource');
  1317. }
  1318. // Do a bunch of sanity checks that the incoming transactions are fresh.
  1319. // They should be unsaved and have only "transactionType" and "newValue"
  1320. // set.
  1321. $types = array_fill_keys($this->getTransactionTypes(), true);
  1322. assert_instances_of($xactions, 'PhabricatorApplicationTransaction');
  1323. foreach ($xactions as $xaction) {
  1324. if ($xaction->getPHID() || $xaction->getID()) {
  1325. throw new PhabricatorApplicationTransactionStructureException(
  1326. $xaction,
  1327. pht('You can not apply transactions which already have IDs/PHIDs!'));
  1328. }
  1329. if ($xaction->getObjectPHID()) {
  1330. throw new PhabricatorApplicationTransactionStructureException(
  1331. $xaction,
  1332. pht(
  1333. 'You can not apply transactions which already have %s!',
  1334. 'objectPHIDs'));
  1335. }
  1336. if ($xaction->getCommentPHID()) {
  1337. throw new PhabricatorApplicationTransactionStructureException(
  1338. $xaction,
  1339. pht(
  1340. 'You can not apply transactions which already have %s!',
  1341. 'commentPHIDs'));
  1342. }
  1343. if ($xaction->getCommentVersion() !== 0) {
  1344. throw new PhabricatorApplicationTransactionStructureException(
  1345. $xaction,
  1346. pht(
  1347. 'You can not apply transactions which already have '.
  1348. 'commentVersions!'));
  1349. }
  1350. $expect_value = !$xaction->shouldGenerateOldValue();
  1351. $has_value = $xaction->hasOldValue();
  1352. // See T13082. In the narrow case of applying inverse edge edits, we
  1353. // expect the old value to be populated.
  1354. if ($this->getIsInverseEdgeEditor()) {
  1355. $expect_value = true;
  1356. }
  1357. if ($expect_value && !$has_value) {
  1358. throw new PhabricatorApplicationTransactionStructureException(
  1359. $xaction,
  1360. pht(
  1361. 'This transaction is supposed to have an %s set, but it does not!',
  1362. 'oldValue'));
  1363. }
  1364. if ($has_value && !$expect_value) {
  1365. throw new PhabricatorApplicationTransactionStructureException(
  1366. $xaction,
  1367. pht(
  1368. 'This transaction should generate its %s automatically, '.
  1369. 'but has already had one set!',
  1370. 'oldValue'));
  1371. }
  1372. $type = $xaction->getTransactionType();
  1373. if (empty($types[$type])) {
  1374. throw new PhabricatorApplicationTransactionStructureException(
  1375. $xaction,
  1376. pht(
  1377. 'Transaction has type "%s", but that transaction type is not '.
  1378. 'supported by this editor (%s).',
  1379. $type,
  1380. get_class($this)));
  1381. }
  1382. }
  1383. }
  1384. private function applyCapabilityChecks(
  1385. PhabricatorLiskDAO $object,
  1386. array $xactions) {
  1387. assert_instances_of($xactions, 'PhabricatorApplicationTransaction');
  1388. $can_edit = PhabricatorPolicyCapability::CAN_EDIT;
  1389. if ($this->getIsNewObject()) {
  1390. // If we're creating a new object, we don't need any special capabilities
  1391. // on the object. The actor has already made it through creation checks,
  1392. // and objects which haven't been created yet often can not be
  1393. // meaningfully tested for capabilities anyway.
  1394. $required_capabilities = array();
  1395. } else {
  1396. if (!$xactions && !$this->xactions) {
  1397. // If we aren't doing anything, require CAN_EDIT to improve consistency.
  1398. $required_capabilities = array($can_edit);
  1399. } else {
  1400. $required_capabilities = array();
  1401. foreach ($xactions as $xaction) {
  1402. $type = $xaction->getTransactionType();
  1403. $xtype = $this->getModularTransactionType($type);
  1404. if (!$xtype) {
  1405. $capabilities = $this->getLegacyRequiredCapabilities($xaction);
  1406. } else {
  1407. $capabilities = $xtype->getRequiredCapabilities($object, $xaction);
  1408. }
  1409. // For convenience, we allow flexibility in the return types because
  1410. // it's very unusual that a transaction actually requires multiple
  1411. // capability checks.
  1412. if ($capabilities === null) {
  1413. $capabilities = array();
  1414. } else {
  1415. $capabilities = (array)$capabilities;
  1416. }
  1417. foreach ($capabilities as $capability) {
  1418. $required_capabilities[$capability] = $capability;
  1419. }
  1420. }
  1421. }
  1422. }
  1423. $required_capabilities = array_fuse($required_capabilities);
  1424. $actor = $this->getActor();
  1425. if ($required_capabilities) {
  1426. id(new PhabricatorPolicyFilter())
  1427. ->setViewer($actor)
  1428. ->requireCapabilities($required_capabilities)
  1429. ->raisePolicyExceptions(true)
  1430. ->apply(array($object));
  1431. }
  1432. }
  1433. private function getLegacyRequiredCapabilities(
  1434. PhabricatorApplicationTransaction $xaction) {
  1435. $type = $xaction->getTransactionType();
  1436. switch ($type) {
  1437. case PhabricatorTransactions::TYPE_COMMENT:
  1438. // TODO: Comments technically require CAN_INTERACT, but this is
  1439. // currently somewhat special and handled through EditEngine. For now,
  1440. // don't enforce it here.
  1441. return null;
  1442. case PhabricatorTransactions::TYPE_SUBSCRIBERS:
  1443. // Anyone can subscribe to or unsubscribe from anything they can view,
  1444. // with no other permissions.
  1445. $old = array_fuse($xaction->getOldValue());
  1446. $new = array_fuse($xaction->getNewValue());
  1447. // To remove users other than yourself, you must be able to edit the
  1448. // object.
  1449. $rem = array_diff_key($old, $new);
  1450. foreach ($rem as $phid) {
  1451. if ($phid !== $this->getActingAsPHID()) {
  1452. return PhabricatorPolicyCapability::CAN_EDIT;
  1453. }
  1454. }
  1455. // To add users other than yourself, you must be able to interact.
  1456. // This allows "@mentioning" users to work as long as you can comment
  1457. // on objects.
  1458. // If you can edit, we return that policy instead so that you can
  1459. // override a soft lock and still make edits.
  1460. // TODO: This is a little bit hacky. We really want to be able to say
  1461. // "this requires either interact or edit", but there's currently no
  1462. // way to specify this kind of requirement.
  1463. $can_edit = PhabricatorPolicyFilter::hasCapability(
  1464. $this->getActor(),
  1465. $this->object,
  1466. PhabricatorPolicyCapability::CAN_EDIT);
  1467. $add = array_diff_key($new, $old);
  1468. foreach ($add as $phid) {
  1469. if ($phid !== $this->getActingAsPHID()) {
  1470. if ($can_edit) {
  1471. return PhabricatorPolicyCapability::CAN_EDIT;
  1472. } else {
  1473. return PhabricatorPolicyCapability::CAN_INTERACT;
  1474. }
  1475. }
  1476. }
  1477. return null;
  1478. case PhabricatorTransactions::TYPE_TOKEN:
  1479. // TODO: This technically requires CAN_INTERACT, like comments.
  1480. return null;
  1481. case PhabricatorTransactions::TYPE_HISTORY:
  1482. // This is a special magic transaction which sends you history via
  1483. // email and is only partially supported in the upstream. You don't
  1484. // need any capabilities to apply it.
  1485. return null;
  1486. case PhabricatorTransactions::TYPE_MFA:
  1487. // Signing a transaction group with MFA does not require permissions
  1488. // on its own.
  1489. return null;
  1490. case PhabricatorTransactions::TYPE_EDGE:
  1491. return $this->getLegacyRequiredEdgeCapabilities($xaction);
  1492. default:
  1493. // For other older (non-modular) transactions, always require exactly
  1494. // CAN_EDIT. Transactions which do not need CAN_EDIT or need additional
  1495. // capabilities must move to ModularTransactions.
  1496. return PhabricatorPolicyCapability::CAN_EDIT;
  1497. }
  1498. }
  1499. private function getLegacyRequiredEdgeCapabilities(
  1500. PhabricatorApplicationTransaction $xaction) {
  1501. // You don't need to have edit permission on an object to mention it or
  1502. // otherwise add a relationship pointing toward it.
  1503. if ($this->getIsInverseEdgeEditor()) {
  1504. return null;
  1505. }
  1506. $edge_type = $xaction->getMetadataValue('edge:type');
  1507. switch ($edge_type) {
  1508. case PhabricatorMutedByEdgeType::EDGECONST:
  1509. // At time of writing, you can only write this edge for yourself, so
  1510. // you don't need permissions. If you can eventually mute an object
  1511. // for other users, this would need to be revisited.
  1512. return null;
  1513. case PhabricatorProjectSilencedEdgeType::EDGECONST:
  1514. // At time of writing, you can only write this edge for yourself, so
  1515. // you don't need permissions. If you can eventually silence project
  1516. // for other users, this would need to be revisited.
  1517. return null;
  1518. case PhabricatorObjectMentionsObjectEdgeType::EDGECONST:
  1519. return null;
  1520. case PhabricatorProjectProjectHasMemberEdgeType::EDGECONST:
  1521. $old = $xaction->getOldValue();
  1522. $new = $xaction->getNewValue();
  1523. $add = array_keys(array_diff_key($new, $old));
  1524. $rem = array_keys(array_diff_key($old, $new));
  1525. $actor_phid = $this->requireActor()->getPHID();
  1526. $is_join = (($add === array($actor_phid)) && !$rem);
  1527. $is_leave = (($rem === array($actor_phid)) && !$add);
  1528. if ($is_join) {
  1529. // You need CAN_JOIN to join a project.
  1530. return PhabricatorPolicyCapability::CAN_JOIN;
  1531. }
  1532. if ($is_leave) {
  1533. $object = $this->object;
  1534. // You usually don't need any capabilities to leave a project...
  1535. if ($object->getIsMembershipLocked()) {
  1536. // ...you must be able to edit to leave locked projects, though.
  1537. return PhabricatorPolicyCapability::CAN_EDIT;
  1538. } else {
  1539. return null;
  1540. }
  1541. }
  1542. // You need CAN_EDIT to change members other than yourself.
  1543. return PhabricatorPolicyCapability::CAN_EDIT;
  1544. case PhabricatorObjectHasWatcherEdgeType::EDGECONST:
  1545. // See PHI1024. Watching a project does not require CAN_EDIT.
  1546. return null;
  1547. default:
  1548. return PhabricatorPolicyCapability::CAN_EDIT;
  1549. }
  1550. }
  1551. private function buildSubscribeTransaction(
  1552. PhabricatorLiskDAO $object,
  1553. array $xactions,
  1554. array $changes) {
  1555. if (!($object instanceof PhabricatorSubscribableInterface)) {
  1556. return null;
  1557. }
  1558. if ($this->shouldEnableMentions($object, $xactions)) {
  1559. // Identify newly mentioned users. We ignore users who were previously
  1560. // mentioned so that we don't re-subscribe users after an edit of text
  1561. // which mentions them.
  1562. $old_texts = mpull($changes, 'getOldValue');
  1563. $new_texts = mpull($changes, 'getNewValue');
  1564. $old_phids = PhabricatorMarkupEngine::extractPHIDsFromMentions(
  1565. $this->getActor(),
  1566. $old_texts);
  1567. $new_phids = PhabricatorMarkupEngine::extractPHIDsFromMentions(
  1568. $this->getActor(),
  1569. $new_texts);
  1570. $phids = array_diff($new_phids, $old_phids);
  1571. } else {
  1572. $phids = array();
  1573. }
  1574. $this->mentionedPHIDs = $phids;
  1575. if ($object->getPHID()) {
  1576. // Don't try to subscribe already-subscribed mentions: we want to generate
  1577. // a dialog about an action having no effect if the user explicitly adds
  1578. // existing CCs, but not if they merely mention existing subscribers.
  1579. $phids = array_diff($phids, $this->subscribers);
  1580. }
  1581. if ($phids) {
  1582. $users = id(new PhabricatorPeopleQuery())
  1583. ->setViewer($this->getActor())
  1584. ->withPHIDs($phids)
  1585. ->execute();
  1586. $users = mpull($users, null, 'getPHID');
  1587. foreach ($phids as $key => $phid) {
  1588. $user = idx($users, $phid);
  1589. // Don't subscribe invalid users.
  1590. if (!$user) {
  1591. unset($phids[$key]);
  1592. continue;
  1593. }
  1594. // Don't subscribe bots that get mentioned. If users truly intend
  1595. // to subscribe them, they can add them explicitly, but it's generally
  1596. // not useful to subscribe bots to objects.
  1597. if ($user->getIsSystemAgent()) {
  1598. unset($phids[$key]);
  1599. continue;
  1600. }
  1601. // Do not subscribe mentioned users who do not have permission to see
  1602. // the object.
  1603. if ($object instanceof PhabricatorPolicyInterface) {
  1604. $can_view = PhabricatorPolicyFilter::hasCapability(
  1605. $user,
  1606. $object,
  1607. PhabricatorPolicyCapability::CAN_VIEW);
  1608. if (!$can_view) {
  1609. unset($phids[$key]);
  1610. continue;
  1611. }
  1612. }
  1613. // Don't subscribe users who are already automatically subscribed.
  1614. if ($object->isAutomaticallySubscribed($phid)) {
  1615. unset($phids[$key]);
  1616. continue;
  1617. }
  1618. }
  1619. $phids = array_values($phids);
  1620. }
  1621. if (!$phids) {
  1622. return null;
  1623. }
  1624. $xaction = $object->getApplicationTransactionTemplate()
  1625. ->setTransactionType(PhabricatorTransactions::TYPE_SUBSCRIBERS)
  1626. ->setNewValue(array('+' => $phids));
  1627. return $xaction;
  1628. }
  1629. protected function mergeTransactions(
  1630. PhabricatorApplicationTransaction $u,
  1631. PhabricatorApplicationTransaction $v) {
  1632. $type = $u->getTransactionType();
  1633. $xtype = $this->getModularTransactionType($type);
  1634. if ($xtype) {
  1635. $object = $this->object;
  1636. return $xtype->mergeTransactions($object, $u, $v);
  1637. }
  1638. switch ($type) {
  1639. case PhabricatorTransactions::TYPE_SUBSCRIBERS:
  1640. return $this->mergePHIDOrEdgeTransactions($u, $v);
  1641. case PhabricatorTransactions::TYPE_EDGE:
  1642. $u_type = $u->getMetadataValue('edge:type');
  1643. $v_type = $v->getMetadataValue('edge:type');
  1644. if ($u_type == $v_type) {
  1645. return $this->mergePHIDOrEdgeTransactions($u, $v);
  1646. }
  1647. return null;
  1648. }
  1649. // By default, do not merge the transactions.
  1650. return null;
  1651. }
  1652. /**
  1653. * Optionally expand transactions which imply other effects. For example,
  1654. * resigning from a revision in Differential implies removing yourself as
  1655. * a reviewer.
  1656. */
  1657. protected function expandTransactions(
  1658. PhabricatorLiskDAO $object,
  1659. array $xactions) {
  1660. $results = array();
  1661. foreach ($xactions as $xaction) {
  1662. foreach ($this->expandTransaction($object, $xaction) as $expanded) {
  1663. $results[] = $expanded;
  1664. }
  1665. }
  1666. return $results;
  1667. }
  1668. protected function expandTransaction(
  1669. PhabricatorLiskDAO $object,
  1670. PhabricatorApplicationTransaction $xaction) {
  1671. return array($xaction);
  1672. }
  1673. public function getExpandedSupportTransactions(
  1674. PhabricatorLiskDAO $object,
  1675. PhabricatorApplicationTransaction $xaction) {
  1676. $xactions = array($xaction);
  1677. $xactions = $this->expandSupportTransactions(
  1678. $object,
  1679. $xactions);
  1680. if (count($xactions) == 1) {
  1681. return array();
  1682. }
  1683. foreach ($xactions as $index => $cxaction) {
  1684. if ($cxaction === $xaction) {
  1685. unset($xactions[$index]);
  1686. break;
  1687. }
  1688. }
  1689. return $xactions;
  1690. }
  1691. private function expandSupportTransactions(
  1692. PhabricatorLiskDAO $object,
  1693. array $xactions) {
  1694. $this->loadSubscribers($object);
  1695. $xactions = $this->applyImplicitCC($object, $xactions);
  1696. $changes = $this->getRemarkupChanges($xactions);
  1697. $subscribe_xaction = $this->buildSubscribeTransaction(
  1698. $object,
  1699. $xactions,
  1700. $changes);
  1701. if ($subscribe_xaction) {
  1702. $xactions[] = $subscribe_xaction;
  1703. }
  1704. // TODO: For now, this is just a placeholder.
  1705. $engine = PhabricatorMarkupEngine::getEngine('extract');
  1706. $engine->setConfig('viewer', $this->requireActor());
  1707. $block_xactions = $this->expandRemarkupBlockTransactions(
  1708. $object,
  1709. $xactions,
  1710. $changes,
  1711. $engine);
  1712. foreach ($block_xactions as $xaction) {
  1713. $xactions[] = $xaction;
  1714. }
  1715. return $xactions;
  1716. }
  1717. private function getRemarkupChanges(array $xactions) {
  1718. $changes = array();
  1719. foreach ($xactions as $key => $xaction) {
  1720. foreach ($this->getRemarkupChangesFromTransaction($xaction) as $change) {
  1721. $changes[] = $change;
  1722. }
  1723. }
  1724. return $changes;
  1725. }
  1726. private function getRemarkupChangesFromTransaction(
  1727. PhabricatorApplicationTransaction $transaction) {
  1728. return $transaction->getRemarkupChanges();
  1729. }
  1730. private function expandRemarkupBlockTransactions(
  1731. PhabricatorLiskDAO $object,
  1732. array $xactions,
  1733. array $changes,
  1734. PhutilMarkupEngine $engine) {
  1735. $block_xactions = $this->expandCustomRemarkupBlockTransactions(
  1736. $object,
  1737. $xactions,
  1738. $changes,
  1739. $engine);
  1740. $mentioned_phids = array();
  1741. if ($this->shouldEnableMentions($object, $xactions)) {
  1742. foreach ($changes as $change) {
  1743. // Here, we don't care about processing only new mentions after an edit
  1744. // because there is no way for an object to ever "unmention" itself on
  1745. // another object, so we can ignore the old value.
  1746. $engine->markupText($change->getNewValue());
  1747. $mentioned_phids += $engine->getTextMetadata(
  1748. PhabricatorObjectRemarkupRule::KEY_MENTIONED_OBJECTS,
  1749. array());
  1750. }
  1751. }
  1752. if (!$mentioned_phids) {
  1753. return $block_xactions;
  1754. }
  1755. $mentioned_objects = id(new PhabricatorObjectQuery())
  1756. ->setViewer($this->getActor())
  1757. ->withPHIDs($mentioned_phids)
  1758. ->execute();
  1759. $unmentionable_map = $this->getUnmentionablePHIDMap();
  1760. $mentionable_phids = array();
  1761. if ($this->shouldEnableMentions($object, $xactions)) {
  1762. foreach ($mentioned_objects as $mentioned_object) {
  1763. if ($mentioned_object instanceof PhabricatorMentionableInterface) {
  1764. $mentioned_phid = $mentioned_object->getPHID();
  1765. if (isset($unmentionable_map[$mentioned_phid])) {
  1766. continue;
  1767. }
  1768. // don't let objects mention themselves
  1769. if ($object->getPHID() && $mentioned_phid == $object->getPHID()) {
  1770. continue;
  1771. }
  1772. $mentionable_phids[$mentioned_phid] = $mentioned_phid;
  1773. }
  1774. }
  1775. }
  1776. if ($mentionable_phids) {
  1777. $edge_type = PhabricatorObjectMentionsObjectEdgeType::EDGECONST;
  1778. $block_xactions[] = newv(get_class(head($xactions)), array())
  1779. ->setIgnoreOnNoEffect(true)
  1780. ->setTransactionType(PhabricatorTransactions::TYPE_EDGE)
  1781. ->setMetadataValue('edge:type', $edge_type)
  1782. ->setNewValue(array('+' => $mentionable_phids));
  1783. }
  1784. return $block_xactions;
  1785. }
  1786. protected function expandCustomRemarkupBlockTransactions(
  1787. PhabricatorLiskDAO $object,
  1788. array $xactions,
  1789. array $changes,
  1790. PhutilMarkupEngine $engine) {
  1791. return array();
  1792. }
  1793. /**
  1794. * Attempt to combine similar transactions into a smaller number of total
  1795. * transactions. For example, two transactions which edit the title of an
  1796. * object can be merged into a single edit.
  1797. */
  1798. private function combineTransactions(array $xactions) {
  1799. $stray_comments = array();
  1800. $result = array();
  1801. $types = array();
  1802. foreach ($xactions as $key => $xaction) {
  1803. $type = $xaction->getTransactionType();
  1804. if (isset($types[$type])) {
  1805. foreach ($types[$type] as $other_key) {
  1806. $other_xaction = $result[$other_key];
  1807. // Don't merge transactions with different authors. For example,
  1808. // don't merge Herald transactions and owners transactions.
  1809. if ($other_xaction->getAuthorPHID() != $xaction->getAuthorPHID()) {
  1810. continue;
  1811. }
  1812. $merged = $this->mergeTransactions($result[$other_key], $xaction);
  1813. if ($merged) {
  1814. $result[$other_key] = $merged;
  1815. if ($xaction->getComment() &&
  1816. ($xaction->getComment() !== $merged->getComment())) {
  1817. $stray_comments[] = $xaction->getComment();
  1818. }
  1819. if ($result[$other_key]->getComment() &&
  1820. ($result[$other_key]->getComment() !== $merged->getComment())) {
  1821. $stray_comments[] = $result[$other_key]->getComment();
  1822. }
  1823. // Move on to the next transaction.
  1824. continue 2;
  1825. }
  1826. }
  1827. }
  1828. $result[$key] = $xaction;
  1829. $types[$type][] = $key;
  1830. }
  1831. // If we merged any comments away, restore them.
  1832. foreach ($stray_comments as $comment) {
  1833. $xaction = newv(get_class(head($result)), array());
  1834. $xaction->setTransactionType(PhabricatorTransactions::TYPE_COMMENT);
  1835. $xaction->setComment($comment);
  1836. $result[] = $xaction;
  1837. }
  1838. return array_values($result);
  1839. }
  1840. public function mergePHIDOrEdgeTransactions(
  1841. PhabricatorApplicationTransaction $u,
  1842. PhabricatorApplicationTransaction $v) {
  1843. $result = $u->getNewValue();
  1844. foreach ($v->getNewValue() as $key => $value) {
  1845. if ($u->getTransactionType() == PhabricatorTransactions::TYPE_EDGE) {
  1846. if (empty($result[$key])) {
  1847. $result[$key] = $value;
  1848. } else {
  1849. // We're merging two lists of edge adds, sets, or removes. Merge
  1850. // them by merging individual PHIDs within them.
  1851. $merged = $result[$key];
  1852. foreach ($value as $dst => $v_spec) {
  1853. if (empty($merged[$dst])) {
  1854. $merged[$dst] = $v_spec;
  1855. } else {
  1856. // Two transactions are trying to perform the same operation on
  1857. // the same edge. Normalize the edge data and then merge it. This
  1858. // allows transactions to specify how data merges execute in a
  1859. // precise way.
  1860. $u_spec = $merged[$dst];
  1861. if (!is_array($u_spec)) {
  1862. $u_spec = array('dst' => $u_spec);
  1863. }
  1864. if (!is_array($v_spec)) {
  1865. $v_spec = array('dst' => $v_spec);
  1866. }
  1867. $ux_data = idx($u_spec, 'data', array());
  1868. $vx_data = idx($v_spec, 'data', array());
  1869. $merged_data = $this->mergeEdgeData(
  1870. $u->getMetadataValue('edge:type'),
  1871. $ux_data,
  1872. $vx_data);
  1873. $u_spec['data'] = $merged_data;
  1874. $merged[$dst] = $u_spec;
  1875. }
  1876. }
  1877. $result[$key] = $merged;
  1878. }
  1879. } else {
  1880. $result[$key] = array_merge($value, idx($result, $key, array()));
  1881. }
  1882. }
  1883. $u->setNewValue($result);
  1884. // When combining an "ignore" transaction with a normal transaction, make
  1885. // sure we don't propagate the "ignore" flag.
  1886. if (!$v->getIgnoreOnNoEffect()) {
  1887. $u->setIgnoreOnNoEffect(false);
  1888. }
  1889. return $u;
  1890. }
  1891. protected function mergeEdgeData($type, array $u, array $v) {
  1892. return $v + $u;
  1893. }
  1894. protected function getPHIDTransactionNewValue(
  1895. PhabricatorApplicationTransaction $xaction,
  1896. $old = null) {
  1897. if ($old !== null) {
  1898. $old = array_fuse($old);
  1899. } else {
  1900. $old = array_fuse($xaction->getOldValue());
  1901. }
  1902. return $this->getPHIDList($old, $xaction->getNewValue());
  1903. }
  1904. public function getPHIDList(array $old, array $new) {
  1905. $new_add = idx($new, '+', array());
  1906. unset($new['+']);
  1907. $new_rem = idx($new, '-', array());
  1908. unset($new['-']);
  1909. $new_set = idx($new, '=', null);
  1910. if ($new_set !== null) {
  1911. $new_set = array_fuse($new_set);
  1912. }
  1913. unset($new['=']);
  1914. if ($new) {
  1915. throw new Exception(
  1916. pht(
  1917. "Invalid '%s' value for PHID transaction. Value should contain only ".
  1918. "keys '%s' (add PHIDs), '%s' (remove PHIDs) and '%s' (set PHIDS).",
  1919. 'new',
  1920. '+',
  1921. '-',
  1922. '='));
  1923. }
  1924. $result = array();
  1925. foreach ($old as $phid) {
  1926. if ($new_set !== null && empty($new_set[$phid])) {
  1927. continue;
  1928. }
  1929. $result[$phid] = $phid;
  1930. }
  1931. if ($new_set !== null) {
  1932. foreach ($new_set as $phid) {
  1933. $result[$phid] = $phid;
  1934. }
  1935. }
  1936. foreach ($new_add as $phid) {
  1937. $result[$phid] = $phid;
  1938. }
  1939. foreach ($new_rem as $phid) {
  1940. unset($result[$phid]);
  1941. }
  1942. return array_values($result);
  1943. }
  1944. protected function getEdgeTransactionNewValue(
  1945. PhabricatorApplicationTransaction $xaction) {
  1946. $new = $xaction->getNewValue();
  1947. $new_add = idx($new, '+', array());
  1948. unset($new['+']);
  1949. $new_rem = idx($new, '-', array());
  1950. unset($new['-']);
  1951. $new_set = idx($new, '=', null);
  1952. unset($new['=']);
  1953. if ($new) {
  1954. throw new Exception(
  1955. pht(
  1956. "Invalid '%s' value for Edge transaction. Value should contain only ".
  1957. "keys '%s' (add edges), '%s' (remove edges) and '%s' (set edges).",
  1958. 'new',
  1959. '+',
  1960. '-',
  1961. '='));
  1962. }
  1963. $old = $xaction->getOldValue();
  1964. $lists = array($new_set, $new_add, $new_rem);
  1965. foreach ($lists as $list) {
  1966. $this->checkEdgeList($list, $xaction->getMetadataValue('edge:type'));
  1967. }
  1968. $result = array();
  1969. foreach ($old as $dst_phid => $edge) {
  1970. if ($new_set !== null && empty($new_set[$dst_phid])) {
  1971. continue;
  1972. }
  1973. $result[$dst_phid] = $this->normalizeEdgeTransactionValue(
  1974. $xaction,
  1975. $edge,
  1976. $dst_phid);
  1977. }
  1978. if ($new_set !== null) {
  1979. foreach ($new_set as $dst_phid => $edge) {
  1980. $result[$dst_phid] = $this->normalizeEdgeTransactionValue(
  1981. $xaction,
  1982. $edge,
  1983. $dst_phid);
  1984. }
  1985. }
  1986. foreach ($new_add as $dst_phid => $edge) {
  1987. $result[$dst_phid] = $this->normalizeEdgeTransactionValue(
  1988. $xaction,
  1989. $edge,
  1990. $dst_phid);
  1991. }
  1992. foreach ($new_rem as $dst_phid => $edge) {
  1993. unset($result[$dst_phid]);
  1994. }
  1995. return $result;
  1996. }
  1997. private function checkEdgeList($list, $edge_type) {
  1998. if (!$list) {
  1999. return;
  2000. }
  2001. foreach ($list as $key => $item) {
  2002. if (phid_get_type($key) === PhabricatorPHIDConstants::PHID_TYPE_UNKNOWN) {
  2003. throw new Exception(
  2004. pht(
  2005. 'Edge transactions must have destination PHIDs as in edge '.
  2006. 'lists (found key "%s" on transaction of type "%s").',
  2007. $key,
  2008. $edge_type));
  2009. }
  2010. if (!is_array($item) && $item !== $key) {
  2011. throw new Exception(
  2012. pht(
  2013. 'Edge transactions must have PHIDs or edge specs as values '.
  2014. '(found value "%s" on transaction of type "%s").',
  2015. $item,
  2016. $edge_type));
  2017. }
  2018. }
  2019. }
  2020. private function normalizeEdgeTransactionValue(
  2021. PhabricatorApplicationTransaction $xaction,
  2022. $edge,
  2023. $dst_phid) {
  2024. if (!is_array($edge)) {
  2025. if ($edge != $dst_phid) {
  2026. throw new Exception(
  2027. pht(
  2028. 'Transaction edge data must either be the edge PHID or an edge '.
  2029. 'specification dictionary.'));
  2030. }
  2031. $edge = array();
  2032. } else {
  2033. foreach ($edge as $key => $value) {
  2034. switch ($key) {
  2035. case 'src':
  2036. case 'dst':
  2037. case 'type':
  2038. case 'data':
  2039. case 'dateCreated':
  2040. case 'dateModified':
  2041. case 'seq':
  2042. case 'dataID':
  2043. break;
  2044. default:
  2045. throw new Exception(
  2046. pht(
  2047. 'Transaction edge specification contains unexpected key "%s".',
  2048. $key));
  2049. }
  2050. }
  2051. }
  2052. $edge['dst'] = $dst_phid;
  2053. $edge_type = $xaction->getMetadataValue('edge:type');
  2054. if (empty($edge['type'])) {
  2055. $edge['type'] = $edge_type;
  2056. } else {
  2057. if ($edge['type'] != $edge_type) {
  2058. $this_type = $edge['type'];
  2059. throw new Exception(
  2060. pht(
  2061. "Edge transaction includes edge of type '%s', but ".
  2062. "transaction is of type '%s'. Each edge transaction ".
  2063. "must alter edges of only one type.",
  2064. $this_type,
  2065. $edge_type));
  2066. }
  2067. }
  2068. if (!isset($edge['data'])) {
  2069. $edge['data'] = array();
  2070. }
  2071. return $edge;
  2072. }
  2073. protected function sortTransactions(array $xactions) {
  2074. $head = array();
  2075. $tail = array();
  2076. // Move bare comments to the end, so the actions precede them.
  2077. foreach ($xactions as $xaction) {
  2078. $type = $xaction->getTransactionType();
  2079. if ($type == PhabricatorTransactions::TYPE_COMMENT) {
  2080. $tail[] = $xaction;
  2081. } else {
  2082. $head[] = $xaction;
  2083. }
  2084. }
  2085. return array_values(array_merge($head, $tail));
  2086. }
  2087. protected function filterTransactions(
  2088. PhabricatorLiskDAO $object,
  2089. array $xactions) {
  2090. $type_comment = PhabricatorTransactions::TYPE_COMMENT;
  2091. $type_mfa = PhabricatorTransactions::TYPE_MFA;
  2092. $no_effect = array();
  2093. $has_comment = false;
  2094. $any_effect = false;
  2095. $meta_xactions = array();
  2096. foreach ($xactions as $key => $xaction) {
  2097. if ($xaction->getTransactionType() === $type_mfa) {
  2098. $meta_xactions[$key] = $xaction;
  2099. continue;
  2100. }
  2101. if ($this->transactionHasEffect($object, $xaction)) {
  2102. if ($xaction->getTransactionType() != $type_comment) {
  2103. $any_effect = true;
  2104. }
  2105. } else if ($xaction->getIgnoreOnNoEffect()) {
  2106. unset($xactions[$key]);
  2107. } else {
  2108. $no_effect[$key] = $xaction;
  2109. }
  2110. if ($xaction->hasComment()) {
  2111. $has_comment = true;
  2112. }
  2113. }
  2114. // If every transaction is a meta-transaction applying to the transaction
  2115. // group, these transactions are junk.
  2116. if (count($meta_xactions) == count($xactions)) {
  2117. $no_effect = $xactions;
  2118. $any_effect = false;
  2119. }
  2120. if (!$no_effect) {
  2121. return $xactions;
  2122. }
  2123. // If none of the transactions have an effect, the meta-transactions also
  2124. // have no effect. Add them to the "no effect" list so we get a full set
  2125. // of errors for everything.
  2126. if (!$any_effect && !$has_comment) {
  2127. $no_effect += $meta_xactions;
  2128. }
  2129. if (!$this->getContinueOnNoEffect() && !$this->getIsPreview()) {
  2130. throw new PhabricatorApplicationTransactionNoEffectException(
  2131. $no_effect,
  2132. $any_effect,
  2133. $has_comment);
  2134. }
  2135. if (!$any_effect && !$has_comment) {
  2136. // If we only have empty comment transactions, just drop them all.
  2137. return array();
  2138. }
  2139. foreach ($no_effect as $key => $xaction) {
  2140. if ($xaction->hasComment()) {
  2141. $xaction->setTransactionType($type_comment);
  2142. $xaction->setOldValue(null);
  2143. $xaction->setNewValue(null);
  2144. } else {
  2145. unset($xactions[$key]);
  2146. }
  2147. }
  2148. return $xactions;
  2149. }
  2150. /**
  2151. * Hook for validating transactions. This callback will be invoked for each
  2152. * available transaction type, even if an edit does not apply any transactions
  2153. * of that type. This allows you to raise exceptions when required fields are
  2154. * missing, by detecting that the object has no field value and there is no
  2155. * transaction which sets one.
  2156. *
  2157. * @param PhabricatorLiskDAO Object being edited.
  2158. * @param string Transaction type to validate.
  2159. * @param list<PhabricatorApplicationTransaction> Transactions of given type,
  2160. * which may be empty if the edit does not apply any transactions of the
  2161. * given type.
  2162. * @return list<PhabricatorApplicationTransactionValidationError> List of
  2163. * validation errors.
  2164. */
  2165. protected function validateTransaction(
  2166. PhabricatorLiskDAO $object,
  2167. $type,
  2168. array $xactions) {
  2169. $errors = array();
  2170. $xtype = $this->getModularTransactionType($type);
  2171. if ($xtype) {
  2172. $errors[] = $xtype->validateTransactions($object, $xactions);
  2173. }
  2174. switch ($type) {
  2175. case PhabricatorTransactions::TYPE_VIEW_POLICY:
  2176. $errors[] = $this->validatePolicyTransaction(
  2177. $object,
  2178. $xactions,
  2179. $type,
  2180. PhabricatorPolicyCapability::CAN_VIEW);
  2181. break;
  2182. case PhabricatorTransactions::TYPE_EDIT_POLICY:
  2183. $errors[] = $this->validatePolicyTransaction(
  2184. $object,
  2185. $xactions,
  2186. $type,
  2187. PhabricatorPolicyCapability::CAN_EDIT);
  2188. break;
  2189. case PhabricatorTransactions::TYPE_SPACE:
  2190. $errors[] = $this->validateSpaceTransactions(
  2191. $object,
  2192. $xactions,
  2193. $type);
  2194. break;
  2195. case PhabricatorTransactions::TYPE_SUBTYPE:
  2196. $errors[] = $this->validateSubtypeTransactions(
  2197. $object,
  2198. $xactions,
  2199. $type);
  2200. break;
  2201. case PhabricatorTransactions::TYPE_MFA:
  2202. $errors[] = $this->validateMFATransactions(
  2203. $object,
  2204. $xactions,
  2205. $type);
  2206. break;
  2207. case PhabricatorTransactions::TYPE_CUSTOMFIELD:
  2208. $groups = array();
  2209. foreach ($xactions as $xaction) {
  2210. $groups[$xaction->getMetadataValue('customfield:key')][] = $xaction;
  2211. }
  2212. $field_list = PhabricatorCustomField::getObjectFields(
  2213. $object,
  2214. PhabricatorCustomField::ROLE_EDIT);
  2215. $field_list->setViewer($this->getActor());
  2216. $role_xactions = PhabricatorCustomField::ROLE_APPLICATIONTRANSACTIONS;
  2217. foreach ($field_list->getFields() as $field) {
  2218. if (!$field->shouldEnableForRole($role_xactions)) {
  2219. continue;
  2220. }
  2221. $errors[] = $field->validateApplicationTransactions(
  2222. $this,
  2223. $type,
  2224. idx($groups, $field->getFieldKey(), array()));
  2225. }
  2226. break;
  2227. }
  2228. return array_mergev($errors);
  2229. }
  2230. public function validatePolicyTransaction(
  2231. PhabricatorLiskDAO $object,
  2232. array $xactions,
  2233. $transaction_type,
  2234. $capability) {
  2235. $actor = $this->requireActor();
  2236. $errors = array();
  2237. // Note $this->xactions is necessary; $xactions is $this->xactions of
  2238. // $transaction_type
  2239. $policy_object = $this->adjustObjectForPolicyChecks(
  2240. $object,
  2241. $this->xactions);
  2242. // Make sure the user isn't editing away their ability to $capability this
  2243. // object.
  2244. foreach ($xactions as $xaction) {
  2245. try {
  2246. PhabricatorPolicyFilter::requireCapabilityWithForcedPolicy(
  2247. $actor,
  2248. $policy_object,
  2249. $capability,
  2250. $xaction->getNewValue());
  2251. } catch (PhabricatorPolicyException $ex) {
  2252. $errors[] = new PhabricatorApplicationTransactionValidationError(
  2253. $transaction_type,
  2254. pht('Invalid'),
  2255. pht(
  2256. 'You can not select this %s policy, because you would no longer '.
  2257. 'be able to %s the object.',
  2258. $capability,
  2259. $capability),
  2260. $xaction);
  2261. }
  2262. }
  2263. if ($this->getIsNewObject()) {
  2264. if (!$xactions) {
  2265. $has_capability = PhabricatorPolicyFilter::hasCapability(
  2266. $actor,
  2267. $policy_object,
  2268. $capability);
  2269. if (!$has_capability) {
  2270. $errors[] = new PhabricatorApplicationTransactionValidationError(
  2271. $transaction_type,
  2272. pht('Invalid'),
  2273. pht(
  2274. 'The selected %s policy excludes you. Choose a %s policy '.
  2275. 'which allows you to %s the object.',
  2276. $capability,
  2277. $capability,
  2278. $capability));
  2279. }
  2280. }
  2281. }
  2282. return $errors;
  2283. }
  2284. private function validateSpaceTransactions(
  2285. PhabricatorLiskDAO $object,
  2286. array $xactions,
  2287. $transaction_type) {
  2288. $errors = array();
  2289. $actor = $this->getActor();
  2290. $has_spaces = PhabricatorSpacesNamespaceQuery::getViewerSpacesExist($actor);
  2291. $actor_spaces = PhabricatorSpacesNamespaceQuery::getViewerSpaces($actor);
  2292. $active_spaces = PhabricatorSpacesNamespaceQuery::getViewerActiveSpaces(
  2293. $actor);
  2294. foreach ($xactions as $xaction) {
  2295. $space_phid = $xaction->getNewValue();
  2296. if ($space_phid === null) {
  2297. if (!$has_spaces) {
  2298. // The install doesn't have any spaces, so this is fine.
  2299. continue;
  2300. }
  2301. // The install has some spaces, so every object needs to be put
  2302. // in a valid space.
  2303. $errors[] = new PhabricatorApplicationTransactionValidationError(
  2304. $transaction_type,
  2305. pht('Invalid'),
  2306. pht('You must choose a space for this object.'),
  2307. $xaction);
  2308. continue;
  2309. }
  2310. // If the PHID isn't `null`, it needs to be a valid space that the
  2311. // viewer can see.
  2312. if (empty($actor_spaces[$space_phid])) {
  2313. $errors[] = new PhabricatorApplicationTransactionValidationError(
  2314. $transaction_type,
  2315. pht('Invalid'),
  2316. pht(
  2317. 'You can not shift this object in the selected space, because '.
  2318. 'the space does not exist or you do not have access to it.'),
  2319. $xaction);
  2320. } else if (empty($active_spaces[$space_phid])) {
  2321. // It's OK to edit objects in an archived space, so just move on if
  2322. // we aren't adjusting the value.
  2323. $old_space_phid = $this->getTransactionOldValue($object, $xaction);
  2324. if ($space_phid == $old_space_phid) {
  2325. continue;
  2326. }
  2327. $errors[] = new PhabricatorApplicationTransactionValidationError(
  2328. $transaction_type,
  2329. pht('Archived'),
  2330. pht(
  2331. 'You can not shift this object into the selected space, because '.
  2332. 'the space is archived. Objects can not be created inside (or '.
  2333. 'moved into) archived spaces.'),
  2334. $xaction);
  2335. }
  2336. }
  2337. return $errors;
  2338. }
  2339. private function validateSubtypeTransactions(
  2340. PhabricatorLiskDAO $object,
  2341. array $xactions,
  2342. $transaction_type) {
  2343. $errors = array();
  2344. $map = $object->newEditEngineSubtypeMap();
  2345. $old = $object->getEditEngineSubtype();
  2346. foreach ($xactions as $xaction) {
  2347. $new = $xaction->getNewValue();
  2348. if ($old == $new) {
  2349. continue;
  2350. }
  2351. if (!$map->isValidSubtype($new)) {
  2352. $errors[] = new PhabricatorApplicationTransactionValidationError(
  2353. $transaction_type,
  2354. pht('Invalid'),
  2355. pht(
  2356. 'The subtype "%s" is not a valid subtype.',
  2357. $new),
  2358. $xaction);
  2359. continue;
  2360. }
  2361. }
  2362. return $errors;
  2363. }
  2364. private function validateMFATransactions(
  2365. PhabricatorLiskDAO $object,
  2366. array $xactions,
  2367. $transaction_type) {
  2368. $errors = array();
  2369. $factors = id(new PhabricatorAuthFactorConfigQuery())
  2370. ->setViewer($this->getActor())
  2371. ->withUserPHIDs(array($this->getActingAsPHID()))
  2372. ->withFactorProviderStatuses(
  2373. array(
  2374. PhabricatorAuthFactorProviderStatus::STATUS_ACTIVE,
  2375. PhabricatorAuthFactorProviderStatus::STATUS_DEPRECATED,
  2376. ))
  2377. ->execute();
  2378. foreach ($xactions as $xaction) {
  2379. if (!$factors) {
  2380. $errors[] = new PhabricatorApplicationTransactionValidationError(
  2381. $transaction_type,
  2382. pht('No MFA'),
  2383. pht(
  2384. 'You do not have any MFA factors attached to your account, so '.
  2385. 'you can not sign this transaction group with MFA. Add MFA to '.
  2386. 'your account in Settings.'),
  2387. $xaction);
  2388. }
  2389. }
  2390. if ($xactions) {
  2391. $this->setShouldRequireMFA(true);
  2392. }
  2393. return $errors;
  2394. }
  2395. protected function adjustObjectForPolicyChecks(
  2396. PhabricatorLiskDAO $object,
  2397. array $xactions) {
  2398. $copy = clone $object;
  2399. foreach ($xactions as $xaction) {
  2400. switch ($xaction->getTransactionType()) {
  2401. case PhabricatorTransactions::TYPE_SUBSCRIBERS:
  2402. $clone_xaction = clone $xaction;
  2403. $clone_xaction->setOldValue(array_values($this->subscribers));
  2404. $clone_xaction->setNewValue(
  2405. $this->getPHIDTransactionNewValue(
  2406. $clone_xaction));
  2407. PhabricatorPolicyRule::passTransactionHintToRule(
  2408. $copy,
  2409. new PhabricatorSubscriptionsSubscribersPolicyRule(),
  2410. array_fuse($clone_xaction->getNewValue()));
  2411. break;
  2412. case PhabricatorTransactions::TYPE_SPACE:
  2413. $space_phid = $this->getTransactionNewValue($object, $xaction);
  2414. $copy->setSpacePHID($space_phid);
  2415. break;
  2416. }
  2417. }
  2418. return $copy;
  2419. }
  2420. protected function validateAllTransactions(
  2421. PhabricatorLiskDAO $object,
  2422. array $xactions) {
  2423. return array();
  2424. }
  2425. /**
  2426. * Check for a missing text field.
  2427. *
  2428. * A text field is missing if the object has no value and there are no
  2429. * transactions which set a value, or if the transactions remove the value.
  2430. * This method is intended to make implementing @{method:validateTransaction}
  2431. * more convenient:
  2432. *
  2433. * $missing = $this->validateIsEmptyTextField(
  2434. * $object->getName(),
  2435. * $xactions);
  2436. *
  2437. * This will return `true` if the net effect of the object and transactions
  2438. * is an empty field.
  2439. *
  2440. * @param wild Current field value.
  2441. * @param list<PhabricatorApplicationTransaction> Transactions editing the
  2442. * field.
  2443. * @return bool True if the field will be an empty text field after edits.
  2444. */
  2445. protected function validateIsEmptyTextField($field_value, array $xactions) {
  2446. if (strlen($field_value) && empty($xactions)) {
  2447. return false;
  2448. }
  2449. if ($xactions && strlen(last($xactions)->getNewValue())) {
  2450. return false;
  2451. }
  2452. return true;
  2453. }
  2454. /* -( Implicit CCs )------------------------------------------------------- */
  2455. /**
  2456. * When a user interacts with an object, we might want to add them to CC.
  2457. */
  2458. final public function applyImplicitCC(
  2459. PhabricatorLiskDAO $object,
  2460. array $xactions) {
  2461. if (!($object instanceof PhabricatorSubscribableInterface)) {
  2462. // If the object isn't subscribable, we can't CC them.
  2463. return $xactions;
  2464. }
  2465. $actor_phid = $this->getActingAsPHID();
  2466. $type_user = PhabricatorPeopleUserPHIDType::TYPECONST;
  2467. if (phid_get_type($actor_phid) != $type_user) {
  2468. // Transactions by application actors like Herald, Harbormaster and
  2469. // Diffusion should not CC the applications.
  2470. return $xactions;
  2471. }
  2472. if ($object->isAutomaticallySubscribed($actor_phid)) {
  2473. // If they're auto-subscribed, don't CC them.
  2474. return $xactions;
  2475. }
  2476. $should_cc = false;
  2477. foreach ($xactions as $xaction) {
  2478. if ($this->shouldImplyCC($object, $xaction)) {
  2479. $should_cc = true;
  2480. break;
  2481. }
  2482. }
  2483. if (!$should_cc) {
  2484. // Only some types of actions imply a CC (like adding a comment).
  2485. return $xactions;
  2486. }
  2487. if ($object->getPHID()) {
  2488. if (isset($this->subscribers[$actor_phid])) {
  2489. // If the user is already subscribed, don't implicitly CC them.
  2490. return $xactions;
  2491. }
  2492. $unsub = PhabricatorEdgeQuery::loadDestinationPHIDs(
  2493. $object->getPHID(),
  2494. PhabricatorObjectHasUnsubscriberEdgeType::EDGECONST);
  2495. $unsub = array_fuse($unsub);
  2496. if (isset($unsub[$actor_phid])) {
  2497. // If the user has previously unsubscribed from this object explicitly,
  2498. // don't implicitly CC them.
  2499. return $xactions;
  2500. }
  2501. }
  2502. $actor = $this->getActor();
  2503. $user = id(new PhabricatorPeopleQuery())
  2504. ->setViewer($actor)
  2505. ->withPHIDs(array($actor_phid))
  2506. ->executeOne();
  2507. if (!$user) {
  2508. return $xactions;
  2509. }
  2510. // When a bot acts (usually via the API), don't automatically subscribe
  2511. // them as a side effect. They can always subscribe explicitly if they
  2512. // want, and bot subscriptions normally just clutter things up since bots
  2513. // usually do not read email.
  2514. if ($user->getIsSystemAgent()) {
  2515. return $xactions;
  2516. }
  2517. $xaction = newv(get_class(head($xactions)), array());
  2518. $xaction->setTransactionType(PhabricatorTransactions::TYPE_SUBSCRIBERS);
  2519. $xaction->setNewValue(array('+' => array($actor_phid)));
  2520. array_unshift($xactions, $xaction);
  2521. return $xactions;
  2522. }
  2523. protected function shouldImplyCC(
  2524. PhabricatorLiskDAO $object,
  2525. PhabricatorApplicationTransaction $xaction) {
  2526. return $xaction->isCommentTransaction();
  2527. }
  2528. /* -( Sending Mail )------------------------------------------------------- */
  2529. /**
  2530. * @task mail
  2531. */
  2532. protected function shouldSendMail(
  2533. PhabricatorLiskDAO $object,
  2534. array $xactions) {
  2535. return false;
  2536. }
  2537. /**
  2538. * @task mail
  2539. */
  2540. private function buildMail(
  2541. PhabricatorLiskDAO $object,
  2542. array $xactions) {
  2543. $email_to = $this->mailToPHIDs;
  2544. $email_cc = $this->mailCCPHIDs;
  2545. $email_cc = array_merge($email_cc, $this->heraldEmailPHIDs);
  2546. $unexpandable = $this->mailUnexpandablePHIDs;
  2547. if (!is_array($unexpandable)) {
  2548. $unexpandable = array();
  2549. }
  2550. $messages = $this->buildMailWithRecipients(
  2551. $object,
  2552. $xactions,
  2553. $email_to,
  2554. $email_cc,
  2555. $unexpandable);
  2556. $this->runHeraldMailRules($messages);
  2557. return $messages;
  2558. }
  2559. private function buildMailWithRecipients(
  2560. PhabricatorLiskDAO $object,
  2561. array $xactions,
  2562. array $email_to,
  2563. array $email_cc,
  2564. array $unexpandable) {
  2565. $targets = $this->buildReplyHandler($object)
  2566. ->setUnexpandablePHIDs($unexpandable)
  2567. ->getMailTargets($email_to, $email_cc);
  2568. // Set this explicitly before we start swapping out the effective actor.
  2569. $this->setActingAsPHID($this->getActingAsPHID());
  2570. $xaction_phids = mpull($xactions, 'getPHID');
  2571. $messages = array();
  2572. foreach ($targets as $target) {
  2573. $original_actor = $this->getActor();
  2574. $viewer = $target->getViewer();
  2575. $this->setActor($viewer);
  2576. $locale = PhabricatorEnv::beginScopedLocale($viewer->getTranslation());
  2577. $caught = null;
  2578. $mail = null;
  2579. try {
  2580. // Reload the transactions for the current viewer.
  2581. if ($xaction_phids) {
  2582. $query = PhabricatorApplicationTransactionQuery::newQueryForObject(
  2583. $object);
  2584. $mail_xactions = $query
  2585. ->setViewer($viewer)
  2586. ->withObjectPHIDs(array($object->getPHID()))
  2587. ->withPHIDs($xaction_phids)
  2588. ->execute();
  2589. // Sort the mail transactions in the input order.
  2590. $mail_xactions = mpull($mail_xactions, null, 'getPHID');
  2591. $mail_xactions = array_select_keys($mail_xactions, $xaction_phids);
  2592. $mail_xactions = array_values($mail_xactions);
  2593. } else {
  2594. $mail_xactions = array();
  2595. }
  2596. // Reload handles for the current viewer. This covers older code which
  2597. // emits a list of handle PHIDs upfront.
  2598. $this->loadHandles($mail_xactions);
  2599. $mail = $this->buildMailForTarget($object, $mail_xactions, $target);
  2600. if ($mail) {
  2601. if ($this->mustEncrypt) {
  2602. $mail
  2603. ->setMustEncrypt(true)
  2604. ->setMustEncryptReasons($this->mustEncrypt);
  2605. }
  2606. }
  2607. } catch (Exception $ex) {
  2608. $caught = $ex;
  2609. }
  2610. $this->setActor($original_actor);
  2611. unset($locale);
  2612. if ($caught) {
  2613. throw $ex;
  2614. }
  2615. if ($mail) {
  2616. $messages[] = $mail;
  2617. }
  2618. }
  2619. return $messages;
  2620. }
  2621. protected function getTransactionsForMail(
  2622. PhabricatorLiskDAO $object,
  2623. array $xactions) {
  2624. return $xactions;
  2625. }
  2626. private function buildMailForTarget(
  2627. PhabricatorLiskDAO $object,
  2628. array $xactions,
  2629. PhabricatorMailTarget $target) {
  2630. // Check if any of the transactions are visible for this viewer. If we
  2631. // don't have any visible transactions, don't send the mail.
  2632. $any_visible = false;
  2633. foreach ($xactions as $xaction) {
  2634. if (!$xaction->shouldHideForMail($xactions)) {
  2635. $any_visible = true;
  2636. break;
  2637. }
  2638. }
  2639. if (!$any_visible) {
  2640. return null;
  2641. }
  2642. $mail_xactions = $this->getTransactionsForMail($object, $xactions);
  2643. $mail = $this->buildMailTemplate($object);
  2644. $body = $this->buildMailBody($object, $mail_xactions);
  2645. $mail_tags = $this->getMailTags($object, $mail_xactions);
  2646. $action = $this->getMailAction($object, $mail_xactions);
  2647. $stamps = $this->generateMailStamps($object, $this->mailStamps);
  2648. if (PhabricatorEnv::getEnvConfig('metamta.email-preferences')) {
  2649. $this->addEmailPreferenceSectionToMailBody(
  2650. $body,
  2651. $object,
  2652. $mail_xactions);
  2653. }
  2654. $muted_phids = $this->mailMutedPHIDs;
  2655. if (!is_array($muted_phids)) {
  2656. $muted_phids = array();
  2657. }
  2658. $mail
  2659. ->setSensitiveContent(false)
  2660. ->setFrom($this->getActingAsPHID())
  2661. ->setSubjectPrefix($this->getMailSubjectPrefix())
  2662. ->setVarySubjectPrefix('['.$action.']')
  2663. ->setThreadID($this->getMailThreadID($object), $this->getIsNewObject())
  2664. ->setRelatedPHID($object->getPHID())
  2665. ->setExcludeMailRecipientPHIDs($this->getExcludeMailRecipientPHIDs())
  2666. ->setMutedPHIDs($muted_phids)
  2667. ->setForceHeraldMailRecipientPHIDs($this->heraldForcedEmailPHIDs)
  2668. ->setMailTags($mail_tags)
  2669. ->setIsBulk(true)
  2670. ->setBody($body->render())
  2671. ->setHTMLBody($body->renderHTML());
  2672. foreach ($body->getAttachments() as $attachment) {
  2673. $mail->addAttachment($attachment);
  2674. }
  2675. if ($this->heraldHeader) {
  2676. $mail->addHeader('X-Herald-Rules', $this->heraldHeader);
  2677. }
  2678. if ($object instanceof PhabricatorProjectInterface) {
  2679. $this->addMailProjectMetadata($object, $mail);
  2680. }
  2681. if ($this->getParentMessageID()) {
  2682. $mail->setParentMessageID($this->getParentMessageID());
  2683. }
  2684. // If we have stamps, attach the raw dictionary version (not the actual
  2685. // objects) to the mail so that debugging tools can see what we used to
  2686. // render the final list.
  2687. if ($this->mailStamps) {
  2688. $mail->setMailStampMetadata($this->mailStamps);
  2689. }
  2690. // If we have rendered stamps, attach them to the mail.
  2691. if ($stamps) {
  2692. $mail->setMailStamps($stamps);
  2693. }
  2694. return $target->willSendMail($mail);
  2695. }
  2696. private function addMailProjectMetadata(
  2697. PhabricatorLiskDAO $object,
  2698. PhabricatorMetaMTAMail $template) {
  2699. $project_phids = PhabricatorEdgeQuery::loadDestinationPHIDs(
  2700. $object->getPHID(),
  2701. PhabricatorProjectObjectHasProjectEdgeType::EDGECONST);
  2702. if (!$project_phids) {
  2703. return;
  2704. }
  2705. // TODO: This viewer isn't quite right. It would be slightly better to use
  2706. // the mail recipient, but that's not very easy given the way rendering
  2707. // works today.
  2708. $handles = id(new PhabricatorHandleQuery())
  2709. ->setViewer($this->requireActor())
  2710. ->withPHIDs($project_phids)
  2711. ->execute();
  2712. $project_tags = array();
  2713. foreach ($handles as $handle) {
  2714. if (!$handle->isComplete()) {
  2715. continue;
  2716. }
  2717. $project_tags[] = '<'.$handle->getObjectName().'>';
  2718. }
  2719. if (!$project_tags) {
  2720. return;
  2721. }
  2722. $project_tags = implode(', ', $project_tags);
  2723. $template->addHeader('X-Phabricator-Projects', $project_tags);
  2724. }
  2725. protected function getMailThreadID(PhabricatorLiskDAO $object) {
  2726. return $object->getPHID();
  2727. }
  2728. /**
  2729. * @task mail
  2730. */
  2731. protected function getStrongestAction(
  2732. PhabricatorLiskDAO $object,
  2733. array $xactions) {
  2734. return head(msortv($xactions, 'newActionStrengthSortVector'));
  2735. }
  2736. /**
  2737. * @task mail
  2738. */
  2739. protected function buildReplyHandler(PhabricatorLiskDAO $object) {
  2740. throw new Exception(pht('Capability not supported.'));
  2741. }
  2742. /**
  2743. * @task mail
  2744. */
  2745. protected function getMailSubjectPrefix() {
  2746. throw new Exception(pht('Capability not supported.'));
  2747. }
  2748. /**
  2749. * @task mail
  2750. */
  2751. protected function getMailTags(
  2752. PhabricatorLiskDAO $object,
  2753. array $xactions) {
  2754. $tags = array();
  2755. foreach ($xactions as $xaction) {
  2756. $tags[] = $xaction->getMailTags();
  2757. }
  2758. return array_mergev($tags);
  2759. }
  2760. /**
  2761. * @task mail
  2762. */
  2763. public function getMailTagsMap() {
  2764. // TODO: We should move shared mail tags, like "comment", here.
  2765. return array();
  2766. }
  2767. /**
  2768. * @task mail
  2769. */
  2770. protected function getMailAction(
  2771. PhabricatorLiskDAO $object,
  2772. array $xactions) {
  2773. return $this->getStrongestAction($object, $xactions)->getActionName();
  2774. }
  2775. /**
  2776. * @task mail
  2777. */
  2778. protected function buildMailTemplate(PhabricatorLiskDAO $object) {
  2779. throw new Exception(pht('Capability not supported.'));
  2780. }
  2781. /**
  2782. * @task mail
  2783. */
  2784. protected function getMailTo(PhabricatorLiskDAO $object) {
  2785. throw new Exception(pht('Capability not supported.'));
  2786. }
  2787. protected function newMailUnexpandablePHIDs(PhabricatorLiskDAO $object) {
  2788. return array();
  2789. }
  2790. /**
  2791. * @task mail
  2792. */
  2793. protected function getMailCC(PhabricatorLiskDAO $object) {
  2794. $phids = array();
  2795. $has_support = false;
  2796. if ($object instanceof PhabricatorSubscribableInterface) {
  2797. $phid = $object->getPHID();
  2798. $phids[] = PhabricatorSubscribersQuery::loadSubscribersForPHID($phid);
  2799. $has_support = true;
  2800. }
  2801. if ($object instanceof PhabricatorProjectInterface) {
  2802. $project_phids = PhabricatorEdgeQuery::loadDestinationPHIDs(
  2803. $object->getPHID(),
  2804. PhabricatorProjectObjectHasProjectEdgeType::EDGECONST);
  2805. if ($project_phids) {
  2806. $projects = id(new PhabricatorProjectQuery())
  2807. ->setViewer(PhabricatorUser::getOmnipotentUser())
  2808. ->withPHIDs($project_phids)
  2809. ->needWatchers(true)
  2810. ->execute();
  2811. $watcher_phids = array();
  2812. foreach ($projects as $project) {
  2813. foreach ($project->getAllAncestorWatcherPHIDs() as $phid) {
  2814. $watcher_phids[$phid] = $phid;
  2815. }
  2816. }
  2817. if ($watcher_phids) {
  2818. // We need to do a visibility check for all the watchers, as
  2819. // watching a project is not a guarantee that you can see objects
  2820. // associated with it.
  2821. $users = id(new PhabricatorPeopleQuery())
  2822. ->setViewer($this->requireActor())
  2823. ->withPHIDs($watcher_phids)
  2824. ->execute();
  2825. $watchers = array();
  2826. foreach ($users as $user) {
  2827. $can_see = PhabricatorPolicyFilter::hasCapability(
  2828. $user,
  2829. $object,
  2830. PhabricatorPolicyCapability::CAN_VIEW);
  2831. if ($can_see) {
  2832. $watchers[] = $user->getPHID();
  2833. }
  2834. }
  2835. $phids[] = $watchers;
  2836. }
  2837. }
  2838. $has_support = true;
  2839. }
  2840. if (!$has_support) {
  2841. throw new Exception(
  2842. pht('The object being edited does not implement any standard '.
  2843. 'interfaces (like PhabricatorSubscribableInterface) which allow '.
  2844. 'CCs to be generated automatically. Override the "getMailCC()" '.
  2845. 'method and generate CCs explicitly.'));
  2846. }
  2847. return array_mergev($phids);
  2848. }
  2849. /**
  2850. * @task mail
  2851. */
  2852. protected function buildMailBody(
  2853. PhabricatorLiskDAO $object,
  2854. array $xactions) {
  2855. $body = id(new PhabricatorMetaMTAMailBody())
  2856. ->setViewer($this->requireActor())
  2857. ->setContextObject($object);
  2858. $button_label = $this->getObjectLinkButtonLabelForMail($object);
  2859. $button_uri = $this->getObjectLinkButtonURIForMail($object);
  2860. $this->addHeadersAndCommentsToMailBody(
  2861. $body,
  2862. $xactions,
  2863. $button_label,
  2864. $button_uri);
  2865. $this->addCustomFieldsToMailBody($body, $object, $xactions);
  2866. return $body;
  2867. }
  2868. protected function getObjectLinkButtonLabelForMail(
  2869. PhabricatorLiskDAO $object) {
  2870. return null;
  2871. }
  2872. protected function getObjectLinkButtonURIForMail(
  2873. PhabricatorLiskDAO $object) {
  2874. // Most objects define a "getURI()" method which does what we want, but
  2875. // this isn't formally part of an interface at time of writing. Try to
  2876. // call the method, expecting an exception if it does not exist.
  2877. try {
  2878. $uri = $object->getURI();
  2879. return PhabricatorEnv::getProductionURI($uri);
  2880. } catch (Exception $ex) {
  2881. return null;
  2882. }
  2883. }
  2884. /**
  2885. * @task mail
  2886. */
  2887. protected function addEmailPreferenceSectionToMailBody(
  2888. PhabricatorMetaMTAMailBody $body,
  2889. PhabricatorLiskDAO $object,
  2890. array $xactions) {
  2891. $href = PhabricatorEnv::getProductionURI(
  2892. '/settings/panel/emailpreferences/');
  2893. $body->addLinkSection(pht('EMAIL PREFERENCES'), $href);
  2894. }
  2895. /**
  2896. * @task mail
  2897. */
  2898. protected function addHeadersAndCommentsToMailBody(
  2899. PhabricatorMetaMTAMailBody $body,
  2900. array $xactions,
  2901. $object_label = null,
  2902. $object_uri = null) {
  2903. // First, remove transactions which shouldn't be rendered in mail.
  2904. foreach ($xactions as $key => $xaction) {
  2905. if ($xaction->shouldHideForMail($xactions)) {
  2906. unset($xactions[$key]);
  2907. }
  2908. }
  2909. $headers = array();
  2910. $headers_html = array();
  2911. $comments = array();
  2912. $details = array();
  2913. $seen_comment = false;
  2914. foreach ($xactions as $xaction) {
  2915. // Most mail has zero or one comments. In these cases, we render the
  2916. // "alice added a comment." transaction in the header, like a normal
  2917. // transaction.
  2918. // Some mail, like Differential undraft mail or "!history" mail, may
  2919. // have two or more comments. In these cases, we'll put the first
  2920. // "alice added a comment." transaction in the header normally, but
  2921. // move the other transactions down so they provide context above the
  2922. // actual comment.
  2923. $comment = $this->getBodyForTextMail($xaction);
  2924. if ($comment !== null) {
  2925. $is_comment = true;
  2926. $comments[] = array(
  2927. 'xaction' => $xaction,
  2928. 'comment' => $comment,
  2929. 'initial' => !$seen_comment,
  2930. );
  2931. } else {
  2932. $is_comment = false;
  2933. }
  2934. if (!$is_comment || !$seen_comment) {
  2935. $header = $this->getTitleForTextMail($xaction);
  2936. if ($header !== null) {
  2937. $headers[] = $header;
  2938. }
  2939. $header_html = $this->getTitleForHTMLMail($xaction);
  2940. if ($header_html !== null) {
  2941. $headers_html[] = $header_html;
  2942. }
  2943. }
  2944. if ($xaction->hasChangeDetailsForMail()) {
  2945. $details[] = $xaction;
  2946. }
  2947. if ($is_comment) {
  2948. $seen_comment = true;
  2949. }
  2950. }
  2951. $headers_text = implode("\n", $headers);
  2952. $body->addRawPlaintextSection($headers_text);
  2953. $headers_html = phutil_implode_html(phutil_tag('br'), $headers_html);
  2954. $header_button = null;
  2955. if ($object_label !== null && $object_uri !== null) {
  2956. $button_style = array(
  2957. 'text-decoration: none;',
  2958. 'padding: 4px 8px;',
  2959. 'margin: 0 8px 8px;',
  2960. 'float: right;',
  2961. 'color: #464C5C;',
  2962. 'font-weight: bold;',
  2963. 'border-radius: 3px;',
  2964. 'background-color: #F7F7F9;',
  2965. 'background-image: linear-gradient(to bottom,#fff,#f1f0f1);',
  2966. 'display: inline-block;',
  2967. 'border: 1px solid rgba(71,87,120,.2);',
  2968. );
  2969. $header_button = phutil_tag(
  2970. 'a',
  2971. array(
  2972. 'style' => implode(' ', $button_style),
  2973. 'href' => $object_uri,
  2974. ),
  2975. $object_label);
  2976. }
  2977. $xactions_style = array();
  2978. $header_action = phutil_tag(
  2979. 'td',
  2980. array(),
  2981. $header_button);
  2982. $header_action = phutil_tag(
  2983. 'td',
  2984. array(
  2985. 'style' => implode(' ', $xactions_style),
  2986. ),
  2987. array(
  2988. $headers_html,
  2989. // Add an extra newline to prevent the "View Object" button from
  2990. // running into the transaction text in Mail.app text snippet
  2991. // previews.
  2992. "\n",
  2993. ));
  2994. $headers_html = phutil_tag(
  2995. 'table',
  2996. array(),
  2997. phutil_tag('tr', array(), array($header_action, $header_button)));
  2998. $body->addRawHTMLSection($headers_html);
  2999. foreach ($comments as $spec) {
  3000. $xaction = $spec['xaction'];
  3001. $comment = $spec['comment'];
  3002. $is_initial = $spec['initial'];
  3003. // If this is not the first comment in the mail, add the header showing
  3004. // who wrote the comment immediately above the comment.
  3005. if (!$is_initial) {
  3006. $header = $this->getTitleForTextMail($xaction);
  3007. if ($header !== null) {
  3008. $body->addRawPlaintextSection($header);
  3009. }
  3010. $header_html = $this->getTitleForHTMLMail($xaction);
  3011. if ($header_html !== null) {
  3012. $body->addRawHTMLSection($header_html);
  3013. }
  3014. }
  3015. $body->addRemarkupSection(null, $comment);
  3016. }
  3017. foreach ($details as $xaction) {
  3018. $details = $xaction->renderChangeDetailsForMail($body->getViewer());
  3019. if ($details !== null) {
  3020. $label = $this->getMailDiffSectionHeader($xaction);
  3021. $body->addHTMLSection($label, $details);
  3022. }
  3023. }
  3024. }
  3025. private function getMailDiffSectionHeader($xaction) {
  3026. $type = $xaction->getTransactionType();
  3027. $xtype = $this->getModularTransactionType($type);
  3028. if ($xtype) {
  3029. return $xtype->getMailDiffSectionHeader();
  3030. }
  3031. return pht('EDIT DETAILS');
  3032. }
  3033. /**
  3034. * @task mail
  3035. */
  3036. protected function addCustomFieldsToMailBody(
  3037. PhabricatorMetaMTAMailBody $body,
  3038. PhabricatorLiskDAO $object,
  3039. array $xactions) {
  3040. if ($object instanceof PhabricatorCustomFieldInterface) {
  3041. $field_list = PhabricatorCustomField::getObjectFields(
  3042. $object,
  3043. PhabricatorCustomField::ROLE_TRANSACTIONMAIL);
  3044. $field_list->setViewer($this->getActor());
  3045. $field_list->readFieldsFromStorage($object);
  3046. foreach ($field_list->getFields() as $field) {
  3047. $field->updateTransactionMailBody(
  3048. $body,
  3049. $this,
  3050. $xactions);
  3051. }
  3052. }
  3053. }
  3054. /**
  3055. * @task mail
  3056. */
  3057. private function runHeraldMailRules(array $messages) {
  3058. foreach ($messages as $message) {
  3059. $engine = new HeraldEngine();
  3060. $adapter = id(new PhabricatorMailOutboundMailHeraldAdapter())
  3061. ->setObject($message);
  3062. $rules = $engine->loadRulesForAdapter($adapter);
  3063. $effects = $engine->applyRules($rules, $adapter);
  3064. $engine->applyEffects($effects, $adapter, $rules);
  3065. }
  3066. }
  3067. /* -( Publishing Feed Stories )-------------------------------------------- */
  3068. /**
  3069. * @task feed
  3070. */
  3071. protected function shouldPublishFeedStory(
  3072. PhabricatorLiskDAO $object,
  3073. array $xactions) {
  3074. return false;
  3075. }
  3076. /**
  3077. * @task feed
  3078. */
  3079. protected function getFeedStoryType() {
  3080. return 'PhabricatorApplicationTransactionFeedStory';
  3081. }
  3082. /**
  3083. * @task feed
  3084. */
  3085. protected function getFeedRelatedPHIDs(
  3086. PhabricatorLiskDAO $object,
  3087. array $xactions) {
  3088. $phids = array(
  3089. $object->getPHID(),
  3090. $this->getActingAsPHID(),
  3091. );
  3092. if ($object instanceof PhabricatorProjectInterface) {
  3093. $project_phids = PhabricatorEdgeQuery::loadDestinationPHIDs(
  3094. $object->getPHID(),
  3095. PhabricatorProjectObjectHasProjectEdgeType::EDGECONST);
  3096. foreach ($project_phids as $project_phid) {
  3097. $phids[] = $project_phid;
  3098. }
  3099. }
  3100. return $phids;
  3101. }
  3102. /**
  3103. * @task feed
  3104. */
  3105. protected function getFeedNotifyPHIDs(
  3106. PhabricatorLiskDAO $object,
  3107. array $xactions) {
  3108. // If some transactions are forcing notification delivery, add the forced
  3109. // recipients to the notify list.
  3110. $force_list = array();
  3111. foreach ($xactions as $xaction) {
  3112. $force_phids = $xaction->getForceNotifyPHIDs();
  3113. if (!$force_phids) {
  3114. continue;
  3115. }
  3116. foreach ($force_phids as $force_phid) {
  3117. $force_list[] = $force_phid;
  3118. }
  3119. }
  3120. $to_list = $this->getMailTo($object);
  3121. $cc_list = $this->getMailCC($object);
  3122. $full_list = array_merge($force_list, $to_list, $cc_list);
  3123. $full_list = array_fuse($full_list);
  3124. return array_keys($full_list);
  3125. }
  3126. /**
  3127. * @task feed
  3128. */
  3129. protected function getFeedStoryData(
  3130. PhabricatorLiskDAO $object,
  3131. array $xactions) {
  3132. $xactions = msortv($xactions, 'newActionStrengthSortVector');
  3133. return array(
  3134. 'objectPHID' => $object->getPHID(),
  3135. 'transactionPHIDs' => mpull($xactions, 'getPHID'),
  3136. );
  3137. }
  3138. /**
  3139. * @task feed
  3140. */
  3141. protected function publishFeedStory(
  3142. PhabricatorLiskDAO $object,
  3143. array $xactions,
  3144. array $mailed_phids) {
  3145. // Remove transactions which don't publish feed stories or notifications.
  3146. // These never show up anywhere, so we don't need to do anything with them.
  3147. foreach ($xactions as $key => $xaction) {
  3148. if (!$xaction->shouldHideForFeed()) {
  3149. continue;
  3150. }
  3151. if (!$xaction->shouldHideForNotifications()) {
  3152. continue;
  3153. }
  3154. unset($xactions[$key]);
  3155. }
  3156. if (!$xactions) {
  3157. return;
  3158. }
  3159. $related_phids = $this->feedRelatedPHIDs;
  3160. $subscribed_phids = $this->feedNotifyPHIDs;
  3161. // Remove muted users from the subscription list so they don't get
  3162. // notifications, either.
  3163. $muted_phids = $this->mailMutedPHIDs;
  3164. if (!is_array($muted_phids)) {
  3165. $muted_phids = array();
  3166. }
  3167. $subscribed_phids = array_fuse($subscribed_phids);
  3168. foreach ($muted_phids as $muted_phid) {
  3169. unset($subscribed_phids[$muted_phid]);
  3170. }
  3171. $subscribed_phids = array_values($subscribed_phids);
  3172. $story_type = $this->getFeedStoryType();
  3173. $story_data = $this->getFeedStoryData($object, $xactions);
  3174. $unexpandable_phids = $this->mailUnexpandablePHIDs;
  3175. if (!is_array($unexpandable_phids)) {
  3176. $unexpandable_phids = array();
  3177. }
  3178. id(new PhabricatorFeedStoryPublisher())
  3179. ->setStoryType($story_type)
  3180. ->setStoryData($story_data)
  3181. ->setStoryTime(time())
  3182. ->setStoryAuthorPHID($this->getActingAsPHID())
  3183. ->setRelatedPHIDs($related_phids)
  3184. ->setPrimaryObjectPHID($object->getPHID())
  3185. ->setSubscribedPHIDs($subscribed_phids)
  3186. ->setUnexpandablePHIDs($unexpandable_phids)
  3187. ->setMailRecipientPHIDs($mailed_phids)
  3188. ->setMailTags($this->getMailTags($object, $xactions))
  3189. ->publish();
  3190. }
  3191. /* -( Search Index )------------------------------------------------------- */
  3192. /**
  3193. * @task search
  3194. */
  3195. protected function supportsSearch() {
  3196. return false;
  3197. }
  3198. /* -( Herald Integration )-------------------------------------------------- */
  3199. protected function shouldApplyHeraldRules(
  3200. PhabricatorLiskDAO $object,
  3201. array $xactions) {
  3202. return false;
  3203. }
  3204. protected function buildHeraldAdapter(
  3205. PhabricatorLiskDAO $object,
  3206. array $xactions) {
  3207. throw new Exception(pht('No herald adapter specified.'));
  3208. }
  3209. private function setHeraldAdapter(HeraldAdapter $adapter) {
  3210. $this->heraldAdapter = $adapter;
  3211. return $this;
  3212. }
  3213. protected function getHeraldAdapter() {
  3214. return $this->heraldAdapter;
  3215. }
  3216. private function setHeraldTranscript(HeraldTranscript $transcript) {
  3217. $this->heraldTranscript = $transcript;
  3218. return $this;
  3219. }
  3220. protected function getHeraldTranscript() {
  3221. return $this->heraldTranscript;
  3222. }
  3223. private function applyHeraldRules(
  3224. PhabricatorLiskDAO $object,
  3225. array $xactions) {
  3226. $adapter = $this->buildHeraldAdapter($object, $xactions)
  3227. ->setContentSource($this->getContentSource())
  3228. ->setIsNewObject($this->getIsNewObject())
  3229. ->setActingAsPHID($this->getActingAsPHID())
  3230. ->setAppliedTransactions($xactions);
  3231. if ($this->getApplicationEmail()) {
  3232. $adapter->setApplicationEmail($this->getApplicationEmail());
  3233. }
  3234. // If this editor is operating in silent mode, tell Herald that we aren't
  3235. // going to send any mail. This allows it to skip "the first time this
  3236. // rule matches, send me an email" rules which would otherwise match even
  3237. // though we aren't going to send any mail.
  3238. if ($this->getIsSilent()) {
  3239. $adapter->setForbiddenAction(
  3240. HeraldMailableState::STATECONST,
  3241. HeraldCoreStateReasons::REASON_SILENT);
  3242. }
  3243. $xscript = HeraldEngine::loadAndApplyRules($adapter);
  3244. $this->setHeraldAdapter($adapter);
  3245. $this->setHeraldTranscript($xscript);
  3246. if ($adapter instanceof HarbormasterBuildableAdapterInterface) {
  3247. $buildable_phid = $adapter->getHarbormasterBuildablePHID();
  3248. HarbormasterBuildable::applyBuildPlans(
  3249. $buildable_phid,
  3250. $adapter->getHarbormasterContainerPHID(),
  3251. $adapter->getQueuedHarbormasterBuildRequests());
  3252. // Whether we queued any builds or not, any automatic buildable for this
  3253. // object is now done preparing builds and can transition into a
  3254. // completed status.
  3255. $buildables = id(new HarbormasterBuildableQuery())
  3256. ->setViewer(PhabricatorUser::getOmnipotentUser())
  3257. ->withManualBuildables(false)
  3258. ->withBuildablePHIDs(array($buildable_phid))
  3259. ->execute();
  3260. foreach ($buildables as $buildable) {
  3261. // If this buildable has already moved beyond preparation, we don't
  3262. // need to nudge it again.
  3263. if (!$buildable->isPreparing()) {
  3264. continue;
  3265. }
  3266. $buildable->sendMessage(
  3267. $this->getActor(),
  3268. HarbormasterMessageType::BUILDABLE_BUILD,
  3269. true);
  3270. }
  3271. }
  3272. $this->mustEncrypt = $adapter->getMustEncryptReasons();
  3273. // See PHI1134. Propagate "Must Encrypt" state to sub-editors.
  3274. foreach ($this->subEditors as $sub_editor) {
  3275. $sub_editor->mustEncrypt = $this->mustEncrypt;
  3276. }
  3277. $apply_xactions = $this->didApplyHeraldRules($object, $adapter, $xscript);
  3278. assert_instances_of($apply_xactions, 'PhabricatorApplicationTransaction');
  3279. $queue_xactions = $adapter->getQueuedTransactions();
  3280. return array_merge(
  3281. array_values($apply_xactions),
  3282. array_values($queue_xactions));
  3283. }
  3284. protected function didApplyHeraldRules(
  3285. PhabricatorLiskDAO $object,
  3286. HeraldAdapter $adapter,
  3287. HeraldTranscript $transcript) {
  3288. return array();
  3289. }
  3290. /* -( Custom Fields )------------------------------------------------------ */
  3291. /**
  3292. * @task customfield
  3293. */
  3294. private function getCustomFieldForTransaction(
  3295. PhabricatorLiskDAO $object,
  3296. PhabricatorApplicationTransaction $xaction) {
  3297. $field_key = $xaction->getMetadataValue('customfield:key');
  3298. if (!$field_key) {
  3299. throw new Exception(
  3300. pht(
  3301. "Custom field transaction has no '%s'!",
  3302. 'customfield:key'));
  3303. }
  3304. $field = PhabricatorCustomField::getObjectField(
  3305. $object,
  3306. PhabricatorCustomField::ROLE_APPLICATIONTRANSACTIONS,
  3307. $field_key);
  3308. if (!$field) {
  3309. throw new Exception(
  3310. pht(
  3311. "Custom field transaction has invalid '%s'; field '%s' ".
  3312. "is disabled or does not exist.",
  3313. 'customfield:key',
  3314. $field_key));
  3315. }
  3316. if (!$field->shouldAppearInApplicationTransactions()) {
  3317. throw new Exception(
  3318. pht(
  3319. "Custom field transaction '%s' does not implement ".
  3320. "integration for %s.",
  3321. $field_key,
  3322. 'ApplicationTransactions'));
  3323. }
  3324. $field->setViewer($this->getActor());
  3325. return $field;
  3326. }
  3327. /* -( Files )-------------------------------------------------------------- */
  3328. /**
  3329. * Extract the PHIDs of any files which these transactions attach.
  3330. *
  3331. * @task files
  3332. */
  3333. private function extractFilePHIDs(
  3334. PhabricatorLiskDAO $object,
  3335. array $xactions) {
  3336. $changes = $this->getRemarkupChanges($xactions);
  3337. $blocks = mpull($changes, 'getNewValue');
  3338. $phids = array();
  3339. if ($blocks) {
  3340. $phids[] = PhabricatorMarkupEngine::extractFilePHIDsFromEmbeddedFiles(
  3341. $this->getActor(),
  3342. $blocks);
  3343. }
  3344. foreach ($xactions as $xaction) {
  3345. $type = $xaction->getTransactionType();
  3346. $xtype = $this->getModularTransactionType($type);
  3347. if ($xtype) {
  3348. $phids[] = $xtype->extractFilePHIDs($object, $xaction->getNewValue());
  3349. } else {
  3350. $phids[] = $this->extractFilePHIDsFromCustomTransaction(
  3351. $object,
  3352. $xaction);
  3353. }
  3354. }
  3355. $phids = array_unique(array_filter(array_mergev($phids)));
  3356. if (!$phids) {
  3357. return array();
  3358. }
  3359. // Only let a user attach files they can actually see, since this would
  3360. // otherwise let you access any file by attaching it to an object you have
  3361. // view permission on.
  3362. $files = id(new PhabricatorFileQuery())
  3363. ->setViewer($this->getActor())
  3364. ->withPHIDs($phids)
  3365. ->execute();
  3366. return mpull($files, 'getPHID');
  3367. }
  3368. /**
  3369. * @task files
  3370. */
  3371. protected function extractFilePHIDsFromCustomTransaction(
  3372. PhabricatorLiskDAO $object,
  3373. PhabricatorApplicationTransaction $xaction) {
  3374. return array();
  3375. }
  3376. /**
  3377. * @task files
  3378. */
  3379. private function attachFiles(
  3380. PhabricatorLiskDAO $object,
  3381. array $file_phids) {
  3382. if (!$file_phids) {
  3383. return;
  3384. }
  3385. $editor = new PhabricatorEdgeEditor();
  3386. $src = $object->getPHID();
  3387. $type = PhabricatorObjectHasFileEdgeType::EDGECONST;
  3388. foreach ($file_phids as $dst) {
  3389. $editor->addEdge($src, $type, $dst);
  3390. }
  3391. $editor->save();
  3392. }
  3393. private function applyInverseEdgeTransactions(
  3394. PhabricatorLiskDAO $object,
  3395. PhabricatorApplicationTransaction $xaction,
  3396. $inverse_type) {
  3397. $old = $xaction->getOldValue();
  3398. $new = $xaction->getNewValue();
  3399. $add = array_keys(array_diff_key($new, $old));
  3400. $rem = array_keys(array_diff_key($old, $new));
  3401. $add = array_fuse($add);
  3402. $rem = array_fuse($rem);
  3403. $all = $add + $rem;
  3404. $nodes = id(new PhabricatorObjectQuery())
  3405. ->setViewer($this->requireActor())
  3406. ->withPHIDs($all)
  3407. ->execute();
  3408. $object_phid = $object->getPHID();
  3409. foreach ($nodes as $node) {
  3410. if (!($node instanceof PhabricatorApplicationTransactionInterface)) {
  3411. continue;
  3412. }
  3413. if ($node instanceof PhabricatorUser) {
  3414. // TODO: At least for now, don't record inverse edge transactions
  3415. // for users (for example, "alincoln joined project X"): Feed fills
  3416. // this role instead.
  3417. continue;
  3418. }
  3419. $node_phid = $node->getPHID();
  3420. $editor = $node->getApplicationTransactionEditor();
  3421. $template = $node->getApplicationTransactionTemplate();
  3422. // See T13082. We have to build these transactions with synthetic values
  3423. // because we've already applied the actual edit to the edge database
  3424. // table. If we try to apply this transaction naturally, it will no-op
  3425. // itself because it doesn't have any effect.
  3426. $edge_query = id(new PhabricatorEdgeQuery())
  3427. ->withSourcePHIDs(array($node_phid))
  3428. ->withEdgeTypes(array($inverse_type));
  3429. $edge_query->execute();
  3430. $edge_phids = $edge_query->getDestinationPHIDs();
  3431. $edge_phids = array_fuse($edge_phids);
  3432. $new_phids = $edge_phids;
  3433. $old_phids = $edge_phids;
  3434. if (isset($add[$node_phid])) {
  3435. unset($old_phids[$object_phid]);
  3436. } else {
  3437. $old_phids[$object_phid] = $object_phid;
  3438. }
  3439. $template
  3440. ->setTransactionType($xaction->getTransactionType())
  3441. ->setMetadataValue('edge:type', $inverse_type)
  3442. ->setOldValue($old_phids)
  3443. ->setNewValue($new_phids);
  3444. $editor = $this->newSubEditor($editor)
  3445. ->setContinueOnNoEffect(true)
  3446. ->setContinueOnMissingFields(true)
  3447. ->setIsInverseEdgeEditor(true);
  3448. $editor->applyTransactions($node, array($template));
  3449. }
  3450. }
  3451. /* -( Workers )------------------------------------------------------------ */
  3452. /**
  3453. * Load any object state which is required to publish transactions.
  3454. *
  3455. * This hook is invoked in the main process before we compute data related
  3456. * to publishing transactions (like email "To" and "CC" lists), and again in
  3457. * the worker before publishing occurs.
  3458. *
  3459. * @return object Publishable object.
  3460. * @task workers
  3461. */
  3462. protected function willPublish(PhabricatorLiskDAO $object, array $xactions) {
  3463. return $object;
  3464. }
  3465. /**
  3466. * Convert the editor state to a serializable dictionary which can be passed
  3467. * to a worker.
  3468. *
  3469. * This data will be loaded with @{method:loadWorkerState} in the worker.
  3470. *
  3471. * @return dict<string, wild> Serializable editor state.
  3472. * @task workers
  3473. */
  3474. final private function getWorkerState() {
  3475. $state = array();
  3476. foreach ($this->getAutomaticStateProperties() as $property) {
  3477. $state[$property] = $this->$property;
  3478. }
  3479. $custom_state = $this->getCustomWorkerState();
  3480. $custom_encoding = $this->getCustomWorkerStateEncoding();
  3481. $state += array(
  3482. 'excludeMailRecipientPHIDs' => $this->getExcludeMailRecipientPHIDs(),
  3483. 'custom' => $this->encodeStateForStorage($custom_state, $custom_encoding),
  3484. 'custom.encoding' => $custom_encoding,
  3485. );
  3486. return $state;
  3487. }
  3488. /**
  3489. * Hook; return custom properties which need to be passed to workers.
  3490. *
  3491. * @return dict<string, wild> Custom properties.
  3492. * @task workers
  3493. */
  3494. protected function getCustomWorkerState() {
  3495. return array();
  3496. }
  3497. /**
  3498. * Hook; return storage encoding for custom properties which need to be
  3499. * passed to workers.
  3500. *
  3501. * This primarily allows binary data to be passed to workers and survive
  3502. * JSON encoding.
  3503. *
  3504. * @return dict<string, string> Property encodings.
  3505. * @task workers
  3506. */
  3507. protected function getCustomWorkerStateEncoding() {
  3508. return array();
  3509. }
  3510. /**
  3511. * Load editor state using a dictionary emitted by @{method:getWorkerState}.
  3512. *
  3513. * This method is used to load state when running worker operations.
  3514. *
  3515. * @param dict<string, wild> Editor state, from @{method:getWorkerState}.
  3516. * @return this
  3517. * @task workers
  3518. */
  3519. final public function loadWorkerState(array $state) {
  3520. foreach ($this->getAutomaticStateProperties() as $property) {
  3521. $this->$property = idx($state, $property);
  3522. }
  3523. $exclude = idx($state, 'excludeMailRecipientPHIDs', array());
  3524. $this->setExcludeMailRecipientPHIDs($exclude);
  3525. $custom_state = idx($state, 'custom', array());
  3526. $custom_encodings = idx($state, 'custom.encoding', array());
  3527. $custom = $this->decodeStateFromStorage($custom_state, $custom_encodings);
  3528. $this->loadCustomWorkerState($custom);
  3529. return $this;
  3530. }
  3531. /**
  3532. * Hook; set custom properties on the editor from data emitted by
  3533. * @{method:getCustomWorkerState}.
  3534. *
  3535. * @param dict<string, wild> Custom state,
  3536. * from @{method:getCustomWorkerState}.
  3537. * @return this
  3538. * @task workers
  3539. */
  3540. protected function loadCustomWorkerState(array $state) {
  3541. return $this;
  3542. }
  3543. /**
  3544. * Get a list of object properties which should be automatically sent to
  3545. * workers in the state data.
  3546. *
  3547. * These properties will be automatically stored and loaded by the editor in
  3548. * the worker.
  3549. *
  3550. * @return list<string> List of properties.
  3551. * @task workers
  3552. */
  3553. private function getAutomaticStateProperties() {
  3554. return array(
  3555. 'parentMessageID',
  3556. 'isNewObject',
  3557. 'heraldEmailPHIDs',
  3558. 'heraldForcedEmailPHIDs',
  3559. 'heraldHeader',
  3560. 'mailToPHIDs',
  3561. 'mailCCPHIDs',
  3562. 'feedNotifyPHIDs',
  3563. 'feedRelatedPHIDs',
  3564. 'feedShouldPublish',
  3565. 'mailShouldSend',
  3566. 'mustEncrypt',
  3567. 'mailStamps',
  3568. 'mailUnexpandablePHIDs',
  3569. 'mailMutedPHIDs',
  3570. 'webhookMap',
  3571. 'silent',
  3572. 'sendHistory',
  3573. );
  3574. }
  3575. /**
  3576. * Apply encodings prior to storage.
  3577. *
  3578. * See @{method:getCustomWorkerStateEncoding}.
  3579. *
  3580. * @param map<string, wild> Map of values to encode.
  3581. * @param map<string, string> Map of encodings to apply.
  3582. * @return map<string, wild> Map of encoded values.
  3583. * @task workers
  3584. */
  3585. final private function encodeStateForStorage(
  3586. array $state,
  3587. array $encodings) {
  3588. foreach ($state as $key => $value) {
  3589. $encoding = idx($encodings, $key);
  3590. switch ($encoding) {
  3591. case self::STORAGE_ENCODING_BINARY:
  3592. // The mechanics of this encoding (serialize + base64) are a little
  3593. // awkward, but it allows us encode arrays and still be JSON-safe
  3594. // with binary data.
  3595. $value = @serialize($value);
  3596. if ($value === false) {
  3597. throw new Exception(
  3598. pht(
  3599. 'Failed to serialize() value for key "%s".',
  3600. $key));
  3601. }
  3602. $value = base64_encode($value);
  3603. if ($value === false) {
  3604. throw new Exception(
  3605. pht(
  3606. 'Failed to base64 encode value for key "%s".',
  3607. $key));
  3608. }
  3609. break;
  3610. }
  3611. $state[$key] = $value;
  3612. }
  3613. return $state;
  3614. }
  3615. /**
  3616. * Undo storage encoding applied when storing state.
  3617. *
  3618. * See @{method:getCustomWorkerStateEncoding}.
  3619. *
  3620. * @param map<string, wild> Map of encoded values.
  3621. * @param map<string, string> Map of encodings.
  3622. * @return map<string, wild> Map of decoded values.
  3623. * @task workers
  3624. */
  3625. final private function decodeStateFromStorage(
  3626. array $state,
  3627. array $encodings) {
  3628. foreach ($state as $key => $value) {
  3629. $encoding = idx($encodings, $key);
  3630. switch ($encoding) {
  3631. case self::STORAGE_ENCODING_BINARY:
  3632. $value = base64_decode($value);
  3633. if ($value === false) {
  3634. throw new Exception(
  3635. pht(
  3636. 'Failed to base64_decode() value for key "%s".',
  3637. $key));
  3638. }
  3639. $value = unserialize($value);
  3640. break;
  3641. }
  3642. $state[$key] = $value;
  3643. }
  3644. return $state;
  3645. }
  3646. /**
  3647. * Remove conflicts from a list of projects.
  3648. *
  3649. * Objects aren't allowed to be tagged with multiple milestones in the same
  3650. * group, nor projects such that one tag is the ancestor of any other tag.
  3651. * If the list of PHIDs include mutually exclusive projects, remove the
  3652. * conflicting projects.
  3653. *
  3654. * @param list<phid> List of project PHIDs.
  3655. * @return list<phid> List with conflicts removed.
  3656. */
  3657. private function applyProjectConflictRules(array $phids) {
  3658. if (!$phids) {
  3659. return array();
  3660. }
  3661. // Overall, the last project in the list wins in cases of conflict (so when
  3662. // you add something, the thing you just added sticks and removes older
  3663. // values).
  3664. // Beyond that, there are two basic cases:
  3665. // Milestones: An object can't be in "A > Sprint 3" and "A > Sprint 4".
  3666. // If multiple projects are milestones of the same parent, we only keep the
  3667. // last one.
  3668. // Ancestor: You can't be in "A" and "A > B". If "A > B" comes later
  3669. // in the list, we remove "A" and keep "A > B". If "A" comes later, we
  3670. // remove "A > B" and keep "A".
  3671. // Note that it's OK to be in "A > B" and "A > C". There's only a conflict
  3672. // if one project is an ancestor of another. It's OK to have something
  3673. // tagged with multiple projects which share a common ancestor, so long as
  3674. // they are not mutual ancestors.
  3675. $viewer = PhabricatorUser::getOmnipotentUser();
  3676. $projects = id(new PhabricatorProjectQuery())
  3677. ->setViewer($viewer)
  3678. ->withPHIDs(array_keys($phids))
  3679. ->execute();
  3680. $projects = mpull($projects, null, 'getPHID');
  3681. // We're going to build a map from each project with milestones to the last
  3682. // milestone in the list. This last milestone is the milestone we'll keep.
  3683. $milestone_map = array();
  3684. // We're going to build a set of the projects which have no descendants
  3685. // later in the list. This allows us to apply both ancestor rules.
  3686. $ancestor_map = array();
  3687. foreach ($phids as $phid => $ignored) {
  3688. $project = idx($projects, $phid);
  3689. if (!$project) {
  3690. continue;
  3691. }
  3692. // This is the last milestone we've seen, so set it as the selection for
  3693. // the project's parent. This might be setting a new value or overwriting
  3694. // an earlier value.
  3695. if ($project->isMilestone()) {
  3696. $parent_phid = $project->getParentProjectPHID();
  3697. $milestone_map[$parent_phid] = $phid;
  3698. }
  3699. // Since this is the last item in the list we've examined so far, add it
  3700. // to the set of projects with no later descendants.
  3701. $ancestor_map[$phid] = $phid;
  3702. // Remove any ancestors from the set, since this is a later descendant.
  3703. foreach ($project->getAncestorProjects() as $ancestor) {
  3704. $ancestor_phid = $ancestor->getPHID();
  3705. unset($ancestor_map[$ancestor_phid]);
  3706. }
  3707. }
  3708. // Now that we've built the maps, we can throw away all the projects which
  3709. // have conflicts.
  3710. foreach ($phids as $phid => $ignored) {
  3711. $project = idx($projects, $phid);
  3712. if (!$project) {
  3713. // If a PHID is invalid, we just leave it as-is. We could clean it up,
  3714. // but leaving it untouched is less likely to cause collateral damage.
  3715. continue;
  3716. }
  3717. // If this was a milestone, check if it was the last milestone from its
  3718. // group in the list. If not, remove it from the list.
  3719. if ($project->isMilestone()) {
  3720. $parent_phid = $project->getParentProjectPHID();
  3721. if ($milestone_map[$parent_phid] !== $phid) {
  3722. unset($phids[$phid]);
  3723. continue;
  3724. }
  3725. }
  3726. // If a later project in the list is a subproject of this one, it will
  3727. // have removed ancestors from the map. If this project does not point
  3728. // at itself in the ancestor map, it should be discarded in favor of a
  3729. // subproject that comes later.
  3730. if (idx($ancestor_map, $phid) !== $phid) {
  3731. unset($phids[$phid]);
  3732. continue;
  3733. }
  3734. // If a later project in the list is an ancestor of this one, it will
  3735. // have added itself to the map. If any ancestor of this project points
  3736. // at itself in the map, this project should be discarded in favor of
  3737. // that later ancestor.
  3738. foreach ($project->getAncestorProjects() as $ancestor) {
  3739. $ancestor_phid = $ancestor->getPHID();
  3740. if (isset($ancestor_map[$ancestor_phid])) {
  3741. unset($phids[$phid]);
  3742. continue 2;
  3743. }
  3744. }
  3745. }
  3746. return $phids;
  3747. }
  3748. /**
  3749. * When the view policy for an object is changed, scramble the secret keys
  3750. * for attached files to invalidate existing URIs.
  3751. */
  3752. private function scrambleFileSecrets($object) {
  3753. // If this is a newly created object, we don't need to scramble anything
  3754. // since it couldn't have been previously published.
  3755. if ($this->getIsNewObject()) {
  3756. return;
  3757. }
  3758. // If the object is a file itself, scramble it.
  3759. if ($object instanceof PhabricatorFile) {
  3760. if ($this->shouldScramblePolicy($object->getViewPolicy())) {
  3761. $object->scrambleSecret();
  3762. $object->save();
  3763. }
  3764. }
  3765. $phid = $object->getPHID();
  3766. $attached_phids = PhabricatorEdgeQuery::loadDestinationPHIDs(
  3767. $phid,
  3768. PhabricatorObjectHasFileEdgeType::EDGECONST);
  3769. if (!$attached_phids) {
  3770. return;
  3771. }
  3772. $omnipotent_viewer = PhabricatorUser::getOmnipotentUser();
  3773. $files = id(new PhabricatorFileQuery())
  3774. ->setViewer($omnipotent_viewer)
  3775. ->withPHIDs($attached_phids)
  3776. ->execute();
  3777. foreach ($files as $file) {
  3778. $view_policy = $file->getViewPolicy();
  3779. if ($this->shouldScramblePolicy($view_policy)) {
  3780. $file->scrambleSecret();
  3781. $file->save();
  3782. }
  3783. }
  3784. }
  3785. /**
  3786. * Check if a policy is strong enough to justify scrambling. Objects which
  3787. * are set to very open policies don't need to scramble their files, and
  3788. * files with very open policies don't need to be scrambled when associated
  3789. * objects change.
  3790. */
  3791. private function shouldScramblePolicy($policy) {
  3792. switch ($policy) {
  3793. case PhabricatorPolicies::POLICY_PUBLIC:
  3794. case PhabricatorPolicies::POLICY_USER:
  3795. return false;
  3796. }
  3797. return true;
  3798. }
  3799. private function updateWorkboardColumns($object, $const, $old, $new) {
  3800. // If an object is removed from a project, remove it from any proxy
  3801. // columns for that project. This allows a task which is moved up from a
  3802. // milestone to the parent to move back into the "Backlog" column on the
  3803. // parent workboard.
  3804. if ($const != PhabricatorProjectObjectHasProjectEdgeType::EDGECONST) {
  3805. return;
  3806. }
  3807. // TODO: This should likely be some future WorkboardInterface.
  3808. $appears_on_workboards = ($object instanceof ManiphestTask);
  3809. if (!$appears_on_workboards) {
  3810. return;
  3811. }
  3812. $removed_phids = array_keys(array_diff_key($old, $new));
  3813. if (!$removed_phids) {
  3814. return;
  3815. }
  3816. // Find any proxy columns for the removed projects.
  3817. $proxy_columns = id(new PhabricatorProjectColumnQuery())
  3818. ->setViewer(PhabricatorUser::getOmnipotentUser())
  3819. ->withProxyPHIDs($removed_phids)
  3820. ->execute();
  3821. if (!$proxy_columns) {
  3822. return array();
  3823. }
  3824. $proxy_phids = mpull($proxy_columns, 'getPHID');
  3825. $position_table = new PhabricatorProjectColumnPosition();
  3826. $conn_w = $position_table->establishConnection('w');
  3827. queryfx(
  3828. $conn_w,
  3829. 'DELETE FROM %T WHERE objectPHID = %s AND columnPHID IN (%Ls)',
  3830. $position_table->getTableName(),
  3831. $object->getPHID(),
  3832. $proxy_phids);
  3833. }
  3834. private function getModularTransactionTypes() {
  3835. if ($this->modularTypes === null) {
  3836. $template = $this->object->getApplicationTransactionTemplate();
  3837. if ($template instanceof PhabricatorModularTransaction) {
  3838. $xtypes = $template->newModularTransactionTypes();
  3839. foreach ($xtypes as $key => $xtype) {
  3840. $xtype = clone $xtype;
  3841. $xtype->setEditor($this);
  3842. $xtypes[$key] = $xtype;
  3843. }
  3844. } else {
  3845. $xtypes = array();
  3846. }
  3847. $this->modularTypes = $xtypes;
  3848. }
  3849. return $this->modularTypes;
  3850. }
  3851. private function getModularTransactionType($type) {
  3852. $types = $this->getModularTransactionTypes();
  3853. return idx($types, $type);
  3854. }
  3855. public function getCreateObjectTitle($author, $object) {
  3856. return pht('%s created this object.', $author);
  3857. }
  3858. public function getCreateObjectTitleForFeed($author, $object) {
  3859. return pht('%s created an object: %s.', $author, $object);
  3860. }
  3861. /* -( Queue )-------------------------------------------------------------- */
  3862. protected function queueTransaction(
  3863. PhabricatorApplicationTransaction $xaction) {
  3864. $this->transactionQueue[] = $xaction;
  3865. return $this;
  3866. }
  3867. private function flushTransactionQueue($object) {
  3868. if (!$this->transactionQueue) {
  3869. return;
  3870. }
  3871. $xactions = $this->transactionQueue;
  3872. $this->transactionQueue = array();
  3873. $editor = $this->newEditorCopy();
  3874. return $editor->applyTransactions($object, $xactions);
  3875. }
  3876. final protected function newSubEditor(
  3877. PhabricatorApplicationTransactionEditor $template = null) {
  3878. $editor = $this->newEditorCopy($template);
  3879. $editor->parentEditor = $this;
  3880. $this->subEditors[] = $editor;
  3881. return $editor;
  3882. }
  3883. private function newEditorCopy(
  3884. PhabricatorApplicationTransactionEditor $template = null) {
  3885. if ($template === null) {
  3886. $template = newv(get_class($this), array());
  3887. }
  3888. $editor = id(clone $template)
  3889. ->setActor($this->getActor())
  3890. ->setContentSource($this->getContentSource())
  3891. ->setContinueOnNoEffect($this->getContinueOnNoEffect())
  3892. ->setContinueOnMissingFields($this->getContinueOnMissingFields())
  3893. ->setParentMessageID($this->getParentMessageID())
  3894. ->setIsSilent($this->getIsSilent());
  3895. if ($this->actingAsPHID !== null) {
  3896. $editor->setActingAsPHID($this->actingAsPHID);
  3897. }
  3898. $editor->mustEncrypt = $this->mustEncrypt;
  3899. $editor->transactionGroupID = $this->getTransactionGroupID();
  3900. return $editor;
  3901. }
  3902. /* -( Stamps )------------------------------------------------------------- */
  3903. public function newMailStampTemplates($object) {
  3904. $actor = $this->getActor();
  3905. $templates = array();
  3906. $extensions = $this->newMailExtensions($object);
  3907. foreach ($extensions as $extension) {
  3908. $stamps = $extension->newMailStampTemplates($object);
  3909. foreach ($stamps as $stamp) {
  3910. $key = $stamp->getKey();
  3911. if (isset($templates[$key])) {
  3912. throw new Exception(
  3913. pht(
  3914. 'Mail extension ("%s") defines a stamp template with the '.
  3915. 'same key ("%s") as another template. Each stamp template '.
  3916. 'must have a unique key.',
  3917. get_class($extension),
  3918. $key));
  3919. }
  3920. $stamp->setViewer($actor);
  3921. $templates[$key] = $stamp;
  3922. }
  3923. }
  3924. return $templates;
  3925. }
  3926. final public function getMailStamp($key) {
  3927. if (!isset($this->stampTemplates)) {
  3928. throw new PhutilInvalidStateException('newMailStampTemplates');
  3929. }
  3930. if (!isset($this->stampTemplates[$key])) {
  3931. throw new Exception(
  3932. pht(
  3933. 'Editor ("%s") has no mail stamp template with provided key ("%s").',
  3934. get_class($this),
  3935. $key));
  3936. }
  3937. return $this->stampTemplates[$key];
  3938. }
  3939. private function newMailStamps($object, array $xactions) {
  3940. $actor = $this->getActor();
  3941. $this->stampTemplates = $this->newMailStampTemplates($object);
  3942. $extensions = $this->newMailExtensions($object);
  3943. $stamps = array();
  3944. foreach ($extensions as $extension) {
  3945. $extension->newMailStamps($object, $xactions);
  3946. }
  3947. return $this->stampTemplates;
  3948. }
  3949. private function newMailExtensions($object) {
  3950. $actor = $this->getActor();
  3951. $all_extensions = PhabricatorMailEngineExtension::getAllExtensions();
  3952. $extensions = array();
  3953. foreach ($all_extensions as $key => $template) {
  3954. $extension = id(clone $template)
  3955. ->setViewer($actor)
  3956. ->setEditor($this);
  3957. if ($extension->supportsObject($object)) {
  3958. $extensions[$key] = $extension;
  3959. }
  3960. }
  3961. return $extensions;
  3962. }
  3963. protected function newAuxiliaryMail($object, array $xactions) {
  3964. return array();
  3965. }
  3966. private function generateMailStamps($object, $data) {
  3967. if (!$data || !is_array($data)) {
  3968. return null;
  3969. }
  3970. $templates = $this->newMailStampTemplates($object);
  3971. foreach ($data as $spec) {
  3972. if (!is_array($spec)) {
  3973. continue;
  3974. }
  3975. $key = idx($spec, 'key');
  3976. if (!isset($templates[$key])) {
  3977. continue;
  3978. }
  3979. $type = idx($spec, 'type');
  3980. if ($templates[$key]->getStampType() !== $type) {
  3981. continue;
  3982. }
  3983. $value = idx($spec, 'value');
  3984. $templates[$key]->setValueFromDictionary($value);
  3985. }
  3986. $results = array();
  3987. foreach ($templates as $template) {
  3988. $value = $template->getValueForRendering();
  3989. $rendered = $template->renderStamps($value);
  3990. if ($rendered === null) {
  3991. continue;
  3992. }
  3993. $rendered = (array)$rendered;
  3994. foreach ($rendered as $stamp) {
  3995. $results[] = $stamp;
  3996. }
  3997. }
  3998. natcasesort($results);
  3999. return $results;
  4000. }
  4001. public function getRemovedRecipientPHIDs() {
  4002. return $this->mailRemovedPHIDs;
  4003. }
  4004. private function buildOldRecipientLists($object, $xactions) {
  4005. // See T4776. Before we start making any changes, build a list of the old
  4006. // recipients. If a change removes a user from the recipient list for an
  4007. // object we still want to notify the user about that change. This allows
  4008. // them to respond if they didn't want to be removed.
  4009. if (!$this->shouldSendMail($object, $xactions)) {
  4010. return;
  4011. }
  4012. $this->oldTo = $this->getMailTo($object);
  4013. $this->oldCC = $this->getMailCC($object);
  4014. return $this;
  4015. }
  4016. private function applyOldRecipientLists() {
  4017. $actor_phid = $this->getActingAsPHID();
  4018. // If you took yourself off the recipient list (for example, by
  4019. // unsubscribing or resigning) assume that you know what you did and
  4020. // don't need to be notified.
  4021. // If you just moved from "To" to "Cc" (or vice versa), you're still a
  4022. // recipient so we don't need to add you back in.
  4023. $map = array_fuse($this->mailToPHIDs) + array_fuse($this->mailCCPHIDs);
  4024. foreach ($this->oldTo as $phid) {
  4025. if ($phid === $actor_phid) {
  4026. continue;
  4027. }
  4028. if (isset($map[$phid])) {
  4029. continue;
  4030. }
  4031. $this->mailToPHIDs[] = $phid;
  4032. $this->mailRemovedPHIDs[] = $phid;
  4033. }
  4034. foreach ($this->oldCC as $phid) {
  4035. if ($phid === $actor_phid) {
  4036. continue;
  4037. }
  4038. if (isset($map[$phid])) {
  4039. continue;
  4040. }
  4041. $this->mailCCPHIDs[] = $phid;
  4042. $this->mailRemovedPHIDs[] = $phid;
  4043. }
  4044. return $this;
  4045. }
  4046. private function queueWebhooks($object, array $xactions) {
  4047. $hook_viewer = PhabricatorUser::getOmnipotentUser();
  4048. $webhook_map = $this->webhookMap;
  4049. if (!is_array($webhook_map)) {
  4050. $webhook_map = array();
  4051. }
  4052. // Add any "Firehose" hooks to the list of hooks we're going to call.
  4053. $firehose_hooks = id(new HeraldWebhookQuery())
  4054. ->setViewer($hook_viewer)
  4055. ->withStatuses(
  4056. array(
  4057. HeraldWebhook::HOOKSTATUS_FIREHOSE,
  4058. ))
  4059. ->execute();
  4060. foreach ($firehose_hooks as $firehose_hook) {
  4061. // This is "the hook itself is the reason this hook is being called",
  4062. // since we're including it because it's configured as a firehose
  4063. // hook.
  4064. $hook_phid = $firehose_hook->getPHID();
  4065. $webhook_map[$hook_phid][] = $hook_phid;
  4066. }
  4067. if (!$webhook_map) {
  4068. return;
  4069. }
  4070. // NOTE: We're going to queue calls to disabled webhooks, they'll just
  4071. // immediately fail in the worker queue. This makes the behavior more
  4072. // visible.
  4073. $call_hooks = id(new HeraldWebhookQuery())
  4074. ->setViewer($hook_viewer)
  4075. ->withPHIDs(array_keys($webhook_map))
  4076. ->execute();
  4077. foreach ($call_hooks as $call_hook) {
  4078. $trigger_phids = idx($webhook_map, $call_hook->getPHID());
  4079. $request = HeraldWebhookRequest::initializeNewWebhookRequest($call_hook)
  4080. ->setObjectPHID($object->getPHID())
  4081. ->setTransactionPHIDs(mpull($xactions, 'getPHID'))
  4082. ->setTriggerPHIDs($trigger_phids)
  4083. ->setRetryMode(HeraldWebhookRequest::RETRY_FOREVER)
  4084. ->setIsSilentAction((bool)$this->getIsSilent())
  4085. ->setIsSecureAction((bool)$this->getMustEncrypt())
  4086. ->save();
  4087. $request->queueCall();
  4088. }
  4089. }
  4090. private function hasWarnings($object, $xaction) {
  4091. // TODO: For the moment, this is a very un-modular hack to support
  4092. // exactly one type of warning (mentioning users on a draft revision)
  4093. // that we want to show. See PHI433.
  4094. if (!($object instanceof DifferentialRevision)) {
  4095. return false;
  4096. }
  4097. $type = $xaction->getTransactionType();
  4098. // TODO: This doesn't warn for inlines in Audit, even though they have
  4099. // the same overall workflow.
  4100. if ($type === DifferentialTransaction::TYPE_INLINE) {
  4101. return (bool)$xaction->getComment()->getAttribute('editing', false);
  4102. }
  4103. if (!$object->isDraft()) {
  4104. return false;
  4105. }
  4106. if ($type != PhabricatorTransactions::TYPE_SUBSCRIBERS) {
  4107. return false;
  4108. }
  4109. // NOTE: This will currently warn even if you're only removing
  4110. // subscribers.
  4111. return true;
  4112. }
  4113. private function buildHistoryMail(PhabricatorLiskDAO $object) {
  4114. $viewer = $this->requireActor();
  4115. $recipient_phid = $this->getActingAsPHID();
  4116. // Load every transaction so we can build a mail message with a complete
  4117. // history for the object.
  4118. $query = PhabricatorApplicationTransactionQuery::newQueryForObject($object);
  4119. $xactions = $query
  4120. ->setViewer($viewer)
  4121. ->withObjectPHIDs(array($object->getPHID()))
  4122. ->execute();
  4123. $xactions = array_reverse($xactions);
  4124. $mail_messages = $this->buildMailWithRecipients(
  4125. $object,
  4126. $xactions,
  4127. array($recipient_phid),
  4128. array(),
  4129. array());
  4130. $mail = head($mail_messages);
  4131. // Since the user explicitly requested "!history", force delivery of this
  4132. // message regardless of their other mail settings.
  4133. $mail->setForceDelivery(true);
  4134. return $mail;
  4135. }
  4136. public function newAutomaticInlineTransactions(
  4137. PhabricatorLiskDAO $object,
  4138. $transaction_type,
  4139. PhabricatorCursorPagedPolicyAwareQuery $query_template) {
  4140. $actor = $this->getActor();
  4141. $inlines = id(clone $query_template)
  4142. ->setViewer($actor)
  4143. ->withObjectPHIDs(array($object->getPHID()))
  4144. ->withPublishableComments(true)
  4145. ->needAppliedDrafts(true)
  4146. ->needReplyToComments(true)
  4147. ->execute();
  4148. $inlines = msort($inlines, 'getID');
  4149. $xactions = array();
  4150. foreach ($inlines as $key => $inline) {
  4151. $xactions[] = $object->getApplicationTransactionTemplate()
  4152. ->setTransactionType($transaction_type)
  4153. ->attachComment($inline);
  4154. }
  4155. $state_xaction = $this->newInlineStateTransaction(
  4156. $object,
  4157. $query_template);
  4158. if ($state_xaction) {
  4159. $xactions[] = $state_xaction;
  4160. }
  4161. return $xactions;
  4162. }
  4163. protected function newInlineStateTransaction(
  4164. PhabricatorLiskDAO $object,
  4165. PhabricatorCursorPagedPolicyAwareQuery $query_template) {
  4166. $actor_phid = $this->getActingAsPHID();
  4167. $author_phid = $object->getAuthorPHID();
  4168. $actor_is_author = ($actor_phid == $author_phid);
  4169. $state_map = PhabricatorTransactions::getInlineStateMap();
  4170. $inline_query = id(clone $query_template)
  4171. ->setViewer($this->getActor())
  4172. ->withObjectPHIDs(array($object->getPHID()))
  4173. ->withFixedStates(array_keys($state_map))
  4174. ->withPublishableComments(true);
  4175. if ($actor_is_author) {
  4176. $inline_query->withPublishedComments(true);
  4177. }
  4178. $inlines = $inline_query->execute();
  4179. if (!$inlines) {
  4180. return null;
  4181. }
  4182. $old_value = mpull($inlines, 'getFixedState', 'getPHID');
  4183. $new_value = array();
  4184. foreach ($old_value as $key => $state) {
  4185. $new_value[$key] = $state_map[$state];
  4186. }
  4187. // See PHI995. Copy some information about the inlines into the transaction
  4188. // so we can tailor rendering behavior. In particular, we don't want to
  4189. // render transactions about users marking their own inlines as "Done".
  4190. $inline_details = array();
  4191. foreach ($inlines as $inline) {
  4192. $inline_details[$inline->getPHID()] = array(
  4193. 'authorPHID' => $inline->getAuthorPHID(),
  4194. );
  4195. }
  4196. return $object->getApplicationTransactionTemplate()
  4197. ->setTransactionType(PhabricatorTransactions::TYPE_INLINESTATE)
  4198. ->setIgnoreOnNoEffect(true)
  4199. ->setMetadataValue('inline.details', $inline_details)
  4200. ->setOldValue($old_value)
  4201. ->setNewValue($new_value);
  4202. }
  4203. private function requireMFA(PhabricatorLiskDAO $object, array $xactions) {
  4204. $actor = $this->getActor();
  4205. // Let omnipotent editors skip MFA. This is mostly aimed at scripts.
  4206. if ($actor->isOmnipotent()) {
  4207. return;
  4208. }
  4209. $editor_class = get_class($this);
  4210. $object_phid = $object->getPHID();
  4211. if ($object_phid) {
  4212. $workflow_key = sprintf(
  4213. 'editor(%s).phid(%s)',
  4214. $editor_class,
  4215. $object_phid);
  4216. } else {
  4217. $workflow_key = sprintf(
  4218. 'editor(%s).new()',
  4219. $editor_class);
  4220. }
  4221. $request = $this->getRequest();
  4222. if ($request === null) {
  4223. $source_type = $this->getContentSource()->getSourceTypeConstant();
  4224. $conduit_type = PhabricatorConduitContentSource::SOURCECONST;
  4225. $is_conduit = ($source_type === $conduit_type);
  4226. if ($is_conduit) {
  4227. throw new Exception(
  4228. pht(
  4229. 'This transaction group requires MFA to apply, but you can not '.
  4230. 'provide an MFA response via Conduit. Edit this object via the '.
  4231. 'web UI.'));
  4232. } else {
  4233. throw new Exception(
  4234. pht(
  4235. 'This transaction group requires MFA to apply, but the Editor was '.
  4236. 'not configured with a Request. This workflow can not perform an '.
  4237. 'MFA check.'));
  4238. }
  4239. }
  4240. $cancel_uri = $this->getCancelURI();
  4241. if ($cancel_uri === null) {
  4242. throw new Exception(
  4243. pht(
  4244. 'This transaction group requires MFA to apply, but the Editor was '.
  4245. 'not configured with a Cancel URI. This workflow can not perform '.
  4246. 'an MFA check.'));
  4247. }
  4248. $token = id(new PhabricatorAuthSessionEngine())
  4249. ->setWorkflowKey($workflow_key)
  4250. ->requireHighSecurityToken($actor, $request, $cancel_uri);
  4251. if (!$token->getIsUnchallengedToken()) {
  4252. foreach ($xactions as $xaction) {
  4253. $xaction->setIsMFATransaction(true);
  4254. }
  4255. }
  4256. }
  4257. private function newMFATransactions(
  4258. PhabricatorLiskDAO $object,
  4259. array $xactions) {
  4260. $has_engine = ($object instanceof PhabricatorEditEngineMFAInterface);
  4261. if ($has_engine) {
  4262. $engine = PhabricatorEditEngineMFAEngine::newEngineForObject($object)
  4263. ->setViewer($this->getActor());
  4264. $require_mfa = $engine->shouldRequireMFA();
  4265. $try_mfa = $engine->shouldTryMFA();
  4266. } else {
  4267. $require_mfa = false;
  4268. $try_mfa = false;
  4269. }
  4270. // If the user is mentioning an MFA object on another object or creating
  4271. // a relationship like "parent" or "child" to this object, we always
  4272. // allow the edit to move forward without requiring MFA.
  4273. if ($this->getIsInverseEdgeEditor()) {
  4274. return $xactions;
  4275. }
  4276. if (!$require_mfa) {
  4277. // If the object hasn't already opted into MFA, see if any of the
  4278. // transactions want it.
  4279. if (!$try_mfa) {
  4280. foreach ($xactions as $xaction) {
  4281. $type = $xaction->getTransactionType();
  4282. $xtype = $this->getModularTransactionType($type);
  4283. if ($xtype) {
  4284. $xtype = clone $xtype;
  4285. $xtype->setStorage($xaction);
  4286. if ($xtype->shouldTryMFA($object, $xaction)) {
  4287. $try_mfa = true;
  4288. break;
  4289. }
  4290. }
  4291. }
  4292. }
  4293. if ($try_mfa) {
  4294. $this->setShouldRequireMFA(true);
  4295. }
  4296. return $xactions;
  4297. }
  4298. $type_mfa = PhabricatorTransactions::TYPE_MFA;
  4299. $has_mfa = false;
  4300. foreach ($xactions as $xaction) {
  4301. if ($xaction->getTransactionType() === $type_mfa) {
  4302. $has_mfa = true;
  4303. break;
  4304. }
  4305. }
  4306. if ($has_mfa) {
  4307. return $xactions;
  4308. }
  4309. $template = $object->getApplicationTransactionTemplate();
  4310. $mfa_xaction = id(clone $template)
  4311. ->setTransactionType($type_mfa)
  4312. ->setNewValue(true);
  4313. array_unshift($xactions, $mfa_xaction);
  4314. return $xactions;
  4315. }
  4316. private function getTitleForTextMail(
  4317. PhabricatorApplicationTransaction $xaction) {
  4318. $type = $xaction->getTransactionType();
  4319. $xtype = $this->getModularTransactionType($type);
  4320. if ($xtype) {
  4321. $xtype = clone $xtype;
  4322. $xtype->setStorage($xaction);
  4323. $comment = $xtype->getTitleForTextMail();
  4324. if ($comment !== false) {
  4325. return $comment;
  4326. }
  4327. }
  4328. return $xaction->getTitleForTextMail();
  4329. }
  4330. private function getTitleForHTMLMail(
  4331. PhabricatorApplicationTransaction $xaction) {
  4332. $type = $xaction->getTransactionType();
  4333. $xtype = $this->getModularTransactionType($type);
  4334. if ($xtype) {
  4335. $xtype = clone $xtype;
  4336. $xtype->setStorage($xaction);
  4337. $comment = $xtype->getTitleForHTMLMail();
  4338. if ($comment !== false) {
  4339. return $comment;
  4340. }
  4341. }
  4342. return $xaction->getTitleForHTMLMail();
  4343. }
  4344. private function getBodyForTextMail(
  4345. PhabricatorApplicationTransaction $xaction) {
  4346. $type = $xaction->getTransactionType();
  4347. $xtype = $this->getModularTransactionType($type);
  4348. if ($xtype) {
  4349. $xtype = clone $xtype;
  4350. $xtype->setStorage($xaction);
  4351. $comment = $xtype->getBodyForTextMail();
  4352. if ($comment !== false) {
  4353. return $comment;
  4354. }
  4355. }
  4356. return $xaction->getBodyForMail();
  4357. }
  4358. private function isLockOverrideTransaction(
  4359. PhabricatorApplicationTransaction $xaction) {
  4360. // See PHI1209. When an object is locked, certain types of transactions
  4361. // can still be applied without requiring a policy check, like subscribing
  4362. // or unsubscribing. We don't want these transactions to show the "Lock
  4363. // Override" icon in the transaction timeline.
  4364. // We could test if a transaction did no direct policy checks, but it may
  4365. // have done additional policy checks during validation, so this is not a
  4366. // reliable test (and could cause false negatives, where edits which did
  4367. // override a lock are not marked properly).
  4368. // For now, do this in a narrow way and just check against a hard-coded
  4369. // list of non-override transaction situations. Some day, this should
  4370. // likely be modularized.
  4371. // Inverse edge edits don't interact with locks.
  4372. if ($this->getIsInverseEdgeEditor()) {
  4373. return false;
  4374. }
  4375. // For now, all edits other than subscribes always override locks.
  4376. $type = $xaction->getTransactionType();
  4377. if ($type !== PhabricatorTransactions::TYPE_SUBSCRIBERS) {
  4378. return true;
  4379. }
  4380. // Subscribes override locks if they affect any users other than the
  4381. // acting user.
  4382. $acting_phid = $this->getActingAsPHID();
  4383. $old = array_fuse($xaction->getOldValue());
  4384. $new = array_fuse($xaction->getNewValue());
  4385. $add = array_diff_key($new, $old);
  4386. $rem = array_diff_key($old, $new);
  4387. $all = $add + $rem;
  4388. foreach ($all as $phid) {
  4389. if ($phid !== $acting_phid) {
  4390. return true;
  4391. }
  4392. }
  4393. return false;
  4394. }
  4395. /* -( Extensions )--------------------------------------------------------- */
  4396. private function validateTransactionsWithExtensions(
  4397. PhabricatorLiskDAO $object,
  4398. array $xactions) {
  4399. $errors = array();
  4400. $extensions = $this->getEditorExtensions();
  4401. foreach ($extensions as $extension) {
  4402. $extension_errors = $extension
  4403. ->setObject($object)
  4404. ->validateTransactions($object, $xactions);
  4405. assert_instances_of(
  4406. $extension_errors,
  4407. 'PhabricatorApplicationTransactionValidationError');
  4408. $errors[] = $extension_errors;
  4409. }
  4410. return array_mergev($errors);
  4411. }
  4412. private function getEditorExtensions() {
  4413. if ($this->extensions === null) {
  4414. $this->extensions = $this->newEditorExtensions();
  4415. }
  4416. return $this->extensions;
  4417. }
  4418. private function newEditorExtensions() {
  4419. $extensions = PhabricatorEditorExtension::getAllExtensions();
  4420. $actor = $this->getActor();
  4421. $object = $this->object;
  4422. foreach ($extensions as $key => $extension) {
  4423. $extension = id(clone $extension)
  4424. ->setViewer($actor)
  4425. ->setEditor($this)
  4426. ->setObject($object);
  4427. if (!$extension->supportsObject($this, $object)) {
  4428. unset($extensions[$key]);
  4429. continue;
  4430. }
  4431. $extensions[$key] = $extension;
  4432. }
  4433. return $extensions;
  4434. }
  4435. }