[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class PhabricatorApplicationTransactionCommentEditor 4 extends PhabricatorEditor { 5 6 private $contentSource; 7 private $actingAsPHID; 8 9 public function setActingAsPHID($acting_as_phid) { 10 $this->actingAsPHID = $acting_as_phid; 11 return $this; 12 } 13 14 public function getActingAsPHID() { 15 if ($this->actingAsPHID) { 16 return $this->actingAsPHID; 17 } 18 return $this->getActor()->getPHID(); 19 } 20 21 public function setContentSource(PhabricatorContentSource $content_source) { 22 $this->contentSource = $content_source; 23 return $this; 24 } 25 26 public function getContentSource() { 27 return $this->contentSource; 28 } 29 30 /** 31 * Edit a transaction's comment. This method effects the required create, 32 * update or delete to set the transaction's comment to the provided comment. 33 */ 34 public function applyEdit( 35 PhabricatorApplicationTransaction $xaction, 36 PhabricatorApplicationTransactionComment $comment) { 37 38 $this->validateEdit($xaction, $comment); 39 40 $actor = $this->requireActor(); 41 42 $comment->setContentSource($this->getContentSource()); 43 $comment->setAuthorPHID($this->getActingAsPHID()); 44 45 // TODO: This needs to be more sophisticated once we have meta-policies. 46 $comment->setViewPolicy(PhabricatorPolicies::POLICY_PUBLIC); 47 $comment->setEditPolicy($this->getActingAsPHID()); 48 49 $file_phids = PhabricatorMarkupEngine::extractFilePHIDsFromEmbeddedFiles( 50 $actor, 51 array( 52 $comment->getContent(), 53 )); 54 55 $xaction->openTransaction(); 56 $xaction->beginReadLocking(); 57 if ($xaction->getID()) { 58 $xaction->reload(); 59 } 60 61 $new_version = $xaction->getCommentVersion() + 1; 62 63 $comment->setCommentVersion($new_version); 64 $comment->setTransactionPHID($xaction->getPHID()); 65 $comment->save(); 66 67 $xaction->setCommentVersion($new_version); 68 $xaction->setCommentPHID($comment->getPHID()); 69 $xaction->setViewPolicy($comment->getViewPolicy()); 70 $xaction->setEditPolicy($comment->getEditPolicy()); 71 $xaction->save(); 72 73 $xaction->endReadLocking(); 74 $xaction->saveTransaction(); 75 76 // Add links to any files newly referenced by the edit. 77 if ($file_phids) { 78 $editor = new PhabricatorEdgeEditor(); 79 foreach ($file_phids as $file_phid) { 80 $editor->addEdge( 81 $xaction->getObjectPHID(), 82 PhabricatorEdgeConfig::TYPE_OBJECT_HAS_FILE, 83 $file_phid); 84 } 85 $editor->save(); 86 } 87 88 $xaction->attachComment($comment); 89 90 return $this; 91 } 92 93 /** 94 * Validate that the edit is permissible, and the actor has permission to 95 * perform it. 96 */ 97 private function validateEdit( 98 PhabricatorApplicationTransaction $xaction, 99 PhabricatorApplicationTransactionComment $comment) { 100 101 if (!$xaction->getPHID()) { 102 throw new Exception( 103 'Transaction must have a PHID before calling applyEdit()!'); 104 } 105 106 $type_comment = PhabricatorTransactions::TYPE_COMMENT; 107 if ($xaction->getTransactionType() == $type_comment) { 108 if ($comment->getPHID()) { 109 throw new Exception( 110 'Transaction comment must not yet have a PHID!'); 111 } 112 } 113 114 if (!$this->getContentSource()) { 115 throw new Exception( 116 'Call setContentSource() before applyEdit()!'); 117 } 118 119 $actor = $this->requireActor(); 120 121 PhabricatorPolicyFilter::requireCapability( 122 $actor, 123 $xaction, 124 PhabricatorPolicyCapability::CAN_VIEW); 125 126 if ($comment->getIsRemoved() && $actor->getIsAdmin()) { 127 // NOTE: Administrators can remove comments by any user, and don't need 128 // to pass the edit check. 129 } else { 130 PhabricatorPolicyFilter::requireCapability( 131 $actor, 132 $xaction, 133 PhabricatorPolicyCapability::CAN_EDIT); 134 } 135 } 136 137 138 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |