xref: /webtrees/app/User.php (revision 45f11dc729c5cca80fe8be06331f7dbce0f4b666)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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;
2401461f86SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
25b0b72ea4SGreg Roachuse stdClass;
2660bc3e3fSGreg 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
40*45f11dc7SGreg Roach    /** @var array<string,string> */
41ee76412bSGreg Roach    private array $preferences;
42a25f0a04SGreg Roach
43a25f0a04SGreg Roach    /**
44e5a6b4d4SGreg Roach     * User constructor.
45f7fb7d41SGreg Roach     *
468b67c11aSGreg Roach     * @param int    $user_id
478b67c11aSGreg Roach     * @param string $user_name
488b67c11aSGreg Roach     * @param string $real_name
498b67c11aSGreg Roach     * @param string $email
50f7fb7d41SGreg Roach     */
51e5a6b4d4SGreg Roach    public function __construct(int $user_id, string $user_name, string $real_name, string $email)
52c1010edaSGreg Roach    {
538b67c11aSGreg Roach        $this->user_id   = $user_id;
548b67c11aSGreg Roach        $this->user_name = $user_name;
558b67c11aSGreg Roach        $this->real_name = $real_name;
568b67c11aSGreg Roach        $this->email     = $email;
57ee76412bSGreg Roach
58ee76412bSGreg Roach        $this->preferences = DB::table('user_setting')
59ee76412bSGreg Roach            ->where('user_id', '=', $this->user_id)
60ee76412bSGreg Roach            ->pluck('setting_value', 'setting_name')
61ee76412bSGreg Roach            ->all();
628b67c11aSGreg Roach    }
638b67c11aSGreg Roach
648b67c11aSGreg Roach    /**
65e5a6b4d4SGreg Roach     * The user‘s internal identifier.
66a25f0a04SGreg Roach     *
67c04dd3c1SGreg Roach     * @return int
68a25f0a04SGreg Roach     */
69895230eeSGreg Roach    public function id(): int
70c1010edaSGreg Roach    {
71a25f0a04SGreg Roach        return $this->user_id;
72a25f0a04SGreg Roach    }
73a25f0a04SGreg Roach
74a25f0a04SGreg Roach    /**
75e5a6b4d4SGreg Roach     * The users email address.
76a25f0a04SGreg Roach     *
77a25f0a04SGreg Roach     * @return string
78a25f0a04SGreg Roach     */
79e5a6b4d4SGreg Roach    public function email(): string
80c1010edaSGreg Roach    {
81a25f0a04SGreg Roach        return $this->email;
82a25f0a04SGreg Roach    }
83a25f0a04SGreg Roach
84a25f0a04SGreg Roach    /**
85a25f0a04SGreg Roach     * Set the email address of this user.
86a25f0a04SGreg Roach     *
87a25f0a04SGreg Roach     * @param string $email
88a25f0a04SGreg Roach     *
89a25f0a04SGreg Roach     * @return User
90a25f0a04SGreg Roach     */
9124f2a3afSGreg Roach    public function setEmail(string $email): User
92c1010edaSGreg Roach    {
93a25f0a04SGreg Roach        if ($this->email !== $email) {
94a25f0a04SGreg Roach            $this->email = $email;
9501461f86SGreg Roach
9601461f86SGreg Roach            DB::table('user')
9701461f86SGreg Roach                ->where('user_id', '=', $this->user_id)
9801461f86SGreg Roach                ->update([
9901461f86SGreg Roach                    'email' => $email,
100c1010edaSGreg Roach                ]);
101a25f0a04SGreg Roach        }
102a25f0a04SGreg Roach
103a25f0a04SGreg Roach        return $this;
104a25f0a04SGreg Roach    }
105a25f0a04SGreg Roach
106a25f0a04SGreg Roach    /**
107e5a6b4d4SGreg Roach     * The user‘s real name.
108a25f0a04SGreg Roach     *
109e5a6b4d4SGreg Roach     * @return string
110e5a6b4d4SGreg Roach     */
111e5a6b4d4SGreg Roach    public function realName(): string
112e5a6b4d4SGreg Roach    {
113e5a6b4d4SGreg Roach        return $this->real_name;
114e5a6b4d4SGreg Roach    }
115e5a6b4d4SGreg Roach
116e5a6b4d4SGreg Roach    /**
117e5a6b4d4SGreg Roach     * Set the real name of this user.
118e5a6b4d4SGreg Roach     *
119e5a6b4d4SGreg Roach     * @param string $real_name
120a25f0a04SGreg Roach     *
121a25f0a04SGreg Roach     * @return User
122a25f0a04SGreg Roach     */
12324f2a3afSGreg Roach    public function setRealName(string $real_name): User
124c1010edaSGreg Roach    {
125e5a6b4d4SGreg Roach        if ($this->real_name !== $real_name) {
126e5a6b4d4SGreg Roach            $this->real_name = $real_name;
127e5a6b4d4SGreg Roach
12801461f86SGreg Roach            DB::table('user')
12901461f86SGreg Roach                ->where('user_id', '=', $this->user_id)
13001461f86SGreg Roach                ->update([
131e5a6b4d4SGreg Roach                    'real_name' => $real_name,
132015c99a2SGreg Roach                ]);
133e5a6b4d4SGreg Roach        }
134e5a6b4d4SGreg Roach
135e5a6b4d4SGreg Roach        return $this;
136e5a6b4d4SGreg Roach    }
137e5a6b4d4SGreg Roach
138e5a6b4d4SGreg Roach    /**
139e5a6b4d4SGreg Roach     * The user‘s login name.
140e5a6b4d4SGreg Roach     *
141e5a6b4d4SGreg Roach     * @return string
142e5a6b4d4SGreg Roach     */
143e5a6b4d4SGreg Roach    public function userName(): string
144e5a6b4d4SGreg Roach    {
145e5a6b4d4SGreg Roach        return $this->user_name;
146e5a6b4d4SGreg Roach    }
147e5a6b4d4SGreg Roach
148e5a6b4d4SGreg Roach    /**
149e5a6b4d4SGreg Roach     * Set the login name for this user.
150e5a6b4d4SGreg Roach     *
151e5a6b4d4SGreg Roach     * @param string $user_name
152e5a6b4d4SGreg Roach     *
153e5a6b4d4SGreg Roach     * @return $this
154e5a6b4d4SGreg Roach     */
15524f2a3afSGreg Roach    public function setUserName(string $user_name): self
156e5a6b4d4SGreg Roach    {
157e5a6b4d4SGreg Roach        if ($this->user_name !== $user_name) {
158e5a6b4d4SGreg Roach            $this->user_name = $user_name;
159e5a6b4d4SGreg Roach
160e5a6b4d4SGreg Roach            DB::table('user')
161e5a6b4d4SGreg Roach                ->where('user_id', '=', $this->user_id)
162e5a6b4d4SGreg Roach                ->update([
163e5a6b4d4SGreg Roach                    'user_name' => $user_name,
164e5a6b4d4SGreg Roach                ]);
165e5a6b4d4SGreg Roach        }
166a25f0a04SGreg Roach
167a25f0a04SGreg Roach        return $this;
168a25f0a04SGreg Roach    }
169a25f0a04SGreg Roach
170a25f0a04SGreg Roach    /**
171a25f0a04SGreg Roach     * Fetch a user option/setting from the wt_user_setting table.
172ee76412bSGreg Roach     * Since we'll fetch several settings for each user, and since there aren't
173a25f0a04SGreg Roach     * that many of them, fetch them all in one database query
174a25f0a04SGreg Roach     *
175a25f0a04SGreg Roach     * @param string $setting_name
17615d603e7SGreg Roach     * @param string $default
177a25f0a04SGreg Roach     *
17815d603e7SGreg Roach     * @return string
179a25f0a04SGreg Roach     */
180e5a6b4d4SGreg Roach    public function getPreference(string $setting_name, string $default = ''): string
181c1010edaSGreg Roach    {
182ee76412bSGreg Roach        return $this->preferences[$setting_name] ?? $default;
183a25f0a04SGreg Roach    }
184a25f0a04SGreg Roach
185a25f0a04SGreg Roach    /**
186a25f0a04SGreg Roach     * Update a setting for the user.
187a25f0a04SGreg Roach     *
188a25f0a04SGreg Roach     * @param string $setting_name
189a25f0a04SGreg Roach     * @param string $setting_value
190a25f0a04SGreg Roach     *
1917c4add84SGreg Roach     * @return void
192a25f0a04SGreg Roach     */
1937c4add84SGreg Roach    public function setPreference(string $setting_name, string $setting_value): void
194c1010edaSGreg Roach    {
195ee76412bSGreg Roach        if ($this->getPreference($setting_name) !== $setting_value) {
19601461f86SGreg Roach            DB::table('user_setting')->updateOrInsert([
19701461f86SGreg Roach                'user_id'      => $this->user_id,
19801461f86SGreg Roach                'setting_name' => $setting_name,
19901461f86SGreg Roach            ], [
20001461f86SGreg Roach                'setting_value' => $setting_value,
201c1010edaSGreg Roach            ]);
20215d603e7SGreg Roach
203a25f0a04SGreg Roach            $this->preferences[$setting_name] = $setting_value;
204a25f0a04SGreg Roach        }
205a25f0a04SGreg Roach    }
206e5a6b4d4SGreg Roach
207e5a6b4d4SGreg Roach    /**
208e5a6b4d4SGreg Roach     * Set the password of this user.
209e5a6b4d4SGreg Roach     *
210e5a6b4d4SGreg Roach     * @param string $password
211e5a6b4d4SGreg Roach     *
212e5a6b4d4SGreg Roach     * @return User
213e5a6b4d4SGreg Roach     */
214e5a6b4d4SGreg Roach    public function setPassword(string $password): User
215e5a6b4d4SGreg Roach    {
216e5a6b4d4SGreg Roach        DB::table('user')
217e5a6b4d4SGreg Roach            ->where('user_id', '=', $this->user_id)
218e5a6b4d4SGreg Roach            ->update([
219e5a6b4d4SGreg Roach                'password' => password_hash($password, PASSWORD_DEFAULT),
220e5a6b4d4SGreg Roach            ]);
221e5a6b4d4SGreg Roach
222e5a6b4d4SGreg Roach        return $this;
223e5a6b4d4SGreg Roach    }
224e5a6b4d4SGreg Roach
225e5a6b4d4SGreg Roach
226e5a6b4d4SGreg Roach    /**
227e5a6b4d4SGreg Roach     * Validate a supplied password
228e5a6b4d4SGreg Roach     *
229e5a6b4d4SGreg Roach     * @param string $password
230e5a6b4d4SGreg Roach     *
231e5a6b4d4SGreg Roach     * @return bool
232e5a6b4d4SGreg Roach     */
233e5a6b4d4SGreg Roach    public function checkPassword(string $password): bool
234e5a6b4d4SGreg Roach    {
235e5a6b4d4SGreg Roach        $password_hash = DB::table('user')
2363e1b7b6eSGreg Roach            ->where('user_id', '=', $this->id())
237e5a6b4d4SGreg Roach            ->value('password');
238e5a6b4d4SGreg Roach
239e5a6b4d4SGreg Roach        if ($password_hash !== null && password_verify($password, $password_hash)) {
240e5a6b4d4SGreg Roach            if (password_needs_rehash($password_hash, PASSWORD_DEFAULT)) {
241e5a6b4d4SGreg Roach                $this->setPassword($password);
242e5a6b4d4SGreg Roach            }
243e5a6b4d4SGreg Roach
244e5a6b4d4SGreg Roach            return true;
245e5a6b4d4SGreg Roach        }
246e5a6b4d4SGreg Roach
247e5a6b4d4SGreg Roach        return false;
248e5a6b4d4SGreg Roach    }
2493e1b7b6eSGreg Roach
2503e1b7b6eSGreg Roach    /**
2513e1b7b6eSGreg Roach     * A closure which will create an object from a database row.
2523e1b7b6eSGreg Roach     *
2533e1b7b6eSGreg Roach     * @return Closure
2543e1b7b6eSGreg Roach     */
2553e1b7b6eSGreg Roach    public static function rowMapper(): Closure
2563e1b7b6eSGreg Roach    {
2576c2179e2SGreg Roach        return static function (stdClass $row): User {
2581e752ebbSGreg Roach            return new self((int) $row->user_id, $row->user_name, $row->real_name, $row->email);
2593e1b7b6eSGreg Roach        };
2603e1b7b6eSGreg Roach    }
261a25f0a04SGreg Roach}
262