xref: /webtrees/app/User.php (revision b2c011d7d8ff495bd3636357907da539925bd5f9)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees;
19
20use Closure;
21use Fisharebest\Webtrees\Contracts\UserInterface;
22use Illuminate\Database\Capsule\Manager as DB;
23use Illuminate\Support\Collection;
24use stdClass;
25
26/**
27 * Provide an interface to the wt_user table.
28 */
29class User implements UserInterface
30{
31    /** @var  int The primary key of this user. */
32    private $user_id;
33
34    /** @var  string The login name of this user. */
35    private $user_name;
36
37    /** @var  string The real (display) name of this user. */
38    private $real_name;
39
40    /** @var  string The email address of this user. */
41    private $email;
42
43    /** @var string[] Cached copy of the wt_user_setting table. */
44    private $preferences = [];
45
46    /**
47     * User constructor.
48     *
49     * @param int    $user_id
50     * @param string $user_name
51     * @param string $real_name
52     * @param string $email
53     */
54    public function __construct(int $user_id, string $user_name, string $real_name, string $email)
55    {
56        $this->user_id   = $user_id;
57        $this->user_name = $user_name;
58        $this->real_name = $real_name;
59        $this->email     = $email;
60    }
61
62    /**
63     * The user‘s internal identifier.
64     *
65     * @return int
66     */
67    public function id(): int
68    {
69        return $this->user_id;
70    }
71
72    /**
73     * The users email address.
74     *
75     * @return string
76     */
77    public function email(): string
78    {
79        return $this->email;
80    }
81
82    /**
83     * Set the email address of this user.
84     *
85     * @param string $email
86     *
87     * @return User
88     */
89    public function setEmail($email): User
90    {
91        if ($this->email !== $email) {
92            $this->email = $email;
93
94            DB::table('user')
95                ->where('user_id', '=', $this->user_id)
96                ->update([
97                    'email' => $email,
98                ]);
99        }
100
101        return $this;
102    }
103
104    /**
105     * The user‘s real name.
106     *
107     * @return string
108     */
109    public function realName(): string
110    {
111        return $this->real_name;
112    }
113
114    /**
115     * Set the real name of this user.
116     *
117     * @param string $real_name
118     *
119     * @return User
120     */
121    public function setRealName($real_name): User
122    {
123        if ($this->real_name !== $real_name) {
124            $this->real_name = $real_name;
125
126            DB::table('user')
127                ->where('user_id', '=', $this->user_id)
128                ->update([
129                    'real_name' => $real_name,
130                ]);
131        }
132
133        return $this;
134    }
135
136    /**
137     * The user‘s login name.
138     *
139     * @return string
140     */
141    public function userName(): string
142    {
143        return $this->user_name;
144    }
145
146    /**
147     * Set the login name for this user.
148     *
149     * @param string $user_name
150     *
151     * @return $this
152     */
153    public function setUserName($user_name): self
154    {
155        if ($this->user_name !== $user_name) {
156            $this->user_name = $user_name;
157
158            DB::table('user')
159                ->where('user_id', '=', $this->user_id)
160                ->update([
161                    'user_name' => $user_name,
162                ]);
163        }
164
165        return $this;
166    }
167
168    /**
169     * Fetch a user option/setting from the wt_user_setting table.
170     * Since we'll fetch several settings for each user, and since there aren’t
171     * that many of them, fetch them all in one database query
172     *
173     * @param string $setting_name
174     * @param string $default
175     *
176     * @return string
177     */
178    public function getPreference(string $setting_name, string $default = ''): string
179    {
180        $preferences = app('cache.array')->rememberForever('user_setting' . $this->user_id, function (): Collection {
181            if ($this->user_id) {
182                return DB::table('user_setting')
183                    ->where('user_id', '=', $this->user_id)
184                    ->pluck('setting_value', 'setting_name');
185            }
186
187            return new Collection();
188        });
189
190        return $preferences->get($setting_name) ?? $default;
191    }
192
193    /**
194     * Update a setting for the user.
195     *
196     * @param string $setting_name
197     * @param string $setting_value
198     *
199     * @return UserInterface
200     */
201    public function setPreference(string $setting_name, string $setting_value): UserInterface
202    {
203        if ($this->user_id !== 0 && $this->getPreference($setting_name) !== $setting_value) {
204            DB::table('user_setting')->updateOrInsert([
205                'user_id'      => $this->user_id,
206                'setting_name' => $setting_name,
207            ], [
208                'setting_value' => $setting_value,
209            ]);
210
211            $this->preferences[$setting_name] = $setting_value;
212        }
213
214        app('cache.array')->forget('user_setting' . $this->user_id);
215
216        return $this;
217    }
218
219    /**
220     * Set the password of this user.
221     *
222     * @param string $password
223     *
224     * @return User
225     */
226    public function setPassword(string $password): User
227    {
228        DB::table('user')
229            ->where('user_id', '=', $this->user_id)
230            ->update([
231                'password' => password_hash($password, PASSWORD_DEFAULT),
232            ]);
233
234        return $this;
235    }
236
237
238    /**
239     * Validate a supplied password
240     *
241     * @param string $password
242     *
243     * @return bool
244     */
245    public function checkPassword(string $password): bool
246    {
247        $password_hash = DB::table('user')
248            ->where('user_id', '=', $this->id())
249            ->value('password');
250
251        if ($password_hash !== null && password_verify($password, $password_hash)) {
252            if (password_needs_rehash($password_hash, PASSWORD_DEFAULT)) {
253                $this->setPassword($password);
254            }
255
256            return true;
257        }
258
259        return false;
260    }
261
262    /**
263     * A closure which will create an object from a database row.
264     *
265     * @return Closure
266     */
267    public static function rowMapper(): Closure
268    {
269        return static function (stdClass $row): User {
270            return new static((int) $row->user_id, $row->user_name, $row->real_name, $row->email);
271        };
272    }
273}
274