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

Add Edit/View Policy to Ponder Questions

Summary: Ref T3578, adds ability to set a default edit and view policy for questions. Not sure what to set viewPolicy to ?

Test Plan: Test an old question, edit policy still on myself. Test a new question, see new default.

Reviewers: epriestley

Reviewed By: epriestley

Subscribers: Korvin

Maniphest Tasks: T3578

Differential Revision: https://secure.phabricator.com/D13791
This commit is contained in:
Chad Little 2015-08-04 15:41:09 -07:00
parent 630fb06c42
commit 135d0c9ee7
10 changed files with 121 additions and 18 deletions

View file

@ -0,0 +1,5 @@
ALTER TABLE {$NAMESPACE}_ponder.ponder_question
ADD editPolicy VARBINARY(64) NOT NULL;
ALTER TABLE {$NAMESPACE}_ponder.ponder_question
ADD viewPolicy VARBINARY(64) NOT NULL;

View file

@ -0,0 +1,2 @@
UPDATE {$NAMESPACE}_ponder.ponder_question
SET editPolicy = authorPHID WHERE editPolicy = '';

View file

@ -0,0 +1,2 @@
UPDATE {$NAMESPACE}_ponder.ponder_question
SET viewPolicy = 'users' WHERE viewPolicy = '';

View file

@ -3399,6 +3399,8 @@ phutil_register_library_map(array(
'PonderEditor' => 'applications/ponder/editor/PonderEditor.php', 'PonderEditor' => 'applications/ponder/editor/PonderEditor.php',
'PonderQuestion' => 'applications/ponder/storage/PonderQuestion.php', 'PonderQuestion' => 'applications/ponder/storage/PonderQuestion.php',
'PonderQuestionCommentController' => 'applications/ponder/controller/PonderQuestionCommentController.php', 'PonderQuestionCommentController' => 'applications/ponder/controller/PonderQuestionCommentController.php',
'PonderQuestionDefaultEditCapability' => 'applications/ponder/capability/PonderQuestionDefaultEditCapability.php',
'PonderQuestionDefaultViewCapability' => 'applications/ponder/capability/PonderQuestionDefaultViewCapability.php',
'PonderQuestionEditController' => 'applications/ponder/controller/PonderQuestionEditController.php', 'PonderQuestionEditController' => 'applications/ponder/controller/PonderQuestionEditController.php',
'PonderQuestionEditor' => 'applications/ponder/editor/PonderQuestionEditor.php', 'PonderQuestionEditor' => 'applications/ponder/editor/PonderQuestionEditor.php',
'PonderQuestionHasVotingUserEdgeType' => 'applications/ponder/edge/PonderQuestionHasVotingUserEdgeType.php', 'PonderQuestionHasVotingUserEdgeType' => 'applications/ponder/edge/PonderQuestionHasVotingUserEdgeType.php',
@ -7590,6 +7592,8 @@ phutil_register_library_map(array(
'PhabricatorDestructibleInterface', 'PhabricatorDestructibleInterface',
), ),
'PonderQuestionCommentController' => 'PonderController', 'PonderQuestionCommentController' => 'PonderController',
'PonderQuestionDefaultEditCapability' => 'PhabricatorPolicyCapability',
'PonderQuestionDefaultViewCapability' => 'PhabricatorPolicyCapability',
'PonderQuestionEditController' => 'PonderController', 'PonderQuestionEditController' => 'PonderController',
'PonderQuestionEditor' => 'PonderEditor', 'PonderQuestionEditor' => 'PonderEditor',
'PonderQuestionHasVotingUserEdgeType' => 'PhabricatorEdgeType', 'PonderQuestionHasVotingUserEdgeType' => 'PhabricatorEdgeType',

View file

@ -91,6 +91,19 @@ final class PhabricatorPonderApplication extends PhabricatorApplication {
); );
} }
protected function getCustomCapabilities() {
return array(
PonderQuestionDefaultViewCapability::CAPABILITY => array(
'template' => PonderQuestionPHIDType::TYPECONST,
'capability' => PhabricatorPolicyCapability::CAN_VIEW,
),
PonderQuestionDefaultEditCapability::CAPABILITY => array(
'template' => PonderQuestionPHIDType::TYPECONST,
'capability' => PhabricatorPolicyCapability::CAN_EDIT,
),
);
}
public function getApplicationSearchDocumentTypes() { public function getApplicationSearchDocumentTypes() {
return array( return array(
PonderQuestionPHIDType::TYPECONST, PonderQuestionPHIDType::TYPECONST,

View file

@ -0,0 +1,12 @@
<?php
final class PonderQuestionDefaultEditCapability
extends PhabricatorPolicyCapability {
const CAPABILITY = 'ponder.question.default.edit';
public function getCapabilityName() {
return pht('Default Question Edit Policy');
}
}

View file

@ -0,0 +1,16 @@
<?php
final class PonderQuestionDefaultViewCapability
extends PhabricatorPolicyCapability {
const CAPABILITY = 'ponder.question.default.view';
public function getCapabilityName() {
return pht('Default Question View Policy');
}
public function shouldAllowPublicPolicySetting() {
return true;
}
}

View file

@ -3,12 +3,12 @@
final class PonderQuestionEditController extends PonderController { final class PonderQuestionEditController extends PonderController {
public function handleRequest(AphrontRequest $request) { public function handleRequest(AphrontRequest $request) {
$user = $request->getViewer(); $viewer = $request->getViewer();
$id = $request->getURIData('id'); $id = $request->getURIData('id');
if ($id) { if ($id) {
$question = id(new PonderQuestionQuery()) $question = id(new PonderQuestionQuery())
->setViewer($user) ->setViewer($viewer)
->withIDs(array($id)) ->withIDs(array($id))
->requireCapabilities( ->requireCapabilities(
array( array(
@ -24,17 +24,14 @@ final class PonderQuestionEditController extends PonderController {
PhabricatorProjectObjectHasProjectEdgeType::EDGECONST); PhabricatorProjectObjectHasProjectEdgeType::EDGECONST);
$v_projects = array_reverse($v_projects); $v_projects = array_reverse($v_projects);
} else { } else {
$question = id(new PonderQuestion()) $question = PonderQuestion::initializeNewQuestion($viewer);
->setStatus(PonderQuestionStatus::STATUS_OPEN)
->setAuthorPHID($user->getPHID())
->setVoteCount(0)
->setAnswerCount(0)
->setHeat(0.0);
$v_projects = array(); $v_projects = array();
} }
$v_title = $question->getTitle(); $v_title = $question->getTitle();
$v_content = $question->getContent(); $v_content = $question->getContent();
$v_view = $question->getViewPolicy();
$v_edit = $question->getEditPolicy();
$errors = array(); $errors = array();
$e_title = true; $e_title = true;
@ -42,6 +39,8 @@ final class PonderQuestionEditController extends PonderController {
$v_title = $request->getStr('title'); $v_title = $request->getStr('title');
$v_content = $request->getStr('content'); $v_content = $request->getStr('content');
$v_projects = $request->getArr('projects'); $v_projects = $request->getArr('projects');
$v_view = $request->getStr('viewPolicy');
$v_edit = $request->getStr('editPolicy');
$len = phutil_utf8_strlen($v_title); $len = phutil_utf8_strlen($v_title);
if ($len < 1) { if ($len < 1) {
@ -64,6 +63,14 @@ final class PonderQuestionEditController extends PonderController {
->setTransactionType(PonderQuestionTransaction::TYPE_CONTENT) ->setTransactionType(PonderQuestionTransaction::TYPE_CONTENT)
->setNewValue($v_content); ->setNewValue($v_content);
$xactions[] = id(clone $template)
->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)
->setNewValue($v_view);
$xactions[] = id(clone $template)
->setTransactionType(PhabricatorTransactions::TYPE_EDIT_POLICY)
->setNewValue($v_edit);
$proj_edge_type = PhabricatorProjectObjectHasProjectEdgeType::EDGECONST; $proj_edge_type = PhabricatorProjectObjectHasProjectEdgeType::EDGECONST;
$xactions[] = id(new PonderQuestionTransaction()) $xactions[] = id(new PonderQuestionTransaction())
->setTransactionType(PhabricatorTransactions::TYPE_EDGE) ->setTransactionType(PhabricatorTransactions::TYPE_EDGE)
@ -71,7 +78,7 @@ final class PonderQuestionEditController extends PonderController {
->setNewValue(array('=' => array_fuse($v_projects))); ->setNewValue(array('=' => array_fuse($v_projects)));
$editor = id(new PonderQuestionEditor()) $editor = id(new PonderQuestionEditor())
->setActor($user) ->setActor($viewer)
->setContentSourceFromRequest($request) ->setContentSourceFromRequest($request)
->setContinueOnNoEffect(true); ->setContinueOnNoEffect(true);
@ -82,8 +89,13 @@ final class PonderQuestionEditController extends PonderController {
} }
} }
$policies = id(new PhabricatorPolicyQuery())
->setViewer($viewer)
->setObject($question)
->execute();
$form = id(new AphrontFormView()) $form = id(new AphrontFormView())
->setUser($user) ->setUser($viewer)
->appendChild( ->appendChild(
id(new AphrontFormTextControl()) id(new AphrontFormTextControl())
->setLabel(pht('Question')) ->setLabel(pht('Question'))
@ -92,12 +104,26 @@ final class PonderQuestionEditController extends PonderController {
->setError($e_title)) ->setError($e_title))
->appendChild( ->appendChild(
id(new PhabricatorRemarkupControl()) id(new PhabricatorRemarkupControl())
->setUser($user) ->setUser($viewer)
->setName('content') ->setName('content')
->setID('content') ->setID('content')
->setValue($v_content) ->setValue($v_content)
->setLabel(pht('Description')) ->setLabel(pht('Description'))
->setUser($user)); ->setUser($viewer))
->appendControl(
id(new AphrontFormPolicyControl())
->setName('viewPolicy')
->setPolicyObject($question)
->setPolicies($policies)
->setValue($v_view)
->setCapability(PhabricatorPolicyCapability::CAN_VIEW))
->appendControl(
id(new AphrontFormPolicyControl())
->setName('editPolicy')
->setPolicyObject($question)
->setPolicies($policies)
->setValue($v_edit)
->setCapability(PhabricatorPolicyCapability::CAN_EDIT));
$form->appendControl( $form->appendControl(
id(new AphrontFormTokenizerControl()) id(new AphrontFormTokenizerControl())

View file

@ -66,6 +66,9 @@ final class PonderQuestionEditor
$types = parent::getTransactionTypes(); $types = parent::getTransactionTypes();
$types[] = PhabricatorTransactions::TYPE_COMMENT; $types[] = PhabricatorTransactions::TYPE_COMMENT;
$types[] = PhabricatorTransactions::TYPE_VIEW_POLICY;
$types[] = PhabricatorTransactions::TYPE_EDIT_POLICY;
$types[] = PonderQuestionTransaction::TYPE_TITLE; $types[] = PonderQuestionTransaction::TYPE_TITLE;
$types[] = PonderQuestionTransaction::TYPE_CONTENT; $types[] = PonderQuestionTransaction::TYPE_CONTENT;
$types[] = PonderQuestionTransaction::TYPE_ANSWERS; $types[] = PonderQuestionTransaction::TYPE_ANSWERS;

View file

@ -21,6 +21,8 @@ final class PonderQuestion extends PonderDAO
protected $status; protected $status;
protected $content; protected $content;
protected $contentSource; protected $contentSource;
protected $viewPolicy;
protected $editPolicy;
protected $voteCount; protected $voteCount;
protected $answerCount; protected $answerCount;
@ -31,6 +33,27 @@ final class PonderQuestion extends PonderDAO
private $vote; private $vote;
private $comments; private $comments;
public static function initializeNewQuestion(PhabricatorUser $actor) {
$app = id(new PhabricatorApplicationQuery())
->setViewer($actor)
->withClasses(array('PhabricatorPonderApplication'))
->executeOne();
$view_policy = $app->getPolicy(
PonderQuestionDefaultViewCapability::CAPABILITY);
$edit_policy = $app->getPolicy(
PonderQuestionDefaultEditCapability::CAPABILITY);
return id(new PonderQuestion())
->setAuthorPHID($actor->getPHID())
->setViewPolicy($view_policy)
->setEditPolicy($edit_policy)
->setStatus(PonderQuestionStatus::STATUS_OPEN)
->setVoteCount(0)
->setAnswerCount(0)
->setHeat(0.0);
}
protected function getConfiguration() { protected function getConfiguration() {
return array( return array(
self::CONFIG_AUX_PHID => true, self::CONFIG_AUX_PHID => true,
@ -234,15 +257,12 @@ final class PonderQuestion extends PonderDAO
} }
public function getPolicy($capability) { public function getPolicy($capability) {
$policy = PhabricatorPolicies::POLICY_NOONE;
switch ($capability) { switch ($capability) {
case PhabricatorPolicyCapability::CAN_VIEW: case PhabricatorPolicyCapability::CAN_VIEW:
$policy = PhabricatorPolicies::POLICY_USER; return $this->getViewPolicy();
break; case PhabricatorPolicyCapability::CAN_EDIT:
return $this->getEditPolicy();
} }
return $policy;
} }
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) { public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {