1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-21 04:50:55 +01:00

Introduce ReleephProjectQuery

Summary:
Adds a policy-aware query class for selecting Releeph projects. This doesn't really change anything.

  - Make `ReleephProject` implment `PhabricatorPolicyInterface`, beginning the long journey to make it policy-aware.
  - Implement `ReleephProjectQuery`, for querying projects using cursor-based, policy-aware paging.
  - Use it on the list view, so we load only ~100 projects instead of all of them.
  - Tweaked some of the URI routing stuff to make it a little more consistent with common practices.

Ref T2714.

Test Plan:
{F36434}
{F36435}

Reviewers: edward

Reviewed By: edward

CC: aran

Maniphest Tasks: T2714

Differential Revision: https://secure.phabricator.com/D5390
This commit is contained in:
epriestley 2013-03-22 06:29:37 -07:00
parent 0c2147336c
commit ae1f0e3b1b
5 changed files with 142 additions and 15 deletions

View file

@ -1631,6 +1631,7 @@ phutil_register_library_map(array(
'ReleephProjectCreateController' => 'applications/releeph/controller/project/ReleephProjectCreateController.php', 'ReleephProjectCreateController' => 'applications/releeph/controller/project/ReleephProjectCreateController.php',
'ReleephProjectEditController' => 'applications/releeph/controller/project/ReleephProjectEditController.php', 'ReleephProjectEditController' => 'applications/releeph/controller/project/ReleephProjectEditController.php',
'ReleephProjectListController' => 'applications/releeph/controller/project/ReleephProjectListController.php', 'ReleephProjectListController' => 'applications/releeph/controller/project/ReleephProjectListController.php',
'ReleephProjectQuery' => 'applications/releeph/query/ReleephProjectQuery.php',
'ReleephProjectView' => 'applications/releeph/view/ReleephProjectView.php', 'ReleephProjectView' => 'applications/releeph/view/ReleephProjectView.php',
'ReleephProjectViewController' => 'applications/releeph/controller/project/ReleephProjectViewController.php', 'ReleephProjectViewController' => 'applications/releeph/controller/project/ReleephProjectViewController.php',
'ReleephReasonFieldSpecification' => 'applications/releeph/field/specification/ReleephReasonFieldSpecification.php', 'ReleephReasonFieldSpecification' => 'applications/releeph/field/specification/ReleephReasonFieldSpecification.php',
@ -3268,11 +3269,16 @@ phutil_register_library_map(array(
'ReleephLevelFieldSpecification' => 'ReleephFieldSpecification', 'ReleephLevelFieldSpecification' => 'ReleephFieldSpecification',
'ReleephObjectHandleLoader' => 'ObjectHandleLoader', 'ReleephObjectHandleLoader' => 'ObjectHandleLoader',
'ReleephOriginalCommitFieldSpecification' => 'ReleephFieldSpecification', 'ReleephOriginalCommitFieldSpecification' => 'ReleephFieldSpecification',
'ReleephProject' => 'ReleephDAO', 'ReleephProject' =>
array(
0 => 'ReleephDAO',
1 => 'PhabricatorPolicyInterface',
),
'ReleephProjectActionController' => 'ReleephController', 'ReleephProjectActionController' => 'ReleephController',
'ReleephProjectCreateController' => 'ReleephController', 'ReleephProjectCreateController' => 'ReleephController',
'ReleephProjectEditController' => 'ReleephController', 'ReleephProjectEditController' => 'ReleephController',
'ReleephProjectListController' => 'PhabricatorController', 'ReleephProjectListController' => 'PhabricatorController',
'ReleephProjectQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
'ReleephProjectView' => 'AphrontView', 'ReleephProjectView' => 'AphrontView',
'ReleephProjectViewController' => 'ReleephController', 'ReleephProjectViewController' => 'ReleephController',
'ReleephReasonFieldSpecification' => 'ReleephFieldSpecification', 'ReleephReasonFieldSpecification' => 'ReleephFieldSpecification',

View file

@ -35,8 +35,7 @@ final class PhabricatorApplicationReleeph extends PhabricatorApplication {
'/releeph/' => array( '/releeph/' => array(
'' => 'ReleephProjectListController', '' => 'ReleephProjectListController',
'project/' => array( 'project/' => array(
'' => 'ReleephProjectListController', '(?:(?P<filter>active|inactive)/)?' => 'ReleephProjectListController',
'inactive/' => 'ReleephProjectListController',
'create/' => 'ReleephProjectCreateController', 'create/' => 'ReleephProjectCreateController',
'(?P<projectID>[1-9]\d*)/' => array( '(?P<projectID>[1-9]\d*)/' => array(
'' => 'ReleephProjectViewController', '' => 'ReleephProjectViewController',

View file

@ -2,15 +2,37 @@
final class ReleephProjectListController extends PhabricatorController { final class ReleephProjectListController extends PhabricatorController {
public function processRequest() { private $filter;
$path = $this->getRequest()->getRequestURI()->getPath();
$is_active = strpos($path, 'inactive/') === false;
$releeph_projects = mfilter( public function willProcessRequest(array $data) {
id(new ReleephProject())->loadAll(), $this->filter = idx($data, 'filter', 'active');
'getIsActive', }
!$is_active);
$releeph_projects = msort($releeph_projects, 'getName'); public function processRequest() {
$request = $this->getRequest();
$user = $request->getUser();
$query = id(new ReleephProjectQuery())
->setViewer($user)
->setOrder(ReleephProjectQuery::ORDER_NAME);
switch ($this->filter) {
case 'inactive':
$query->withActive(0);
$is_active = false;
break;
case 'active':
$query->withActive(1);
$is_active = true;
break;
default:
throw new Exception("Unknown filter '{$this->filter}'!");
}
$pager = new AphrontCursorPagerView();
$pager->readFromRequest($request);
$releeph_projects = $query->executeWithCursorPager($pager);
$releeph_projects_set = new LiskDAOSet(); $releeph_projects_set = new LiskDAOSet();
foreach ($releeph_projects as $releeph_project) { foreach ($releeph_projects as $releeph_project) {
@ -60,10 +82,13 @@ final class ReleephProjectListController extends PhabricatorController {
$panel->addButton($create_new_project_button); $panel->addButton($create_new_project_button);
} }
return $this->buildStandardPageResponse( return $this->buildApplicationPage(
$panel,
array( array(
'title' => 'List Releeph Projects' $panel,
$pager,
),
array(
'title' => 'List Releeph Projects',
)); ));
} }

View file

@ -0,0 +1,79 @@
<?php
final class ReleephProjectQuery
extends PhabricatorCursorPagedPolicyAwareQuery {
private $active;
private $order = 'order-id';
const ORDER_ID = 'order-id';
const ORDER_NAME = 'order-name';
public function withActive($active) {
$this->active = $active;
return $this;
}
public function setOrder($order) {
$this->order = $order;
return $this;
}
public function loadPage() {
$table = new ReleephProject();
$conn_r = $table->establishConnection('r');
$rows = queryfx_all(
$conn_r,
'SELECT * FROM %T %Q %Q %Q',
$table->getTableName(),
$this->buildWhereClause($conn_r),
$this->buildOrderClause($conn_r),
$this->buildLimitClause($conn_r));
return $table->loadAllFromArray($rows);
}
private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
$where = array();
if ($this->active !== null) {
$where[] = qsprintf(
$conn_r,
'isActive = %d',
$this->active);
}
$where[] = $this->buildPagingClause($conn_r);
return $this->formatWhereClause($where);
}
protected function getReversePaging() {
switch ($this->order) {
case self::ORDER_NAME:
return true;
}
return parent::getReversePaging();
}
protected function getPagingValue($result) {
switch ($this->order) {
case self::ORDER_NAME:
return $result->getName();
}
return parent::getPagingValue();
}
protected function getPagingColumn() {
switch ($this->order) {
case self::ORDER_NAME:
return 'name';
case self::ORDER_ID:
return parent::getPagingColumn();
default:
throw new Exception("Uknown order '{$this->order}'!");
}
}
}

View file

@ -1,6 +1,7 @@
<?php <?php
final class ReleephProject extends ReleephDAO { final class ReleephProject extends ReleephDAO
implements PhabricatorPolicyInterface {
const DEFAULT_BRANCH_NAMESPACE = 'releeph-releases'; const DEFAULT_BRANCH_NAMESPACE = 'releeph-releases';
const SYSTEM_AGENT_USERNAME_PREFIX = 'releeph-agent-'; const SYSTEM_AGENT_USERNAME_PREFIX = 'releeph-agent-';
@ -173,4 +174,21 @@ final class ReleephProject extends ReleephDAO {
} }
return false; return false;
} }
/* -( PhabricatorPolicyInterface )----------------------------------------- */
public function getCapabilities() {
return array(
PhabricatorPolicyCapability::CAN_VIEW,
);
}
public function getPolicy($capability) {
return PhabricatorPolicies::POLICY_USER;
}
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
return false;
}
} }