Skip to content

Commit

Permalink
Added support to save xml and certificate data along with defining us…
Browse files Browse the repository at this point in the history
…er attributes for SAML
  • Loading branch information
alanhartless committed Dec 18, 2016
1 parent 9217eaa commit b92893e
Show file tree
Hide file tree
Showing 26 changed files with 1,009 additions and 257 deletions.
16 changes: 16 additions & 0 deletions app/Resources/LightSamlSpBundle/views/discovery.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="{{ asset('/media/css/libraries.css') }}" data-source="mautic">
<link rel="stylesheet" href="{{ asset('/media/css/app.css') }}" data-source="mautic">
</head>
<body>
<div class="container">
<div class="well mt-15">
<h4 class="text-center">SAML not configured or configured incorrectly.</h4>
</div>
</div>
</body>
</html>
4 changes: 2 additions & 2 deletions app/bundles/ConfigBundle/Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
'routes' => [
'main' => [
'mautic_config_action' => [
'path' => '/config/{objectAction}',
'path' => '/config/{objectAction}/{objectId}',
'controller' => 'MauticConfigBundle:Config:execute',
],
'mautic_sysinfo_index' => [
Expand Down Expand Up @@ -56,7 +56,7 @@
'forms' => [
'mautic.form.type.config' => [
'class' => 'Mautic\ConfigBundle\Form\Type\ConfigType',
'arguments' => 'mautic.factory',
'arguments' => 'translator',
'alias' => 'config',
],
],
Expand Down
173 changes: 122 additions & 51 deletions app/bundles/ConfigBundle/Controller/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use Mautic\ConfigBundle\Event\ConfigEvent;
use Mautic\CoreBundle\Controller\FormController;
use Mautic\CoreBundle\Helper\EncryptionHelper;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\Response;

/**
* Class ConfigController.
Expand All @@ -35,9 +37,11 @@ public function editAction()
return $this->accessDenied();
}

$event = new ConfigBuilderEvent($this->factory);
$event = new ConfigBuilderEvent($this->get('mautic.helper.paths'), $this->get('mautic.helper.bundle'));
$dispatcher = $this->get('event_dispatcher');
$dispatcher->dispatch(ConfigEvents::CONFIG_ON_GENERATE, $event);
// Extract and base64 encode file contents
$fileFields = $event->getFileFields();
$formConfigs = $event->getForms();
$formThemes = $event->getFormThemes();
$doNotChange = $this->coreParametersHelper->getParameter('security.restrictedConfigFields');
Expand All @@ -50,11 +54,16 @@ public function editAction()

// Create the form
$action = $this->generateUrl('mautic_config_action', ['objectAction' => 'edit']);
$form = $model->createForm($formConfigs, $this->get('form.factory'), [
'action' => $action,
'doNotChange' => $doNotChange,
'doNotChangeDisplayMode' => $doNotChangeDisplayMode,
]);
$form = $model->createForm(
$formConfigs,
$this->get('form.factory'),
[
'action' => $action,
'doNotChange' => $doNotChange,
'doNotChangeDisplayMode' => $doNotChangeDisplayMode,
'fileFields' => $fileFields,
]
);

/** @var \Mautic\CoreBundle\Configurator\Configurator $configurator */
$configurator = $this->get('mautic.configurator');
Expand All @@ -75,47 +84,66 @@ public function editAction()
$dispatcher->dispatch(ConfigEvents::CONFIG_PRE_SAVE, $configEvent);
$formValues = $configEvent->getConfig();

foreach ($configEvent->getErrors() as $message => $messageVars) {
$this->addFlash($message, $messageVars);
}
$errors = $configEvent->getErrors();
$fieldErrors = $configEvent->getFieldErrors();

// Prevent these from getting overwritten with empty values
$unsetIfEmpty = $configEvent->getPreservedFields();
if ($errors || $fieldErrors) {
foreach ($errors as $message => $messageVars) {
$form->addError(
new FormError($this->translator->trans($message, $messageVars, 'validators'))
);
}

// Merge each bundle's updated configuration into the local configuration
foreach ($formValues as $object) {
$checkThese = array_intersect(array_keys($object), $unsetIfEmpty);
foreach ($checkThese as $checkMe) {
if (empty($object[$checkMe])) {
unset($object[$checkMe]);
foreach ($fieldErrors as $key => $fields) {
foreach ($fields as $field => $fieldError) {
$form[$key][$field]->addError(
new FormError($this->translator->trans($fieldError[0], $fieldError[1], 'validators'))
);
}
}
$isValid = false;
} else {
// Prevent these from getting overwritten with empty values
$unsetIfEmpty = $configEvent->getPreservedFields();
$unsetIfEmpty = array_merge($unsetIfEmpty, $fileFields);

// Merge each bundle's updated configuration into the local configuration
foreach ($formValues as $key => $object) {
$checkThese = array_intersect(array_keys($object), $unsetIfEmpty);
foreach ($checkThese as $checkMe) {
if (empty($object[$checkMe])) {
unset($object[$checkMe]);
}
}

$configurator->mergeParameters($object);
}

try {
// Ensure the config has a secret key
$params = $configurator->getParameters();
if (empty($params['secret_key'])) {
$configurator->mergeParameters(['secret_key' => EncryptionHelper::generateKey()]);
$configurator->mergeParameters($object);
}

$configurator->write();
try {
// Ensure the config has a secret key
$params = $configurator->getParameters();
if (empty($params['secret_key'])) {
$configurator->mergeParameters(['secret_key' => EncryptionHelper::generateKey()]);
}

$configurator->write();

$this->addFlash('mautic.config.config.notice.updated');
$this->addFlash('mautic.config.config.notice.updated');

// We must clear the application cache for the updated values to take effect
/** @var \Mautic\CoreBundle\Helper\CacheHelper $cacheHelper */
$cacheHelper = $this->factory->getHelper('cache');
$cacheHelper->clearContainerFile();
} catch (\RuntimeException $exception) {
$this->addFlash('mautic.config.config.error.not.updated', ['%exception%' => $exception->getMessage()], 'error');
// We must clear the application cache for the updated values to take effect
/** @var \Mautic\CoreBundle\Helper\CacheHelper $cacheHelper */
$cacheHelper = $this->get('mautic.helper.cache');
$cacheHelper->clearContainerFile();
} catch (\RuntimeException $exception) {
$this->addFlash('mautic.config.config.error.not.updated', ['%exception%' => $exception->getMessage()], 'error');
}
}
} elseif (!$isWritabale) {
$form->addError(new FormError(
$this->translator->trans('mautic.config.notwritable')
));
$form->addError(
new FormError(
$this->translator->trans('mautic.config.notwritable')
)
);
}
}

Expand All @@ -131,21 +159,64 @@ public function editAction()

$tmpl = $this->request->isXmlHttpRequest() ? $this->request->get('tmpl', 'index') : 'index';

return $this->delegateView([
'viewParameters' => [
'tmpl' => $tmpl,
'security' => $this->get('mautic.security'),
'form' => $this->setFormTheme($form, 'MauticConfigBundle:Config:form.html.php', $formThemes),
'formConfigs' => $formConfigs,
'isWritable' => $isWritabale,
],
'contentTemplate' => 'MauticConfigBundle:Config:form.html.php',
'passthroughVars' => [
'activeLink' => '#mautic_config_index',
'mauticContent' => 'config',
'route' => $this->generateUrl('mautic_config_action', ['objectAction' => 'edit']),
],
]);
return $this->delegateView(
[
'viewParameters' => [
'tmpl' => $tmpl,
'security' => $this->get('mautic.security'),
'form' => $this->setFormTheme($form, 'MauticConfigBundle:Config:form.html.php', $formThemes),
'formConfigs' => $formConfigs,
'isWritable' => $isWritabale,
],
'contentTemplate' => 'MauticConfigBundle:Config:form.html.php',
'passthroughVars' => [
'activeLink' => '#mautic_config_index',
'mauticContent' => 'config',
'route' => $this->generateUrl('mautic_config_action', ['objectAction' => 'edit']),
],
]
);
}

/**
* @param $objectId
*
* @return array|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|Response
*/
public function downloadAction($objectId)
{
//admin only allowed
if (!$this->user->isAdmin()) {
return $this->accessDenied();
}

$event = new ConfigBuilderEvent($this->get('mautic.helper.paths'), $this->get('mautic.helper.bundle'));
$dispatcher = $this->get('event_dispatcher');
$dispatcher->dispatch(ConfigEvents::CONFIG_ON_GENERATE, $event);

// Extract and base64 encode file contents
$fileFields = $event->getFileFields();

if (!in_array($objectId, $fileFields)) {
return $this->accessDenied();
}

$content = $this->get('mautic.helper.core_parameters')->getParameter($objectId);
$filename = $this->request->get('filename', $objectId);

if ($decoded = base64_decode($content)) {
$response = new Response($decoded);
$response->headers->set('Content-Type', 'application/force-download');
$response->headers->set('Content-Type', 'application/octet-stream');
$response->headers->set('Content-Disposition', 'attachment; filename="'.$filename);
$response->headers->set('Expires', 0);
$response->headers->set('Cache-Control', 'must-revalidate');
$response->headers->set('Pragma', 'public');

return $response;
}

return $this->notFound();
}

/**
Expand Down
75 changes: 56 additions & 19 deletions app/bundles/ConfigBundle/Event/ConfigBuilderEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

namespace Mautic\ConfigBundle\Event;

use Mautic\CoreBundle\Factory\MauticFactory;
use Mautic\CoreBundle\Helper\BundleHelper;
use Mautic\CoreBundle\Helper\PathsHelper;
use Symfony\Component\EventDispatcher\Event;

/**
Expand All @@ -30,22 +31,38 @@ class ConfigBuilderEvent extends Event
private $formThemes = [];

/**
* @var MauticFactory
* @var PathsHelper
*/
private $factory;
private $pathsHelper;

/**
* @param MauticFactory $factory
* @var BundleHelper
*/
public function __construct(MauticFactory $factory)
private $bundleHelper;

/**
* @var array
*/
protected $encodedFields = [];

/**
* ConfigBuilderEvent constructor.
*
* @param PathsHelper $pathsHelper
* @param BundleHelper $bundleHelper
*/
public function __construct(PathsHelper $pathsHelper, BundleHelper $bundleHelper)
{
$this->factory = $factory;
$this->pathsHelper = $pathsHelper;
$this->bundleHelper = $bundleHelper;
}

/**
* Set new form to the forms array.
*
* @param array $form
* @param $form
*
* @return $this
*/
public function addForm($form)
{
Expand All @@ -54,6 +71,8 @@ public function addForm($form)
}

$this->forms[$form['formAlias']] = $form;

return $this;
}

/**
Expand All @@ -76,16 +95,6 @@ public function getFormThemes()
return $this->formThemes;
}

/**
* Returns the factory.
*
* @return MauticFactory
*/
public function getFactory()
{
return $this->factory;
}

/**
* Helper method can load $parameters array from a config file.
*
Expand All @@ -95,7 +104,7 @@ public function getFactory()
*/
public function getParameters($path = null)
{
$paramsFile = $this->factory->getSystemPath('app').$path;
$paramsFile = $this->pathsHelper->getSystemPath('app').$path;

if (file_exists($paramsFile)) {
// Import the bundle configuration, $parameters is defined in this file
Expand All @@ -106,6 +115,14 @@ public function getParameters($path = null)
$parameters = [];
}

$fields = $this->getBase64EncodedFields();
$checkThese = array_intersect(array_keys($parameters), $fields);
foreach ($checkThese as $checkMe) {
if (!empty($parameters[$checkMe])) {
$parameters[$checkMe] = base64_decode($parameters[$checkMe]);
}
}

return $parameters;
}

Expand All @@ -119,7 +136,7 @@ public function getParametersFromConfig($bundle)
static $allBundles;

if (empty($allBundles)) {
$allBundles = $this->factory->getMauticBundles(true);
$allBundles = $this->bundleHelper->getMauticBundles(true);
}

if (isset($allBundles[$bundle]) && $allBundles[$bundle]['config']['parameters']) {
Expand All @@ -128,4 +145,24 @@ public function getParametersFromConfig($bundle)
return [];
}
}

/**
* @param $fields
*
* @return $this
*/
public function addFileFields($fields)
{
$this->encodedFields = array_merge($this->encodedFields, (array) $fields);

return $this;
}

/**
* @return array
*/
public function getFileFields()
{
return $this->encodedFields;
}
}
Loading

0 comments on commit b92893e

Please sign in to comment.