xref: /webtrees/app/User.php (revision e22e42f733f4ab630c81dc26a61e9a5470ca33de)
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    /** @var array<string,string> */
41    private array $preferences;
42
43    /**
44     * User constructor.
45     *
46     * @param int    $user_id
47     * @param string $user_name
48     * @param string $real_name
49     * @param string $email
50     */
51    public function __construct(int $user_id, string $user_name, string $real_name, string $email)
52    {
53        $this->user_id   = $user_id;
54        $this->user_name = $user_name;
55        $this->real_name = $real_name;
56        $this->email     = $email;
57
58        $this->preferences = DB::table('user_setting')
59            ->where('user_id', '=', $this->user_id)
60            ->pluck('setting_value', 'setting_name')
61            ->all();
62    }
63
64    /**
65     * The user‘s internal identifier.
66     *
67     * @return int
68     */
69    public function id(): int
70    {
71        return $this->user_id;
72    }
73
74    /**
75     * The users email address.
76     *
77     * @return string
78     */
79    public function email(): string
80    {
81        return $this->email;
82    }
83
84    /**
85     * Set the email address of this user.
86     *
87     * @param string $email
88     *
89     * @return User
90     */
91    public function setEmail(string $email): User
92    {
93        if ($this->email !== $email) {
94            $this->email = $email;
95
96            DB::table('user')
97                ->where('user_id', '=', $this->user_id)
98                ->update([
99                    'email' => $email,
100                ]);
101        }
102
103        return $this;
104    }
105
106    /**
107     * The user‘s real name.
108     *
109     * @return string
110     */
111    public function realName(): string
112    {
113        return $this->real_name;
114    }
115
116    /**
117     * Set the real name of this user.
118     *
119     * @param string $real_name
120     *
121     * @return User
122     */
123    public function setRealName(string $real_name): User
124    {
125        if ($this->real_name !== $real_name) {
126            $this->real_name = $real_name;
127
128            DB::table('user')
129                ->where('user_id', '=', $this->user_id)
130                ->update([
131                    'real_name' => $real_name,
132                ]);
133        }
134
135        return $this;
136    }
137
138    /**
139     * The user‘s login name.
140     *
141     * @return string
142     */
143    public function userName(): string
144    {
145        return $this->user_name;
146    }
147
148    /**
149     * Set the login name for this user.
150     *
151     * @param string $user_name
152     *
153     * @return $this
154     */
155    public function setUserName(string $user_name): self
156    {
157        if ($this->user_name !== $user_name) {
158            $this->user_name = $user_name;
159
160            DB::table('user')
161                ->where('user_id', '=', $this->user_id)
162                ->update([
163                    'user_name' => $user_name,
164                ]);
165        }
166
167        return $this;
168    }
169
170    /**
171     * Fetch a user option/setting from the wt_user_setting table.
172     * Since we'll fetch several settings for each user, and since there aren't
173     * that many of them, fetch them all in one database query
174     *
175     * @param string $setting_name
176     * @param string $default
177     *
178     * @return string
179     */
180    public function getPreference(string $setting_name, string $default = ''): string
181    {
182        return $this->preferences[$setting_name] ?? $default;
183    }
184
185    /**
186     * Update a setting for the user.
187     *
188     * @param string $setting_name
189     * @param string $setting_value
190     *
191     * @return void
192     */
193    public function setPreference(string $setting_name, string $setting_value): void
194    {
195        if ($this->getPreference($setting_name) !== $setting_value) {
196            DB::table('user_setting')->updateOrInsert([
197                'user_id'      => $this->user_id,
198                'setting_name' => $setting_name,
199            ], [
200                'setting_value' => $setting_value,
201            ]);
202
203            $this->preferences[$setting_name] = $setting_value;
204        }
205    }
206
207    /**
208     * Set the password of this user.
209     *
210     * @param string $password
211     *
212     * @return User
213     */
214    public function setPassword(string $password): User
215    {
216        DB::table('user')
217            ->where('user_id', '=', $this->user_id)
218            ->update([
219                'password' => password_hash($password, PASSWORD_DEFAULT),
220            ]);
221
222        return $this;
223    }
224
225
226    /**
227     * Validate a supplied password
228     *
229     * @param string $password
230     *
231     * @return bool
232     */
233    public function checkPassword(string $password): bool
234    {
235        $password_hash = DB::table('user')
236            ->where('user_id', '=', $this->id())
237            ->value('password');
238
239        if ($password_hash !== null && password_verify($password, $password_hash)) {
240            if (password_needs_rehash($password_hash, PASSWORD_DEFAULT)) {
241                $this->setPassword($password);
242            }
243
244            return true;
245        }
246
247        return false;
248    }
249
250    /**
251     * A closure which will create an object from a database row.
252     *
253     * @return Closure
254     */
255    public static function rowMapper(): Closure
256    {
257        return static function (stdClass $row): User {
258            return new self((int) $row->user_id, $row->user_name, $row->real_name, $row->email);
259        };
260    }
261}
262