1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-18 11:30: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',
'PonderQuestion' => 'applications/ponder/storage/PonderQuestion.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',
'PonderQuestionEditor' => 'applications/ponder/editor/PonderQuestionEditor.php',
'PonderQuestionHasVotingUserEdgeType' => 'applications/ponder/edge/PonderQuestionHasVotingUserEdgeType.php',
@ -7590,6 +7592,8 @@ phutil_register_library_map(array(
'PhabricatorDestructibleInterface',
),
'PonderQuestionCommentController' => 'PonderController',
'PonderQuestionDefaultEditCapability' => 'PhabricatorPolicyCapability',
'PonderQuestionDefaultViewCapability' => 'PhabricatorPolicyCapability',
'PonderQuestionEditController' => 'PonderController',
'PonderQuestionEditor' => 'PonderEditor',
'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() {
return array(
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 {
public function handleRequest(AphrontRequest $request) {
$user = $request->getViewer();
$viewer = $request->getViewer();
$id = $request->getURIData('id');
if ($id) {
$question = id(new PonderQuestionQuery())
->setViewer($user)
->setViewer($viewer)
->withIDs(array($id))
->requireCapabilities(
array(
@ -24,17 +24,14 @@ final class PonderQuestionEditController extends PonderController {
PhabricatorProjectObjectHasProjectEdgeType::EDGECONST);
$v_projects = array_reverse($v_projects);
} else {
$question = id(new PonderQuestion())
->setStatus(PonderQuestionStatus::STATUS_OPEN)
->setAuthorPHID($user->getPHID())
->setVoteCount(0)
->setAnswerCount(0)
->setHeat(0.0);
$question = PonderQuestion::initializeNewQuestion($viewer);
$v_projects = array();
}
$v_title = $question->getTitle();
$v_content = $question->getContent();
$v_view = $question->getViewPolicy();
$v_edit = $question->getEditPolicy();
$errors = array();
$e_title = true;
@ -42,6 +39,8 @@ final class PonderQuestionEditController extends PonderController {
$v_title = $request->getStr('title');
$v_content = $request->getStr('content');
$v_projects = $request->getArr('projects');
$v_view = $request->getStr('viewPolicy');
$v_edit = $request->getStr('editPolicy');
$len = phutil_utf8_strlen($v_title);
if ($len < 1) {
@ -64,6 +63,14 @@ final class PonderQuestionEditController extends PonderController {
->setTransactionType(PonderQuestionTransaction::TYPE_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;
$xactions[] = id(new PonderQuestionTransaction())
->setTransactionType(PhabricatorTransactions::TYPE_EDGE)
@ -71,7 +78,7 @@ final class PonderQuestionEditController extends PonderController {
->setNewValue(array('=' => array_fuse($v_projects)));
$editor = id(new PonderQuestionEditor())
->setActor($user)
->setActor($viewer)
->setContentSourceFromRequest($request)
->setContinueOnNoEffect(true);
@ -82,8 +89,13 @@ final class PonderQuestionEditController extends PonderController {
}
}
$policies = id(new PhabricatorPolicyQuery())
->setViewer($viewer)
->setObject($question)
->execute();
$form = id(new AphrontFormView())
->setUser($user)
->setUser($viewer)
->appendChild(
id(new AphrontFormTextControl())
->setLabel(pht('Question'))
@ -92,12 +104,26 @@ final class PonderQuestionEditController extends PonderController {
->setError($e_title))
->appendChild(
id(new PhabricatorRemarkupControl())
->setUser($user)
->setUser($viewer)
->setName('content')
->setID('content')
->setValue($v_content)
->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(
id(new AphrontFormTokenizerControl())

View file

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

View file

@ -21,6 +21,8 @@ final class PonderQuestion extends PonderDAO
protected $status;
protected $content;
protected $contentSource;
protected $viewPolicy;
protected $editPolicy;
protected $voteCount;
protected $answerCount;
@ -31,6 +33,27 @@ final class PonderQuestion extends PonderDAO
private $vote;
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() {
return array(
self::CONFIG_AUX_PHID => true,
@ -234,15 +257,12 @@ final class PonderQuestion extends PonderDAO
}
public function getPolicy($capability) {
$policy = PhabricatorPolicies::POLICY_NOONE;
switch ($capability) {
case PhabricatorPolicyCapability::CAN_VIEW:
$policy = PhabricatorPolicies::POLICY_USER;
break;
return $this->getViewPolicy();
case PhabricatorPolicyCapability::CAN_EDIT:
return $this->getEditPolicy();
}
return $policy;
}
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {