From 135d0c9ee7a6d0552ba2f16bd36aef9b574de16c Mon Sep 17 00:00:00 2001 From: Chad Little Date: Tue, 4 Aug 2015 15:41:09 -0700 Subject: [PATCH] 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 --- .../20150804.ponder.question.1.sql | 5 ++ .../20150804.ponder.question.2.sql | 2 + .../20150804.ponder.question.3.sql | 2 + src/__phutil_library_map__.php | 4 ++ .../PhabricatorPonderApplication.php | 13 +++++ .../PonderQuestionDefaultEditCapability.php | 12 +++++ .../PonderQuestionDefaultViewCapability.php | 16 ++++++ .../PonderQuestionEditController.php | 50 ++++++++++++++----- .../ponder/editor/PonderQuestionEditor.php | 3 ++ .../ponder/storage/PonderQuestion.php | 32 +++++++++--- 10 files changed, 121 insertions(+), 18 deletions(-) create mode 100644 resources/sql/autopatches/20150804.ponder.question.1.sql create mode 100644 resources/sql/autopatches/20150804.ponder.question.2.sql create mode 100644 resources/sql/autopatches/20150804.ponder.question.3.sql create mode 100644 src/applications/ponder/capability/PonderQuestionDefaultEditCapability.php create mode 100644 src/applications/ponder/capability/PonderQuestionDefaultViewCapability.php diff --git a/resources/sql/autopatches/20150804.ponder.question.1.sql b/resources/sql/autopatches/20150804.ponder.question.1.sql new file mode 100644 index 0000000000..8ea3b2c34c --- /dev/null +++ b/resources/sql/autopatches/20150804.ponder.question.1.sql @@ -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; diff --git a/resources/sql/autopatches/20150804.ponder.question.2.sql b/resources/sql/autopatches/20150804.ponder.question.2.sql new file mode 100644 index 0000000000..ddca10f753 --- /dev/null +++ b/resources/sql/autopatches/20150804.ponder.question.2.sql @@ -0,0 +1,2 @@ +UPDATE {$NAMESPACE}_ponder.ponder_question + SET editPolicy = authorPHID WHERE editPolicy = ''; diff --git a/resources/sql/autopatches/20150804.ponder.question.3.sql b/resources/sql/autopatches/20150804.ponder.question.3.sql new file mode 100644 index 0000000000..0a359453ea --- /dev/null +++ b/resources/sql/autopatches/20150804.ponder.question.3.sql @@ -0,0 +1,2 @@ +UPDATE {$NAMESPACE}_ponder.ponder_question + SET viewPolicy = 'users' WHERE viewPolicy = ''; diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 779c48bd3c..9636e776db 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -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', diff --git a/src/applications/ponder/application/PhabricatorPonderApplication.php b/src/applications/ponder/application/PhabricatorPonderApplication.php index 953f683128..11e90dc274 100644 --- a/src/applications/ponder/application/PhabricatorPonderApplication.php +++ b/src/applications/ponder/application/PhabricatorPonderApplication.php @@ -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, diff --git a/src/applications/ponder/capability/PonderQuestionDefaultEditCapability.php b/src/applications/ponder/capability/PonderQuestionDefaultEditCapability.php new file mode 100644 index 0000000000..2f9ca73b0f --- /dev/null +++ b/src/applications/ponder/capability/PonderQuestionDefaultEditCapability.php @@ -0,0 +1,12 @@ +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()) diff --git a/src/applications/ponder/editor/PonderQuestionEditor.php b/src/applications/ponder/editor/PonderQuestionEditor.php index 9dbe58cb0e..5d375b2972 100644 --- a/src/applications/ponder/editor/PonderQuestionEditor.php +++ b/src/applications/ponder/editor/PonderQuestionEditor.php @@ -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; diff --git a/src/applications/ponder/storage/PonderQuestion.php b/src/applications/ponder/storage/PonderQuestion.php index 1568c59245..17eee8ffb1 100644 --- a/src/applications/ponder/storage/PonderQuestion.php +++ b/src/applications/ponder/storage/PonderQuestion.php @@ -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) {