diff --git a/datamodels/2.x/itop-portal-base/portal/src/handlers/exceptionhandler.class.inc.php b/datamodels/2.x/itop-portal-base/portal/src/handlers/exceptionhandler.class.inc.php new file mode 100644 index 0000000000..35364dae48 --- /dev/null +++ b/datamodels/2.x/itop-portal-base/portal/src/handlers/exceptionhandler.class.inc.php @@ -0,0 +1,146 @@ + + +namespace Combodo\iTop\Portal\Handler; + +use \Exception; +use \Silex\Application; +use \Symfony\Component\Debug\ExceptionHandler as BaseExceptionHandler; +use \Symfony\Component\Debug\Exception\FlattenException; +use \Symfony\Component\HttpFoundation\Request; +use \IssueLog; +use \Dict; + +/** + * Extends the default ExceptionHandler to provide a better template. + * + * @author Guillaume Lajarige + */ +class ExceptionHandler extends BaseExceptionHandler +{ + private $debug; + private $charset; + private $handler; + private $caughtBuffer; + private $caughtLength; + private $fileLinkFormat; + + /** + * Sends a response for the given Exception. + * + * To be as fail-safe as possible, the exception is first handled + * by our simple exception handler, then by the user exception handler. + * The latter takes precedence and any output from the former is cancelled, + * if and only if nothing bad happens in this handling path. + */ + public function handle(\Exception $exception) + { + IssueLog::Error('Portal: '.$exception->getMessage()); + + parent::handle($exception); + } + + /** + * Gets the HTML content associated with the given exception. + * + * @param FlattenException $exception A FlattenException instance + * + * @return string The content as a string + */ + public function getContent(FlattenException $exception) + { + switch ($exception->getStatusCode()) { + case 404: + $title = Dict::S('Error:HTTP:404'); + break; + default: + $title = Dict::S('Error:HTTP:500'); + } + + $content = ''; + if ($this->debug) { + try { + $count = count($exception->getAllPrevious()); + $total = $count + 1; + foreach ($exception->toArray() as $position => $e) { + $ind = $count - $position + 1; + $class = $this->formatClass($e['class']); + $message = nl2br($this->escapeHtml($e['message'])); + $content .= sprintf(<< + %d/%d + %s%s: + %s + +
+
    + +EOF + , $ind, $total, $class, $this->formatPath($e['trace'][0]['file'], $e['trace'][0]['line']), $message); + foreach ($e['trace'] as $trace) { + $content .= '
  1. '; + if ($trace['function']) { + $content .= sprintf('at %s%s%s(%s)', $this->formatClass($trace['class']), $trace['type'], $trace['function'], $this->formatArgs($trace['args'])); + } + if (isset($trace['file']) && isset($trace['line'])) { + $content .= $this->formatPath($trace['file'], $trace['line']); + } + $content .= "
  2. \n"; + } + + $content .= "
\n
\n"; + } + } catch (\Exception $e) { + // something nasty happened and we cannot throw an exception anymore + if ($this->debug) { + $title = sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $this->escapeHtml($e->getMessage())); + } else { + $title = 'Whoops, looks like something went wrong.'; + } + } + } + else{ + $content = $exception->getMessage(); + } + + return << +

$title

+
$content
+ +EOF; + } + + /** + * Gets the stylesheet associated with the given exception. + * + * @param FlattenException $exception A FlattenException instance + * + * @return string The stylesheet as a string + */ + public function getStylesheet(FlattenException $exception) + { + $parentStylesheet = parent::getStylesheet($exception); + $stylesheet = <<error(function(Exception $e, $code) use ($oApp) @@ -489,7 +492,22 @@ class ApplicationHelper // Contact $sContactPhotoUrl = $oApp['combodo.portal.base.absolute_url'] . 'img/user-profile-default-256px.png'; - $oContact = UserRights::GetContactObject(); + // - Checking if we can load the contact + try{ + $oContact = UserRights::GetContactObject(); + } + catch(Exception $e) + { + $oAllowedOrgSet = $oUser->Get('allowed_org_list'); + if($oAllowedOrgSet->Count() > 0) + { + throw new Exception('Could not load contact related to connected user. (Tip: Make sure the contact\'s organization is among the user\'s allowed organizations)'); + } + else{ + throw new Exception('Could not load contact related to connected user.'); + } + } + // - Retrieving picture if ($oContact) { if (MetaModel::IsValidAttCode(get_class($oContact), 'picture')) diff --git a/datamodels/2.x/itop-portal-base/portal/web/index.php b/datamodels/2.x/itop-portal-base/portal/web/index.php index 8fbfa92529..c7531bb620 100644 --- a/datamodels/2.x/itop-portal-base/portal/web/index.php +++ b/datamodels/2.x/itop-portal-base/portal/web/index.php @@ -31,6 +31,7 @@ require_once APPROOT . '/core/moduledesign.class.inc.php'; require_once APPROOT . '/application/loginwebpage.class.inc.php'; require_once APPROOT . '/sources/autoload.php'; // Portal +require_once __DIR__ . '/../src/handlers/exceptionhandler.class.inc.php'; require_once __DIR__ . '/../src/providers/urlgeneratorserviceprovider.class.inc.php'; require_once __DIR__ . '/../src/helpers/urlgeneratorhelper.class.inc.php'; require_once __DIR__ . '/../src/providers/contextmanipulatorserviceprovider.class.inc.php';