Spaces:
No application file
No application file
File size: 4,789 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
<?php
namespace Mautic\PluginBundle\Integration;
use Mautic\CoreBundle\Form\Type\YesNoButtonGroupType;
use Mautic\UserBundle\Entity\Role;
use Mautic\UserBundle\Form\Type\RoleListType;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
/**
* Used by SSO auth plugins that use OAuth2, etc means of logins.
*/
abstract class AbstractSsoServiceIntegration extends AbstractIntegration
{
/**
* Called after the user is authenticated with the 3rd party service to obtain the users
* details.
*
* @param $response mixed Typically the response from request to authenticating service
*
* @return mixed
*/
abstract public function getUser($response);
/**
* Get the user role for new users.
*
* @return bool|\Doctrine\Common\Proxy\Proxy|object|null
*
* @throws \Doctrine\ORM\ORMException
*/
public function getUserRole()
{
$featureSettings = $this->settings->getFeatureSettings();
$role = $featureSettings['new_user_role'] ?? false;
if ($role) {
return $this->em->getReference(Role::class, $role);
}
throw new AuthenticationException('mautic.integration.sso.error.no_role');
}
/**
* Returns if a new user should be created if authenticated and not found locally.
*/
public function shouldAutoCreateNewUser(): bool
{
$featureSettings = $this->settings->getFeatureSettings();
return isset($featureSettings['auto_create_user']) && (bool) $featureSettings['auto_create_user'];
}
/**
* Set the callback URL to sso_login.
*/
public function getAuthCallbackUrl()
{
return $this->router->generate('mautic_sso_login_check',
['integration' => $this->getName()],
\Symfony\Component\Routing\Generator\UrlGeneratorInterface::ABSOLUTE_URL // absolute
);
}
/**
* @param array $settings
* @param array $parameters
*
* @return bool|string
*/
public function ssoAuthCallback($settings = [], $parameters = [])
{
$response = $this->authCallback($settings, $parameters);
// Get user data
return $this->getUser($response);
}
/**
* Don't save the keys as they are only used to validate user login.
*
* @return array
*/
public function extractAuthKeys($data, $tokenOverride = null)
{
// Prepare the keys for extraction such as renaming, setting expiry, etc
$data = $this->prepareResponseForExtraction($data);
// parse the response
$authTokenKey = $tokenOverride ?: $this->getAuthTokenKey();
if (is_array($data) && isset($data[$authTokenKey])) {
return $data;
}
$error = $this->getErrorsFromResponse($data);
if (empty($error)) {
$error = $this->translator->trans('mautic.integration.error.genericerror', [], 'flashes');
}
throw new AuthenticationException($error);
}
/**
* @return array
*/
public function getSupportedFeatures()
{
return [
'sso_service',
];
}
/**
* Get form settings; authorization is not needed since it is done when a user logs in.
*
* @return array<string, mixed>
*/
public function getFormSettings(): array
{
return [
'requires_callback' => true,
'requires_authorization' => false,
];
}
/**
* @param Form|\Symfony\Component\Form\FormBuilder $builder
* @param array $data
* @param string $formArea
*/
public function appendToForm(&$builder, $data, $formArea): void
{
if ('features' == $formArea) {
$builder->add('auto_create_user',
YesNoButtonGroupType::class,
[
'label' => 'mautic.integration.sso.auto_create_user',
'data' => isset($data['auto_create_user']) && (bool) $data['auto_create_user'],
'attr' => [
'tooltip' => 'mautic.integration.sso.auto_create_user.tooltip',
],
]
);
$builder->add(
'new_user_role',
RoleListType::class,
[
'label' => 'mautic.integration.sso.new_user_role',
'label_attr' => ['class' => 'control-label'],
'attr' => [
'class' => 'form-control',
'tooltip' => 'mautic.integration.sso.new_user_role.tooltip',
],
]
);
}
}
}
|