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 @@ -5078,6 +5078,7 @@ 'PhabricatorTypeaheadDatasourceTestCase' => 'applications/typeahead/datasource/__tests__/PhabricatorTypeaheadDatasourceTestCase.php', 'PhabricatorTypeaheadFunctionHelpController' => 'applications/typeahead/controller/PhabricatorTypeaheadFunctionHelpController.php', 'PhabricatorTypeaheadInvalidTokenException' => 'applications/typeahead/exception/PhabricatorTypeaheadInvalidTokenException.php', + 'PhabricatorTypeaheadLoginRequiredException' => 'applications/typeahead/exception/PhabricatorTypeaheadLoginRequiredException.php', 'PhabricatorTypeaheadModularDatasourceController' => 'applications/typeahead/controller/PhabricatorTypeaheadModularDatasourceController.php', 'PhabricatorTypeaheadMonogramDatasource' => 'applications/typeahead/datasource/PhabricatorTypeaheadMonogramDatasource.php', 'PhabricatorTypeaheadProxyDatasource' => 'applications/typeahead/datasource/PhabricatorTypeaheadProxyDatasource.php', @@ -11819,6 +11820,7 @@ 'PhabricatorTypeaheadDatasourceTestCase' => 'PhabricatorTestCase', 'PhabricatorTypeaheadFunctionHelpController' => 'PhabricatorTypeaheadDatasourceController', 'PhabricatorTypeaheadInvalidTokenException' => 'Exception', + 'PhabricatorTypeaheadLoginRequiredException' => 'Exception', 'PhabricatorTypeaheadModularDatasourceController' => 'PhabricatorTypeaheadDatasourceController', 'PhabricatorTypeaheadMonogramDatasource' => 'PhabricatorTypeaheadDatasource', 'PhabricatorTypeaheadProxyDatasource' => 'PhabricatorTypeaheadCompositeDatasource', diff --git a/src/applications/calendar/typeahead/PhabricatorCalendarInviteeViewerFunctionDatasource.php b/src/applications/calendar/typeahead/PhabricatorCalendarInviteeViewerFunctionDatasource.php --- a/src/applications/calendar/typeahead/PhabricatorCalendarInviteeViewerFunctionDatasource.php +++ b/src/applications/calendar/typeahead/PhabricatorCalendarInviteeViewerFunctionDatasource.php @@ -28,8 +28,12 @@ ); } + protected function isFunctionWithLoginRequired($function) { + return true; + } + public function loadResults() { - if ($this->getViewer()->getPHID()) { + if ($this->getViewer()->isLoggedIn()) { $results = array($this->renderViewerFunctionToken()); } else { $results = array(); @@ -39,7 +43,7 @@ } protected function canEvaluateFunction($function) { - if (!$this->getViewer()->getPHID()) { + if (!$this->getViewer()->isLoggedIn()) { return false; } diff --git a/src/applications/differential/typeahead/DifferentialResponsibleViewerFunctionDatasource.php b/src/applications/differential/typeahead/DifferentialResponsibleViewerFunctionDatasource.php --- a/src/applications/differential/typeahead/DifferentialResponsibleViewerFunctionDatasource.php +++ b/src/applications/differential/typeahead/DifferentialResponsibleViewerFunctionDatasource.php @@ -28,8 +28,12 @@ ); } + protected function isFunctionWithLoginRequired($function) { + return true; + } + public function loadResults() { - if ($this->getViewer()->getPHID()) { + if ($this->getViewer()->isLoggedIn()) { $results = array($this->renderViewerFunctionToken()); } else { $results = array(); @@ -39,7 +43,7 @@ } protected function canEvaluateFunction($function) { - if (!$this->getViewer()->getPHID()) { + if (!$this->getViewer()->isLoggedIn()) { return false; } diff --git a/src/applications/people/typeahead/PhabricatorViewerDatasource.php b/src/applications/people/typeahead/PhabricatorViewerDatasource.php --- a/src/applications/people/typeahead/PhabricatorViewerDatasource.php +++ b/src/applications/people/typeahead/PhabricatorViewerDatasource.php @@ -34,8 +34,12 @@ ); } + protected function isFunctionWithLoginRequired($function) { + return true; + } + public function loadResults() { - if ($this->getViewer()->getPHID()) { + if ($this->getViewer()->isLoggedIn()) { $results = array($this->renderViewerFunctionToken()); } else { $results = array(); @@ -45,7 +49,7 @@ } protected function canEvaluateFunction($function) { - if (!$this->getViewer()->getPHID()) { + if (!$this->getViewer()->isLoggedIn()) { return false; } diff --git a/src/applications/project/typeahead/PhabricatorProjectLogicalViewerDatasource.php b/src/applications/project/typeahead/PhabricatorProjectLogicalViewerDatasource.php --- a/src/applications/project/typeahead/PhabricatorProjectLogicalViewerDatasource.php +++ b/src/applications/project/typeahead/PhabricatorProjectLogicalViewerDatasource.php @@ -35,8 +35,12 @@ ); } + protected function isFunctionWithLoginRequired($function) { + return true; + } + public function loadResults() { - if ($this->getViewer()->getPHID()) { + if ($this->getViewer()->isLoggedIn()) { $results = array($this->renderViewerProjectsFunctionToken()); } else { $results = array(); @@ -46,7 +50,7 @@ } protected function canEvaluateFunction($function) { - if (!$this->getViewer()->getPHID()) { + if (!$this->getViewer()->isLoggedIn()) { return false; } diff --git a/src/applications/search/controller/PhabricatorApplicationSearchController.php b/src/applications/search/controller/PhabricatorApplicationSearchController.php --- a/src/applications/search/controller/PhabricatorApplicationSearchController.php +++ b/src/applications/search/controller/PhabricatorApplicationSearchController.php @@ -346,6 +346,15 @@ $body[] = $pager_box; } } + } catch (PhabricatorTypeaheadLoginRequiredException $ex) { + + // A specific token requires login. Show login page. + $auth_class = PhabricatorAuthApplication::class; + $auth_application = PhabricatorApplication::getByClass($auth_class); + $login_controller = new PhabricatorAuthStartController(); + $this->setCurrentApplication($auth_application); + return $this->delegateToController($login_controller); + } catch (PhabricatorTypeaheadInvalidTokenException $ex) { $exec_errors[] = pht( 'This query specifies an invalid parameter. Review the '. diff --git a/src/applications/typeahead/datasource/PhabricatorTypeaheadCompositeDatasource.php b/src/applications/typeahead/datasource/PhabricatorTypeaheadCompositeDatasource.php --- a/src/applications/typeahead/datasource/PhabricatorTypeaheadCompositeDatasource.php +++ b/src/applications/typeahead/datasource/PhabricatorTypeaheadCompositeDatasource.php @@ -304,6 +304,15 @@ return parent::evaluateFunction($function, $argv); } + protected function isFunctionWithLoginRequired($function) { + foreach ($this->getUsableDatasources() as $source) { + if ($source->isFunctionWithLoginRequired($function)) { + return true; + } + } + return parent::isFunctionWithLoginRequired($function); + } + public function renderFunctionTokens($function, array $argv_list) { foreach ($this->getUsableDatasources() as $source) { if ($source->canEvaluateFunction($function)) { diff --git a/src/applications/typeahead/datasource/PhabricatorTypeaheadDatasource.php b/src/applications/typeahead/datasource/PhabricatorTypeaheadDatasource.php --- a/src/applications/typeahead/datasource/PhabricatorTypeaheadDatasource.php +++ b/src/applications/typeahead/datasource/PhabricatorTypeaheadDatasource.php @@ -365,6 +365,19 @@ } + /** + * Check if this datasource requires a logged-in viewer. + * @task functions + * @param string $function Function name. + * @return bool + */ + protected function isFunctionWithLoginRequired($function) { + // This is just a default. + // Make sure to override this method to require login. + return false; + } + + /** * @task functions */ @@ -498,6 +511,18 @@ if (!$this->canEvaluateFunction($function)) { if (!$allow_partial) { + + if ($this->isFunctionWithLoginRequired($function)) { + if (!$this->getViewer() || !$this->getViewer()->isLoggedIn()) { + throw new PhabricatorTypeaheadLoginRequiredException( + pht( + 'This datasource ("%s") requires to be logged-in to use the '. + 'function "%s(...)".', + get_class($this), + $function)); + } + } + throw new PhabricatorTypeaheadInvalidTokenException( pht( 'This datasource ("%s") can not evaluate the function "%s(...)".', diff --git a/src/applications/typeahead/exception/PhabricatorTypeaheadLoginRequiredException.php b/src/applications/typeahead/exception/PhabricatorTypeaheadLoginRequiredException.php new file mode 100644 --- /dev/null +++ b/src/applications/typeahead/exception/PhabricatorTypeaheadLoginRequiredException.php @@ -0,0 +1,6 @@ +