xref: /webtrees/app/User.php (revision b62a8ecaef02a45d7e018fdb0f702d4575d8d0de)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://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 Illuminate\Support\Collection;
26use stdClass;
27
28/**
29 * Provide an interface to the wt_user table.
30 */
31class User implements UserInterface
32{
33    /** @var  int The primary key of this user. */
34    private $user_id;
35
36    /** @var  string The login name of this user. */
37    private $user_name;
38
39    /** @var  string The real (display) name of this user. */
40    private $real_name;
41
42    /** @var  string The email address of this user. */
43    private $email;
44
45    /** @var string[] Cached copy of the wt_user_setting table. */
46    private $preferences = [];
47
48    /**
49     * User constructor.
50     *
51     * @param int    $user_id
52     * @param string $user_name
53     * @param string $real_name
54     * @param string $email
55     */
56    public function __construct(int $user_id, string $user_name, string $real_name, string $email)
57    {
58        $this->user_id   = $user_id;
59        $this->user_name = $user_name;
60        $this->real_name = $real_name;
61        $this->email     = $email;
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($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($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($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        $preferences = Registry::cache()->array()->remember('user-prefs-' . $this->user_id, function (): Collection {
183            if ($this->user_id) {
184                return DB::table('user_setting')
185                    ->where('user_id', '=', $this->user_id)
186                    ->pluck('setting_value', 'setting_name');
187            }
188
189            return new Collection();
190        });
191
192        return $preferences->get($setting_name, $default);
193    }
194
195    /**
196     * Update a setting for the user.
197     *
198     * @param string $setting_name
199     * @param string $setting_value
200     *
201     * @return void
202     */
203    public function setPreference(string $setting_name, string $setting_value): void
204    {
205        if ($this->user_id !== 0 && $this->getPreference($setting_name) !== $setting_value) {
206            DB::table('user_setting')->updateOrInsert([
207                'user_id'      => $this->user_id,
208                'setting_name' => $setting_name,
209            ], [
210                'setting_value' => $setting_value,
211            ]);
212
213            $this->preferences[$setting_name] = $setting_value;
214        }
215    }
216
217    /**
218     * Set the password of this user.
219     *
220     * @param string $password
221     *
222     * @return User
223     */
224    public function setPassword(string $password): User
225    {
226        DB::table('user')
227            ->where('user_id', '=', $this->user_id)
228            ->update([
229                'password' => password_hash($password, PASSWORD_DEFAULT),
230            ]);
231
232        return $this;
233    }
234
235
236    /**
237     * Validate a supplied password
238     *
239     * @param string $password
240     *
241     * @return bool
242     */
243    public function checkPassword(string $password): bool
244    {
245        $password_hash = DB::table('user')
246            ->where('user_id', '=', $this->id())
247            ->value('password');
248
249        if ($password_hash !== null && password_verify($password, $password_hash)) {
250            if (password_needs_rehash($password_hash, PASSWORD_DEFAULT)) {
251                $this->setPassword($password);
252            }
253
254            return true;
255        }
256
257        return false;
258    }
259
260    /**
261     * A closure which will create an object from a database row.
262     *
263     * @return Closure
264     */
265    public static function rowMapper(): Closure
266    {
267        return static function (stdClass $row): User {
268            return new self((int) $row->user_id, $row->user_name, $row->real_name, $row->email);
269        };
270    }
271}
272