1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-20 20:40:56 +01:00

Use a modern query object to query Calendar events

Summary: Ref T4375. Calendar uses oldschool `loadOneWhere()` calls. Make CalendarEvent policy-aware, do the edit/delete policy checks through the policy framework, and use modern query infrastructure.

Test Plan:
  - Viewed calendar;
  - created, edited, deleted event;
  - viewed calendar tab in Conpherence.

Reviewers: btrahan, chad

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T4375

Differential Revision: https://secure.phabricator.com/D8146
This commit is contained in:
epriestley 2014-02-06 10:07:42 -08:00
parent ab636f36bf
commit 258cf23684
8 changed files with 153 additions and 31 deletions

View file

@ -1265,6 +1265,7 @@ phutil_register_library_map(array(
'PhabricatorCalendarEvent' => 'applications/calendar/storage/PhabricatorCalendarEvent.php',
'PhabricatorCalendarEventInvalidEpochException' => 'applications/calendar/exception/PhabricatorCalendarEventInvalidEpochException.php',
'PhabricatorCalendarEventOverlapException' => 'applications/calendar/exception/PhabricatorCalendarEventOverlapException.php',
'PhabricatorCalendarEventQuery' => 'applications/calendar/query/PhabricatorCalendarEventQuery.php',
'PhabricatorCalendarHoliday' => 'applications/calendar/storage/PhabricatorCalendarHoliday.php',
'PhabricatorCalendarHolidayTestCase' => 'applications/calendar/storage/__tests__/PhabricatorCalendarHolidayTestCase.php',
'PhabricatorCalendarViewStatusController' => 'applications/calendar/controller/PhabricatorCalendarViewStatusController.php',
@ -3900,9 +3901,14 @@ phutil_register_library_map(array(
'PhabricatorCalendarDAO' => 'PhabricatorLiskDAO',
'PhabricatorCalendarDeleteStatusController' => 'PhabricatorCalendarController',
'PhabricatorCalendarEditStatusController' => 'PhabricatorCalendarController',
'PhabricatorCalendarEvent' => 'PhabricatorCalendarDAO',
'PhabricatorCalendarEvent' =>
array(
0 => 'PhabricatorCalendarDAO',
1 => 'PhabricatorPolicyInterface',
),
'PhabricatorCalendarEventInvalidEpochException' => 'Exception',
'PhabricatorCalendarEventOverlapException' => 'Exception',
'PhabricatorCalendarEventQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
'PhabricatorCalendarHoliday' => 'PhabricatorCalendarDAO',
'PhabricatorCalendarHolidayTestCase' => 'PhabricatorTestCase',
'PhabricatorCalendarViewStatusController' => 'PhabricatorCalendarController',

View file

@ -19,11 +19,12 @@ final class PhabricatorCalendarBrowseController
"{$year}-{$month}-01",
"{$year}-{$month}-31");
$statuses = id(new PhabricatorCalendarEvent())
->loadAllWhere(
'dateTo >= %d AND dateFrom <= %d',
$statuses = id(new PhabricatorCalendarEventQuery())
->setViewer($user)
->withDateRange(
strtotime("{$year}-{$month}-01"),
strtotime("{$year}-{$month}-01 next month"));
strtotime("{$year}-{$month}-01 next month"))
->execute();
if ($month == $month_d && $year == $year_d) {
$month_view = new AphrontCalendarMonthView($month, $year, $day);

View file

@ -12,15 +12,20 @@ final class PhabricatorCalendarDeleteStatusController
public function processRequest() {
$request = $this->getRequest();
$user = $request->getUser();
$status = id(new PhabricatorCalendarEvent())
->loadOneWhere('id = %d', $this->id);
$status = id(new PhabricatorCalendarEventQuery())
->setViewer($user)
->withIDs(array($this->id))
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->executeOne();
if (!$status) {
return new Aphront404Response();
}
if ($status->getUserPHID() != $user->getPHID()) {
return new Aphront403Response();
}
if ($request->isFormPost()) {
$status->delete();
@ -36,10 +41,8 @@ final class PhabricatorCalendarDeleteStatusController
$dialog = new AphrontDialogView();
$dialog->setUser($user);
$dialog->setTitle(pht('Really delete status?'));
$dialog->appendChild(phutil_tag(
'p',
array(),
pht('Permanently delete this status? This action can not be undone.')));
$dialog->appendChild(
pht('Permanently delete this status? This action can not be undone.'));
$dialog->addSubmitButton(pht('Delete'));
$dialog->addCancelButton(
$this->getApplicationURI('status/edit/'.$status->getID().'/'));

View file

@ -38,18 +38,22 @@ final class PhabricatorCalendarEditStatusController
$page_title = pht('Create Status');
$redirect = 'created';
} else {
$status = id(new PhabricatorCalendarEvent())
->loadOneWhere('id = %d', $this->id);
$status = id(new PhabricatorCalendarEventQuery())
->setViewer($user)
->withIDs(array($this->id))
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->executeOne();
$end_time->setValue($status->getDateTo());
$start_time->setValue($status->getDateFrom());
$submit_label = pht('Update');
$filter = 'status/edit/'.$status->getID().'/';
$page_title = pht('Update Status');
$redirect = 'updated';
if ($status->getUserPHID() != $user->getPHID()) {
return new Aphront403Response();
}
}
$errors = array();

View file

@ -12,13 +12,15 @@ final class PhabricatorCalendarViewStatusController
}
public function processRequest() {
$request = $this->getRequest();
$user = $request->getUser();
$handle = $this->getHandle($this->phid);
$statuses = id(new PhabricatorCalendarEvent())
->loadAllWhere('userPHID = %s AND dateTo > UNIX_TIMESTAMP()',
$this->phid);
$statuses = id(new PhabricatorCalendarEventQuery())
->setViewer($user)
->withInvitedPHIDs(array($this->phid))
->withDateRange(time(), strtotime('2037-01-01 12:00:00'))
->execute();
$nav = $this->buildSideNavView();
$nav->selectFilter($this->getFilter());

View file

@ -0,0 +1,76 @@
<?php
final class PhabricatorCalendarEventQuery
extends PhabricatorCursorPagedPolicyAwareQuery {
private $ids;
private $rangeBegin;
private $rangeEnd;
private $invitedPHIDs;
public function withIDs(array $ids) {
$this->ids = $ids;
return $this;
}
public function withDateRange($begin, $end) {
$this->rangeBegin = $begin;
$this->rangeEnd = $end;
return $this;
}
public function withInvitedPHIDs(array $phids) {
$this->invitedPHIDs = $phids;
return $this;
}
protected function loadPage() {
$table = new PhabricatorCalendarEvent();
$conn_r = $table->establishConnection('r');
$data = 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($data);
}
protected function buildWhereClause($conn_r) {
$where = array();
if ($this->ids) {
$where[] = qsprintf(
$conn_r,
'id IN (%Ld)',
$this->ids);
}
if ($this->rangeBegin || $this->rangeEnd) {
$where[] = qsprintf(
$conn_r,
'dateTo >= %d AND dateFrom <= %d',
$this->rangeBegin,
$this->rangeEnd);
}
if ($this->invitedPHIDs) {
$where[] = qsprintf(
$conn_r,
'userPHID IN (%Ls)',
$this->invitedPHIDs);
}
$where[] = $this->buildPagingClause($conn_r);
return $this->formatWhereClause($where);
}
public function getQueryApplicationClass() {
return 'PhabricatorApplicationCalendar';
}
}

View file

@ -1,6 +1,8 @@
<?php
final class PhabricatorCalendarEvent extends PhabricatorCalendarDAO {
final class PhabricatorCalendarEvent
extends PhabricatorCalendarDAO
implements PhabricatorPolicyInterface {
protected $userPHID;
protected $dateFrom;
@ -92,4 +94,32 @@ final class PhabricatorCalendarEvent extends PhabricatorCalendarDAO {
return $this->saveTransaction();
}
/* -( PhabricatorPolicyInterface )----------------------------------------- */
public function getCapabilities() {
return array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
);
}
public function getPolicy($capability) {
switch ($capability) {
case PhabricatorPolicyCapability::CAN_VIEW:
return PhabricatorPolicies::getMostOpenPolicy();
case PhabricatorPolicyCapability::CAN_EDIT:
return $this->getUserPHID();
}
}
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
return false;
}
public function describeAutomaticCapability($capability) {
return null;
}
}

View file

@ -221,12 +221,12 @@ final class ConpherenceThreadQuery
$this->getViewer());
$start_epoch = $epochs['start_epoch'];
$end_epoch = $epochs['end_epoch'];
$statuses = id(new PhabricatorCalendarEvent())
->loadAllWhere(
'userPHID in (%Ls) AND dateTo >= %d AND dateFrom <= %d',
$participant_phids,
$start_epoch,
$end_epoch);
$statuses = id(new PhabricatorCalendarEventQuery())
->setViewer($this->getViewer())
->withInvitedPHIDs($participant_phids)
->withDateRange($start_epoch, $end_epoch)
->execute();
$statuses = mgroup($statuses, 'getUserPHID');
// attached files