Allow pastes to be edited
Summary: Permits the name and langauge of a paste to be edited. This will eventually allow the visibility policy to be edited as well. Test Plan: Edited name/langauge of some pastes. Tried to edit a paste I didn't own, was harshly rebuffed. Reviewers: vrana, btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T1690 Differential Revision: https://secure.phabricator.com/D3376
This commit is contained in:
@@ -18,26 +18,54 @@
|
||||
|
||||
final class PhabricatorPasteEditController extends PhabricatorPasteController {
|
||||
|
||||
private $id;
|
||||
|
||||
public function willProcessRequest(array $data) {
|
||||
$this->id = idx($data, 'id');
|
||||
}
|
||||
|
||||
|
||||
public function processRequest() {
|
||||
$request = $this->getRequest();
|
||||
$user = $request->getUser();
|
||||
|
||||
$paste = new PhabricatorPaste();
|
||||
$title = 'Create Paste';
|
||||
|
||||
$parent_id = $request->getStr('parent');
|
||||
$parent = null;
|
||||
if ($parent_id) {
|
||||
// NOTE: If the Paste is forked from a paste which the user no longer
|
||||
// has permission to see, we still let them edit it.
|
||||
$parent = id(new PhabricatorPasteQuery())
|
||||
->setViewer($user)
|
||||
->withIDs(array($parent_id))
|
||||
->execute();
|
||||
$parent = head($parent);
|
||||
$parent_id = null;
|
||||
if (!$this->id) {
|
||||
$is_create = true;
|
||||
|
||||
if ($parent) {
|
||||
$paste->setParentPHID($parent->getPHID());
|
||||
$paste = new PhabricatorPaste();
|
||||
|
||||
$parent_id = $request->getStr('parent');
|
||||
if ($parent_id) {
|
||||
// NOTE: If the Paste is forked from a paste which the user no longer
|
||||
// has permission to see, we still let them edit it.
|
||||
$parent = id(new PhabricatorPasteQuery())
|
||||
->setViewer($user)
|
||||
->withIDs(array($parent_id))
|
||||
->execute();
|
||||
$parent = head($parent);
|
||||
|
||||
if ($parent) {
|
||||
$paste->setParentPHID($parent->getPHID());
|
||||
}
|
||||
}
|
||||
|
||||
$paste->setAuthorPHID($user->getPHID());
|
||||
} else {
|
||||
$is_create = false;
|
||||
|
||||
$paste = id(new PhabricatorPasteQuery())
|
||||
->setViewer($user)
|
||||
->requireCapabilities(
|
||||
array(
|
||||
PhabricatorPolicyCapability::CAN_VIEW,
|
||||
PhabricatorPolicyCapability::CAN_EDIT,
|
||||
))
|
||||
->withIDs(array($this->id))
|
||||
->executeOne();
|
||||
if (!$paste) {
|
||||
return new Aphront404Response();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,33 +73,36 @@ final class PhabricatorPasteEditController extends PhabricatorPasteController {
|
||||
$e_text = true;
|
||||
$errors = array();
|
||||
if ($request->isFormPost()) {
|
||||
$text = $request->getStr('text');
|
||||
if (!strlen($text)) {
|
||||
$e_text = 'Required';
|
||||
$errors[] = 'The paste may not be blank.';
|
||||
} else {
|
||||
$e_text = null;
|
||||
|
||||
if ($is_create) {
|
||||
$text = $request->getStr('text');
|
||||
if (!strlen($text)) {
|
||||
$e_text = 'Required';
|
||||
$errors[] = 'The paste may not be blank.';
|
||||
} else {
|
||||
$e_text = null;
|
||||
}
|
||||
}
|
||||
|
||||
$paste->setTitle($request->getStr('title'));
|
||||
$paste->setLanguage($request->getStr('language'));
|
||||
|
||||
if (!$errors) {
|
||||
$paste_file = PhabricatorFile::newFromFileData(
|
||||
$text,
|
||||
array(
|
||||
'name' => $title,
|
||||
'mime-type' => 'text/plain; charset=utf-8',
|
||||
'authorPHID' => $user->getPHID(),
|
||||
));
|
||||
$paste->setFilePHID($paste_file->getPHID());
|
||||
$paste->setAuthorPHID($user->getPHID());
|
||||
if ($is_create) {
|
||||
$paste_file = PhabricatorFile::newFromFileData(
|
||||
$text,
|
||||
array(
|
||||
'name' => $paste->getTitle(),
|
||||
'mime-type' => 'text/plain; charset=utf-8',
|
||||
'authorPHID' => $user->getPHID(),
|
||||
));
|
||||
$paste->setFilePHID($paste_file->getPHID());
|
||||
}
|
||||
$paste->save();
|
||||
|
||||
return id(new AphrontRedirectResponse())->setURI($paste->getURI());
|
||||
}
|
||||
} else {
|
||||
if ($parent) {
|
||||
if ($is_create && $parent) {
|
||||
$paste->setTitle('Fork of '.$parent->getFullName());
|
||||
$paste->setLanguage($parent->getLanguage());
|
||||
|
||||
@@ -96,9 +127,6 @@ final class PhabricatorPasteEditController extends PhabricatorPasteController {
|
||||
'' => '(Detect With Wizardly Powers)',
|
||||
) + PhabricatorEnv::getEnvConfig('pygments.dropdown-choices');
|
||||
|
||||
$submit = id(new AphrontFormSubmitControl())
|
||||
->setValue('Create Paste');
|
||||
|
||||
$form
|
||||
->setUser($user)
|
||||
->addHiddenInput('parent', $parent_id)
|
||||
@@ -112,15 +140,19 @@ final class PhabricatorPasteEditController extends PhabricatorPasteController {
|
||||
->setLabel('Language')
|
||||
->setName('language')
|
||||
->setValue($paste->getLanguage())
|
||||
->setOptions($langs))
|
||||
->appendChild(
|
||||
id(new AphrontFormTextAreaControl())
|
||||
->setLabel('Text')
|
||||
->setError($e_text)
|
||||
->setValue($text)
|
||||
->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)
|
||||
->setCustomClass('PhabricatorMonospaced')
|
||||
->setName('text'))
|
||||
->setOptions($langs));
|
||||
|
||||
if ($is_create) {
|
||||
$form
|
||||
->appendChild(
|
||||
id(new AphrontFormTextAreaControl())
|
||||
->setLabel('Text')
|
||||
->setError($e_text)
|
||||
->setValue($text)
|
||||
->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)
|
||||
->setCustomClass('PhabricatorMonospaced')
|
||||
->setName('text'));
|
||||
}
|
||||
|
||||
/* TODO: Doesn't have any useful options yet.
|
||||
->appendChild(
|
||||
@@ -132,13 +164,25 @@ final class PhabricatorPasteEditController extends PhabricatorPasteController {
|
||||
->setName('policy'))
|
||||
*/
|
||||
|
||||
$submit = new AphrontFormSubmitControl();
|
||||
|
||||
if (!$is_create) {
|
||||
$submit->addCancelButton($paste->getURI());
|
||||
$submit->setValue('Save Paste');
|
||||
$title = 'Edit '.$paste->getFullName();
|
||||
} else {
|
||||
$submit->setValue('Create Paste');
|
||||
$title = 'Create Paste';
|
||||
}
|
||||
|
||||
$form
|
||||
->appendChild($submit);
|
||||
|
||||
$nav = $this->buildSideNavView();
|
||||
$nav->selectFilter('edit');
|
||||
$nav->appendChild(
|
||||
array(
|
||||
id(new PhabricatorHeaderView())->setHeader('Create Paste'),
|
||||
id(new PhabricatorHeaderView())->setHeader($title),
|
||||
$error_view,
|
||||
$form,
|
||||
));
|
||||
|
||||
@@ -59,7 +59,7 @@ final class PhabricatorPasteViewController extends PhabricatorPasteController {
|
||||
$fork_phids));
|
||||
|
||||
$header = $this->buildHeaderView($paste);
|
||||
$actions = $this->buildActionView($paste, $file);
|
||||
$actions = $this->buildActionView($user, $paste, $file);
|
||||
$properties = $this->buildPropertyView($paste, $fork_phids);
|
||||
$source_code = $this->buildSourceCodeView($paste, $file);
|
||||
|
||||
@@ -89,9 +89,15 @@ final class PhabricatorPasteViewController extends PhabricatorPasteController {
|
||||
}
|
||||
|
||||
private function buildActionView(
|
||||
PhabricatorUser $user,
|
||||
PhabricatorPaste $paste,
|
||||
PhabricatorFile $file) {
|
||||
|
||||
$can_edit = PhabricatorPolicyFilter::hasCapability(
|
||||
$user,
|
||||
$paste,
|
||||
PhabricatorPolicyCapability::CAN_EDIT);
|
||||
|
||||
return id(new PhabricatorActionListView())
|
||||
->addAction(
|
||||
id(new PhabricatorActionView())
|
||||
@@ -102,7 +108,14 @@ final class PhabricatorPasteViewController extends PhabricatorPasteController {
|
||||
id(new PhabricatorActionView())
|
||||
->setName(pht('View Raw File'))
|
||||
->setIcon('file')
|
||||
->setHref($file->getBestURI()));
|
||||
->setHref($file->getBestURI()))
|
||||
->addAction(
|
||||
id(new PhabricatorActionView())
|
||||
->setName(pht('Edit Paste'))
|
||||
->setIcon('edit')
|
||||
->setDisabled(!$can_edit)
|
||||
->setWorkflow(!$can_edit)
|
||||
->setHref($this->getApplicationURI('/edit/'.$paste->getID().'/')));
|
||||
}
|
||||
|
||||
private function buildPropertyView(
|
||||
|
||||
Reference in New Issue
Block a user