2013-07-03 20:15:45 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class LegalpadDocumentQuery
|
|
|
|
extends PhabricatorCursorPagedPolicyAwareQuery {
|
|
|
|
|
|
|
|
private $ids;
|
|
|
|
private $phids;
|
|
|
|
private $creatorPHIDs;
|
2013-07-08 22:37:36 +02:00
|
|
|
private $contributorPHIDs;
|
2014-01-16 01:48:44 +01:00
|
|
|
private $signerPHIDs;
|
2013-07-03 20:15:45 +02:00
|
|
|
private $dateCreatedAfter;
|
|
|
|
private $dateCreatedBefore;
|
|
|
|
|
|
|
|
private $needDocumentBodies;
|
|
|
|
private $needContributors;
|
2014-01-16 23:36:57 +01:00
|
|
|
private $needSignatures;
|
2013-07-03 20:15:45 +02:00
|
|
|
|
|
|
|
public function withIDs(array $ids) {
|
|
|
|
$this->ids = $ids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withPHIDs(array $phids) {
|
|
|
|
$this->phids = $phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withCreatorPHIDs(array $phids) {
|
|
|
|
$this->creatorPHIDs = $phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withContributorPHIDs(array $phids) {
|
|
|
|
$this->contributorPHIDs = $phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-01-16 01:48:44 +01:00
|
|
|
public function withSignerPHIDs(array $phids) {
|
|
|
|
$this->signerPHIDs = $phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-07-03 20:15:45 +02:00
|
|
|
public function needDocumentBodies($need_bodies) {
|
|
|
|
$this->needDocumentBodies = $need_bodies;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function needContributors($need_contributors) {
|
|
|
|
$this->needContributors = $need_contributors;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-01-16 23:36:57 +01:00
|
|
|
public function needSignatures($need_signatures) {
|
|
|
|
$this->needSignatures = $need_signatures;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-07-03 20:15:45 +02:00
|
|
|
public function withDateCreatedBefore($date_created_before) {
|
|
|
|
$this->dateCreatedBefore = $date_created_before;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withDateCreatedAfter($date_created_after) {
|
|
|
|
$this->dateCreatedAfter = $date_created_after;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function loadPage() {
|
|
|
|
$table = new LegalpadDocument();
|
|
|
|
$conn_r = $table->establishConnection('r');
|
|
|
|
|
|
|
|
$data = queryfx_all(
|
|
|
|
$conn_r,
|
2013-07-08 22:37:36 +02:00
|
|
|
'SELECT d.* FROM %T d %Q %Q %Q %Q',
|
2013-07-03 20:15:45 +02:00
|
|
|
$table->getTableName(),
|
2013-07-08 22:37:36 +02:00
|
|
|
$this->buildJoinClause($conn_r),
|
2013-07-03 20:15:45 +02:00
|
|
|
$this->buildWhereClause($conn_r),
|
|
|
|
$this->buildOrderClause($conn_r),
|
|
|
|
$this->buildLimitClause($conn_r));
|
|
|
|
|
|
|
|
$documents = $table->loadAllFromArray($data);
|
|
|
|
|
|
|
|
return $documents;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function willFilterPage(array $documents) {
|
2014-01-16 01:48:44 +01:00
|
|
|
if ($this->signerPHIDs) {
|
|
|
|
$document_map = mpull($documents, null, 'getPHID');
|
2014-01-17 20:40:26 +01:00
|
|
|
$signatures = id(new LegalpadDocumentSignatureQuery())
|
|
|
|
->setViewer($this->getViewer())
|
|
|
|
->withDocumentPHIDs(array_keys($document_map))
|
2014-01-17 20:49:18 +01:00
|
|
|
->withSignerPHIDs($this->signerPHIDs)
|
2014-01-17 20:40:26 +01:00
|
|
|
->execute();
|
2014-01-16 01:48:44 +01:00
|
|
|
$signatures = mgroup($signatures, 'getDocumentPHID');
|
|
|
|
foreach ($document_map as $document_phid => $document) {
|
|
|
|
$sigs = idx($signatures, $document_phid, array());
|
|
|
|
foreach ($sigs as $index => $sig) {
|
|
|
|
if ($sig->getDocumentVersion() != $document->getVersions()) {
|
|
|
|
unset($sigs[$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$signer_phids = mpull($sigs, 'getSignerPHID');
|
|
|
|
if (array_diff($this->signerPHIDs, $signer_phids)) {
|
|
|
|
unset($documents[$document->getID()]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-16 23:36:57 +01:00
|
|
|
|
2013-07-03 20:15:45 +02:00
|
|
|
if ($this->needDocumentBodies) {
|
|
|
|
$documents = $this->loadDocumentBodies($documents);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->needContributors) {
|
|
|
|
$documents = $this->loadContributors($documents);
|
|
|
|
}
|
|
|
|
|
2014-01-16 23:36:57 +01:00
|
|
|
if ($this->needSignatures) {
|
|
|
|
$documents = $this->loadSignatures($documents);
|
|
|
|
}
|
|
|
|
|
2013-07-03 20:15:45 +02:00
|
|
|
return $documents;
|
|
|
|
}
|
|
|
|
|
2013-07-08 22:37:36 +02:00
|
|
|
private function buildJoinClause($conn_r) {
|
|
|
|
$joins = array();
|
|
|
|
|
|
|
|
if ($this->contributorPHIDs) {
|
|
|
|
$joins[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'JOIN edge e ON e.src = d.phid');
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode(' ', $joins);
|
|
|
|
}
|
|
|
|
|
2013-07-03 20:15:45 +02:00
|
|
|
protected function buildWhereClause($conn_r) {
|
|
|
|
$where = array();
|
|
|
|
|
|
|
|
$where[] = $this->buildPagingClause($conn_r);
|
|
|
|
|
|
|
|
if ($this->ids) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
2013-07-08 22:37:36 +02:00
|
|
|
'd.id IN (%Ld)',
|
2013-07-03 20:15:45 +02:00
|
|
|
$this->ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->phids) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
2013-07-08 22:37:36 +02:00
|
|
|
'd.phid IN (%Ls)',
|
2013-07-03 20:15:45 +02:00
|
|
|
$this->phids);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->creatorPHIDs) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
2013-07-08 22:37:36 +02:00
|
|
|
'd.creatorPHID IN (%Ls)',
|
2013-07-03 20:15:45 +02:00
|
|
|
$this->creatorPHIDs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->dateCreatedAfter) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
2013-07-08 22:37:36 +02:00
|
|
|
'd.dateCreated >= %d',
|
2013-07-03 20:15:45 +02:00
|
|
|
$this->dateCreatedAfter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->dateCreatedBefore) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
2013-07-08 22:37:36 +02:00
|
|
|
'd.dateCreated <= %d',
|
2013-07-03 20:15:45 +02:00
|
|
|
$this->dateCreatedBefore);
|
|
|
|
}
|
|
|
|
|
2013-07-08 22:37:36 +02:00
|
|
|
if ($this->contributorPHIDs) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'e.type = %s AND e.dst IN (%Ls)',
|
|
|
|
PhabricatorEdgeConfig::TYPE_OBJECT_HAS_CONTRIBUTOR,
|
|
|
|
$this->contributorPHIDs);
|
|
|
|
}
|
|
|
|
|
2013-07-03 20:15:45 +02:00
|
|
|
return $this->formatWhereClause($where);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function loadDocumentBodies(array $documents) {
|
|
|
|
$body_phids = mpull($documents, 'getDocumentBodyPHID');
|
|
|
|
$bodies = id(new LegalpadDocumentBody())->loadAllWhere(
|
|
|
|
'phid IN (%Ls)',
|
|
|
|
$body_phids);
|
|
|
|
$bodies = mpull($bodies, null, 'getPHID');
|
|
|
|
|
|
|
|
foreach ($documents as $document) {
|
|
|
|
$body = idx($bodies, $document->getDocumentBodyPHID());
|
|
|
|
$document->attachDocumentBody($body);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $documents;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function loadContributors(array $documents) {
|
|
|
|
$document_map = mpull($documents, null, 'getPHID');
|
|
|
|
$edge_type = PhabricatorEdgeConfig::TYPE_OBJECT_HAS_CONTRIBUTOR;
|
|
|
|
$contributor_data = id(new PhabricatorEdgeQuery())
|
|
|
|
->withSourcePHIDs(array_keys($document_map))
|
|
|
|
->withEdgeTypes(array($edge_type))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
foreach ($document_map as $document_phid => $document) {
|
|
|
|
$data = $contributor_data[$document_phid];
|
|
|
|
$contributors = array_keys(idx($data, $edge_type, array()));
|
|
|
|
$document->attachContributors($contributors);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $documents;
|
|
|
|
}
|
|
|
|
|
2014-01-16 23:36:57 +01:00
|
|
|
private function loadSignatures(array $documents) {
|
|
|
|
$document_map = mpull($documents, null, 'getPHID');
|
|
|
|
|
2014-01-17 20:40:26 +01:00
|
|
|
$signatures = id(new LegalpadDocumentSignatureQuery())
|
|
|
|
->setViewer($this->getViewer())
|
|
|
|
->withDocumentPHIDs(array_keys($document_map))
|
|
|
|
->execute();
|
2014-01-16 23:36:57 +01:00
|
|
|
$signatures = mgroup($signatures, 'getDocumentPHID');
|
|
|
|
|
|
|
|
foreach ($documents as $document) {
|
2014-01-17 23:05:18 +01:00
|
|
|
$sigs = idx($signatures, $document->getPHID(), array());
|
2014-01-16 23:36:57 +01:00
|
|
|
foreach ($sigs as $index => $sig) {
|
|
|
|
if ($sig->getDocumentVersion() != $document->getVersions()) {
|
|
|
|
unset($sigs[$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$document->attachSignatures($sigs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $documents;
|
|
|
|
}
|
|
|
|
|
Lock policy queries to their applications
Summary:
While we mostly have reasonable effective object accessibility when you lock a user out of an application, it's primarily enforced at the controller level. Users can still, e.g., load the handles of objects they can't actually see. Instead, lock the queries to the applications so that you can, e.g., never load a revision if you don't have access to Differential.
This has several parts:
- For PolicyAware queries, provide an application class name method.
- If the query specifies a class name and the user doesn't have permission to use it, fail the entire query unconditionally.
- For handles, simplify query construction and count all the PHIDs as "restricted" so we get a UI full of "restricted" instead of "unknown" handles.
Test Plan:
- Added a unit test to verify I got all the class names right.
- Browsed around, logged in/out as a normal user with public policies on and off.
- Browsed around, logged in/out as a restricted user with public policies on and off. With restrictions, saw all traces of restricted apps removed or restricted.
Reviewers: btrahan
Reviewed By: btrahan
CC: aran
Differential Revision: https://secure.phabricator.com/D7367
2013-10-22 02:20:27 +02:00
|
|
|
public function getQueryApplicationClass() {
|
|
|
|
return 'PhabricatorApplicationLegalpad';
|
|
|
|
}
|
2013-07-03 20:15:45 +02:00
|
|
|
|
|
|
|
}
|