2011-06-10 08:53:53 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-01-10 23:48:55 +01:00
|
|
|
* Copyright 2012 Facebook, Inc.
|
2011-06-10 08:53:53 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-03-10 00:46:25 +01:00
|
|
|
final class PhabricatorPasteViewController extends PhabricatorPasteController {
|
2011-06-10 08:53:53 +02:00
|
|
|
|
|
|
|
private $id;
|
2012-08-15 19:45:06 +02:00
|
|
|
private $handles;
|
2011-06-10 08:53:53 +02:00
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->id = $data['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$user = $request->getUser();
|
|
|
|
|
Add basic per-object privacy policies
Summary:
Provides a basic start for access policies. Objects expose various capabilities, like CAN_VIEW, CAN_EDIT, etc., and set a policy for each capability. We currently implement three policies, PUBLIC (anyone, including logged-out), USERS (any logged-in) and NOONE (nobody). There's also a way to provide automatic capability grants (e.g., the owner of an object can always see it, even if some capability is set to "NOONE"), but I'm not sure how great the implementation feels and it might change.
Most of the code here is providing a primitive for efficient policy-aware list queries. The problem with doing queries naively is that you have to do crazy amounts of filtering, e.g. to show the user page 6, you need to filter at least 600 objects (and likely more) before you can figure out which ones are 500-600 for them. You can't just do "LIMIT 500, 100" because that might have only 50 results, or no results. Instead, the query looks like "WHERE id > last_visible_id", and then we fetch additional pages as necessary to satisfy the request.
The general idea is that we move all data access to Query classes and have them do object filtering. The ID paging primitive allows efficient paging in most cases, and the executeOne() method provides a concise way to do policy checks for edit/view screens.
We'll probably end up with mostly broader policy UIs or configuration-based policies, but there are at least a few cases for per-object privacy (e.g., marking tasks as "Security", and restricting things to the members of projects) so I figured we'd start with a flexible primitive and the simplify it in the UI where we can.
Test Plan: Unit tests, played around in the UI with various policy settings.
Reviewers: btrahan, vrana, jungejason
Reviewed By: btrahan
CC: aran
Maniphest Tasks: T603
Differential Revision: https://secure.phabricator.com/D2210
2012-04-14 19:13:29 +02:00
|
|
|
$paste = id(new PhabricatorPasteQuery())
|
|
|
|
->setViewer($user)
|
2012-08-15 19:45:06 +02:00
|
|
|
->withIDs(array($this->id))
|
Add basic per-object privacy policies
Summary:
Provides a basic start for access policies. Objects expose various capabilities, like CAN_VIEW, CAN_EDIT, etc., and set a policy for each capability. We currently implement three policies, PUBLIC (anyone, including logged-out), USERS (any logged-in) and NOONE (nobody). There's also a way to provide automatic capability grants (e.g., the owner of an object can always see it, even if some capability is set to "NOONE"), but I'm not sure how great the implementation feels and it might change.
Most of the code here is providing a primitive for efficient policy-aware list queries. The problem with doing queries naively is that you have to do crazy amounts of filtering, e.g. to show the user page 6, you need to filter at least 600 objects (and likely more) before you can figure out which ones are 500-600 for them. You can't just do "LIMIT 500, 100" because that might have only 50 results, or no results. Instead, the query looks like "WHERE id > last_visible_id", and then we fetch additional pages as necessary to satisfy the request.
The general idea is that we move all data access to Query classes and have them do object filtering. The ID paging primitive allows efficient paging in most cases, and the executeOne() method provides a concise way to do policy checks for edit/view screens.
We'll probably end up with mostly broader policy UIs or configuration-based policies, but there are at least a few cases for per-object privacy (e.g., marking tasks as "Security", and restricting things to the members of projects) so I figured we'd start with a flexible primitive and the simplify it in the UI where we can.
Test Plan: Unit tests, played around in the UI with various policy settings.
Reviewers: btrahan, vrana, jungejason
Reviewed By: btrahan
CC: aran
Maniphest Tasks: T603
Differential Revision: https://secure.phabricator.com/D2210
2012-04-14 19:13:29 +02:00
|
|
|
->executeOne();
|
2011-06-10 08:53:53 +02:00
|
|
|
if (!$paste) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
|
|
|
$file = id(new PhabricatorFile())->loadOneWhere(
|
|
|
|
'phid = %s',
|
|
|
|
$paste->getFilePHID());
|
|
|
|
if (!$file) {
|
|
|
|
return new Aphront400Response();
|
|
|
|
}
|
|
|
|
|
2012-08-15 19:45:06 +02:00
|
|
|
$this->loadHandles(
|
|
|
|
array(
|
|
|
|
$paste->getAuthorPHID(),
|
|
|
|
$paste->getParentPHID(),
|
|
|
|
));
|
2011-06-10 08:53:53 +02:00
|
|
|
|
2012-08-15 19:45:06 +02:00
|
|
|
$header = $this->buildHeaderView($paste);
|
|
|
|
$actions = $this->buildActionView($paste, $file);
|
|
|
|
$properties = $this->buildPropertyView($paste);
|
|
|
|
$source_code = $this->buildSourceCodeView($paste, $file);
|
2011-07-15 22:49:45 +02:00
|
|
|
|
2012-08-15 19:45:06 +02:00
|
|
|
$nav = $this->buildSideNavView($paste);
|
|
|
|
$nav->selectFilter('paste');
|
|
|
|
$nav->appendChild(
|
2011-07-21 08:17:30 +02:00
|
|
|
array(
|
2012-08-15 19:45:06 +02:00
|
|
|
$header,
|
|
|
|
$actions,
|
|
|
|
$properties,
|
|
|
|
$source_code,
|
|
|
|
// $forks_panel,
|
|
|
|
));
|
|
|
|
|
|
|
|
return $this->buildApplicationPage(
|
|
|
|
$nav,
|
2011-06-10 08:53:53 +02:00
|
|
|
array(
|
2012-08-15 19:45:06 +02:00
|
|
|
'title' => 'P'.$paste->getID().' '.$paste->getTitle(),
|
|
|
|
'device' => true,
|
2011-06-10 08:53:53 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2012-08-15 19:45:06 +02:00
|
|
|
private function buildHeaderView(PhabricatorPaste $paste) {
|
|
|
|
return id(new PhabricatorHeaderView())
|
|
|
|
->setObjectName('P'.$paste->getID())
|
|
|
|
->setHeader($paste->getTitle());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildActionView(
|
|
|
|
PhabricatorPaste $paste,
|
|
|
|
PhabricatorFile $file) {
|
|
|
|
|
|
|
|
return id(new PhabricatorActionListView())
|
|
|
|
->addAction(
|
|
|
|
id(new PhabricatorActionView())
|
|
|
|
->setName(pht('Fork This Paste'))
|
|
|
|
->setIcon('fork')
|
|
|
|
->setHref($this->getApplicationURI('?fork='.$paste->getID())))
|
|
|
|
->addAction(
|
|
|
|
id(new PhabricatorActionView())
|
|
|
|
->setName(pht('View Raw File'))
|
|
|
|
->setIcon('file')
|
|
|
|
->setHref($file->getBestURI()));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildPropertyView(PhabricatorPaste $paste) {
|
|
|
|
$user = $this->getRequest()->getUser();
|
|
|
|
$properties = new PhabricatorPropertyListView();
|
2011-06-10 08:53:53 +02:00
|
|
|
|
2012-08-15 19:45:06 +02:00
|
|
|
$properties->addProperty(
|
|
|
|
pht('Author'),
|
|
|
|
$this->getHandle($paste->getAuthorPHID())->renderLink());
|
|
|
|
|
|
|
|
$properties->addProperty(
|
|
|
|
pht('Created'),
|
|
|
|
phabricator_datetime($paste->getDateCreated(), $user));
|
|
|
|
|
|
|
|
if ($paste->getParentPHID()) {
|
|
|
|
$properties->addProperty(
|
|
|
|
pht('Forked From'),
|
|
|
|
$this->getHandle($paste->getParentPHID())->renderLink());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildSourceCodeView(
|
|
|
|
PhabricatorPaste $paste,
|
|
|
|
PhabricatorFile $file) {
|
2011-06-10 08:53:53 +02:00
|
|
|
|
2011-07-06 20:10:40 +02:00
|
|
|
$language = $paste->getLanguage();
|
2011-07-06 21:12:17 +02:00
|
|
|
$source = $file->loadFileData();
|
2012-08-15 19:45:06 +02:00
|
|
|
|
2011-07-06 20:10:40 +02:00
|
|
|
if (empty($language)) {
|
2011-07-06 21:12:17 +02:00
|
|
|
$source = PhabricatorSyntaxHighlighter::highlightWithFilename(
|
|
|
|
$paste->getTitle(),
|
|
|
|
$source);
|
|
|
|
} else {
|
|
|
|
$source = PhabricatorSyntaxHighlighter::highlightWithLanguage(
|
|
|
|
$language,
|
|
|
|
$source);
|
2011-07-06 20:10:40 +02:00
|
|
|
}
|
|
|
|
|
2012-08-15 19:45:06 +02:00
|
|
|
$lines = explode("\n", $source);
|
2011-06-10 08:53:53 +02:00
|
|
|
|
2012-08-15 19:45:06 +02:00
|
|
|
return id(new PhabricatorSourceCodeView())
|
|
|
|
->setLines($lines);
|
2011-06-10 08:53:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|