1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Mautic |
4
|
|
|
* @copyright 2019 Monogramm. All rights reserved |
5
|
|
|
* @author Monogramm |
6
|
|
|
* @link https://www.monogramm.io |
7
|
|
|
* @license GNU/AGPLv3 http://www.gnu.org/licenses/agpl.html |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace MauticPlugin\MauticLdapAuthBundle\EventListener; |
11
|
|
|
|
12
|
|
|
use Mautic\CoreBundle\Helper\CoreParametersHelper; |
13
|
|
|
use Mautic\PluginBundle\Integration\AbstractSsoFormIntegration; |
14
|
|
|
use Mautic\UserBundle\Entity\User; |
15
|
|
|
use Mautic\UserBundle\Event\AuthenticationEvent; |
16
|
|
|
use Mautic\UserBundle\UserEvents; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
18
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class UserSubscriber |
23
|
|
|
*/ |
24
|
|
|
class UserSubscriber implements EventSubscriberInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var CoreParametersHelper |
28
|
|
|
*/ |
29
|
|
|
private $parametersHelper; |
30
|
|
|
|
31
|
|
|
private $supportedServices = array( |
32
|
|
|
'LdapAuth', |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
public function __construct(CoreParametersHelper $parametersHelper) |
36
|
|
|
{ |
37
|
|
|
$this->parametersHelper = $parametersHelper; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
public static function getSubscribedEvents() |
44
|
|
|
{ |
45
|
|
|
return array( |
46
|
|
|
UserEvents::USER_FORM_AUTHENTICATION => array('onUserFormAuthentication', 0), |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Authenticate via the form using users defined in LDAP server(s). |
52
|
|
|
* |
53
|
|
|
* @param AuthenticationEvent $event |
54
|
|
|
* |
55
|
|
|
* @return bool|void |
56
|
|
|
*/ |
57
|
|
|
public function onUserFormAuthentication(AuthenticationEvent $event) |
58
|
|
|
{ |
59
|
|
|
$username = $event->getUsername(); |
60
|
|
|
$password = $event->getToken()->getCredentials(); |
61
|
|
|
|
62
|
|
|
$integration = null; |
63
|
|
|
$result = false; |
64
|
|
|
if ($authenticatingService = $event->getAuthenticatingService()) { |
65
|
|
|
if (in_array($authenticatingService, $this->supportedServices) |
66
|
|
|
&& $integration = $event->getIntegration($authenticatingService)) { |
67
|
|
|
$result = $this->authenticateService($integration, $username, $password); |
68
|
|
|
} |
69
|
|
|
} else { |
70
|
|
|
foreach ($this->supportedServices as $supportedService) { |
71
|
|
|
if ($integration = $event->getIntegration($supportedService)) { |
72
|
|
|
$authenticatingService = $supportedService; |
73
|
|
|
$result = $this->authenticateService($integration, $username, $password); |
74
|
|
|
break; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($integration && $result instanceof User) { |
80
|
|
|
$event->setIsAuthenticated($authenticatingService, $result, $integration->shouldAutoCreateNewUser()); |
81
|
|
|
} elseif ($result instanceof Response) { |
82
|
|
|
$event->setResponse($result); |
83
|
|
|
} // else do nothing |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param AbstractSsoFormIntegration $integration |
88
|
|
|
* @param string $username |
89
|
|
|
* @param string $password |
90
|
|
|
* |
91
|
|
|
* @return bool|RedirectResponse |
92
|
|
|
*/ |
93
|
|
|
private function authenticateService(AbstractSsoFormIntegration $integration, $username, $password) |
94
|
|
|
{ |
95
|
|
|
$settings = [ |
96
|
|
|
'hostname' => $this->parametersHelper->getParameter('ldap_auth_host'), |
97
|
|
|
'port' => $this->parametersHelper->getParameter('ldap_auth_port', 389), |
98
|
|
|
'ssl' => $this->parametersHelper->getParameter('ldap_auth_ssl', false), |
99
|
|
|
'starttls' => $this->parametersHelper->getParameter('ldap_auth_starttls', true), |
100
|
|
|
'version' => $this->parametersHelper->getParameter('ldap_auth_version', 3), |
101
|
|
|
// TODO Coming feature: Bind DN |
102
|
|
|
//'bind_dn' => $this->parametersHelper->getParameter('ldap_auth_bind_dn'), |
103
|
|
|
//'bind_passwd' => $this->parametersHelper->getParameter('ldap_auth_bind_passwd'), |
104
|
|
|
'base_dn' => $this->parametersHelper->getParameter('ldap_auth_base_dn'), |
105
|
|
|
'user_query' => $this->parametersHelper->getParameter('ldap_auth_user_query', ''), |
106
|
|
|
'is_ad' => $this->parametersHelper->getParameter('ldap_auth_isactivedirectory', false), |
107
|
|
|
'ad_domain' => $this->parametersHelper->getParameter('ldap_auth_activedirectory_domain', null), |
108
|
|
|
'user_key' => $this->parametersHelper->getParameter('ldap_auth_username_attribute', 'uid'), |
109
|
|
|
'user_email' => $this->parametersHelper->getParameter('ldap_auth_email_attribute', 'mail'), |
110
|
|
|
'user_firstname'=> $this->parametersHelper->getParameter('ldap_auth_firstname_attribute', 'givenName'), |
111
|
|
|
'user_lastname' => $this->parametersHelper->getParameter('ldap_auth_lastname_attribute', 'sn'), |
112
|
|
|
'user_fullname' => $this->parametersHelper->getParameter('ldap_auth_fullname_attribute', 'displayName'), |
113
|
|
|
]; |
114
|
|
|
|
115
|
|
|
$parameters = [ |
116
|
|
|
'login' => $username, |
117
|
|
|
'password' => $password, |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
if ($authenticatedUser = $integration->ssoAuthCallback($settings, $parameters)) { |
121
|
|
|
return $authenticatedUser; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|