1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-30 01:10:58 +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', 'PhabricatorCalendarEvent' => 'applications/calendar/storage/PhabricatorCalendarEvent.php',
'PhabricatorCalendarEventInvalidEpochException' => 'applications/calendar/exception/PhabricatorCalendarEventInvalidEpochException.php', 'PhabricatorCalendarEventInvalidEpochException' => 'applications/calendar/exception/PhabricatorCalendarEventInvalidEpochException.php',
'PhabricatorCalendarEventOverlapException' => 'applications/calendar/exception/PhabricatorCalendarEventOverlapException.php', 'PhabricatorCalendarEventOverlapException' => 'applications/calendar/exception/PhabricatorCalendarEventOverlapException.php',
'PhabricatorCalendarEventQuery' => 'applications/calendar/query/PhabricatorCalendarEventQuery.php',
'PhabricatorCalendarHoliday' => 'applications/calendar/storage/PhabricatorCalendarHoliday.php', 'PhabricatorCalendarHoliday' => 'applications/calendar/storage/PhabricatorCalendarHoliday.php',
'PhabricatorCalendarHolidayTestCase' => 'applications/calendar/storage/__tests__/PhabricatorCalendarHolidayTestCase.php', 'PhabricatorCalendarHolidayTestCase' => 'applications/calendar/storage/__tests__/PhabricatorCalendarHolidayTestCase.php',
'PhabricatorCalendarViewStatusController' => 'applications/calendar/controller/PhabricatorCalendarViewStatusController.php', 'PhabricatorCalendarViewStatusController' => 'applications/calendar/controller/PhabricatorCalendarViewStatusController.php',
@ -3900,9 +3901,14 @@ phutil_register_library_map(array(
'PhabricatorCalendarDAO' => 'PhabricatorLiskDAO', 'PhabricatorCalendarDAO' => 'PhabricatorLiskDAO',
'PhabricatorCalendarDeleteStatusController' => 'PhabricatorCalendarController', 'PhabricatorCalendarDeleteStatusController' => 'PhabricatorCalendarController',
'PhabricatorCalendarEditStatusController' => 'PhabricatorCalendarController', 'PhabricatorCalendarEditStatusController' => 'PhabricatorCalendarController',
'PhabricatorCalendarEvent' => 'PhabricatorCalendarDAO', 'PhabricatorCalendarEvent' =>
array(
0 => 'PhabricatorCalendarDAO',
1 => 'PhabricatorPolicyInterface',
),
'PhabricatorCalendarEventInvalidEpochException' => 'Exception', 'PhabricatorCalendarEventInvalidEpochException' => 'Exception',
'PhabricatorCalendarEventOverlapException' => 'Exception', 'PhabricatorCalendarEventOverlapException' => 'Exception',
'PhabricatorCalendarEventQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
'PhabricatorCalendarHoliday' => 'PhabricatorCalendarDAO', 'PhabricatorCalendarHoliday' => 'PhabricatorCalendarDAO',
'PhabricatorCalendarHolidayTestCase' => 'PhabricatorTestCase', 'PhabricatorCalendarHolidayTestCase' => 'PhabricatorTestCase',
'PhabricatorCalendarViewStatusController' => 'PhabricatorCalendarController', 'PhabricatorCalendarViewStatusController' => 'PhabricatorCalendarController',

View file

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

View file

@ -12,15 +12,20 @@ final class PhabricatorCalendarDeleteStatusController
public function processRequest() { public function processRequest() {
$request = $this->getRequest(); $request = $this->getRequest();
$user = $request->getUser(); $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) { if (!$status) {
return new Aphront404Response(); return new Aphront404Response();
} }
if ($status->getUserPHID() != $user->getPHID()) {
return new Aphront403Response();
}
if ($request->isFormPost()) { if ($request->isFormPost()) {
$status->delete(); $status->delete();
@ -36,10 +41,8 @@ final class PhabricatorCalendarDeleteStatusController
$dialog = new AphrontDialogView(); $dialog = new AphrontDialogView();
$dialog->setUser($user); $dialog->setUser($user);
$dialog->setTitle(pht('Really delete status?')); $dialog->setTitle(pht('Really delete status?'));
$dialog->appendChild(phutil_tag( $dialog->appendChild(
'p', pht('Permanently delete this status? This action can not be undone.'));
array(),
pht('Permanently delete this status? This action can not be undone.')));
$dialog->addSubmitButton(pht('Delete')); $dialog->addSubmitButton(pht('Delete'));
$dialog->addCancelButton( $dialog->addCancelButton(
$this->getApplicationURI('status/edit/'.$status->getID().'/')); $this->getApplicationURI('status/edit/'.$status->getID().'/'));

View file

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

View file

@ -12,13 +12,15 @@ final class PhabricatorCalendarViewStatusController
} }
public function processRequest() { public function processRequest() {
$request = $this->getRequest(); $request = $this->getRequest();
$user = $request->getUser(); $user = $request->getUser();
$handle = $this->getHandle($this->phid); $handle = $this->getHandle($this->phid);
$statuses = id(new PhabricatorCalendarEvent())
->loadAllWhere('userPHID = %s AND dateTo > UNIX_TIMESTAMP()', $statuses = id(new PhabricatorCalendarEventQuery())
$this->phid); ->setViewer($user)
->withInvitedPHIDs(array($this->phid))
->withDateRange(time(), strtotime('2037-01-01 12:00:00'))
->execute();
$nav = $this->buildSideNavView(); $nav = $this->buildSideNavView();
$nav->selectFilter($this->getFilter()); $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 <?php
final class PhabricatorCalendarEvent extends PhabricatorCalendarDAO { final class PhabricatorCalendarEvent
extends PhabricatorCalendarDAO
implements PhabricatorPolicyInterface {
protected $userPHID; protected $userPHID;
protected $dateFrom; protected $dateFrom;
@ -92,4 +94,32 @@ final class PhabricatorCalendarEvent extends PhabricatorCalendarDAO {
return $this->saveTransaction(); 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()); $this->getViewer());
$start_epoch = $epochs['start_epoch']; $start_epoch = $epochs['start_epoch'];
$end_epoch = $epochs['end_epoch']; $end_epoch = $epochs['end_epoch'];
$statuses = id(new PhabricatorCalendarEvent()) $statuses = id(new PhabricatorCalendarEventQuery())
->loadAllWhere( ->setViewer($this->getViewer())
'userPHID in (%Ls) AND dateTo >= %d AND dateFrom <= %d', ->withInvitedPHIDs($participant_phids)
$participant_phids, ->withDateRange($start_epoch, $end_epoch)
$start_epoch, ->execute();
$end_epoch);
$statuses = mgroup($statuses, 'getUserPHID'); $statuses = mgroup($statuses, 'getUserPHID');
// attached files // attached files