xref: /webtrees/app/User.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
15a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a25f0a04SGreg Roach */
17*fcfa147eSGreg 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;
252b7831a1SGreg Roachuse Illuminate\Support\Collection;
26b0b72ea4SGreg Roachuse stdClass;
2760bc3e3fSGreg Roach
28a25f0a04SGreg Roach/**
2976692c8bSGreg Roach * Provide an interface to the wt_user table.
30a25f0a04SGreg Roach */
31e5a6b4d4SGreg Roachclass User implements UserInterface
32c1010edaSGreg Roach{
33c04dd3c1SGreg Roach    /** @var  int The primary key of this user. */
34a25f0a04SGreg Roach    private $user_id;
35a25f0a04SGreg Roach
36a25f0a04SGreg Roach    /** @var  string The login name of this user. */
37a25f0a04SGreg Roach    private $user_name;
38a25f0a04SGreg Roach
39a25f0a04SGreg Roach    /** @var  string The real (display) name of this user. */
40a25f0a04SGreg Roach    private $real_name;
41a25f0a04SGreg Roach
42a25f0a04SGreg Roach    /** @var  string The email address of this user. */
43a25f0a04SGreg Roach    private $email;
44a25f0a04SGreg Roach
4515d603e7SGreg Roach    /** @var string[] Cached copy of the wt_user_setting table. */
4615d603e7SGreg Roach    private $preferences = [];
47a25f0a04SGreg Roach
48a25f0a04SGreg Roach    /**
49e5a6b4d4SGreg Roach     * User constructor.
50f7fb7d41SGreg Roach     *
518b67c11aSGreg Roach     * @param int    $user_id
528b67c11aSGreg Roach     * @param string $user_name
538b67c11aSGreg Roach     * @param string $real_name
548b67c11aSGreg Roach     * @param string $email
55f7fb7d41SGreg Roach     */
56e5a6b4d4SGreg Roach    public function __construct(int $user_id, string $user_name, string $real_name, string $email)
57c1010edaSGreg Roach    {
588b67c11aSGreg Roach        $this->user_id   = $user_id;
598b67c11aSGreg Roach        $this->user_name = $user_name;
608b67c11aSGreg Roach        $this->real_name = $real_name;
618b67c11aSGreg Roach        $this->email     = $email;
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     */
918f53f488SRico Sonntag    public function setEmail($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     */
123e5a6b4d4SGreg Roach    public function setRealName($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     */
155e5a6b4d4SGreg Roach    public function setUserName($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.
172a25f0a04SGreg 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    {
1822b7831a1SGreg Roach        $preferences = app('cache.array')->rememberForever('user_setting' . $this->user_id, function (): Collection {
18383c7613eSGreg Roach            if ($this->user_id) {
18483c7613eSGreg Roach                return DB::table('user_setting')
18501461f86SGreg Roach                    ->where('user_id', '=', $this->user_id)
1862b7831a1SGreg Roach                    ->pluck('setting_value', 'setting_name');
187a25f0a04SGreg Roach            }
188e364afe4SGreg Roach
189e364afe4SGreg Roach            return new Collection();
19083c7613eSGreg Roach        });
191a25f0a04SGreg Roach
1922b7831a1SGreg Roach        return $preferences->get($setting_name) ?? $default;
193a25f0a04SGreg Roach    }
194a25f0a04SGreg Roach
195a25f0a04SGreg Roach    /**
196a25f0a04SGreg Roach     * Update a setting for the user.
197a25f0a04SGreg Roach     *
198a25f0a04SGreg Roach     * @param string $setting_name
199a25f0a04SGreg Roach     * @param string $setting_value
200a25f0a04SGreg Roach     *
201e5a6b4d4SGreg Roach     * @return UserInterface
202a25f0a04SGreg Roach     */
203e5a6b4d4SGreg Roach    public function setPreference(string $setting_name, string $setting_value): UserInterface
204c1010edaSGreg Roach    {
205c04dd3c1SGreg Roach        if ($this->user_id !== 0 && $this->getPreference($setting_name) !== $setting_value) {
20601461f86SGreg Roach            DB::table('user_setting')->updateOrInsert([
20701461f86SGreg Roach                'user_id'      => $this->user_id,
20801461f86SGreg Roach                'setting_name' => $setting_name,
20901461f86SGreg Roach            ], [
21001461f86SGreg Roach                'setting_value' => $setting_value,
211c1010edaSGreg Roach            ]);
21215d603e7SGreg Roach
213a25f0a04SGreg Roach            $this->preferences[$setting_name] = $setting_value;
214a25f0a04SGreg Roach        }
215a25f0a04SGreg Roach
216611d1316SGreg Roach        app('cache.array')->forget('user_setting' . $this->user_id);
217611d1316SGreg Roach
218a25f0a04SGreg Roach        return $this;
219a25f0a04SGreg Roach    }
220e5a6b4d4SGreg Roach
221e5a6b4d4SGreg Roach    /**
222e5a6b4d4SGreg Roach     * Set the password of this user.
223e5a6b4d4SGreg Roach     *
224e5a6b4d4SGreg Roach     * @param string $password
225e5a6b4d4SGreg Roach     *
226e5a6b4d4SGreg Roach     * @return User
227e5a6b4d4SGreg Roach     */
228e5a6b4d4SGreg Roach    public function setPassword(string $password): User
229e5a6b4d4SGreg Roach    {
230e5a6b4d4SGreg Roach        DB::table('user')
231e5a6b4d4SGreg Roach            ->where('user_id', '=', $this->user_id)
232e5a6b4d4SGreg Roach            ->update([
233e5a6b4d4SGreg Roach                'password' => password_hash($password, PASSWORD_DEFAULT),
234e5a6b4d4SGreg Roach            ]);
235e5a6b4d4SGreg Roach
236e5a6b4d4SGreg Roach        return $this;
237e5a6b4d4SGreg Roach    }
238e5a6b4d4SGreg Roach
239e5a6b4d4SGreg Roach
240e5a6b4d4SGreg Roach    /**
241e5a6b4d4SGreg Roach     * Validate a supplied password
242e5a6b4d4SGreg Roach     *
243e5a6b4d4SGreg Roach     * @param string $password
244e5a6b4d4SGreg Roach     *
245e5a6b4d4SGreg Roach     * @return bool
246e5a6b4d4SGreg Roach     */
247e5a6b4d4SGreg Roach    public function checkPassword(string $password): bool
248e5a6b4d4SGreg Roach    {
249e5a6b4d4SGreg Roach        $password_hash = DB::table('user')
2503e1b7b6eSGreg Roach            ->where('user_id', '=', $this->id())
251e5a6b4d4SGreg Roach            ->value('password');
252e5a6b4d4SGreg Roach
253e5a6b4d4SGreg Roach        if ($password_hash !== null && password_verify($password, $password_hash)) {
254e5a6b4d4SGreg Roach            if (password_needs_rehash($password_hash, PASSWORD_DEFAULT)) {
255e5a6b4d4SGreg Roach                $this->setPassword($password);
256e5a6b4d4SGreg Roach            }
257e5a6b4d4SGreg Roach
258e5a6b4d4SGreg Roach            return true;
259e5a6b4d4SGreg Roach        }
260e5a6b4d4SGreg Roach
261e5a6b4d4SGreg Roach        return false;
262e5a6b4d4SGreg Roach    }
2633e1b7b6eSGreg Roach
2643e1b7b6eSGreg Roach    /**
2653e1b7b6eSGreg Roach     * A closure which will create an object from a database row.
2663e1b7b6eSGreg Roach     *
2673e1b7b6eSGreg Roach     * @return Closure
2683e1b7b6eSGreg Roach     */
2693e1b7b6eSGreg Roach    public static function rowMapper(): Closure
2703e1b7b6eSGreg Roach    {
2716c2179e2SGreg Roach        return static function (stdClass $row): User {
2723e1b7b6eSGreg Roach            return new static((int) $row->user_id, $row->user_name, $row->real_name, $row->email);
2733e1b7b6eSGreg Roach        };
2743e1b7b6eSGreg Roach    }
275a25f0a04SGreg Roach}
276