diff --git a/resources/sql/autopatches/20150804.ponder.question.1.sql b/resources/sql/autopatches/20150804.ponder.question.1.sql new file mode 100644 --- /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 --- /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 --- /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 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -3399,6 +3399,8 @@ '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 @@ '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 --- a/src/applications/ponder/application/PhabricatorPonderApplication.php +++ b/src/applications/ponder/application/PhabricatorPonderApplication.php @@ -91,6 +91,19 @@ ); } + 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 --- /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 @@ 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 @@ $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 @@ ->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 @@ ->setNewValue(array('=' => array_fuse($v_projects))); $editor = id(new PonderQuestionEditor()) - ->setActor($user) + ->setActor($viewer) ->setContentSourceFromRequest($request) ->setContinueOnNoEffect(true); @@ -82,8 +89,13 @@ } } + $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 @@ ->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 --- a/src/applications/ponder/editor/PonderQuestionEditor.php +++ b/src/applications/ponder/editor/PonderQuestionEditor.php @@ -66,6 +66,9 @@ $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 --- a/src/applications/ponder/storage/PonderQuestion.php +++ b/src/applications/ponder/storage/PonderQuestion.php @@ -21,6 +21,8 @@ protected $status; protected $content; protected $contentSource; + protected $viewPolicy; + protected $editPolicy; protected $voteCount; protected $answerCount; @@ -31,6 +33,27 @@ 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 @@ } 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) {