Issues (3627)

bundles/PluginBundle/Controller/AuthController.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\PluginBundle\Controller;
13
14
use Mautic\CoreBundle\Controller\FormController;
15
use Mautic\PluginBundle\Event\PluginIntegrationAuthRedirectEvent;
16
use Mautic\PluginBundle\PluginEvents;
17
use Symfony\Component\HttpFoundation\JsonResponse;
18
use Symfony\Component\HttpFoundation\RedirectResponse;
19
20
/**
21
 * Class AuthController.
22
 */
23
class AuthController extends FormController
0 ignored issues
show
Deprecated Code introduced by
The class Mautic\CoreBundle\Controller\FormController has been deprecated: 2.3 - to be removed in 3.0; use AbstractFormController instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

23
class AuthController extends /** @scrutinizer ignore-deprecated */ FormController
Loading history...
24
{
25
    /**
26
     * @param string $integration
27
     *
28
     * @return JsonResponse
29
     */
30
    public function authCallbackAction($integration)
31
    {
32
        $isAjax  = $this->request->isXmlHttpRequest();
33
        $session = $this->get('session');
34
35
        /** @var \Mautic\PluginBundle\Helper\IntegrationHelper $integrationHelper */
36
        $integrationHelper = $this->factory->getHelper('integration');
37
        $integrationObject = $integrationHelper->getIntegrationObject($integration);
38
39
        //check to see if the service exists
40
        if (!$integrationObject) {
41
            $session->set('mautic.integration.postauth.message', ['mautic.integration.notfound', ['%name%' => $integration], 'error']);
42
            if ($isAjax) {
43
                return new JsonResponse(['url' => $this->generateUrl('mautic_integration_auth_postauth', ['integration' => $integration])]);
44
            } else {
45
                return new RedirectResponse($this->generateUrl('mautic_integration_auth_postauth', ['integration' => $integration]));
46
            }
47
        }
48
49
        try {
50
            $error = $integrationObject->authCallback();
51
        } catch (\InvalidArgumentException $e) {
52
            $session->set('mautic.integration.postauth.message', [$e->getMessage(), [], 'error']);
53
            $redirectUrl = $this->generateUrl('mautic_integration_auth_postauth', ['integration' => $integration]);
54
            if ($isAjax) {
55
                return new JsonResponse(['url' => $redirectUrl]);
56
            } else {
57
                return new RedirectResponse($redirectUrl);
58
            }
59
        }
60
61
        //check for error
62
        if ($error) {
63
            $type    = 'error';
64
            $message = 'mautic.integration.error.oauthfail';
65
            $params  = ['%error%' => $error];
66
        } else {
67
            $type    = 'notice';
68
            $message = 'mautic.integration.notice.oauthsuccess';
69
            $params  = [];
70
        }
71
72
        $session->set('mautic.integration.postauth.message', [$message, $params, $type]);
73
74
        $identifier[$integration] = null;
75
        $socialCache              = [];
76
        $userData                 = $integrationObject->getUserData($identifier, $socialCache);
77
78
        $session->set('mautic.integration.'.$integration.'.userdata', $userData);
79
80
        return new RedirectResponse($this->generateUrl('mautic_integration_auth_postauth', ['integration' => $integration]));
81
    }
82
83
    /**
84
     * @param $integration
85
     *
86
     * @return \Symfony\Component\HttpFoundation\Response
87
     */
88
    public function authStatusAction($integration)
89
    {
90
        $postAuthTemplate = 'MauticPluginBundle:Auth:postauth.html.php';
91
92
        $session     = $this->get('session');
93
        $postMessage = $session->get('mautic.integration.postauth.message');
94
        $userData    = [];
95
96
        if (isset($integration)) {
97
            $userData = $session->get('mautic.integration.'.$integration.'.userdata');
98
        }
99
100
        $message = $type = '';
101
        $alert   = 'success';
102
        if (!empty($postMessage)) {
103
            $message = $this->translator->trans($postMessage[0], $postMessage[1], 'flashes');
104
            $session->remove('mautic.integration.postauth.message');
105
            $type = $postMessage[2];
106
            if ('error' == $type) {
107
                $alert = 'danger';
108
            }
109
        }
110
111
        return $this->render($postAuthTemplate, ['message' => $message, 'alert' => $alert, 'data' => $userData]);
112
    }
113
114
    /**
115
     * @param $integration
116
     *
117
     * @return RedirectResponse
118
     */
119
    public function authUserAction($integration)
120
    {
121
        /** @var \Mautic\PluginBundle\Helper\IntegrationHelper $integrationHelper */
122
        $integrationHelper = $this->factory->getHelper('integration');
123
        $integrationObject = $integrationHelper->getIntegrationObject($integration);
124
125
        $settings['method']      = 'GET';
126
        $settings['integration'] = $integrationObject->getName();
127
128
        /** @var \Mautic\PluginBundle\Integration\AbstractIntegration $integrationObject */
129
        $event = $this->dispatcher->dispatch(
130
            PluginEvents::PLUGIN_ON_INTEGRATION_AUTH_REDIRECT,
131
            new PluginIntegrationAuthRedirectEvent(
132
                $integrationObject,
133
                $integrationObject->getAuthLoginUrl()
134
            )
135
        );
136
        $oauthUrl = $event->getAuthUrl();
137
138
        return new RedirectResponse($oauthUrl);
139
    }
140
}
141