2012-03-28 01:22:40 +02:00
|
|
|
<?php
|
|
|
|
|
2013-08-14 01:17:42 +02:00
|
|
|
final class PhabricatorFlagQuery
|
|
|
|
extends PhabricatorCursorPagedPolicyAwareQuery {
|
2012-03-28 01:22:40 +02:00
|
|
|
|
2013-10-02 00:06:35 +02:00
|
|
|
const GROUP_COLOR = 'color';
|
|
|
|
const GROUP_NONE = 'none';
|
|
|
|
|
2012-03-28 01:22:40 +02:00
|
|
|
private $ownerPHIDs;
|
|
|
|
private $types;
|
|
|
|
private $objectPHIDs;
|
2013-08-14 01:17:42 +02:00
|
|
|
private $colors;
|
2013-10-02 00:06:35 +02:00
|
|
|
private $groupBy = self::GROUP_NONE;
|
2012-03-28 01:22:40 +02:00
|
|
|
|
|
|
|
private $needHandles;
|
|
|
|
private $needObjects;
|
|
|
|
|
|
|
|
public function withOwnerPHIDs(array $owner_phids) {
|
|
|
|
$this->ownerPHIDs = $owner_phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withTypes(array $types) {
|
|
|
|
$this->types = $types;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withObjectPHIDs(array $object_phids) {
|
|
|
|
$this->objectPHIDs = $object_phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-08-14 01:17:42 +02:00
|
|
|
public function withColors(array $colors) {
|
|
|
|
$this->colors = $colors;
|
2013-03-26 22:05:45 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-10-02 00:06:35 +02:00
|
|
|
/**
|
|
|
|
* Note this is done in php and not in mySQL, which means its inappropriate
|
|
|
|
* for large datasets. Pragmatically, this is fine for user flags which are
|
|
|
|
* typically well under 100 flags per user.
|
|
|
|
*/
|
|
|
|
public function setGroupBy($group) {
|
|
|
|
$this->groupBy = $group;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-03-28 01:22:40 +02:00
|
|
|
public function needHandles($need) {
|
|
|
|
$this->needHandles = $need;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function needObjects($need) {
|
|
|
|
$this->needObjects = $need;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function loadUserFlag(PhabricatorUser $user, $object_phid) {
|
|
|
|
// Specifying the type in the query allows us to use a key.
|
2013-08-14 01:17:42 +02:00
|
|
|
return id(new PhabricatorFlagQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->withOwnerPHIDs(array($user->getPHID()))
|
|
|
|
->withTypes(array(phid_get_type($object_phid)))
|
|
|
|
->withObjectPHIDs(array($object_phid))
|
|
|
|
->executeOne();
|
2012-03-28 01:22:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-14 01:17:42 +02:00
|
|
|
public function loadPage() {
|
2012-03-28 01:22:40 +02:00
|
|
|
$table = new PhabricatorFlag();
|
|
|
|
$conn_r = $table->establishConnection('r');
|
|
|
|
|
|
|
|
$data = queryfx_all(
|
|
|
|
$conn_r,
|
|
|
|
'SELECT * FROM %T flag %Q %Q %Q',
|
|
|
|
$table->getTableName(),
|
2013-08-14 01:17:42 +02:00
|
|
|
$this->buildWhereClause($conn_r),
|
|
|
|
$this->buildOrderClause($conn_r),
|
|
|
|
$this->buildLimitClause($conn_r));
|
|
|
|
|
|
|
|
return $table->loadAllFromArray($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function willFilterPage(array $flags) {
|
|
|
|
|
|
|
|
if ($this->needObjects) {
|
|
|
|
$objects = id(new PhabricatorObjectQuery())
|
|
|
|
->setViewer($this->getViewer())
|
|
|
|
->withPHIDs(mpull($flags, 'getObjectPHID'))
|
|
|
|
->execute();
|
|
|
|
$objects = mpull($objects, null, 'getPHID');
|
|
|
|
foreach ($flags as $key => $flag) {
|
|
|
|
$object = idx($objects, $flag->getObjectPHID());
|
|
|
|
if ($object) {
|
|
|
|
$flags[$key]->attachObject($object);
|
|
|
|
} else {
|
|
|
|
unset($flags[$key]);
|
2012-03-28 01:22:40 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-14 01:17:42 +02:00
|
|
|
}
|
2012-03-28 01:22:40 +02:00
|
|
|
|
2013-08-14 01:17:42 +02:00
|
|
|
if ($this->needHandles) {
|
|
|
|
$handles = id(new PhabricatorHandleQuery())
|
|
|
|
->setViewer($this->getViewer())
|
|
|
|
->withPHIDs(mpull($flags, 'getObjectPHID'))
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
foreach ($flags as $flag) {
|
|
|
|
$flag->attachHandle($handles[$flag->getObjectPHID()]);
|
2012-03-28 01:22:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 00:06:35 +02:00
|
|
|
switch ($this->groupBy) {
|
|
|
|
case self::GROUP_COLOR:
|
|
|
|
$flags = msort($flags, 'getColor');
|
|
|
|
break;
|
|
|
|
case self::GROUP_NONE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception("Unknown groupBy parameter: $this->groupBy");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-03-28 01:22:40 +02:00
|
|
|
return $flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildWhereClause($conn_r) {
|
|
|
|
$where = array();
|
|
|
|
|
|
|
|
if ($this->ownerPHIDs) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'flag.ownerPHID IN (%Ls)',
|
|
|
|
$this->ownerPHIDs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->types) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'flag.type IN (%Ls)',
|
|
|
|
$this->types);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->objectPHIDs) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'flag.objectPHID IN (%Ls)',
|
|
|
|
$this->objectPHIDs);
|
|
|
|
}
|
|
|
|
|
2013-08-14 01:17:42 +02:00
|
|
|
if ($this->colors) {
|
2013-08-09 03:54:35 +02:00
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
2013-08-14 01:17:42 +02:00
|
|
|
'flag.color IN (%Ld)',
|
|
|
|
$this->colors);
|
2013-08-09 03:54:35 +02:00
|
|
|
}
|
|
|
|
|
2013-08-14 01:17:42 +02:00
|
|
|
$where[] = $this->buildPagingClause($conn_r);
|
2012-03-28 01:22:40 +02:00
|
|
|
|
2013-08-14 01:17:42 +02:00
|
|
|
return $this->formatWhereClause($where);
|
2012-03-28 01:22:40 +02:00
|
|
|
}
|
|
|
|
|
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 'PhabricatorApplicationFlags';
|
|
|
|
}
|
|
|
|
|
2012-03-28 01:22:40 +02:00
|
|
|
}
|