Skip to content

Commit

Permalink
Merge pull request #3335 from zikula/FC
Browse files Browse the repository at this point in the history
Fc
  • Loading branch information
craigh authored Dec 28, 2016
2 parents 82ab75a + 0802fae commit 9d37184
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 123 deletions.
1 change: 1 addition & 0 deletions src/lib/Zikula/Bundle/CoreBundle/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ protected function registerCommands()
/**
* Grants admin access for console commands (#1908).
* This avoids subsequent permission problems from any components used.
* @deprecated remove at Core-2.0
*/
protected function loginAsAdministrator()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

/**
* Doctrine listeners.
* @deprecated remove at Core-2.0
*/
class Doctrine1ConnectorListener implements EventSubscriberInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Zikula\Bundle\CoreBundle\EventListener;

use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand All @@ -22,33 +21,66 @@
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Zikula\Bundle\CoreBundle\CacheClearer;
use Zikula\Core\Exception\ExtensionNotAvailableException;
use Zikula\PermissionsModule\Api\PermissionApi;
use Zikula\UsersModule\Api\CurrentUserApi;

/**
* ExceptionListener catches exceptions and converts them to Response instances.
*/
class ExceptionListener implements EventSubscriberInterface
{
private $logger;
/**
* @var RouterInterface
*/
private $router;

/**
* @var EventDispatcherInterface
*/
private $dispatcher;

/**
* @var CacheClearer
*/
private $cacheClearer;

/**
* @var CurrentUserApi
*/
private $currentUserApi;

/**
* @var PermissionApi
*/
private $permissionApi;

/**
* @var bool
*/
private $installed;

/**
* ExceptionListener constructor.
* @param RouterInterface $router
* @param EventDispatcherInterface $dispatcher
* @param CacheClearer $cacheClearer
* @param CurrentUserApi $currentUserApi
* @param PermissionApi $permissionApi
* @param bool $installed
*/
public function __construct(
LoggerInterface $logger = null,
RouterInterface $router = null,
EventDispatcherInterface $dispatcher = null,
RouterInterface $router,
EventDispatcherInterface $dispatcher,
CacheClearer $cacheClearer,
CurrentUserApi $currentUserApi,
PermissionApi $permissionApi,
$installed
) {
$this->logger = $logger;
$this->router = $router;
$this->dispatcher = $dispatcher;
$this->cacheClearer = $cacheClearer;
$this->currentUserApi = $currentUserApi;
$this->permissionApi = $permissionApi;
$this->installed = $installed;
}

Expand Down Expand Up @@ -131,9 +163,9 @@ private function handleRouteNotFoundException(GetResponseForExceptionEvent $even
{
$message = $event->getException()->getMessage();
$event->getRequest()->getSession()->getFlashBag()->add('error', $message);
if ($userLoggedIn && \SecurityUtil::checkPermission('ZikulaRoutesModule::', '::', ACCESS_ADMIN)) {
if ($userLoggedIn && $this->permissionApi->hasPermission('ZikulaRoutesModule::', '::', ACCESS_ADMIN)) {
try {
$url = $this->router->generate('zikularoutesmodule_route_reload', ['lct' => 'admin'], RouterInterface::ABSOLUTE_URL);
$url = $this->router->generate('zikularoutesmodule_route_adminreload', [], RouterInterface::ABSOLUTE_URL);
$link = '<a href="' . $url . '" title="' . __('Re-load the routes') . '">' . __('re-loading the routes') . '</a>';
$event->getRequest()->getSession()->getFlashBag()->add('error', __f('You might try %s for the extension in question.', $link));
} catch (RouteNotFoundException $e) {
Expand Down
24 changes: 21 additions & 3 deletions src/lib/Zikula/Bundle/CoreBundle/Resources/config/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ services:

zikula_core.common.csrf_token_handler:
class: %zikula_core.common.csrf_token_handler.class%
arguments: ["@zikula_core.internal.token.generator", "@zikula_core.internal.token.validator", "@request_stack", "@zikula_extensions_module.api.variable", "@session"]
arguments:
- "@zikula_core.internal.token.generator"
- "@zikula_core.internal.token.validator"
- "@request_stack"
- "@zikula_extensions_module.api.variable"
- "@session"

zikula_core.internal.clickjack_protection_listener:
class: %zikula_core.internal.clickjack_protection_listener.class%
Expand All @@ -69,7 +74,14 @@ services:

zikula_core.internal.site_off_listener:
class: %zikula_core.internal.site_off_listener.class%
arguments: ["@zikula_extensions_module.api.variable", "@zikula_permissions_module.api.permission", "@zikula_users_module.current_user", "@templating", "@form.factory", "@router", "%installed%"]
arguments:
- "@zikula_extensions_module.api.variable"
- "@zikula_permissions_module.api.permission"
- "@zikula_users_module.current_user"
- "@templating"
- "@form.factory"
- "@router"
- "%installed%"
tags:
- { name: kernel.event_subscriber }

Expand All @@ -81,7 +93,13 @@ services:

zikula_core.internal.exception_listener:
class: %zikula_core.internal.exception_listener.class%
arguments: ["@?logger", "@router", "@event_dispatcher", "@zikula.cache_clearer", "@zikula_users_module.current_user", "%installed%"]
arguments:
- "@router"
- "@event_dispatcher"
- "@zikula.cache_clearer"
- "@zikula_users_module.current_user"
- "@zikula_permissions_module.api.permission"
- "%installed%"
tags:
- { name: kernel.event_subscriber }
- { name: monolog.logger, channel: request }
Expand Down
116 changes: 55 additions & 61 deletions src/lib/Zikula/Bundle/CoreBundle/Twig/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Zikula\Bundle\CoreBundle\Twig;
use Zikula\Bundle\CoreBundle\Twig\Extension\SimpleFunction\DefaultPathSimpleFunction;
use Zikula\Bundle\CoreBundle\Twig\Extension\SimpleFunction\DispatchEventSimpleFunction;
use Zikula\Common\Translator\TranslatorInterface;
use Zikula\ThemeModule\Engine\AssetBag;

class CoreExtension extends \Twig_Extension
Expand All @@ -24,9 +25,15 @@ class CoreExtension extends \Twig_Extension
*/
private $container;

/**
* @var TranslatorInterface
*/
private $translator;

public function __construct(ContainerInterface $container = null)
{
$this->container = $container;
$this->translator = $container->get('translator.default');
}

/**
Expand Down Expand Up @@ -212,9 +219,7 @@ public function yesNo($string)
return $string;
}

$translator = $this->container->get('translator.default');

return (bool)$string ? $translator->__('Yes') : $translator->__('No');
return (bool)$string ? $this->translator->__('Yes') : $this->translator->__('No');
}

/**
Expand Down Expand Up @@ -277,10 +282,7 @@ public function profileLinkByUserId($userId, $class = '', $image = '', $maxLengt
return $userId;
}

$userId = (int)$userId;
$userName = \UserUtil::getVar('uname', $userId);

return $this->determineProfileLink($userId, $userName, $class, $image, $maxLength);
return $this->determineProfileLink((int)$userId, null, $class, $image, $maxLength);
}

/**
Expand All @@ -307,9 +309,7 @@ public function profileLinkByUserName($userName, $class = '', $image = '', $maxL
return $userName;
}

$userId = \UserUtil::getIdFromName($userName);

return $this->determineProfileLink($userId, $userName, $class, $image, $maxLength);
return $this->determineProfileLink(null, $userName, $class, $image, $maxLength);
}

/**
Expand All @@ -318,45 +318,47 @@ public function profileLinkByUserName($userName, $class = '', $image = '', $maxL
* @param integer $userId The users uid
* @param string $userName The users name
* @param string $class The class name for the link (optional)
* @param string $image Path to the image to show instead of the username (optional)
* @param string $imagePath Path to the image to show instead of the username (optional)
* @param integer $maxLength If set then user names are truncated to x chars
* @return string The output
*/
private function determineProfileLink($userId, $userName, $class = '', $image = '', $maxLength = 0)
private function determineProfileLink($userId = null, $userName = null, $class = '', $imagePath = '', $maxLength = 0)
{
$profileLink = '';

$profileModule = \System::getVar('profilemodule', '');

if ($userId && $userId > 1 && !empty($profileModule) && \ModUtil::available($profileModule)) {
$userDisplayName = \ModUtil::apiFunc($profileModule, 'user', 'getUserDisplayName', ['uid' => $userId]);
if (empty($userDisplayName)) {
$userDisplayName = $userName;
}

if (!empty($class)) {
$class = ' class="' . \DataUtil::formatForDisplay($class) . '"';
}
if (!isset($userId) && !isset($userName)) {
throw new \InvalidArgumentException();
}
if ($userId) {
$user = $this->container->get('zikula_users_module.user_repository')->find($userId);
} else {
$user = $this->container->get('zikula_users_module.user_repository')->findBy(['uname' => $userName]);
}

if (!empty($image)) {
$show = '<img src="' . \DataUtil::formatForDisplay($image) . '" alt="' . \DataUtil::formatForDisplay($userDisplayName) . '" />';
} elseif ($maxLength > 0) {
// truncate the user name to $maxLength chars
$length = strlen($userDisplayName);
$truncEnd = ($maxLength > $length) ? $length : $maxLength;
$show = \DataUtil::formatForDisplay(substr($userDisplayName, 0, $truncEnd));
} else {
$show = \DataUtil::formatForDisplay($userDisplayName);
}
$profileModule = $this->container->get('zikula_extensions_module.api.variable')->getSystemVar('profilemodule', '');
if (empty($profileModule) || !$this->container->get('kernel')->isBundle($profileModule)) {
return $user->getUname();
}

$profileLink = '<a' . $class . ' title="' . \DataUtil::formatForDisplay(__('Profile')) . ': ' . \DataUtil::formatForDisplay($userDisplayName) . '" href="' . \DataUtil::formatForDisplay(\ModUtil::url($profileModule, 'user', 'view', ['uid' => $userId], null, null, true)) . '">' . $show . '</a>';
} elseif (!empty($image)) {
$profileLink = ''; // image for anonymous user should be "empty"
// @todo replace with ProfileInterface usage
$userDisplayName = \ModUtil::apiFunc($profileModule, 'user', 'getUserDisplayName', ['uid' => $user->getUid()]);
if (empty($userDisplayName)) {
$userDisplayName = $user->getUname();
}
$class = !empty($class) ? ' class="' . htmlspecialchars($class, ENT_QUOTES) . '"' : '';

if (!empty($imagePath)) {
$show = '<img src="' . htmlspecialchars($imagePath, ENT_QUOTES) . '" alt="' . htmlspecialchars($userDisplayName, ENT_QUOTES) . '" />';
} elseif ($maxLength > 0) {
// truncate the user name to $maxLength chars
$length = strlen($userDisplayName);
$truncEnd = ($maxLength > $length) ? $length : $maxLength;
$show = htmlspecialchars(substr($userDisplayName, 0, $truncEnd), ENT_QUOTES);
} else {
$profileLink = \DataUtil::formatForDisplay($userName);
$show = htmlspecialchars($userDisplayName, ENT_QUOTES);
}
// @todo replace with ProfileInterface usage
$href = htmlspecialchars(\ModUtil::url($profileModule, 'user', 'view', ['uid' => $userId], null, null, true));

return $profileLink;
return '<a' . $class . ' title="' . (__('Profile')) . ': ' . htmlspecialchars($userDisplayName, ENT_QUOTES) . '" href="' . $href . '">' . $show . '</a>';
}

/**
Expand All @@ -369,8 +371,7 @@ private function determineProfileLink($userId, $userName, $class = '', $image =
public function pageSetVar($name, $value)
{
if (empty($name) || empty($value)) {
$translator = $this->container->get('translator.default');
throw new \InvalidArgumentException($translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
throw new \InvalidArgumentException($this->translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
}

$this->container->get('zikula_core.common.theme.pagevars')->set($name, $value);
Expand Down Expand Up @@ -408,12 +409,10 @@ public function pageAddVar($name, $value)
public function pageAddAsset($type, $value, $weight = AssetBag::WEIGHT_DEFAULT)
{
if (empty($type) || empty($value)) {
$translator = $this->container->get('translator.default');
throw new \InvalidArgumentException($translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
throw new \InvalidArgumentException($this->translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
}
if (!in_array($type, ['stylesheet', 'javascript', 'header', 'footer']) || !is_numeric($weight)) {
$translator = $this->container->get('translator.default');
throw new \InvalidArgumentException($translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
throw new \InvalidArgumentException($this->translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
}

// ensure proper variable types
Expand Down Expand Up @@ -448,8 +447,7 @@ public function pageAddAsset($type, $value, $weight = AssetBag::WEIGHT_DEFAULT)
public function pageGetVar($name, $default = null)
{
if (empty($name)) {
$translator = $this->container->get('translator.default');
throw new \InvalidArgumentException($translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
throw new \InvalidArgumentException($this->translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
}

return $this->container->get('zikula_core.common.theme.pagevars')->get($name, $default);
Expand All @@ -464,8 +462,7 @@ public function pageGetVar($name, $default = null)
public function getModVar($module, $name, $default = null)
{
if (empty($module) || empty($name)) {
$translator = $this->container->get('translator.default');
throw new \InvalidArgumentException($translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
throw new \InvalidArgumentException($this->translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
}

return $this->container->get('zikula_extensions_module.api.variable')->get($module, $name, $default);
Expand All @@ -479,8 +476,7 @@ public function getModVar($module, $name, $default = null)
public function getSystemVar($name, $default = null)
{
if (empty($name)) {
$translator = $this->container->get('translator.default');
throw new \InvalidArgumentException($translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
throw new \InvalidArgumentException($this->translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
}

return $this->container->get('zikula_extensions_module.api.variable')->getSystemVar($name, $default);
Expand All @@ -493,16 +489,16 @@ public function getSystemVar($name, $default = null)
public function setMetaTag($name, $value)
{
if (empty($name) || empty($value)) {
$translator = $this->container->get('translator.default');
throw new \InvalidArgumentException($translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
throw new \InvalidArgumentException($this->translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__);
}

$metaTags = $this->container->hasParameter('zikula_view.metatags') ? $this->container->getParameter('zikula_view.metatags') : [];
$metaTags[$name] = \DataUtil::formatForDisplay($value);
$metaTags[$name] = htmlspecialchars($value, ENT_QUOTES);
$this->container->setParameter('zikula_view.metatags', $metaTags);
}

/**
* @deprecated remove at Core-2.0
* @param string $modname
* @param bool $force
* @return bool
Expand All @@ -516,17 +512,15 @@ public function modAvailable($modname, $force = false)

/**
* Call a php callable with parameters.
* @param array|string $callable
* @param callable $callable
* @param array $params
* @return mixed
*/
public function callFunc($callable, array $params = [])
public function callFunc(callable $callable, array $params = [])
{
if (function_exists($callable) && is_callable($callable)) {
if (function_exists($callable)) {
return call_user_func_array($callable, $params);
}

$translator = $this->container->get('translator.default');
throw new \InvalidArgumentException($translator->__('Function does not exist or is not callable.') . ':' . __FILE__ . '::' . __LINE__);
throw new \InvalidArgumentException($this->translator->__('Function does not exist or is not callable.') . ':' . __FILE__ . '::' . __LINE__);
}
}
Loading

0 comments on commit 9d37184

Please sign in to comment.