xref: /webtrees/app/User.php (revision 5a8afed46297e8105e3e5a33ce37e6a8e88bc79d)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18c04dd3c1SGreg Roachdeclare(strict_types=1);
19c04dd3c1SGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21a25f0a04SGreg Roach
228b67c11aSGreg Roachuse Closure;
23e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
2460bc3e3fSGreg Roach
25b55cbc6bSGreg Roachuse function is_string;
26b55cbc6bSGreg Roach
27a25f0a04SGreg Roach/**
2876692c8bSGreg Roach * Provide an interface to the wt_user table.
29a25f0a04SGreg Roach */
30e5a6b4d4SGreg Roachclass User implements UserInterface
31c1010edaSGreg Roach{
32ee76412bSGreg Roach    private int $user_id;
33a25f0a04SGreg Roach
34ee76412bSGreg Roach    private string $user_name;
35a25f0a04SGreg Roach
36ee76412bSGreg Roach    private string $real_name;
37a25f0a04SGreg Roach
38ee76412bSGreg Roach    private string $email;
39a25f0a04SGreg Roach
4045f11dc7SGreg Roach    /** @var array<string,string> */
41ee76412bSGreg Roach    private array $preferences;
42a25f0a04SGreg Roach
43a25f0a04SGreg Roach    /**
448b67c11aSGreg Roach     * @param int    $user_id
458b67c11aSGreg Roach     * @param string $user_name
468b67c11aSGreg Roach     * @param string $real_name
478b67c11aSGreg Roach     * @param string $email
48f7fb7d41SGreg Roach     */
49e5a6b4d4SGreg Roach    public function __construct(int $user_id, string $user_name, string $real_name, string $email)
50c1010edaSGreg Roach    {
518b67c11aSGreg Roach        $this->user_id   = $user_id;
528b67c11aSGreg Roach        $this->user_name = $user_name;
538b67c11aSGreg Roach        $this->real_name = $real_name;
548b67c11aSGreg Roach        $this->email     = $email;
55ee76412bSGreg Roach
56ee76412bSGreg Roach        $this->preferences = DB::table('user_setting')
57ee76412bSGreg Roach            ->where('user_id', '=', $this->user_id)
58ee76412bSGreg Roach            ->pluck('setting_value', 'setting_name')
59ee76412bSGreg Roach            ->all();
608b67c11aSGreg Roach    }
618b67c11aSGreg Roach
628b67c11aSGreg Roach    /**
63e5a6b4d4SGreg Roach     * The user‘s internal identifier.
64a25f0a04SGreg Roach     *
65c04dd3c1SGreg Roach     * @return int
66a25f0a04SGreg Roach     */
67895230eeSGreg Roach    public function id(): int
68c1010edaSGreg Roach    {
69a25f0a04SGreg Roach        return $this->user_id;
70a25f0a04SGreg Roach    }
71a25f0a04SGreg Roach
72a25f0a04SGreg Roach    /**
73e5a6b4d4SGreg Roach     * The users email address.
74a25f0a04SGreg Roach     *
75a25f0a04SGreg Roach     * @return string
76a25f0a04SGreg Roach     */
77e5a6b4d4SGreg Roach    public function email(): string
78c1010edaSGreg Roach    {
79a25f0a04SGreg Roach        return $this->email;
80a25f0a04SGreg Roach    }
81a25f0a04SGreg Roach
82a25f0a04SGreg Roach    /**
83a25f0a04SGreg Roach     * Set the email address of this user.
84a25f0a04SGreg Roach     *
85a25f0a04SGreg Roach     * @param string $email
86a25f0a04SGreg Roach     *
87a25f0a04SGreg Roach     * @return User
88a25f0a04SGreg Roach     */
8924f2a3afSGreg Roach    public function setEmail(string $email): User
90c1010edaSGreg Roach    {
91a25f0a04SGreg Roach        if ($this->email !== $email) {
92a25f0a04SGreg Roach            $this->email = $email;
9301461f86SGreg Roach
9401461f86SGreg Roach            DB::table('user')
9501461f86SGreg Roach                ->where('user_id', '=', $this->user_id)
9601461f86SGreg Roach                ->update([
9701461f86SGreg Roach                    'email' => $email,
98c1010edaSGreg Roach                ]);
99a25f0a04SGreg Roach        }
100a25f0a04SGreg Roach
101a25f0a04SGreg Roach        return $this;
102a25f0a04SGreg Roach    }
103a25f0a04SGreg Roach
104a25f0a04SGreg Roach    /**
105e5a6b4d4SGreg Roach     * The user‘s real name.
106a25f0a04SGreg Roach     *
107e5a6b4d4SGreg Roach     * @return string
108e5a6b4d4SGreg Roach     */
109e5a6b4d4SGreg Roach    public function realName(): string
110e5a6b4d4SGreg Roach    {
111e5a6b4d4SGreg Roach        return $this->real_name;
112e5a6b4d4SGreg Roach    }
113e5a6b4d4SGreg Roach
114e5a6b4d4SGreg Roach    /**
115e5a6b4d4SGreg Roach     * Set the real name of this user.
116e5a6b4d4SGreg Roach     *
117e5a6b4d4SGreg Roach     * @param string $real_name
118a25f0a04SGreg Roach     *
119a25f0a04SGreg Roach     * @return User
120a25f0a04SGreg Roach     */
12124f2a3afSGreg Roach    public function setRealName(string $real_name): User
122c1010edaSGreg Roach    {
123e5a6b4d4SGreg Roach        if ($this->real_name !== $real_name) {
124e5a6b4d4SGreg Roach            $this->real_name = $real_name;
125e5a6b4d4SGreg Roach
12601461f86SGreg Roach            DB::table('user')
12701461f86SGreg Roach                ->where('user_id', '=', $this->user_id)
12801461f86SGreg Roach                ->update([
129e5a6b4d4SGreg Roach                    'real_name' => $real_name,
130015c99a2SGreg Roach                ]);
131e5a6b4d4SGreg Roach        }
132e5a6b4d4SGreg Roach
133e5a6b4d4SGreg Roach        return $this;
134e5a6b4d4SGreg Roach    }
135e5a6b4d4SGreg Roach
136e5a6b4d4SGreg Roach    /**
137e5a6b4d4SGreg Roach     * The user‘s login name.
138e5a6b4d4SGreg Roach     *
139e5a6b4d4SGreg Roach     * @return string
140e5a6b4d4SGreg Roach     */
141e5a6b4d4SGreg Roach    public function userName(): string
142e5a6b4d4SGreg Roach    {
143e5a6b4d4SGreg Roach        return $this->user_name;
144e5a6b4d4SGreg Roach    }
145e5a6b4d4SGreg Roach
146e5a6b4d4SGreg Roach    /**
147e5a6b4d4SGreg Roach     * Set the login name for this user.
148e5a6b4d4SGreg Roach     *
149e5a6b4d4SGreg Roach     * @param string $user_name
150e5a6b4d4SGreg Roach     *
1516612c384SGreg Roach     * @return self
152e5a6b4d4SGreg Roach     */
15324f2a3afSGreg Roach    public function setUserName(string $user_name): self
154e5a6b4d4SGreg Roach    {
155e5a6b4d4SGreg Roach        if ($this->user_name !== $user_name) {
156e5a6b4d4SGreg Roach            $this->user_name = $user_name;
157e5a6b4d4SGreg Roach
158e5a6b4d4SGreg Roach            DB::table('user')
159e5a6b4d4SGreg Roach                ->where('user_id', '=', $this->user_id)
160e5a6b4d4SGreg Roach                ->update([
161e5a6b4d4SGreg Roach                    'user_name' => $user_name,
162e5a6b4d4SGreg Roach                ]);
163e5a6b4d4SGreg Roach        }
164a25f0a04SGreg Roach
165a25f0a04SGreg Roach        return $this;
166a25f0a04SGreg Roach    }
167a25f0a04SGreg Roach
168a25f0a04SGreg Roach    /**
169a25f0a04SGreg Roach     * Fetch a user option/setting from the wt_user_setting table.
170ee76412bSGreg Roach     * Since we'll fetch several settings for each user, and since there aren't
171a25f0a04SGreg Roach     * that many of them, fetch them all in one database query
172a25f0a04SGreg Roach     *
173a25f0a04SGreg Roach     * @param string $setting_name
17415d603e7SGreg Roach     * @param string $default
175a25f0a04SGreg Roach     *
17615d603e7SGreg Roach     * @return string
177a25f0a04SGreg Roach     */
178e5a6b4d4SGreg Roach    public function getPreference(string $setting_name, string $default = ''): string
179c1010edaSGreg Roach    {
180ee76412bSGreg Roach        return $this->preferences[$setting_name] ?? $default;
181a25f0a04SGreg Roach    }
182a25f0a04SGreg Roach
183a25f0a04SGreg Roach    /**
184a25f0a04SGreg Roach     * Update a setting for the user.
185a25f0a04SGreg Roach     *
186a25f0a04SGreg Roach     * @param string $setting_name
187a25f0a04SGreg Roach     * @param string $setting_value
188a25f0a04SGreg Roach     *
1897c4add84SGreg Roach     * @return void
190a25f0a04SGreg Roach     */
1917c4add84SGreg Roach    public function setPreference(string $setting_name, string $setting_value): void
192c1010edaSGreg Roach    {
193ee76412bSGreg Roach        if ($this->getPreference($setting_name) !== $setting_value) {
19401461f86SGreg Roach            DB::table('user_setting')->updateOrInsert([
19501461f86SGreg Roach                'user_id'      => $this->user_id,
19601461f86SGreg Roach                'setting_name' => $setting_name,
19701461f86SGreg Roach            ], [
19801461f86SGreg Roach                'setting_value' => $setting_value,
199c1010edaSGreg Roach            ]);
20015d603e7SGreg Roach
201a25f0a04SGreg Roach            $this->preferences[$setting_name] = $setting_value;
202a25f0a04SGreg Roach        }
203a25f0a04SGreg Roach    }
204e5a6b4d4SGreg Roach
205e5a6b4d4SGreg Roach    /**
206e5a6b4d4SGreg Roach     * Set the password of this user.
207e5a6b4d4SGreg Roach     *
208e5a6b4d4SGreg Roach     * @param string $password
209e5a6b4d4SGreg Roach     *
210e5a6b4d4SGreg Roach     * @return User
211e5a6b4d4SGreg Roach     */
212c5d4d20fSGreg Roach    public function setPassword(#[\SensitiveParameter] string $password): User
213e5a6b4d4SGreg Roach    {
214e5a6b4d4SGreg Roach        DB::table('user')
215e5a6b4d4SGreg Roach            ->where('user_id', '=', $this->user_id)
216e5a6b4d4SGreg Roach            ->update([
217e5a6b4d4SGreg Roach                'password' => password_hash($password, PASSWORD_DEFAULT),
218e5a6b4d4SGreg Roach            ]);
219e5a6b4d4SGreg Roach
220e5a6b4d4SGreg Roach        return $this;
221e5a6b4d4SGreg Roach    }
222e5a6b4d4SGreg Roach
223e5a6b4d4SGreg Roach    /**
224e5a6b4d4SGreg Roach     * Validate a supplied password
225e5a6b4d4SGreg Roach     *
226e5a6b4d4SGreg Roach     * @param string $password
227e5a6b4d4SGreg Roach     *
228e5a6b4d4SGreg Roach     * @return bool
229e5a6b4d4SGreg Roach     */
230c5d4d20fSGreg Roach    public function checkPassword(#[\SensitiveParameter] string $password): bool
231e5a6b4d4SGreg Roach    {
232e5a6b4d4SGreg Roach        $password_hash = DB::table('user')
2333e1b7b6eSGreg Roach            ->where('user_id', '=', $this->id())
234e5a6b4d4SGreg Roach            ->value('password');
235e5a6b4d4SGreg Roach
236b55cbc6bSGreg Roach        if (is_string($password_hash) && password_verify($password, $password_hash)) {
237e5a6b4d4SGreg Roach            if (password_needs_rehash($password_hash, PASSWORD_DEFAULT)) {
238e5a6b4d4SGreg Roach                $this->setPassword($password);
239e5a6b4d4SGreg Roach            }
240e5a6b4d4SGreg Roach
241e5a6b4d4SGreg Roach            return true;
242e5a6b4d4SGreg Roach        }
243e5a6b4d4SGreg Roach
244e5a6b4d4SGreg Roach        return false;
245e5a6b4d4SGreg Roach    }
2463e1b7b6eSGreg Roach
2473e1b7b6eSGreg Roach    /**
2483e1b7b6eSGreg Roach     * A closure which will create an object from a database row.
2493e1b7b6eSGreg Roach     *
250*c6921a17SGreg Roach     * @return Closure(object):User
2513e1b7b6eSGreg Roach     */
2523e1b7b6eSGreg Roach    public static function rowMapper(): Closure
2533e1b7b6eSGreg Roach    {
254743491b8SGreg Roach        return static fn (object $row): User => new self((int) $row->user_id, $row->user_name, $row->real_name, $row->email);
2553e1b7b6eSGreg Roach    }
256a25f0a04SGreg Roach}
257