Page MenuHomePhabricator

D21835.id52037.diff
No OneTemporary

D21835.id52037.diff

diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -3453,6 +3453,7 @@
'PhabricatorFileAES256StorageFormat' => 'applications/files/format/PhabricatorFileAES256StorageFormat.php',
'PhabricatorFileAltTextTransaction' => 'applications/files/xaction/PhabricatorFileAltTextTransaction.php',
'PhabricatorFileAttachment' => 'applications/files/storage/PhabricatorFileAttachment.php',
+ 'PhabricatorFileAttachmentQuery' => 'applications/files/query/PhabricatorFileAttachmentQuery.php',
'PhabricatorFileBundleLoader' => 'applications/files/query/PhabricatorFileBundleLoader.php',
'PhabricatorFileChunk' => 'applications/files/storage/PhabricatorFileChunk.php',
'PhabricatorFileChunkIterator' => 'applications/files/engine/PhabricatorFileChunkIterator.php',
@@ -9892,7 +9893,12 @@
),
'PhabricatorFileAES256StorageFormat' => 'PhabricatorFileStorageFormat',
'PhabricatorFileAltTextTransaction' => 'PhabricatorFileTransactionType',
- 'PhabricatorFileAttachment' => 'PhabricatorFileDAO',
+ 'PhabricatorFileAttachment' => array(
+ 'PhabricatorFileDAO',
+ 'PhabricatorPolicyInterface',
+ 'PhabricatorExtendedPolicyInterface',
+ ),
+ 'PhabricatorFileAttachmentQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
'PhabricatorFileBundleLoader' => 'Phobject',
'PhabricatorFileChunk' => array(
'PhabricatorFileDAO',
diff --git a/src/applications/files/query/PhabricatorFileAttachmentQuery.php b/src/applications/files/query/PhabricatorFileAttachmentQuery.php
new file mode 100644
--- /dev/null
+++ b/src/applications/files/query/PhabricatorFileAttachmentQuery.php
@@ -0,0 +1,110 @@
+<?php
+
+final class PhabricatorFileAttachmentQuery
+ extends PhabricatorCursorPagedPolicyAwareQuery {
+
+ private $objectPHIDs;
+ private $needFiles;
+
+ public function withObjectPHIDs(array $object_phids) {
+ $this->objectPHIDs = $object_phids;
+ return $this;
+ }
+
+ public function needFiles($need) {
+ $this->needFiles = $need;
+ return $this;
+ }
+
+ public function newResultObject() {
+ return new PhabricatorFileAttachment();
+ }
+
+ protected function loadPage() {
+ return $this->loadStandardPage($this->newResultObject());
+ }
+
+ protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
+ $where = parent::buildWhereClauseParts($conn);
+
+ if ($this->objectPHIDs !== null) {
+ $where[] = qsprintf(
+ $conn,
+ 'attachments.objectPHID IN (%Ls)',
+ $this->objectPHIDs);
+ }
+
+ return $where;
+ }
+
+ protected function willFilterPage(array $attachments) {
+ $viewer = $this->getViewer();
+ $object_phids = array();
+
+ foreach ($attachments as $attachment) {
+ $object_phid = $attachment->getObjectPHID();
+ $object_phids[$object_phid] = $object_phid;
+ }
+
+ if ($object_phids) {
+ $objects = id(new PhabricatorObjectQuery())
+ ->setViewer($viewer)
+ ->setParentQuery($this)
+ ->withPHIDs($object_phids)
+ ->execute();
+ $objects = mpull($objects, null, 'getPHID');
+ } else {
+ $objects = array();
+ }
+
+ foreach ($attachments as $key => $attachment) {
+ $object_phid = $attachment->getObjectPHID();
+ $object = idx($objects, $object_phid);
+
+ if (!$object) {
+ $this->didRejectResult($attachment);
+ unset($attachments[$key]);
+ continue;
+ }
+
+ $attachment->attachObject($object);
+ }
+
+ if ($this->needFiles) {
+ $file_phids = array();
+ foreach ($attachments as $attachment) {
+ $file_phid = $attachment->getFilePHID();
+ $file_phids[$file_phid] = $file_phid;
+ }
+
+ if ($file_phids) {
+ $files = id(new PhabricatorFileQuery())
+ ->setViewer($viewer)
+ ->setParentQuery($this)
+ ->withPHIDs($file_phids)
+ ->execute();
+ $files = mpull($files, null, 'getPHID');
+ } else {
+ $files = array();
+ }
+
+ foreach ($attachments as $key => $attachment) {
+ $file_phid = $attachment->getFilePHID();
+ $file = idx($files, $file_phid);
+
+ $attachment->attachFile($file);
+ }
+ }
+
+ return $attachments;
+ }
+
+ protected function getPrimaryTableAlias() {
+ return 'attachments';
+ }
+
+ public function getQueryApplicationClass() {
+ return 'PhabricatorFilesApplication';
+ }
+
+}
diff --git a/src/applications/files/storage/PhabricatorFileAttachment.php b/src/applications/files/storage/PhabricatorFileAttachment.php
--- a/src/applications/files/storage/PhabricatorFileAttachment.php
+++ b/src/applications/files/storage/PhabricatorFileAttachment.php
@@ -1,13 +1,19 @@
<?php
final class PhabricatorFileAttachment
- extends PhabricatorFileDAO {
+ extends PhabricatorFileDAO
+ implements
+ PhabricatorPolicyInterface,
+ PhabricatorExtendedPolicyInterface {
protected $objectPHID;
protected $filePHID;
protected $attacherPHID;
protected $attachmentMode;
+ private $object = self::ATTACHABLE;
+ private $file = self::ATTACHABLE;
+
const MODE_ATTACH = 'attach';
const MODE_REFERENCE = 'reference';
const MODE_DETACH = 'detach';
@@ -40,4 +46,53 @@
);
}
+ public function attachObject($object) {
+ $this->object = $object;
+ return $this;
+ }
+
+ public function getObject() {
+ return $this->assertAttached($this->object);
+ }
+
+ public function attachFile(PhabricatorFile $file = null) {
+ $this->file = $file;
+ return $this;
+ }
+
+ public function getFile() {
+ return $this->assertAttached($this->file);
+ }
+
+
+/* -( PhabricatorPolicyInterface )----------------------------------------- */
+
+
+ public function getCapabilities() {
+ return array(
+ PhabricatorPolicyCapability::CAN_VIEW,
+ );
+ }
+
+ public function getPolicy($capability) {
+ switch ($capability) {
+ case PhabricatorPolicyCapability::CAN_VIEW:
+ return PhabricatorPolicies::getMostOpenPolicy();
+ }
+ }
+
+ public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
+ return false;
+ }
+
+
+/* -( PhabricatorExtendedPolicyInterface )--------------------------------- */
+
+
+ public function getExtendedPolicy($capability, PhabricatorUser $viewer) {
+ return array(
+ array($this->getObject(), $capability),
+ );
+ }
+
}

File Metadata

Mime Type
text/plain
Expires
Sun, Nov 24, 4:40 PM (15 h, 27 m)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6783483
Default Alt Text
D21835.id52037.diff (6 KB)

Event Timeline