xref: /webtrees/app/User.php (revision ca50fb84724cb523ca8f4be8abd453a667296dcf)
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 = app('cache.array')->rememberForever('user_setting' . $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 UserInterface
202     */
203    public function setPreference(string $setting_name, string $setting_value): UserInterface
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        app('cache.array')->forget('user_setting' . $this->user_id);
217
218        return $this;
219    }
220
221    /**
222     * Set the password of this user.
223     *
224     * @param string $password
225     *
226     * @return User
227     */
228    public function setPassword(string $password): User
229    {
230        DB::table('user')
231            ->where('user_id', '=', $this->user_id)
232            ->update([
233                'password' => password_hash($password, PASSWORD_DEFAULT),
234            ]);
235
236        return $this;
237    }
238
239
240    /**
241     * Validate a supplied password
242     *
243     * @param string $password
244     *
245     * @return bool
246     */
247    public function checkPassword(string $password): bool
248    {
249        $password_hash = DB::table('user')
250            ->where('user_id', '=', $this->id())
251            ->value('password');
252
253        if ($password_hash !== null && password_verify($password, $password_hash)) {
254            if (password_needs_rehash($password_hash, PASSWORD_DEFAULT)) {
255                $this->setPassword($password);
256            }
257
258            return true;
259        }
260
261        return false;
262    }
263
264    /**
265     * A closure which will create an object from a database row.
266     *
267     * @return Closure
268     */
269    public static function rowMapper(): Closure
270    {
271        return static function (stdClass $row): User {
272            return new static((int) $row->user_id, $row->user_name, $row->real_name, $row->email);
273        };
274    }
275}
276