1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-22 23:02:42 +01:00

Implement PolicyInterface, ExtendedPolicyInterface, and DestructibleInterface on PhrictionContent

Summary:
Depends on D19093. Ref T13077. Although content objects normally don't have any edges today, they may in the future.

Also implement Policy stuff properly.

Test Plan: Used `bin/remove destroy` to destroy a document, verified it also loaded and destroyed the correspoding Content correctly by looking at `--trace` and the database rows.

Maniphest Tasks: T13077

Differential Revision: https://secure.phabricator.com/D19094
This commit is contained in:
epriestley 2018-02-15 04:52:09 -08:00
parent b2c829f274
commit 9404e2b3d4
4 changed files with 130 additions and 11 deletions

View file

@ -10755,7 +10755,11 @@ phutil_register_library_map(array(
'PhrictionChangeType' => 'PhrictionConstants',
'PhrictionConduitAPIMethod' => 'ConduitAPIMethod',
'PhrictionConstants' => 'Phobject',
'PhrictionContent' => 'PhrictionDAO',
'PhrictionContent' => array(
'PhrictionDAO',
'PhabricatorPolicyInterface',
'PhabricatorDestructibleInterface',
),
'PhrictionContentPHIDType' => 'PhabricatorPHIDType',
'PhrictionContentQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
'PhrictionController' => 'PhabricatorController',

View file

@ -5,6 +5,7 @@ final class PhrictionContentQuery
private $ids;
private $phids;
private $documentPHIDs;
public function withIDs(array $ids) {
$this->ids = $ids;
@ -16,6 +17,11 @@ final class PhrictionContentQuery
return $this;
}
public function withDocumentPHIDs(array $phids) {
$this->documentPHIDs = $phids;
return $this;
}
public function newResultObject() {
return new PhrictionContent();
}
@ -30,20 +36,78 @@ final class PhrictionContentQuery
if ($this->ids !== null) {
$where[] = qsprintf(
$conn,
'id IN (%Ld)',
'c.id IN (%Ld)',
$this->ids);
}
if ($this->phids !== null) {
$where[] = qsprintf(
$conn,
'phid IN (%Ls)',
'c.phid IN (%Ls)',
$this->phids);
}
if ($this->documentPHIDs !== null) {
$where[] = qsprintf(
$conn,
'd.phid IN (%Ls)',
$this->documentPHIDs);
}
return $where;
}
protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {
$joins = parent::buildJoinClauseParts($conn);
if ($this->shouldJoinDocumentTable()) {
$joins[] = qsprintf(
$conn,
'JOIN %T d ON d.id = c.documentID',
id(new PhrictionDocument())->getTableName());
}
return $joins;
}
protected function willFilterPage(array $contents) {
$document_ids = mpull($contents, 'getDocumentID');
$documents = id(new PhrictionDocumentQuery())
->setViewer($this->getViewer())
->setParentQuery($this)
->withIDs($document_ids)
->execute();
$documents = mpull($documents, null, 'getID');
foreach ($contents as $key => $content) {
$document_id = $content->getDocumentID();
$document = idx($documents, $document_id);
if (!$document) {
unset($contents[$key]);
$this->didRejectResult($content);
continue;
}
$content->attachDocument($document);
}
return $contents;
}
private function shouldJoinDocumentTable() {
if ($this->documentPHIDs !== null) {
return true;
}
return false;
}
protected function getPrimaryTableAlias() {
return 'c';
}
public function getQueryApplicationClass() {
return 'PhabricatorPhrictionApplication';
}

View file

@ -1,9 +1,11 @@
<?php
final class PhrictionContent
extends PhrictionDAO {
extends PhrictionDAO
implements
PhabricatorPolicyInterface,
PhabricatorDestructibleInterface {
protected $id;
protected $documentID;
protected $version;
protected $authorPHID;
@ -16,6 +18,8 @@ final class PhrictionContent
protected $changeType;
protected $changeRef;
private $document = self::ATTACHABLE;
protected function getConfiguration() {
return array(
self::CONFIG_AUX_PHID => true,
@ -56,4 +60,50 @@ final class PhrictionContent
->setGenerateTableOfContents(true);
}
public function attachDocument(PhrictionDocument $document) {
$this->document = $document;
return $this;
}
public function getDocument() {
return $this->assertAttached($this->document);
}
/* -( PhabricatorPolicyInterface )----------------------------------------- */
public function getCapabilities() {
return array(
PhabricatorPolicyCapability::CAN_VIEW,
);
}
public function getPolicy($capability) {
return PhabricatorPolicies::getMostOpenPolicy();
}
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
return false;
}
/* -( PhabricatorExtendedPolicyInterface )--------------------------------- */
public function getExtendedPolicy($capability, PhabricatorUser $viewer) {
return array(
array($this->getDocument(), PhabricatorPolicyCapability::CAN_VIEW),
);
}
/* -( PhabricatorDestructibleInterface )----------------------------------- */
public function destroyObjectPermanently(
PhabricatorDestructionEngine $engine) {
$this->delete();
}
}

View file

@ -236,15 +236,16 @@ final class PhrictionDocument extends PhrictionDAO
$this->openTransaction();
$this->delete();
$contents = id(new PhrictionContent())->loadAllWhere(
'documentID = %d',
$this->getID());
$contents = id(new PhrictionContentQuery())
->setViewer($engine->getViewer())
->withDocumentPHIDs(array($this->getPHID()))
->execute();
foreach ($contents as $content) {
$content->delete();
$engine->destroyObject($content);
}
$this->delete();
$this->saveTransaction();
}