Passed
Push — master ( 9ba014...497c56 )
by Greg
07:49 queued 13s
created

app/Http/Controllers/AccountController.php (4 issues)

Labels
Severity
1
<?php
2
/**
3
 * webtrees: online genealogy
4
 * Copyright (C) 2019 webtrees development team
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
 * GNU General Public License for more details.
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15
 */
16
declare(strict_types=1);
17
18
namespace Fisharebest\Webtrees\Http\Controllers;
19
20
use DateTimeZone;
21
use Fisharebest\Webtrees\Auth;
22
use Fisharebest\Webtrees\Contracts\UserInterface;
23
use Fisharebest\Webtrees\FlashMessages;
24
use Fisharebest\Webtrees\Functions\FunctionsEdit;
25
use Fisharebest\Webtrees\I18N;
26
use Fisharebest\Webtrees\Individual;
27
use Fisharebest\Webtrees\Module\ModuleThemeInterface;
28
use Fisharebest\Webtrees\Services\ModuleService;
29
use Fisharebest\Webtrees\Services\UserService;
30
use Fisharebest\Webtrees\Session;
31
use Fisharebest\Webtrees\Tree;
32
use Fisharebest\Webtrees\User;
33
use Illuminate\Support\Collection;
34
use Psr\Http\Message\ResponseInterface;
35
use Psr\Http\Message\ServerRequestInterface;
36
37
/**
38
 * Controller to allow the user to edit their account details.
39
 */
40
class AccountController extends AbstractBaseController
41
{
42
    /**
43
     * @var ModuleService
44
     */
45
    private $module_service;
46
    /**
47
     * @var UserService
48
     */
49
    private $user_service;
50
51
    /**
52
     * AccountController constructor.
53
     *
54
     * @param ModuleService $module_service
55
     * @param UserService   $user_service
56
     */
57
    public function __construct(ModuleService $module_service, UserService $user_service)
58
    {
59
        $this->module_service = $module_service;
60
        $this->user_service   = $user_service;
61
    }
62
63
    /**
64
     * @param Tree          $tree
65
     * @param UserInterface $user
66
     *
67
     * @return ResponseInterface
68
     */
69
    public function edit(Tree $tree, UserInterface $user): ResponseInterface
70
    {
71
        $my_individual_record = Individual::getInstance($tree->getUserPreference(Auth::user(), 'gedcomid'), $tree);
72
        $contact_methods      = FunctionsEdit::optionsContactMethods();
73
        $default_individual   = Individual::getInstance($tree->getUserPreference(Auth::user(), 'rootid'), $tree);
74
        $installed_languages  = FunctionsEdit::optionsInstalledLanguages();
75
        $show_delete_option   = !$user->getPreference('canadmin');
76
        $themes               = $this->themeOptions();
77
        $timezone_ids         = DateTimeZone::listIdentifiers();
78
        $timezones            = array_combine($timezone_ids, $timezone_ids);
79
        $title                = I18N::translate('My account');
80
81
        return $this->viewResponse('edit-account-page', [
82
            'contact_methods'      => $contact_methods,
83
            'default_individual'   => $default_individual,
84
            'installed_languages'  => $installed_languages,
85
            'my_individual_record' => $my_individual_record,
86
            'show_delete_option'   => $show_delete_option,
87
            'themes'               => $themes,
88
            'timezones'            => $timezones,
89
            'title'                => $title,
90
            'user'                 => $user,
91
        ]);
92
    }
93
94
    /**
95
     * @param ServerRequestInterface $request
96
     * @param Tree                   $tree
97
     * @param UserInterface          $user
98
     *
99
     * @return ResponseInterface
100
     */
101
    public function update(ServerRequestInterface $request, Tree $tree, UserInterface $user): ResponseInterface
102
    {
103
        $params = $request->getParsedBody();
104
105
        $contact_method = $params['contact_method'];
106
        $email          = $params['email'];
107
        $language       = $params['language'];
108
        $real_name      = $params['real_name'];
109
        $password       = $params['password'];
110
        $rootid         = $params['root_id'];
111
        $theme          = $params['theme'];
112
        $time_zone      = $params['timezone'];
113
        $user_name      = $params['user_name'];
114
        $visible_online = $params['visible_online'] ?? '';
115
116
        // Change the password
117
        if ($password !== '') {
118
            $user->setPassword($password);
0 ignored issues
show
The method setPassword() does not exist on Fisharebest\Webtrees\Contracts\UserInterface. It seems like you code against a sub-type of Fisharebest\Webtrees\Contracts\UserInterface such as Fisharebest\Webtrees\User. ( Ignorable by Annotation )

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

118
            $user->/** @scrutinizer ignore-call */ 
119
                   setPassword($password);
Loading history...
119
        }
120
121
        // Change the username
122
        if ($user_name !== $user->userName()) {
123
            if ($this->user_service->findByUserName($user_name) === null) {
124
                $user->setUserName($user_name);
0 ignored issues
show
The method setUserName() does not exist on Fisharebest\Webtrees\Contracts\UserInterface. It seems like you code against a sub-type of Fisharebest\Webtrees\Contracts\UserInterface such as Fisharebest\Webtrees\User. ( Ignorable by Annotation )

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

124
                $user->/** @scrutinizer ignore-call */ 
125
                       setUserName($user_name);
Loading history...
125
            } else {
126
                FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.'));
127
            }
128
        }
129
130
        // Change the email
131
        if ($email !== $user->email()) {
132
            if ($this->user_service->findByEmail($email) === null) {
133
                $user->setEmail($email);
0 ignored issues
show
The method setEmail() does not exist on Fisharebest\Webtrees\Contracts\UserInterface. It seems like you code against a sub-type of Fisharebest\Webtrees\Contracts\UserInterface such as Fisharebest\Webtrees\User. ( Ignorable by Annotation )

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

133
                $user->/** @scrutinizer ignore-call */ 
134
                       setEmail($email);
Loading history...
134
            } else {
135
                FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.'));
136
            }
137
        }
138
139
        $user
140
            ->setRealName($real_name)
0 ignored issues
show
The method setRealName() does not exist on Fisharebest\Webtrees\Contracts\UserInterface. It seems like you code against a sub-type of Fisharebest\Webtrees\Contracts\UserInterface such as Fisharebest\Webtrees\User. ( Ignorable by Annotation )

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

140
            ->/** @scrutinizer ignore-call */ 
141
              setRealName($real_name)
Loading history...
141
            ->setPreference('contactmethod', $contact_method)
142
            ->setPreference('language', $language)
143
            ->setPreference('theme', $theme)
144
            ->setPreference('TIMEZONE', $time_zone)
145
            ->setPreference('visibleonline', $visible_online);
146
147
        $tree->setUserPreference($user, 'rootid', $rootid);
148
149
        // Switch to the new theme now
150
        Session::put('theme', $theme);
151
152
        // Switch to the new language now
153
        Session::put('language', $language);
154
155
        return redirect(route('my-account', ['ged' => $tree->name()]));
156
    }
157
158
    /**
159
     * @param UserInterface $user
160
     *
161
     * @return ResponseInterface
162
     */
163
    public function delete(UserInterface $user): ResponseInterface
164
    {
165
        // An administrator can only be deleted by another administrator
166
        if (!$user->getPreference('canadmin') && $user instanceof User) {
167
            $this->user_service->delete($user);
168
            Auth::logout();
169
        }
170
171
        return redirect(route('my-account'));
172
    }
173
174
    /**
175
     * @return Collection
176
     */
177
    private function themeOptions(): Collection
178
    {
179
        return $this->module_service
180
            ->findByInterface(ModuleThemeInterface::class)
181
            ->map($this->module_service->titleMapper())
182
            ->prepend(I18N::translate('<default theme>'), '');
183
    }
184
}
185