xref: /webtrees/app/User.php (revision ee76412b29adcac8f3ae80c00ba78bebfe108130)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use Closure;
23use Fisharebest\Webtrees\Contracts\UserInterface;
24use Illuminate\Database\Capsule\Manager as DB;
25use stdClass;
26
27/**
28 * Provide an interface to the wt_user table.
29 */
30class User implements UserInterface
31{
32    private int $user_id;
33
34    private string $user_name;
35
36    private string $real_name;
37
38    private string $email;
39
40    private array $preferences;
41
42    /**
43     * User constructor.
44     *
45     * @param int    $user_id
46     * @param string $user_name
47     * @param string $real_name
48     * @param string $email
49     */
50    public function __construct(int $user_id, string $user_name, string $real_name, string $email)
51    {
52        $this->user_id   = $user_id;
53        $this->user_name = $user_name;
54        $this->real_name = $real_name;
55        $this->email     = $email;
56
57        $this->preferences = DB::table('user_setting')
58            ->where('user_id', '=', $this->user_id)
59            ->pluck('setting_value', 'setting_name')
60            ->all();
61    }
62
63    /**
64     * The user‘s internal identifier.
65     *
66     * @return int
67     */
68    public function id(): int
69    {
70        return $this->user_id;
71    }
72
73    /**
74     * The users email address.
75     *
76     * @return string
77     */
78    public function email(): string
79    {
80        return $this->email;
81    }
82
83    /**
84     * Set the email address of this user.
85     *
86     * @param string $email
87     *
88     * @return User
89     */
90    public function setEmail(string $email): User
91    {
92        if ($this->email !== $email) {
93            $this->email = $email;
94
95            DB::table('user')
96                ->where('user_id', '=', $this->user_id)
97                ->update([
98                    'email' => $email,
99                ]);
100        }
101
102        return $this;
103    }
104
105    /**
106     * The user‘s real name.
107     *
108     * @return string
109     */
110    public function realName(): string
111    {
112        return $this->real_name;
113    }
114
115    /**
116     * Set the real name of this user.
117     *
118     * @param string $real_name
119     *
120     * @return User
121     */
122    public function setRealName(string $real_name): User
123    {
124        if ($this->real_name !== $real_name) {
125            $this->real_name = $real_name;
126
127            DB::table('user')
128                ->where('user_id', '=', $this->user_id)
129                ->update([
130                    'real_name' => $real_name,
131                ]);
132        }
133
134        return $this;
135    }
136
137    /**
138     * The user‘s login name.
139     *
140     * @return string
141     */
142    public function userName(): string
143    {
144        return $this->user_name;
145    }
146
147    /**
148     * Set the login name for this user.
149     *
150     * @param string $user_name
151     *
152     * @return $this
153     */
154    public function setUserName(string $user_name): self
155    {
156        if ($this->user_name !== $user_name) {
157            $this->user_name = $user_name;
158
159            DB::table('user')
160                ->where('user_id', '=', $this->user_id)
161                ->update([
162                    'user_name' => $user_name,
163                ]);
164        }
165
166        return $this;
167    }
168
169    /**
170     * Fetch a user option/setting from the wt_user_setting table.
171     * Since we'll fetch several settings for each user, and since there aren't
172     * that many of them, fetch them all in one database query
173     *
174     * @param string $setting_name
175     * @param string $default
176     *
177     * @return string
178     */
179    public function getPreference(string $setting_name, string $default = ''): string
180    {
181        return $this->preferences[$setting_name] ?? $default;
182    }
183
184    /**
185     * Update a setting for the user.
186     *
187     * @param string $setting_name
188     * @param string $setting_value
189     *
190     * @return void
191     */
192    public function setPreference(string $setting_name, string $setting_value): void
193    {
194        if ($this->getPreference($setting_name) !== $setting_value) {
195            DB::table('user_setting')->updateOrInsert([
196                'user_id'      => $this->user_id,
197                'setting_name' => $setting_name,
198            ], [
199                'setting_value' => $setting_value,
200            ]);
201
202            $this->preferences[$setting_name] = $setting_value;
203        }
204    }
205
206    /**
207     * Set the password of this user.
208     *
209     * @param string $password
210     *
211     * @return User
212     */
213    public function setPassword(string $password): User
214    {
215        DB::table('user')
216            ->where('user_id', '=', $this->user_id)
217            ->update([
218                'password' => password_hash($password, PASSWORD_DEFAULT),
219            ]);
220
221        return $this;
222    }
223
224
225    /**
226     * Validate a supplied password
227     *
228     * @param string $password
229     *
230     * @return bool
231     */
232    public function checkPassword(string $password): bool
233    {
234        $password_hash = DB::table('user')
235            ->where('user_id', '=', $this->id())
236            ->value('password');
237
238        if ($password_hash !== null && password_verify($password, $password_hash)) {
239            if (password_needs_rehash($password_hash, PASSWORD_DEFAULT)) {
240                $this->setPassword($password);
241            }
242
243            return true;
244        }
245
246        return false;
247    }
248
249    /**
250     * A closure which will create an object from a database row.
251     *
252     * @return Closure
253     */
254    public static function rowMapper(): Closure
255    {
256        return static function (stdClass $row): User {
257            return new self((int) $row->user_id, $row->user_name, $row->real_name, $row->email);
258        };
259    }
260}
261