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

Make Flags policy aware

Summary:
Ref T1809. Ref T603. Ref T3599. Makes flags policy aware.

This change reduces the utility of flag search/browse; the next change will switch it to ApplicationSearch to restore utility. Representing all that ordering in terms of cursor paging is also a giant pain.

Test Plan: Viewed Differential, Flags, etc. Grepped for all PhabricatorFlagQuery callsites.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T603, T1809, T3599

Differential Revision: https://secure.phabricator.com/D6746
This commit is contained in:
epriestley 2013-08-13 16:17:42 -07:00
parent 3f9a4d42f7
commit 275f67294c
5 changed files with 83 additions and 206 deletions

View file

@ -3251,7 +3251,11 @@ phutil_register_library_map(array(
'PhabricatorFilesManagementPurgeWorkflow' => 'PhabricatorFilesManagementWorkflow', 'PhabricatorFilesManagementPurgeWorkflow' => 'PhabricatorFilesManagementWorkflow',
'PhabricatorFilesManagementRebuildWorkflow' => 'PhabricatorFilesManagementWorkflow', 'PhabricatorFilesManagementRebuildWorkflow' => 'PhabricatorFilesManagementWorkflow',
'PhabricatorFilesManagementWorkflow' => 'PhutilArgumentWorkflow', 'PhabricatorFilesManagementWorkflow' => 'PhutilArgumentWorkflow',
'PhabricatorFlag' => 'PhabricatorFlagDAO', 'PhabricatorFlag' =>
array(
0 => 'PhabricatorFlagDAO',
1 => 'PhabricatorPolicyInterface',
),
'PhabricatorFlagColor' => 'PhabricatorFlagConstants', 'PhabricatorFlagColor' => 'PhabricatorFlagConstants',
'PhabricatorFlagController' => 'PhabricatorController', 'PhabricatorFlagController' => 'PhabricatorController',
'PhabricatorFlagDAO' => 'PhabricatorLiskDAO', 'PhabricatorFlagDAO' => 'PhabricatorLiskDAO',
@ -3259,6 +3263,7 @@ phutil_register_library_map(array(
'PhabricatorFlagEditController' => 'PhabricatorFlagController', 'PhabricatorFlagEditController' => 'PhabricatorFlagController',
'PhabricatorFlagListController' => 'PhabricatorFlagController', 'PhabricatorFlagListController' => 'PhabricatorFlagController',
'PhabricatorFlagListView' => 'AphrontView', 'PhabricatorFlagListView' => 'AphrontView',
'PhabricatorFlagQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
'PhabricatorFlagsUIEventListener' => 'PhutilEventListener', 'PhabricatorFlagsUIEventListener' => 'PhutilEventListener',
'PhabricatorFormExample' => 'PhabricatorUIExample', 'PhabricatorFormExample' => 'PhabricatorUIExample',
'PhabricatorGarbageCollectorConfigOptions' => 'PhabricatorApplicationConfigOptions', 'PhabricatorGarbageCollectorConfigOptions' => 'PhabricatorApplicationConfigOptions',

View file

@ -19,94 +19,21 @@ final class PhabricatorFlagListController extends PhabricatorFlagController {
->setHref($request->getRequestURI())); ->setHref($request->getRequestURI()));
$nav->setCrumbs($crumbs); $nav->setCrumbs($crumbs);
$filter_form = new AphrontFormView();
$filter_form->setNoShading(true);
$filter_form->setUser($user);
$filter_form->appendChild(
id(new AphrontFormToggleButtonsControl())
->setName('o')
->setLabel(pht('Sort Order'))
->setBaseURI($request->getRequestURI(), 'o')
->setValue($flag_order)
->setButtons(
array(
'n' => pht('Date'),
'c' => pht('Color'),
'o' => pht('Object Type'),
)));
$filter = new AphrontListFilterView();
$filter->appendChild($filter_form);
$nav->appendChild($filter);
$query = new PhabricatorFlagQuery(); $query = new PhabricatorFlagQuery();
$query->withOwnerPHIDs(array($user->getPHID())); $query->withOwnerPHIDs(array($user->getPHID()));
$query->setViewer($user); $query->setViewer($user);
$query->needHandles(true); $query->needHandles(true);
switch ($flag_order) {
// 'r'
// 'a'
case 'n':
$order = PhabricatorFlagQuery::ORDER_ID;
break;
case 'c':
$order = PhabricatorFlagQuery::ORDER_COLOR;
break;
case 'o':
$order = PhabricatorFlagQuery::ORDER_OBJECT;
break;
default:
throw new Exception("Unknown order!");
}
$query->withOrder($order);
$flags = $query->execute(); $flags = $query->execute();
$views = array(); $views = array();
if ($flag_order == 'n') { $view = new PhabricatorFlagListView();
$view = new PhabricatorFlagListView(); $view->setFlags($flags);
$view->setFlags($flags); $view->setUser($user);
$view->setUser($user); $view->setFlush(true);
$view->setFlush(true); $views[] = array(
$views[] = array( 'view' => $view,
'view' => $view, );
);
} else {
switch ($flag_order) {
case 'c':
$flags_tmp = mgroup($flags, 'getColor');
$flags = array();
foreach ($flags_tmp as $color => $flag_group) {
$title = pht('%s Flags',
PhabricatorFlagColor::getColorName($color));
$flags[$title] = $flag_group;
}
break;
case 'o':
$flags_tmp = mgroup($flags, 'getType');
$flags = array();
foreach ($flags_tmp as $color => $flag_group) {
// Appending an 's' to fake plurals
$title = head($flag_group)->getHandle()->getTypeName() . 's';
$flags[$title] = $flag_group;
}
break;
default:
throw new Exception("Unknown order!");
}
foreach ($flags as $group_title => $flag_group) {
$view = new PhabricatorFlagListView();
$view->setFlags($flag_group);
$view->setUser($user);
$view->setFlush(true);
$views[] = array(
'title' => pht('%s (%d)', $group_title, count($flag_group)),
'view' => $view,
);
}
}
foreach ($views as $view) { foreach ($views as $view) {
$panel = new AphrontPanelView(); $panel = new AphrontPanelView();

View file

@ -1,29 +1,15 @@
<?php <?php
final class PhabricatorFlagQuery { final class PhabricatorFlagQuery
extends PhabricatorCursorPagedPolicyAwareQuery {
private $ownerPHIDs; private $ownerPHIDs;
private $types; private $types;
private $objectPHIDs; private $objectPHIDs;
private $color; private $colors;
private $limit;
private $offset;
private $needHandles; private $needHandles;
private $needObjects; private $needObjects;
private $viewer;
private $order = 'order-id';
const ORDER_ID = 'order-id';
const ORDER_COLOR = 'order-color';
const ORDER_OBJECT = 'order-object';
const ORDER_REASON = 'order-reason';
public function setViewer($viewer) {
$this->viewer = $viewer;
return $this;
}
public function withOwnerPHIDs(array $owner_phids) { public function withOwnerPHIDs(array $owner_phids) {
$this->ownerPHIDs = $owner_phids; $this->ownerPHIDs = $owner_phids;
@ -40,13 +26,8 @@ final class PhabricatorFlagQuery {
return $this; return $this;
} }
public function withColor($color) { public function withColors(array $colors) {
$this->color = $color; $this->colors = $colors;
return $this;
}
public function withOrder($order) {
$this->order = $order;
return $this; return $this;
} }
@ -60,67 +41,58 @@ final class PhabricatorFlagQuery {
return $this; return $this;
} }
public function setLimit($limit) {
$this->limit = $limit;
return $this;
}
public function setOffset($offset) {
$this->offset = $offset;
return $this;
}
public static function loadUserFlag(PhabricatorUser $user, $object_phid) { public static function loadUserFlag(PhabricatorUser $user, $object_phid) {
// Specifying the type in the query allows us to use a key. // Specifying the type in the query allows us to use a key.
return id(new PhabricatorFlag())->loadOneWhere( return id(new PhabricatorFlagQuery())
'ownerPHID = %s AND type = %s AND objectPHID = %s', ->setViewer($user)
$user->getPHID(), ->withOwnerPHIDs(array($user->getPHID()))
phid_get_type($object_phid), ->withTypes(array(phid_get_type($object_phid)))
$object_phid); ->withObjectPHIDs(array($object_phid))
->executeOne();
} }
public function execute() { public function loadPage() {
$table = new PhabricatorFlag(); $table = new PhabricatorFlag();
$conn_r = $table->establishConnection('r'); $conn_r = $table->establishConnection('r');
$where = $this->buildWhereClause($conn_r);
$limit = $this->buildLimitClause($conn_r);
$order = $this->buildOrderClause($conn_r);
$data = queryfx_all( $data = queryfx_all(
$conn_r, $conn_r,
'SELECT * FROM %T flag %Q %Q %Q', 'SELECT * FROM %T flag %Q %Q %Q',
$table->getTableName(), $table->getTableName(),
$where, $this->buildWhereClause($conn_r),
$order, $this->buildOrderClause($conn_r),
$limit); $this->buildLimitClause($conn_r));
$flags = $table->loadAllFromArray($data); return $table->loadAllFromArray($data);
}
if ($this->needHandles || $this->needObjects) { public function willFilterPage(array $flags) {
$phids = ipull($data, 'objectPHID');
$query = new PhabricatorObjectHandleData($phids);
$query->setViewer($this->viewer);
if ($this->needHandles) { if ($this->needObjects) {
$handles = $query->loadHandles(); $objects = id(new PhabricatorObjectQuery())
foreach ($flags as $flag) { ->setViewer($this->getViewer())
$handle = idx($handles, $flag->getObjectPHID()); ->withPHIDs(mpull($flags, 'getObjectPHID'))
if ($handle) { ->execute();
$flag->attachHandle($handle); $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]);
} }
} }
}
if ($this->needObjects) { if ($this->needHandles) {
$objects = $query->loadObjects(); $handles = id(new PhabricatorHandleQuery())
foreach ($flags as $flag) { ->setViewer($this->getViewer())
$object = idx($objects, $flag->getObjectPHID()); ->withPHIDs(mpull($flags, 'getObjectPHID'))
if ($object) { ->execute();
$flag->attachObject($object);
} foreach ($flags as $flag) {
} $flag->attachHandle($handles[$flag->getObjectPHID()]);
} }
} }
@ -128,7 +100,6 @@ final class PhabricatorFlagQuery {
} }
private function buildWhereClause($conn_r) { private function buildWhereClause($conn_r) {
$where = array(); $where = array();
if ($this->ownerPHIDs) { if ($this->ownerPHIDs) {
@ -152,56 +123,16 @@ final class PhabricatorFlagQuery {
$this->objectPHIDs); $this->objectPHIDs);
} }
if (strlen($this->color)) { if ($this->colors) {
$where[] = qsprintf( $where[] = qsprintf(
$conn_r, $conn_r,
'flag.color = %d', 'flag.color IN (%Ld)',
$this->color); $this->colors);
} }
if ($where) { $where[] = $this->buildPagingClause($conn_r);
return 'WHERE ('.implode(') AND (', $where).')';
} else {
return '';
}
}
private function buildOrderClause($conn_r) { return $this->formatWhereClause($where);
return qsprintf($conn_r,
'ORDER BY %Q',
$this->getOrderColumn($conn_r));
}
private function getOrderColumn($conn_r) {
switch ($this->order) {
case self::ORDER_ID:
return 'id DESC';
break;
case self::ORDER_COLOR:
return 'color ASC';
break;
case self::ORDER_OBJECT:
return 'type DESC';
break;
case self::ORDER_REASON:
return 'reasonPHID DESC';
break;
default:
throw new Exception("Unknown order {$this->order}!");
break;
}
}
private function buildLimitClause($conn_r) {
if ($this->limit && $this->offset) {
return qsprintf($conn_r, 'LIMIT %d, %d', $this->offset, $this->limit);
} else if ($this->limit) {
return qsprintf($conn_r, 'LIMIT %d', $this->limit);
} else if ($this->offset) {
return qsprintf($conn_r, 'LIMIT %d, %d', $this->offset, PHP_INT_MAX);
} else {
return '';
}
} }
} }

View file

@ -1,6 +1,7 @@
<?php <?php
final class PhabricatorFlag extends PhabricatorFlagDAO { final class PhabricatorFlag extends PhabricatorFlagDAO
implements PhabricatorPolicyInterface {
protected $ownerPHID; protected $ownerPHID;
protected $type; protected $type;
@ -9,14 +10,11 @@ final class PhabricatorFlag extends PhabricatorFlagDAO {
protected $color = PhabricatorFlagColor::COLOR_BLUE; protected $color = PhabricatorFlagColor::COLOR_BLUE;
protected $note; protected $note;
private $handle = false; private $handle = self::ATTACHABLE;
private $object = false; private $object = self::ATTACHABLE;
public function getObject() { public function getObject() {
if ($this->object === false) { return $this->assertAttached($this->object);
throw new Exception('Call attachObject() before getObject()!');
}
return $this->object;
} }
public function attachObject($object) { public function attachObject($object) {
@ -25,10 +23,7 @@ final class PhabricatorFlag extends PhabricatorFlagDAO {
} }
public function getHandle() { public function getHandle() {
if ($this->handle === false) { return $this->assertAttached($this->handle);
throw new Exception('Call attachHandle() before getHandle()!');
}
return $this->handle;
} }
public function attachHandle(PhabricatorObjectHandle $handle) { public function attachHandle(PhabricatorObjectHandle $handle) {
@ -36,4 +31,23 @@ final class PhabricatorFlag extends PhabricatorFlagDAO {
return $this; return $this;
} }
/* -( PhabricatorPolicyInterface )----------------------------------------- */
public function getCapabilities() {
return array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
);
}
public function getPolicy($capability) {
return PhabricatorPolicies::POLICY_NOONE;
}
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
return ($viewer->getPHID() == $this->getOwnerPHID());
}
} }

View file

@ -172,7 +172,7 @@ final class PhabricatorMacroQuery
$flags = id(new PhabricatorFlagQuery()) $flags = id(new PhabricatorFlagQuery())
->withOwnerPHIDs(array($this->getViewer()->getPHID())) ->withOwnerPHIDs(array($this->getViewer()->getPHID()))
->withTypes(array(PhabricatorMacroPHIDTypeMacro::TYPECONST)) ->withTypes(array(PhabricatorMacroPHIDTypeMacro::TYPECONST))
->withColor($this->flagColor) ->withColors(array($this->flagColor))
->setViewer($this->getViewer()) ->setViewer($this->getViewer())
->execute(); ->execute();