Issues (3627)

UserBundle/Controller/ProfileController.php (3 issues)

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\UserBundle\Controller;
13
14
use Mautic\CoreBundle\Controller\FormController;
15
use Mautic\CoreBundle\Helper\LanguageHelper;
16
use Symfony\Component\Form\Extension\Core\Type\TextType;
17
18
/**
19
 * Class ProfileController.
20
 */
21
class ProfileController extends FormController
22
{
23
    /**
24
     * Generate's account profile.
25
     *
26
     * @return \Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response
27
     */
28
    public function indexAction()
29
    {
30
        //get current user
31
        $me    = $this->get('security.token_storage')->getToken()->getUser();
32
        $model = $this->getModel('user');
33
34
        //set some permissions
35
        $permissions = [
36
            'apiAccess' => ($this->get('mautic.helper.core_parameters')->get('api_enabled')) ?
37
                $this->get('mautic.security')->isGranted('api:access:full')
38
                : 0,
39
            'editName'     => $this->get('mautic.security')->isGranted('user:profile:editname'),
40
            'editUsername' => $this->get('mautic.security')->isGranted('user:profile:editusername'),
41
            'editPosition' => $this->get('mautic.security')->isGranted('user:profile:editposition'),
42
            'editEmail'    => $this->get('mautic.security')->isGranted('user:profile:editemail'),
43
        ];
44
45
        $action = $this->generateUrl('mautic_user_account');
46
        $form   = $model->createForm($me, $this->get('form.factory'), $action, ['in_profile' => true]);
47
48
        $overrides = [];
49
50
        //make sure this user has access to edit privileged fields
51
        foreach ($permissions as $permName => $hasAccess) {
52
            if ('apiAccess' == $permName) {
53
                continue;
54
            }
55
56
            if (!$hasAccess) {
57
                //set the value to its original
58
                switch ($permName) {
59
                    case 'editName':
60
                        $overrides['firstName'] = $me->getFirstName();
61
                        $overrides['lastName']  = $me->getLastName();
62
                        $form->remove('firstName');
63
                        $form->add(
64
                            'firstName_unbound',
65
                            TextType::class,
66
                            [
67
                                'label'      => 'mautic.core.firstname',
68
                                'label_attr' => ['class' => 'control-label'],
69
                                'attr'       => ['class' => 'form-control'],
70
                                'mapped'     => false,
71
                                'disabled'   => true,
72
                                'data'       => $me->getFirstName(),
73
                                'required'   => false,
74
                            ]
75
                        );
76
77
                        $form->remove('lastName');
78
                        $form->add(
79
                            'lastName_unbound',
80
                            TextType::class,
81
                            [
82
                                'label'      => 'mautic.core.lastname',
83
                                'label_attr' => ['class' => 'control-label'],
84
                                'attr'       => ['class' => 'form-control'],
85
                                'mapped'     => false,
86
                                'disabled'   => true,
87
                                'data'       => $me->getLastName(),
88
                                'required'   => false,
89
                            ]
90
                        );
91
                        break;
92
93
                    case 'editUsername':
94
                        $overrides['username'] = $me->getUsername();
95
                        $form->remove('username');
96
                        $form->add(
97
                            'username_unbound',
98
                            TextType::class,
99
                            [
100
                                'label'      => 'mautic.core.username',
101
                                'label_attr' => ['class' => 'control-label'],
102
                                'attr'       => ['class' => 'form-control'],
103
                                'mapped'     => false,
104
                                'disabled'   => true,
105
                                'data'       => $me->getUsername(),
106
                                'required'   => false,
107
                            ]
108
                        );
109
                        break;
110
                    case 'editPosition':
111
                        $overrides['position'] = $me->getPosition();
112
                        $form->remove('position');
113
                        $form->add(
114
                            'position_unbound',
115
                            TextType::class,
116
                            [
117
                                'label'      => 'mautic.core.position',
118
                                'label_attr' => ['class' => 'control-label'],
119
                                'attr'       => ['class' => 'form-control'],
120
                                'mapped'     => false,
121
                                'disabled'   => true,
122
                                'data'       => $me->getPosition(),
123
                                'required'   => false,
124
                            ]
125
                        );
126
                        break;
127
                    case 'editEmail':
128
                        $overrides['email'] = $me->getEmail();
129
                        $form->remove('email');
130
                        $form->add(
131
                            'email_unbound',
132
                            TextType::class,
133
                            [
134
                                'label'      => 'mautic.core.type.email',
135
                                'label_attr' => ['class' => 'control-label'],
136
                                'attr'       => ['class' => 'form-control'],
137
                                'mapped'     => false,
138
                                'disabled'   => true,
139
                                'data'       => $me->getEmail(),
140
                                'required'   => false,
141
                            ]
142
                        );
143
                        break;
144
                }
145
            }
146
        }
147
148
        //Check for a submitted form and process it
149
        $submitted = $this->get('session')->get('formProcessed', 0);
150
        if ('POST' == $this->request->getMethod() && !$submitted) {
151
            $this->get('session')->set('formProcessed', 1);
152
153
            //check to see if the password needs to be rehashed
154
            $formUser              = $this->request->request->get('user', []);
155
            $submittedPassword     = $formUser['plainPassword']['password'] ?? null;
156
            $encoder               = $this->get('security.encoder_factory')->getEncoder($me);
157
            $overrides['password'] = $model->checkNewPassword($me, $encoder, $submittedPassword);
158
            if (!$cancelled = $this->isFormCancelled($form)) {
0 ignored issues
show
The assignment to $cancelled is dead and can be removed.
Loading history...
159
                if ($this->isFormValid($form)) {
160
                    foreach ($overrides as $k => $v) {
161
                        $func = 'set'.ucfirst($k);
162
                        $me->$func($v);
163
                    }
164
165
                    //form is valid so process the data
166
                    $model->saveEntity($me);
0 ignored issues
show
The method saveEntity() does not exist on Mautic\CoreBundle\Model\AbstractCommonModel. It seems like you code against a sub-type of Mautic\CoreBundle\Model\AbstractCommonModel such as Mautic\CampaignBundle\Model\EventLogModel or Mautic\CoreBundle\Model\FormModel. ( Ignorable by Annotation )

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

166
                    $model->/** @scrutinizer ignore-call */ 
167
                            saveEntity($me);
Loading history...
167
168
                    //check if the user's locale has been downloaded already, fetch it if not
169
                    /** @var LanguageHelper $languageHelper */
170
                    $languageHelper     = $this->container->get('mautic.helper.language');
171
                    $installedLanguages = $languageHelper->getSupportedLanguages();
172
173
                    if ($me->getLocale() && !array_key_exists($me->getLocale(), $installedLanguages)) {
174
                        $fetchLanguage = $languageHelper->extractLanguagePackage($me->getLocale());
175
176
                        // If there is an error, we need to reset the user's locale to the default
177
                        if ($fetchLanguage['error']) {
178
                            $me->setLocale(null);
179
                            $model->saveEntity($me);
180
                            $message     = 'mautic.core.could.not.set.language';
181
                            $messageVars = [];
182
183
                            if (isset($fetchLanguage['message'])) {
184
                                $message = $fetchLanguage['message'];
185
                            }
186
187
                            if (isset($fetchLanguage['vars'])) {
188
                                $messageVars = $fetchLanguage['vars'];
189
                            }
190
191
                            $this->addFlash($message, $messageVars);
0 ignored issues
show
Deprecated Code introduced by
The function Mautic\CoreBundle\Contro...nController::addFlash() has been deprecated: Will be removed in Mautic 3.0. Use CommonController::flashBag->addFlash() instead. ( Ignorable by Annotation )

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

191
                            /** @scrutinizer ignore-deprecated */ $this->addFlash($message, $messageVars);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
192
                        }
193
                    }
194
195
                    // Update timezone and locale
196
                    $tz = $me->getTimezone();
197
                    if (empty($tz)) {
198
                        $tz = $this->get('mautic.helper.core_parameters')->get('default_timezone');
199
                    }
200
                    $this->get('session')->set('_timezone', $tz);
201
202
                    $locale = $me->getLocale();
203
                    if (empty($locale)) {
204
                        $locale = $this->get('mautic.helper.core_parameters')->get('locale');
205
                    }
206
                    $this->get('session')->set('_locale', $locale);
207
208
                    $returnUrl = $this->generateUrl('mautic_user_account');
209
210
                    return $this->postActionRedirect(
211
                        [
212
                            'returnUrl'       => $returnUrl,
213
                            'contentTemplate' => 'MauticUserBundle:Profile:index',
214
                            'passthroughVars' => [
215
                                'mauticContent' => 'user',
216
                            ],
217
                            'flashes' => [ //success
218
                                [
219
                                    'type' => 'notice',
220
                                    'msg'  => 'mautic.user.account.notice.updated',
221
                                ],
222
                            ],
223
                        ]
224
                    );
225
                }
226
            } else {
227
                return $this->redirect($this->generateUrl('mautic_dashboard_index'));
228
            }
229
        }
230
        $this->get('session')->set('formProcessed', 0);
231
232
        $parameters = [
233
            'permissions'       => $permissions,
234
            'me'                => $me,
235
            'userForm'          => $form->createView(),
236
            'authorizedClients' => $this->forward('MauticApiBundle:Client:authorizedClients')->getContent(),
237
        ];
238
239
        return $this->delegateView(
240
            [
241
                'viewParameters'  => $parameters,
242
                'contentTemplate' => 'MauticUserBundle:Profile:index.html.php',
243
                'passthroughVars' => [
244
                    'route'         => $this->generateUrl('mautic_user_account'),
245
                    'mauticContent' => 'user',
246
                ],
247
            ]
248
        );
249
    }
250
}
251