1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-09-23 02:38:48 +02:00
phorge-phorge/src/applications/base/controller/PhabricatorController.php

347 lines
9.9 KiB
PHP
Raw Normal View History

<?php
2011-01-23 02:48:55 +01:00
abstract class PhabricatorController extends AphrontController {
private $handles;
2011-01-26 22:21:12 +01:00
public function shouldRequireLogin() {
// If this install is configured to allow public resources and the
// controller works in public mode, allow the request through.
$is_public_allowed = PhabricatorEnv::getEnvConfig('policy.allow-public');
if ($is_public_allowed && $this->shouldAllowPublic()) {
return false;
}
2011-01-26 22:21:12 +01:00
return true;
}
public function shouldRequireAdmin() {
return false;
}
public function shouldRequireEnabledUser() {
return true;
}
public function shouldAllowPublic() {
return false;
}
public function shouldRequireEmailVerification() {
$need_verify = PhabricatorUserEmail::isEmailVerificationRequired();
$need_login = $this->shouldRequireLogin();
return ($need_login && $need_verify);
}
2011-01-26 22:21:12 +01:00
final public function willBeginExecution() {
$request = $this->getRequest();
$user = new PhabricatorUser();
$phusr = $request->getCookie('phusr');
$phsid = $request->getCookie('phsid');
if (strlen($phusr) && $phsid) {
2011-01-26 22:21:12 +01:00
$info = queryfx_one(
$user->establishConnection('r'),
'SELECT u.* FROM %T u JOIN %T s ON u.phid = s.userPHID
AND s.type LIKE %> AND s.sessionKey = %s',
2011-01-26 22:21:12 +01:00
$user->getTableName(),
'phabricator_session',
'web-',
PhabricatorHash::digest($phsid));
2011-01-26 22:21:12 +01:00
if ($info) {
$user->loadFromArray($info);
}
}
$translation = $user->getTranslation();
if ($translation &&
$translation != PhabricatorEnv::getEnvConfig('translation.provider')) {
$translation = newv($translation, array());
PhutilTranslator::getInstance()
->setLanguage($translation->getLanguage())
->addTranslations($translation->getTranslations());
}
2011-01-26 22:21:12 +01:00
$request->setUser($user);
if ($user->getIsDisabled() && $this->shouldRequireEnabledUser()) {
$disabled_user_controller = new PhabricatorDisabledUserController(
$request);
return $this->delegateToController($disabled_user_controller);
}
$event = new PhabricatorEvent(
PhabricatorEventType::TYPE_CONTROLLER_CHECKREQUEST,
array(
'request' => $request,
'controller' => $this,
));
$event->setUser($user);
PhutilEventEngine::dispatchEvent($event);
$checker_controller = $event->getValue('controller');
if ($checker_controller != $this) {
return $this->delegateToController($checker_controller);
}
$preferences = $user->loadPreferences();
2011-02-05 20:45:13 +01:00
if (PhabricatorEnv::getEnvConfig('darkconsole.enabled')) {
$dark_console = PhabricatorUserPreferences::PREFERENCE_DARK_CONSOLE;
if ($preferences->getPreference($dark_console) ||
PhabricatorEnv::getEnvConfig('darkconsole.always-on')) {
2011-02-05 20:45:13 +01:00
$console = new DarkConsoleCore();
$request->getApplicationConfiguration()->setConsole($console);
}
}
2011-01-26 22:21:12 +01:00
if ($this->shouldRequireLogin() && !$user->getPHID()) {
$login_controller = new PhabricatorAuthStartController($request);
$this->setCurrentApplication(
PhabricatorApplication::getByClass('PhabricatorApplicationAuth'));
return $this->delegateToController($login_controller);
}
if ($this->shouldRequireEmailVerification()) {
$email = $user->loadPrimaryEmail();
if (!$email) {
throw new Exception(
"No primary email address associated with this account!");
}
if (!$email->getIsVerified()) {
$verify_controller = new PhabricatorMustVerifyEmailController($request);
return $this->delegateToController($verify_controller);
}
}
if ($this->shouldRequireAdmin() && !$user->getIsAdmin()) {
return new Aphront403Response();
}
If a user can't see an application, prevent them from using its controllers Summary: Ref T603. Broadly, this allows you to implement a policy like "Only users in Engineering can use Differential." This isn't complete, and there will be a long tail of special cases to deal with. Some examples: - If you can't use Differential, should you still be able to attach/detach revisions from tasks? - You currently will be able to. - This actually seems pretty reasonable. - But in other cases it might not be: the "send user a message" action should probably require access to Conpherence. - If you can't use Differential, should you still be able to see feed stories about it? - You currently will be able to, if you can see the revisions. - This seems not-so-reasonable and we should probably lock it down. - If you can't use Differential, can users CC you on revisions? - Currently, they can, and you can't do anything about it. - Probably they shouldn't be able to? This seems challenging to explain in the UI. - If you can't use Differential, can you write a Herald rule against it? - You currently will be able to. - Seems like you obviously shouldn't be able to. - I think this is a general issue right now (you can still write Differential herald rules even if you uninstall the application, I believe). There are probably a few more things I haven't thought of. However, there are a finite number of these things and I suspect there aren't //too/ many more than this -- I can't come up with like 100 of them, and half of the ones above have easy fixes. Despite the rough edges, I think this accomplishes 95% of what installs expect from it. Test Plan: Restricted Differential and saw it vanish from the home page. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T603 Differential Revision: https://secure.phabricator.com/D7203
2013-10-03 21:39:41 +02:00
// If the user doesn't have access to the application, don't let them use
// any of its controllers. We query the application in order to generate
// a policy exception if the viewer doesn't have permission.
$application = $this->getCurrentApplication();
if ($application) {
/*
TODO: Reenable this, but it's breaking some applications which need public
access in all cases, like Files and Conduit.
If a user can't see an application, prevent them from using its controllers Summary: Ref T603. Broadly, this allows you to implement a policy like "Only users in Engineering can use Differential." This isn't complete, and there will be a long tail of special cases to deal with. Some examples: - If you can't use Differential, should you still be able to attach/detach revisions from tasks? - You currently will be able to. - This actually seems pretty reasonable. - But in other cases it might not be: the "send user a message" action should probably require access to Conpherence. - If you can't use Differential, should you still be able to see feed stories about it? - You currently will be able to, if you can see the revisions. - This seems not-so-reasonable and we should probably lock it down. - If you can't use Differential, can users CC you on revisions? - Currently, they can, and you can't do anything about it. - Probably they shouldn't be able to? This seems challenging to explain in the UI. - If you can't use Differential, can you write a Herald rule against it? - You currently will be able to. - Seems like you obviously shouldn't be able to. - I think this is a general issue right now (you can still write Differential herald rules even if you uninstall the application, I believe). There are probably a few more things I haven't thought of. However, there are a finite number of these things and I suspect there aren't //too/ many more than this -- I can't come up with like 100 of them, and half of the ones above have easy fixes. Despite the rough edges, I think this accomplishes 95% of what installs expect from it. Test Plan: Restricted Differential and saw it vanish from the home page. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T603 Differential Revision: https://secure.phabricator.com/D7203
2013-10-03 21:39:41 +02:00
id(new PhabricatorApplicationQuery())
->setViewer($user)
->withPHIDs(array($application->getPHID()))
->executeOne();
*/
If a user can't see an application, prevent them from using its controllers Summary: Ref T603. Broadly, this allows you to implement a policy like "Only users in Engineering can use Differential." This isn't complete, and there will be a long tail of special cases to deal with. Some examples: - If you can't use Differential, should you still be able to attach/detach revisions from tasks? - You currently will be able to. - This actually seems pretty reasonable. - But in other cases it might not be: the "send user a message" action should probably require access to Conpherence. - If you can't use Differential, should you still be able to see feed stories about it? - You currently will be able to, if you can see the revisions. - This seems not-so-reasonable and we should probably lock it down. - If you can't use Differential, can users CC you on revisions? - Currently, they can, and you can't do anything about it. - Probably they shouldn't be able to? This seems challenging to explain in the UI. - If you can't use Differential, can you write a Herald rule against it? - You currently will be able to. - Seems like you obviously shouldn't be able to. - I think this is a general issue right now (you can still write Differential herald rules even if you uninstall the application, I believe). There are probably a few more things I haven't thought of. However, there are a finite number of these things and I suspect there aren't //too/ many more than this -- I can't come up with like 100 of them, and half of the ones above have easy fixes. Despite the rough edges, I think this accomplishes 95% of what installs expect from it. Test Plan: Restricted Differential and saw it vanish from the home page. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T603 Differential Revision: https://secure.phabricator.com/D7203
2013-10-03 21:39:41 +02:00
}
2011-01-26 22:21:12 +01:00
}
public function buildStandardPageView() {
$view = new PhabricatorStandardPageView();
$view->setRequest($this->getRequest());
$view->setController($this);
2011-01-26 22:21:12 +01:00
return $view;
}
2011-02-03 07:38:42 +01:00
public function buildStandardPageResponse($view, array $data) {
2011-01-26 22:21:12 +01:00
$page = $this->buildStandardPageView();
2011-01-23 02:48:55 +01:00
$page->appendChild($view);
$response = new AphrontWebpageResponse();
$response->setContent($page->render());
return $response;
}
public function getApplicationURI($path = '') {
if (!$this->getCurrentApplication()) {
throw new Exception("No application!");
}
return $this->getCurrentApplication()->getApplicationURI($path);
}
public function buildApplicationPage($view, array $options) {
$page = $this->buildStandardPageView();
$title = PhabricatorEnv::getEnvConfig('phabricator.serious-business') ?
'Phabricator' :
pht('Bacon Ice Cream for Breakfast');
$application = $this->getCurrentApplication();
$page->setTitle(idx($options, 'title', $title));
if ($application) {
$page->setApplicationName($application->getName());
if ($application->getTitleGlyph()) {
$page->setGlyph($application->getTitleGlyph());
}
}
if (!($view instanceof AphrontSideNavFilterView)) {
$nav = new AphrontSideNavFilterView();
$nav->appendChild($view);
$view = $nav;
}
$user = $this->getRequest()->getUser();
$view->setUser($user);
$page->appendChild($view);
$object_phids = idx($options, 'pageObjects', array());
if ($object_phids) {
$page->appendPageObjects($object_phids);
foreach ($object_phids as $object_phid) {
PhabricatorFeedStoryNotification::updateObjectNotificationViews(
$user,
$object_phid);
}
}
if (idx($options, 'device')) {
$page->setDeviceReady(true);
}
$page->setShowChrome(idx($options, 'chrome', true));
$application_menu = $this->buildApplicationMenu();
if ($application_menu) {
$page->setApplicationMenu($application_menu);
}
$response = new AphrontWebpageResponse();
return $response->setContent($page->render());
}
public function didProcessRequest($response) {
$request = $this->getRequest();
$response->setRequest($request);
$seen = array();
while ($response instanceof AphrontProxyResponse) {
$hash = spl_object_hash($response);
if (isset($seen[$hash])) {
$seen[] = get_class($response);
throw new Exception(
"Cycle while reducing proxy responses: ".
implode(' -> ', $seen));
}
$seen[$hash] = get_class($response);
$response = $response->reduceProxyResponse();
}
if ($response instanceof AphrontDialogResponse) {
if (!$request->isAjax()) {
$view = new PhabricatorStandardPageView();
$view->setRequest($request);
$view->setController($this);
2013-02-13 23:50:15 +01:00
$view->appendChild(hsprintf(
'<div style="padding: 2em 0;">%s</div>',
$response->buildResponseString()));
$response = new AphrontWebpageResponse();
$response->setContent($view->render());
return $response;
} else {
$response->getDialog()->setIsStandalone(true);
return id(new AphrontAjaxResponse())
->setContent(array(
'dialog' => $response->buildResponseString(),
));
}
} else if ($response instanceof AphrontRedirectResponse) {
if ($request->isAjax()) {
return id(new AphrontAjaxResponse())
->setContent(
array(
'redirect' => $response->getURI(),
));
}
}
return $response;
}
protected function getHandle($phid) {
if (empty($this->handles[$phid])) {
throw new Exception(
"Attempting to access handle which wasn't loaded: {$phid}");
}
return $this->handles[$phid];
}
protected function loadHandles(array $phids) {
$phids = array_filter($phids);
$this->handles = $this->loadViewerHandles($phids);
return $this;
}
protected function getLoadedHandles() {
return $this->handles;
}
protected function loadViewerHandles(array $phids) {
return id(new PhabricatorHandleQuery())
->setViewer($this->getRequest()->getUser())
->withPHIDs($phids)
->execute();
}
/**
* Render a list of links to handles, identified by PHIDs. The handles must
* already be loaded.
*
* @param list<phid> List of PHIDs to render links to.
* @param string Style, one of "\n" (to put each item on its own line)
* or "," (to list items inline, separated by commas).
* @return string Rendered list of handle links.
*/
protected function renderHandlesForPHIDs(array $phids, $style = "\n") {
$style_map = array(
"\n" => phutil_tag('br'),
',' => ', ',
);
if (empty($style_map[$style])) {
throw new Exception("Unknown handle list style '{$style}'!");
}
return implode_selected_handle_links($style_map[$style],
$this->getLoadedHandles(),
$phids);
}
protected function buildApplicationMenu() {
return null;
}
protected function buildApplicationCrumbs() {
$crumbs = array();
$application = $this->getCurrentApplication();
if ($application) {
$sprite = $application->getIconName();
if (!$sprite) {
$sprite = 'application';
}
$crumbs[] = id(new PhabricatorCrumbView())
->setHref($this->getApplicationURI())
->setIcon($sprite);
}
$view = new PhabricatorCrumbsView();
foreach ($crumbs as $crumb) {
$view->addCrumb($crumb);
}
return $view;
}
}