2013-03-08 16:12:24 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhrictionDocumentQuery
|
|
|
|
extends PhabricatorCursorPagedPolicyAwareQuery {
|
|
|
|
|
|
|
|
private $ids;
|
|
|
|
private $phids;
|
2013-07-24 20:32:13 +02:00
|
|
|
private $slugs;
|
2014-11-11 22:18:37 +01:00
|
|
|
private $depths;
|
|
|
|
private $slugPrefix;
|
|
|
|
private $statuses;
|
|
|
|
|
2014-05-19 21:41:12 +02:00
|
|
|
private $needContent;
|
|
|
|
|
2013-03-08 16:12:24 +01:00
|
|
|
private $status = 'status-any';
|
|
|
|
const STATUS_ANY = 'status-any';
|
|
|
|
const STATUS_OPEN = 'status-open';
|
|
|
|
const STATUS_NONSTUB = 'status-nonstub';
|
|
|
|
|
|
|
|
private $order = 'order-created';
|
|
|
|
const ORDER_CREATED = 'order-created';
|
|
|
|
const ORDER_UPDATED = 'order-updated';
|
2014-11-11 22:18:37 +01:00
|
|
|
const ORDER_HIERARCHY = 'order-hierarchy';
|
2013-03-08 16:12:24 +01:00
|
|
|
|
|
|
|
public function withIDs(array $ids) {
|
|
|
|
$this->ids = $ids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withPHIDs(array $phids) {
|
|
|
|
$this->phids = $phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-07-24 20:32:13 +02:00
|
|
|
public function withSlugs(array $slugs) {
|
|
|
|
$this->slugs = $slugs;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-11-11 22:18:37 +01:00
|
|
|
public function withDepths(array $depths) {
|
|
|
|
$this->depths = $depths;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withSlugPrefix($slug_prefix) {
|
|
|
|
$this->slugPrefix = $slug_prefix;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withStatuses(array $statuses) {
|
|
|
|
$this->statuses = $statuses;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-03-08 16:12:24 +01:00
|
|
|
public function withStatus($status) {
|
|
|
|
$this->status = $status;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-05-19 21:41:12 +02:00
|
|
|
public function needContent($need_content) {
|
|
|
|
$this->needContent = $need_content;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-03-08 16:12:24 +01:00
|
|
|
public function setOrder($order) {
|
|
|
|
$this->order = $order;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function loadPage() {
|
2014-05-19 21:41:30 +02:00
|
|
|
$table = new PhrictionDocument();
|
|
|
|
$conn_r = $table->establishConnection('r');
|
2013-03-08 16:12:24 +01:00
|
|
|
|
2014-11-12 03:47:22 +01:00
|
|
|
if ($this->order == self::ORDER_HIERARCHY) {
|
2014-11-11 22:18:37 +01:00
|
|
|
$order_clause = $this->buildHierarchicalOrderClause($conn_r);
|
|
|
|
} else {
|
|
|
|
$order_clause = $this->buildOrderClause($conn_r);
|
|
|
|
}
|
|
|
|
|
2013-03-08 16:12:24 +01:00
|
|
|
$rows = queryfx_all(
|
|
|
|
$conn_r,
|
2014-11-12 03:47:22 +01:00
|
|
|
'SELECT d.* FROM %T d %Q %Q %Q %Q',
|
2014-05-19 21:41:30 +02:00
|
|
|
$table->getTableName(),
|
2014-11-11 22:18:37 +01:00
|
|
|
$this->buildJoinClause($conn_r),
|
2013-03-08 16:12:24 +01:00
|
|
|
$this->buildWhereClause($conn_r),
|
2014-11-11 22:18:37 +01:00
|
|
|
$order_clause,
|
2013-03-08 16:12:24 +01:00
|
|
|
$this->buildLimitClause($conn_r));
|
|
|
|
|
2014-05-19 21:41:30 +02:00
|
|
|
$documents = $table->loadAllFromArray($rows);
|
|
|
|
|
|
|
|
if ($documents) {
|
|
|
|
$ancestor_slugs = array();
|
|
|
|
foreach ($documents as $key => $document) {
|
|
|
|
$document_slug = $document->getSlug();
|
|
|
|
foreach (PhabricatorSlug::getAncestry($document_slug) as $ancestor) {
|
|
|
|
$ancestor_slugs[$ancestor][] = $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($ancestor_slugs) {
|
|
|
|
$ancestors = queryfx_all(
|
|
|
|
$conn_r,
|
|
|
|
'SELECT * FROM %T WHERE slug IN (%Ls)',
|
|
|
|
$document->getTableName(),
|
|
|
|
array_keys($ancestor_slugs));
|
|
|
|
$ancestors = $table->loadAllFromArray($ancestors);
|
|
|
|
$ancestors = mpull($ancestors, null, 'getSlug');
|
|
|
|
|
|
|
|
foreach ($ancestor_slugs as $ancestor_slug => $document_keys) {
|
|
|
|
$ancestor = idx($ancestors, $ancestor_slug);
|
|
|
|
foreach ($document_keys as $document_key) {
|
|
|
|
$documents[$document_key]->attachAncestor(
|
|
|
|
$ancestor_slug,
|
|
|
|
$ancestor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $documents;
|
2013-03-08 16:12:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function willFilterPage(array $documents) {
|
2014-05-19 21:41:30 +02:00
|
|
|
// To view a Phriction document, you must also be able to view all of the
|
|
|
|
// ancestor documents. Filter out documents which have ancestors that are
|
|
|
|
// not visible.
|
|
|
|
|
|
|
|
$document_map = array();
|
|
|
|
foreach ($documents as $document) {
|
|
|
|
$document_map[$document->getSlug()] = $document;
|
|
|
|
foreach ($document->getAncestors() as $key => $ancestor) {
|
|
|
|
if ($ancestor) {
|
|
|
|
$document_map[$key] = $ancestor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$filtered_map = $this->applyPolicyFilter(
|
|
|
|
$document_map,
|
|
|
|
array(PhabricatorPolicyCapability::CAN_VIEW));
|
|
|
|
|
|
|
|
// Filter all of the documents where a parent is not visible.
|
|
|
|
foreach ($documents as $document_key => $document) {
|
|
|
|
// If the document itself is not visible, filter it.
|
|
|
|
if (!isset($filtered_map[$document->getSlug()])) {
|
|
|
|
$this->didRejectResult($documents[$document_key]);
|
|
|
|
unset($documents[$document_key]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If an ancestor exists but is not visible, filter the document.
|
|
|
|
foreach ($document->getAncestors() as $ancestor_key => $ancestor) {
|
|
|
|
if (!$ancestor) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($filtered_map[$ancestor_key])) {
|
|
|
|
$this->didRejectResult($documents[$document_key]);
|
|
|
|
unset($documents[$document_key]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$documents) {
|
|
|
|
return $documents;
|
|
|
|
}
|
|
|
|
|
2014-05-19 21:41:12 +02:00
|
|
|
if ($this->needContent) {
|
|
|
|
$contents = id(new PhrictionContent())->loadAllWhere(
|
|
|
|
'id IN (%Ld)',
|
|
|
|
mpull($documents, 'getContentID'));
|
2013-03-08 16:12:24 +01:00
|
|
|
|
2014-05-19 21:41:12 +02:00
|
|
|
foreach ($documents as $key => $document) {
|
|
|
|
$content_id = $document->getContentID();
|
|
|
|
if (empty($contents[$content_id])) {
|
|
|
|
unset($documents[$key]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$document->attachContent($contents[$content_id]);
|
2013-03-08 16:12:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $documents;
|
|
|
|
}
|
|
|
|
|
2014-11-11 22:18:37 +01:00
|
|
|
private function buildJoinClause(AphrontDatabaseConnection $conn) {
|
2014-11-12 03:47:22 +01:00
|
|
|
$join = '';
|
|
|
|
if ($this->order == self::ORDER_HIERARCHY) {
|
2014-11-11 22:18:37 +01:00
|
|
|
$content_dao = new PhrictionContent();
|
|
|
|
$join = qsprintf(
|
|
|
|
$conn,
|
|
|
|
'JOIN %T c ON d.contentID = c.id',
|
|
|
|
$content_dao->getTableName());
|
|
|
|
}
|
|
|
|
return $join;
|
|
|
|
}
|
2013-03-08 16:12:24 +01:00
|
|
|
protected function buildWhereClause(AphrontDatabaseConnection $conn) {
|
|
|
|
$where = array();
|
|
|
|
|
|
|
|
if ($this->ids) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn,
|
2014-11-11 22:18:37 +01:00
|
|
|
'd.id IN (%Ld)',
|
2013-03-08 16:12:24 +01:00
|
|
|
$this->ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->phids) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn,
|
2014-11-11 22:18:37 +01:00
|
|
|
'd.phid IN (%Ls)',
|
2013-03-08 16:12:24 +01:00
|
|
|
$this->phids);
|
|
|
|
}
|
|
|
|
|
2013-07-24 20:32:13 +02:00
|
|
|
if ($this->slugs) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn,
|
2014-11-11 22:18:37 +01:00
|
|
|
'd.slug IN (%Ls)',
|
2013-07-24 20:32:13 +02:00
|
|
|
$this->slugs);
|
|
|
|
}
|
|
|
|
|
2014-11-11 22:18:37 +01:00
|
|
|
if ($this->statuses) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn,
|
|
|
|
'd.status IN (%Ld)',
|
|
|
|
$this->statuses);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->slugPrefix) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn,
|
|
|
|
'd.slug LIKE %>',
|
|
|
|
$this->slugPrefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->depths) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn,
|
|
|
|
'd.depth IN (%Ld)',
|
|
|
|
$this->depths);
|
|
|
|
}
|
|
|
|
|
2013-03-08 16:12:24 +01:00
|
|
|
switch ($this->status) {
|
|
|
|
case self::STATUS_OPEN:
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn,
|
2014-11-11 22:18:37 +01:00
|
|
|
'd.status NOT IN (%Ld)',
|
2013-03-08 16:12:24 +01:00
|
|
|
array(
|
|
|
|
PhrictionDocumentStatus::STATUS_DELETED,
|
|
|
|
PhrictionDocumentStatus::STATUS_MOVED,
|
|
|
|
PhrictionDocumentStatus::STATUS_STUB,
|
|
|
|
));
|
|
|
|
break;
|
|
|
|
case self::STATUS_NONSTUB:
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn,
|
2014-11-11 22:18:37 +01:00
|
|
|
'd.status NOT IN (%Ld)',
|
2013-03-08 16:12:24 +01:00
|
|
|
array(
|
2013-03-22 18:28:13 +01:00
|
|
|
PhrictionDocumentStatus::STATUS_MOVED,
|
2013-03-08 16:12:24 +01:00
|
|
|
PhrictionDocumentStatus::STATUS_STUB,
|
|
|
|
));
|
|
|
|
break;
|
|
|
|
case self::STATUS_ANY:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception("Unknown status '{$this->status}'!");
|
|
|
|
}
|
|
|
|
|
|
|
|
$where[] = $this->buildPagingClause($conn);
|
|
|
|
|
|
|
|
return $this->formatWhereClause($where);
|
|
|
|
}
|
|
|
|
|
2014-11-11 22:18:37 +01:00
|
|
|
private function buildHierarchicalOrderClause(
|
|
|
|
AphrontDatabaseConnection $conn_r) {
|
|
|
|
|
|
|
|
if ($this->getBeforeID()) {
|
|
|
|
return qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'ORDER BY d.depth, c.title, %Q %Q',
|
|
|
|
$this->getPagingColumn(),
|
|
|
|
$this->getReversePaging() ? 'DESC' : 'ASC');
|
|
|
|
} else {
|
|
|
|
return qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'ORDER BY d.depth, c.title, %Q %Q',
|
|
|
|
$this->getPagingColumn(),
|
|
|
|
$this->getReversePaging() ? 'ASC' : 'DESC');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-08 16:12:24 +01:00
|
|
|
protected function getPagingColumn() {
|
|
|
|
switch ($this->order) {
|
|
|
|
case self::ORDER_CREATED:
|
2014-11-11 22:18:37 +01:00
|
|
|
case self::ORDER_HIERARCHY:
|
|
|
|
return 'd.id';
|
2013-03-08 16:12:24 +01:00
|
|
|
case self::ORDER_UPDATED:
|
2014-11-11 22:18:37 +01:00
|
|
|
return 'd.contentID';
|
2013-03-08 16:12:24 +01:00
|
|
|
default:
|
|
|
|
throw new Exception("Unknown order '{$this->order}'!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getPagingValue($result) {
|
|
|
|
switch ($this->order) {
|
|
|
|
case self::ORDER_CREATED:
|
2014-11-11 22:18:37 +01:00
|
|
|
case self::ORDER_HIERARCHY:
|
2013-03-08 16:12:24 +01:00
|
|
|
return $result->getID();
|
|
|
|
case self::ORDER_UPDATED:
|
|
|
|
return $result->getContentID();
|
|
|
|
default:
|
|
|
|
throw new Exception("Unknown order '{$this->order}'!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
2014-07-23 02:03:09 +02:00
|
|
|
return 'PhabricatorPhrictionApplication';
|
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
|
|
|
}
|
|
|
|
|
2013-03-08 16:12:24 +01:00
|
|
|
}
|