| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @package     Mautic | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright   2019 Monogramm. All rights reserved | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author      enguerr | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @link        https://www.septeo.fr | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @license     GNU/AGPLv3 http://www.gnu.org/licenses/agpl.html | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace MauticPlugin\MauticLdapAuthBundle\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Mautic\CoreBundle\Controller\CommonController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\HttpFoundation\RedirectResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\HttpKernel\Event\FilterControllerEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Security\Core\Security; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Mautic\UserBundle\Entity\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class LdapAuthController extends CommonController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $parametersHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $supportedServices = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         'LdapAuth', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     public function __construct() | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function authAction(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $session = $this->request->getSession(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $integrationHelper = $this->get('mautic.helper.integration'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $integrations      = $integrationHelper->getIntegrationObjects(null, [], true, null, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         return $this->delegateView([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             'viewParameters' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 'last_username' => $session->get(Security::LAST_USERNAME), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 'integrations'  => $integrations, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             'contentTemplate' => 'MauticUserBundle:Security:login.html.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             'passthroughVars' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 'route'          => $this->generateUrl('login'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 'mauticContent'  => 'user', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 'sessionExpired' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function ssoAction($integration,Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $session = $this->request->getSession(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if (isset($_SERVER['PHP_AUTH_USER'])){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             //$session->remove('_security.last_error'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             //$session->remove('_csrf/https-authenticate'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             //$session->clear(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             //die(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             //return $this->redirect( '/s/sso_login/LdapAuth',302); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->parametersHelper = $this->get('mautic.helper.core_parameters'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         //print_r($session); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         // Get a list of SSO integrations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $integrationHelper = $this->get('mautic.helper.integration'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $integrations      = $integrationHelper->getIntegrationObjects(null, [], true, null, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $integration = $integrations['LdapAuth']; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         /*$settings = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             'hostname'      => $this->parametersHelper->getParameter('ldap_auth_host'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             'port'          => $this->parametersHelper->getParameter('ldap_auth_port', 389), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             'ssl'           => $this->parametersHelper->getParameter('ldap_auth_ssl', false), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             'starttls'      => $this->parametersHelper->getParameter('ldap_auth_starttls', true), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             'version'       => $this->parametersHelper->getParameter('ldap_auth_version', 3), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             // TODO Coming feature: Bind DN | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             //'bind_dn'       => $this->parametersHelper->getParameter('ldap_auth_bind_dn'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             //'bind_passwd'   => $this->parametersHelper->getParameter('ldap_auth_bind_passwd'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             'base_dn'       => $this->parametersHelper->getParameter('ldap_auth_base_dn'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             'user_query'    => $this->parametersHelper->getParameter('ldap_auth_user_query', ''), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'is_ad'         => $this->parametersHelper->getParameter('ldap_auth_isactivedirectory', false), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             'ad_domain'     => $this->parametersHelper->getParameter('ldap_auth_activedirectory_domain', null), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             'user_key'      => $this->parametersHelper->getParameter('ldap_auth_username_attribute', 'uid'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             'user_email'    => $this->parametersHelper->getParameter('ldap_auth_email_attribute', 'mail'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             'user_firstname'=> $this->parametersHelper->getParameter('ldap_auth_firstname_attribute', 'givenName'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             'user_lastname' => $this->parametersHelper->getParameter('ldap_auth_lastname_attribute', 'sn'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             'user_fullname' => $this->parametersHelper->getParameter('ldap_auth_fullname_attribute', 'displayName'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $parameters = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             'login'=>$_SERVER['PHP_AUTH_USER'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             'password'=>'', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             'email'=> '[email protected]' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         //GET USER | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         //$user = $integration->getUserSimple($settings,$parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $user = new User(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $user->setUsername($parameters['login']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             ->setEmail($parameters['email']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             ->setFirstName($parameters['login']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             ->setLastName($parameters['login']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             ->setRole( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 $integration->getUserRole() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $token = new UsernamePasswordToken($user, '', 'main',array()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $this->get('security.token_storage')->setToken($token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $this->get('session')->set('_security_main', serialize($token)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $event = new InteractiveLoginEvent($request, $token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $this->get("event_dispatcher")->dispatch("security.interactive_login", $event);*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         return $this->delegateView([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             'viewParameters' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 'last_username' => $session->get(Security::LAST_USERNAME), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 'integrations'  => $integrations, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             'contentTemplate' => 'MauticUserBundle:Security:login.html.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             'passthroughVars' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 'route'          => $this->generateUrl('login'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 'mauticContent'  => 'user', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 'sessionExpired' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         ]); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 127 |  |  | } |