xref: /webtrees/app/User.php (revision bc09afc5fa65fb4779f9edb519c1f4bd626d2cfb)
1a25f0a04SGreg Roach<?php
2a25f0a04SGreg Roach/**
3a25f0a04SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
5a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
6a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
7a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8a25f0a04SGreg Roach * (at your option) any later version.
9a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
10a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12a25f0a04SGreg Roach * GNU General Public License for more details.
13a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
14a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15a25f0a04SGreg Roach */
16c04dd3c1SGreg Roachdeclare(strict_types=1);
17c04dd3c1SGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees;
19a25f0a04SGreg Roach
2001461f86SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
2101461f86SGreg Roachuse Illuminate\Database\Query\Builder;
22b0b72ea4SGreg Roachuse stdClass;
2360bc3e3fSGreg Roach
24a25f0a04SGreg Roach/**
2576692c8bSGreg Roach * Provide an interface to the wt_user table.
26a25f0a04SGreg Roach */
27c1010edaSGreg Roachclass User
28c1010edaSGreg Roach{
29c04dd3c1SGreg Roach    /** @var  int The primary key of this user. */
30a25f0a04SGreg Roach    private $user_id;
31a25f0a04SGreg Roach
32a25f0a04SGreg Roach    /** @var  string The login name of this user. */
33a25f0a04SGreg Roach    private $user_name;
34a25f0a04SGreg Roach
35a25f0a04SGreg Roach    /** @var  string The real (display) name of this user. */
36a25f0a04SGreg Roach    private $real_name;
37a25f0a04SGreg Roach
38a25f0a04SGreg Roach    /** @var  string The email address of this user. */
39a25f0a04SGreg Roach    private $email;
40a25f0a04SGreg Roach
4115d603e7SGreg Roach    /** @var string[] Cached copy of the wt_user_setting table. */
4215d603e7SGreg Roach    private $preferences = [];
43a25f0a04SGreg Roach
4436e59714SGreg Roach    /** @var  User[]|null[] Only fetch users from the database once. */
4532f20c14SGreg Roach    public static $cache = [];
46a25f0a04SGreg Roach
47a25f0a04SGreg Roach    /**
48f7fb7d41SGreg Roach     * Create a new user object from a row in the database.
49f7fb7d41SGreg Roach     *
50b0b72ea4SGreg Roach     * @param stdClass $user A row from the wt_user table
51f7fb7d41SGreg Roach     */
52c1010edaSGreg Roach    public function __construct(stdClass $user)
53c1010edaSGreg Roach    {
54c04dd3c1SGreg Roach        $this->user_id   = (int) $user->user_id;
55f7fb7d41SGreg Roach        $this->user_name = $user->user_name;
56f7fb7d41SGreg Roach        $this->real_name = $user->real_name;
57f7fb7d41SGreg Roach        $this->email     = $user->email;
58f7fb7d41SGreg Roach    }
59f7fb7d41SGreg Roach
60f7fb7d41SGreg Roach    /**
61f7fb7d41SGreg Roach     * Create a new user.
62f7fb7d41SGreg Roach     * The calling code needs to check for duplicates identifiers before calling
63f7fb7d41SGreg Roach     * this function.
64f7fb7d41SGreg Roach     *
65f7fb7d41SGreg Roach     * @param string $user_name
66f7fb7d41SGreg Roach     * @param string $real_name
67f7fb7d41SGreg Roach     * @param string $email
68f7fb7d41SGreg Roach     * @param string $password
69f7fb7d41SGreg Roach     *
70f7fb7d41SGreg Roach     * @return User
71f7fb7d41SGreg Roach     */
728f53f488SRico Sonntag    public static function create($user_name, $real_name, $email, $password): User
73c1010edaSGreg Roach    {
7401461f86SGreg Roach        DB::table('user')->insert([
75f7fb7d41SGreg Roach            'user_name' => $user_name,
76f7fb7d41SGreg Roach            'real_name' => $real_name,
77f7fb7d41SGreg Roach            'email'     => $email,
78f7fb7d41SGreg Roach            'password'  => password_hash($password, PASSWORD_DEFAULT),
79f7fb7d41SGreg Roach        ]);
80f7fb7d41SGreg Roach
81f7fb7d41SGreg Roach        // Set default blocks for this user
82f7fb7d41SGreg Roach        $user = self::findByIdentifier($user_name);
8301461f86SGreg Roach
8401461f86SGreg Roach        (new Builder(DB::connection()))->from('block')->insertUsing(
8501461f86SGreg Roach            ['user_id', 'location', 'block_order', 'module_name'],
8601461f86SGreg Roach            function (Builder $query) use ($user): void {
8701461f86SGreg Roach                $query
8801461f86SGreg Roach                    ->select([DB::raw($user->getuserId()), 'location', 'block_order', 'module_name'])
8901461f86SGreg Roach                    ->from('block')
9001461f86SGreg Roach                    ->where('user_id', '=', -1);
9101461f86SGreg Roach            }
9201461f86SGreg Roach        );
93f7fb7d41SGreg Roach
94f7fb7d41SGreg Roach        return $user;
95f7fb7d41SGreg Roach    }
96f7fb7d41SGreg Roach
97f7fb7d41SGreg Roach    /**
98f7fb7d41SGreg Roach     * Delete a user
99fa4036e8SGreg Roach     *
100fa4036e8SGreg Roach     * @return void
101f7fb7d41SGreg Roach     */
102c1010edaSGreg Roach    public function delete()
103c1010edaSGreg Roach    {
10437b0f34fSGreg Roach        // Don't delete the logs, just set the user to null.
10537b0f34fSGreg Roach        DB::table('log')
10637b0f34fSGreg Roach            ->where('user_id', '=', $this->user_id)
10737b0f34fSGreg Roach            ->update(['user_id' => null]);
10837b0f34fSGreg Roach
109f7fb7d41SGreg Roach        // Take over the user’s pending changes. (What else could we do with them?)
11037b0f34fSGreg Roach        DB::table('change')
11137b0f34fSGreg Roach            ->where('user_id', '=', $this->user_id)
11237b0f34fSGreg Roach            ->where('status', '=', 'rejected')
11337b0f34fSGreg Roach            ->delete();
11437b0f34fSGreg Roach
11537b0f34fSGreg Roach        DB::table('change')
11637b0f34fSGreg Roach            ->where('user_id', '=', $this->user_id)
11737b0f34fSGreg Roach            ->update(['user_id' => Auth::id()]);
11837b0f34fSGreg Roach
11937b0f34fSGreg Roach        // Take over the user's contact details
12037b0f34fSGreg Roach        DB::table('gedcom_setting')
12137b0f34fSGreg Roach            ->where('setting_value', '=', $this->user_id)
12237b0f34fSGreg Roach            ->whereIn('setting_name', ['CONTACT_USER_ID', 'WEBMASTER_USER_ID'])
12337b0f34fSGreg Roach            ->update(['setting_value' => Auth::id()]);
12437b0f34fSGreg Roach
12537b0f34fSGreg Roach        // Delete settings and preferences
12637b0f34fSGreg Roach        DB::table('block_setting')
12737b0f34fSGreg Roach            ->join('block', 'block_setting.block_id', '=', 'block.block_id')
12837b0f34fSGreg Roach            ->where('user_id', '=', $this->user_id)
12937b0f34fSGreg Roach            ->delete();
13037b0f34fSGreg Roach
13137b0f34fSGreg Roach        DB::table('block')->where('user_id', '=', $this->user_id)->delete();
13237b0f34fSGreg Roach        DB::table('user_gedcom_setting')->where('user_id', '=', $this->user_id)->delete();
13337b0f34fSGreg Roach        DB::table('user_setting')->where('user_id', '=', $this->user_id)->delete();
13437b0f34fSGreg Roach        DB::table('message')->where('user_id', '=', $this->user_id)->delete();
13537b0f34fSGreg Roach        DB::table('user')->where('user_id', '=', $this->user_id)->delete();
13637b0f34fSGreg Roach
13737b0f34fSGreg Roach        unset(self::$cache[$this->user_id]);
138f7fb7d41SGreg Roach    }
139f7fb7d41SGreg Roach
140f7fb7d41SGreg Roach    /**
141a25f0a04SGreg Roach     * Find the user with a specified user_id.
142a25f0a04SGreg Roach     *
143cbc1590aSGreg Roach     * @param int|null $user_id
144a25f0a04SGreg Roach     *
145a25f0a04SGreg Roach     * @return User|null
146a25f0a04SGreg Roach     */
147c1010edaSGreg Roach    public static function find($user_id)
148c1010edaSGreg Roach    {
149a25f0a04SGreg Roach        if (!array_key_exists($user_id, self::$cache)) {
15001461f86SGreg Roach            $row = DB::table('user')
15101461f86SGreg Roach                ->where('user_id', '=', $user_id)
15201461f86SGreg Roach                ->first();
15301461f86SGreg Roach
154a25f0a04SGreg Roach            if ($row) {
15506ef8e02SGreg Roach                self::$cache[$user_id] = new self($row);
156a25f0a04SGreg Roach            } else {
157a25f0a04SGreg Roach                self::$cache[$user_id] = null;
158a25f0a04SGreg Roach            }
159a25f0a04SGreg Roach        }
160a25f0a04SGreg Roach
161a25f0a04SGreg Roach        return self::$cache[$user_id];
162a25f0a04SGreg Roach    }
163a25f0a04SGreg Roach
164a25f0a04SGreg Roach    /**
165a982a56eSGreg Roach     * Find the user with a specified email address.
166a982a56eSGreg Roach     *
167a982a56eSGreg Roach     * @param string $email
168a982a56eSGreg Roach     *
169a982a56eSGreg Roach     * @return User|null
170a982a56eSGreg Roach     */
171c1010edaSGreg Roach    public static function findByEmail($email)
172c1010edaSGreg Roach    {
17301461f86SGreg Roach        $user_id = (int) DB::table('user')
17401461f86SGreg Roach            ->where('email', '=', $email)
17501461f86SGreg Roach            ->value('user_id');
176a982a56eSGreg Roach
177a982a56eSGreg Roach        return self::find($user_id);
178a982a56eSGreg Roach    }
179a982a56eSGreg Roach
180a982a56eSGreg Roach    /**
181a982a56eSGreg Roach     * Find the user with a specified user_name or email address.
182a25f0a04SGreg Roach     *
183a25f0a04SGreg Roach     * @param string $identifier
184a25f0a04SGreg Roach     *
185a25f0a04SGreg Roach     * @return User|null
186a25f0a04SGreg Roach     */
187c1010edaSGreg Roach    public static function findByIdentifier($identifier)
188c1010edaSGreg Roach    {
18901461f86SGreg Roach        $user_id = (int) DB::table('user')
19001461f86SGreg Roach            ->where('user_name', '=', $identifier)
19101461f86SGreg Roach            ->orWhere('email', '=', $identifier)
19201461f86SGreg Roach            ->value('user_id');
193a25f0a04SGreg Roach
194a25f0a04SGreg Roach        return self::find($user_id);
195a25f0a04SGreg Roach    }
196a25f0a04SGreg Roach
197a25f0a04SGreg Roach    /**
198*bc09afc5SGreg Roach     * Find the user(s) with a specified genealogy record.
199a25f0a04SGreg Roach     *
200a25f0a04SGreg Roach     * @param Individual $individual
201a25f0a04SGreg Roach     *
202*bc09afc5SGreg Roach     * @return User[]
203a25f0a04SGreg Roach     */
204*bc09afc5SGreg Roach    public static function findByIndividual(Individual $individual): array
205c1010edaSGreg Roach    {
206*bc09afc5SGreg Roach        $user_ids = DB::table('user_gedcom_setting')
207*bc09afc5SGreg Roach            ->where('gedcom_id', '=', $individual->tree()->id())
208*bc09afc5SGreg Roach            ->where('setting_value', '=', $individual->xref())
209*bc09afc5SGreg Roach            ->where('setting_name', '=', 'gedcomid')
210*bc09afc5SGreg Roach            ->pluck('user_id');
211a25f0a04SGreg Roach
212*bc09afc5SGreg Roach        return $user_ids->map(function (string $user_id): User {
213*bc09afc5SGreg Roach            return self::find((int) $user_id);
214*bc09afc5SGreg Roach        })->all();
215a25f0a04SGreg Roach    }
216a25f0a04SGreg Roach
217a25f0a04SGreg Roach    /**
218f7fb7d41SGreg Roach     * Find the user with a specified user_name.
219f7fb7d41SGreg Roach     *
220f7fb7d41SGreg Roach     * @param string $user_name
221f7fb7d41SGreg Roach     *
222f7fb7d41SGreg Roach     * @return User|null
223f7fb7d41SGreg Roach     */
224c1010edaSGreg Roach    public static function findByUserName($user_name)
225c1010edaSGreg Roach    {
22601461f86SGreg Roach        $user_id = (int) DB::table('user')
22701461f86SGreg Roach            ->where('user_name', '=', $user_name)
22801461f86SGreg Roach            ->value('user_id');
229f7fb7d41SGreg Roach
230f7fb7d41SGreg Roach        return self::find($user_id);
231f7fb7d41SGreg Roach    }
232f7fb7d41SGreg Roach
233f7fb7d41SGreg Roach    /**
234a25f0a04SGreg Roach     * Get a list of all users.
235a25f0a04SGreg Roach     *
236a25f0a04SGreg Roach     * @return User[]
237a25f0a04SGreg Roach     */
238fa4036e8SGreg Roach    public static function all(): array
239c1010edaSGreg Roach    {
240a25f0a04SGreg Roach        $rows = Database::prepare(
241e5588fb0SGreg Roach            "SELECT user_id, user_name, real_name, email" .
242a25f0a04SGreg Roach            " FROM `##user`" .
243a25f0a04SGreg Roach            " WHERE user_id > 0" .
244f7fb7d41SGreg Roach            " ORDER BY real_name"
245a25f0a04SGreg Roach        )->fetchAll();
246a25f0a04SGreg Roach
24759f2f229SGreg Roach        return array_map(function (stdClass $row): User {
2488d68cabeSGreg Roach            return new static($row);
2498d68cabeSGreg Roach        }, $rows);
250a25f0a04SGreg Roach    }
251a25f0a04SGreg Roach
252a25f0a04SGreg Roach    /**
253a25f0a04SGreg Roach     * Get a list of all administrators.
254a25f0a04SGreg Roach     *
255a25f0a04SGreg Roach     * @return User[]
256a25f0a04SGreg Roach     */
257fa4036e8SGreg Roach    public static function administrators(): array
258c1010edaSGreg Roach    {
259a25f0a04SGreg Roach        $rows = Database::prepare(
260e5588fb0SGreg Roach            "SELECT user_id, user_name, real_name, email" .
261a25f0a04SGreg Roach            " FROM `##user`" .
262a25f0a04SGreg Roach            " JOIN `##user_setting` USING (user_id)" .
263f7fb7d41SGreg Roach            " WHERE user_id > 0 AND setting_name = 'canadmin' AND setting_value = '1'" .
264f7fb7d41SGreg Roach            " ORDER BY real_name"
265a25f0a04SGreg Roach        )->fetchAll();
266a25f0a04SGreg Roach
267c40ccc62SGreg Roach        return array_map(function (stdClass $row): User {
2688d68cabeSGreg Roach            return new static($row);
2698d68cabeSGreg Roach        }, $rows);
270a25f0a04SGreg Roach    }
271a25f0a04SGreg Roach
272fa4036e8SGreg Roach    /**
273fa4036e8SGreg Roach     * Validate a supplied password
274c1010edaSGreg Roach     *
275a25f0a04SGreg Roach     * @param string $password
276a25f0a04SGreg Roach     *
277cbc1590aSGreg Roach     * @return bool
278a25f0a04SGreg Roach     */
279fa4036e8SGreg Roach    public function checkPassword(string $password): bool
280c1010edaSGreg Roach    {
281a25f0a04SGreg Roach        $password_hash = Database::prepare(
282a25f0a04SGreg Roach            "SELECT password FROM `##user` WHERE user_id = ?"
28313abd6f3SGreg Roach        )->execute([$this->user_id])->fetchOne();
284a25f0a04SGreg Roach
285bbd8bd1bSGreg Roach        if ($password_hash !== null && password_verify($password, $password_hash)) {
286015c99a2SGreg Roach            if (password_needs_rehash($password_hash, PASSWORD_DEFAULT)) {
287a25f0a04SGreg Roach                $this->setPassword($password);
288a25f0a04SGreg Roach            }
289cbc1590aSGreg Roach
290a25f0a04SGreg Roach            return true;
291a25f0a04SGreg Roach        }
292b2ce94c6SRico Sonntag
293b2ce94c6SRico Sonntag        return false;
294a25f0a04SGreg Roach    }
295a25f0a04SGreg Roach
296a25f0a04SGreg Roach    /**
297f7fb7d41SGreg Roach     * Get a list of all managers.
298f7fb7d41SGreg Roach     *
299f7fb7d41SGreg Roach     * @return User[]
300f7fb7d41SGreg Roach     */
301fa4036e8SGreg Roach    public static function managers(): array
302c1010edaSGreg Roach    {
303f7fb7d41SGreg Roach        $rows = Database::prepare(
304e5588fb0SGreg Roach            "SELECT user_id, user_name, real_name, email" .
305f7fb7d41SGreg Roach            " FROM `##user` JOIN `##user_gedcom_setting` USING (user_id)" .
306f7fb7d41SGreg Roach            " WHERE setting_name = 'canedit' AND setting_value='admin'" .
307f7fb7d41SGreg Roach            " GROUP BY user_id, real_name" .
308f7fb7d41SGreg Roach            " ORDER BY real_name"
309f7fb7d41SGreg Roach        )->fetchAll();
310f7fb7d41SGreg Roach
31159f2f229SGreg Roach        return array_map(function (stdClass $row): User {
3128d68cabeSGreg Roach            return new static($row);
3138d68cabeSGreg Roach        }, $rows);
314f7fb7d41SGreg Roach    }
315f7fb7d41SGreg Roach
316f7fb7d41SGreg Roach    /**
317f7fb7d41SGreg Roach     * Get a list of all moderators.
318f7fb7d41SGreg Roach     *
319f7fb7d41SGreg Roach     * @return User[]
320f7fb7d41SGreg Roach     */
321fa4036e8SGreg Roach    public static function moderators(): array
322c1010edaSGreg Roach    {
323f7fb7d41SGreg Roach        $rows = Database::prepare(
324e5588fb0SGreg Roach            "SELECT user_id, user_name, real_name, email" .
325f7fb7d41SGreg Roach            " FROM `##user` JOIN `##user_gedcom_setting` USING (user_id)" .
326f7fb7d41SGreg Roach            " WHERE setting_name = 'canedit' AND setting_value='accept'" .
327f7fb7d41SGreg Roach            " GROUP BY user_id, real_name" .
328f7fb7d41SGreg Roach            " ORDER BY real_name"
329f7fb7d41SGreg Roach        )->fetchAll();
330f7fb7d41SGreg Roach
33159f2f229SGreg Roach        return array_map(function (stdClass $row): User {
3328d68cabeSGreg Roach            return new static($row);
3338d68cabeSGreg Roach        }, $rows);
334f7fb7d41SGreg Roach    }
335f7fb7d41SGreg Roach
336f7fb7d41SGreg Roach    /**
337f7fb7d41SGreg Roach     * Get a list of all verified users.
338f7fb7d41SGreg Roach     *
339f7fb7d41SGreg Roach     * @return User[]
340f7fb7d41SGreg Roach     */
341fa4036e8SGreg Roach    public static function unapproved(): array
342c1010edaSGreg Roach    {
343f7fb7d41SGreg Roach        $rows = Database::prepare(
344e5588fb0SGreg Roach            "SELECT user_id, user_name, real_name, email" .
345f7fb7d41SGreg Roach            " FROM `##user` JOIN `##user_setting` USING (user_id)" .
346f7fb7d41SGreg Roach            " WHERE setting_name = 'verified_by_admin' AND setting_value = '0'" .
347f7fb7d41SGreg Roach            " ORDER BY real_name"
348f7fb7d41SGreg Roach        )->fetchAll();
349f7fb7d41SGreg Roach
35059f2f229SGreg Roach        return array_map(function (stdClass $row): User {
3518d68cabeSGreg Roach            return new static($row);
3528d68cabeSGreg Roach        }, $rows);
353f7fb7d41SGreg Roach    }
354f7fb7d41SGreg Roach
355f7fb7d41SGreg Roach    /**
356f7fb7d41SGreg Roach     * Get a list of all verified users.
357f7fb7d41SGreg Roach     *
358f7fb7d41SGreg Roach     * @return User[]
359f7fb7d41SGreg Roach     */
360fa4036e8SGreg Roach    public static function unverified(): array
361c1010edaSGreg Roach    {
362f7fb7d41SGreg Roach        $rows = Database::prepare(
363e5588fb0SGreg Roach            "SELECT user_id, user_name, real_name, email" .
364f7fb7d41SGreg Roach            " FROM `##user` JOIN `##user_setting` USING (user_id)" .
365f7fb7d41SGreg Roach            " WHERE setting_name = 'verified' AND setting_value = '0'" .
366f7fb7d41SGreg Roach            " ORDER BY real_name"
367f7fb7d41SGreg Roach        )->fetchAll();
368f7fb7d41SGreg Roach
36959f2f229SGreg Roach        return array_map(function (stdClass $row): User {
3708d68cabeSGreg Roach            return new static($row);
3718d68cabeSGreg Roach        }, $rows);
372f7fb7d41SGreg Roach    }
373f7fb7d41SGreg Roach
374f7fb7d41SGreg Roach    /**
375f7fb7d41SGreg Roach     * Get a list of all users who are currently logged in.
376f7fb7d41SGreg Roach     *
377f7fb7d41SGreg Roach     * @return User[]
378f7fb7d41SGreg Roach     */
379fa4036e8SGreg Roach    public static function allLoggedIn(): array
380c1010edaSGreg Roach    {
381f7fb7d41SGreg Roach        $rows = Database::prepare(
382e5588fb0SGreg Roach            "SELECT DISTINCT user_id, user_name, real_name, email" .
383f7fb7d41SGreg Roach            " FROM `##user`" .
384f7fb7d41SGreg Roach            " JOIN `##session` USING (user_id)"
385f7fb7d41SGreg Roach        )->fetchAll();
386f7fb7d41SGreg Roach
38759f2f229SGreg Roach        return array_map(function (stdClass $row): User {
3888d68cabeSGreg Roach            return new static($row);
3898d68cabeSGreg Roach        }, $rows);
390f7fb7d41SGreg Roach    }
391f7fb7d41SGreg Roach
392f7fb7d41SGreg Roach    /**
393a25f0a04SGreg Roach     * Get the numeric ID for this user.
394a25f0a04SGreg Roach     *
395c04dd3c1SGreg Roach     * @return int
396a25f0a04SGreg Roach     */
397c1010edaSGreg Roach    public function getUserId(): int
398c1010edaSGreg Roach    {
399a25f0a04SGreg Roach        return $this->user_id;
400a25f0a04SGreg Roach    }
401a25f0a04SGreg Roach
402a25f0a04SGreg Roach    /**
403a25f0a04SGreg Roach     * Get the login name for this user.
404a25f0a04SGreg Roach     *
405a25f0a04SGreg Roach     * @return string
406a25f0a04SGreg Roach     */
4078f53f488SRico Sonntag    public function getUserName(): string
408c1010edaSGreg Roach    {
409a25f0a04SGreg Roach        return $this->user_name;
410a25f0a04SGreg Roach    }
411a25f0a04SGreg Roach
412a25f0a04SGreg Roach    /**
413a25f0a04SGreg Roach     * Set the login name for this user.
414a25f0a04SGreg Roach     *
415a25f0a04SGreg Roach     * @param string $user_name
416a25f0a04SGreg Roach     *
417a25f0a04SGreg Roach     * @return $this
418a25f0a04SGreg Roach     */
4198f53f488SRico Sonntag    public function setUserName($user_name): self
420c1010edaSGreg Roach    {
421a25f0a04SGreg Roach        if ($this->user_name !== $user_name) {
422a25f0a04SGreg Roach            $this->user_name = $user_name;
42301461f86SGreg Roach
42401461f86SGreg Roach            DB::table('user')
42501461f86SGreg Roach                ->where('user_id', '=', $this->user_id)
42601461f86SGreg Roach                ->update([
42701461f86SGreg Roach                    'user_name' => $user_name,
428c1010edaSGreg Roach                ]);
429a25f0a04SGreg Roach        }
430a25f0a04SGreg Roach
431a25f0a04SGreg Roach        return $this;
432a25f0a04SGreg Roach    }
433a25f0a04SGreg Roach
434a25f0a04SGreg Roach    /**
435a25f0a04SGreg Roach     * Get the real name of this user.
436a25f0a04SGreg Roach     *
437a25f0a04SGreg Roach     * @return string
438a25f0a04SGreg Roach     */
4398f53f488SRico Sonntag    public function getRealName(): string
440c1010edaSGreg Roach    {
441a25f0a04SGreg Roach        return $this->real_name;
442a25f0a04SGreg Roach    }
443a25f0a04SGreg Roach
444a25f0a04SGreg Roach    /**
445a25f0a04SGreg Roach     * Set the real name of this user.
446a25f0a04SGreg Roach     *
447a25f0a04SGreg Roach     * @param string $real_name
448a25f0a04SGreg Roach     *
449a25f0a04SGreg Roach     * @return User
450a25f0a04SGreg Roach     */
4518f53f488SRico Sonntag    public function setRealName($real_name): User
452c1010edaSGreg Roach    {
453a25f0a04SGreg Roach        if ($this->real_name !== $real_name) {
454a25f0a04SGreg Roach            $this->real_name = $real_name;
45501461f86SGreg Roach
45601461f86SGreg Roach            DB::table('user')
45701461f86SGreg Roach                ->where('user_id', '=', $this->user_id)
45801461f86SGreg Roach                ->update([
45901461f86SGreg Roach                    'real_name' => $real_name,
460c1010edaSGreg Roach                ]);
461a25f0a04SGreg Roach        }
462a25f0a04SGreg Roach
463a25f0a04SGreg Roach        return $this;
464a25f0a04SGreg Roach    }
465a25f0a04SGreg Roach
466a25f0a04SGreg Roach    /**
467a25f0a04SGreg Roach     * Get the email address of this user.
468a25f0a04SGreg Roach     *
469a25f0a04SGreg Roach     * @return string
470a25f0a04SGreg Roach     */
4718f53f488SRico Sonntag    public function getEmail(): string
472c1010edaSGreg Roach    {
473a25f0a04SGreg Roach        return $this->email;
474a25f0a04SGreg Roach    }
475a25f0a04SGreg Roach
476a25f0a04SGreg Roach    /**
477a25f0a04SGreg Roach     * Set the email address of this user.
478a25f0a04SGreg Roach     *
479a25f0a04SGreg Roach     * @param string $email
480a25f0a04SGreg Roach     *
481a25f0a04SGreg Roach     * @return User
482a25f0a04SGreg Roach     */
4838f53f488SRico Sonntag    public function setEmail($email): User
484c1010edaSGreg Roach    {
485a25f0a04SGreg Roach        if ($this->email !== $email) {
486a25f0a04SGreg Roach            $this->email = $email;
48701461f86SGreg Roach
48801461f86SGreg Roach            DB::table('user')
48901461f86SGreg Roach                ->where('user_id', '=', $this->user_id)
49001461f86SGreg Roach                ->update([
49101461f86SGreg Roach                    'email' => $email,
492c1010edaSGreg Roach                ]);
493a25f0a04SGreg Roach        }
494a25f0a04SGreg Roach
495a25f0a04SGreg Roach        return $this;
496a25f0a04SGreg Roach    }
497a25f0a04SGreg Roach
498a25f0a04SGreg Roach    /**
499a25f0a04SGreg Roach     * Set the password of this user.
500a25f0a04SGreg Roach     *
501a25f0a04SGreg Roach     * @param string $password
502a25f0a04SGreg Roach     *
503a25f0a04SGreg Roach     * @return User
504a25f0a04SGreg Roach     */
5058f53f488SRico Sonntag    public function setPassword($password): User
506c1010edaSGreg Roach    {
50701461f86SGreg Roach        DB::table('user')
50801461f86SGreg Roach            ->where('user_id', '=', $this->user_id)
50901461f86SGreg Roach            ->update([
510015c99a2SGreg Roach                'password' => password_hash($password, PASSWORD_DEFAULT),
511015c99a2SGreg Roach            ]);
512a25f0a04SGreg Roach
513a25f0a04SGreg Roach        return $this;
514a25f0a04SGreg Roach    }
515a25f0a04SGreg Roach
516a25f0a04SGreg Roach    /**
517a25f0a04SGreg Roach     * Fetch a user option/setting from the wt_user_setting table.
518a25f0a04SGreg Roach     * Since we'll fetch several settings for each user, and since there aren’t
519a25f0a04SGreg Roach     * that many of them, fetch them all in one database query
520a25f0a04SGreg Roach     *
521a25f0a04SGreg Roach     * @param string $setting_name
52215d603e7SGreg Roach     * @param string $default
523a25f0a04SGreg Roach     *
52415d603e7SGreg Roach     * @return string
525a25f0a04SGreg Roach     */
5268f53f488SRico Sonntag    public function getPreference($setting_name, $default = ''): string
527c1010edaSGreg Roach    {
528c04dd3c1SGreg Roach        if (empty($this->preferences) && $this->user_id !== 0) {
52901461f86SGreg Roach            $this->preferences = DB::table('user_setting')
53001461f86SGreg Roach                ->where('user_id', '=', $this->user_id)
53101461f86SGreg Roach                ->pluck('setting_value', 'setting_name')
53201461f86SGreg Roach                ->all();
533a25f0a04SGreg Roach        }
534a25f0a04SGreg Roach
53501461f86SGreg Roach        return $this->preferences[$setting_name] ?? $default;
536a25f0a04SGreg Roach    }
537a25f0a04SGreg Roach
538a25f0a04SGreg Roach    /**
539a25f0a04SGreg Roach     * Update a setting for the user.
540a25f0a04SGreg Roach     *
541a25f0a04SGreg Roach     * @param string $setting_name
542a25f0a04SGreg Roach     * @param string $setting_value
543a25f0a04SGreg Roach     *
544a25f0a04SGreg Roach     * @return User
545a25f0a04SGreg Roach     */
5468f53f488SRico Sonntag    public function setPreference($setting_name, $setting_value): User
547c1010edaSGreg Roach    {
548c04dd3c1SGreg Roach        if ($this->user_id !== 0 && $this->getPreference($setting_name) !== $setting_value) {
54901461f86SGreg Roach            DB::table('user_setting')->updateOrInsert([
55001461f86SGreg Roach                'user_id'      => $this->user_id,
55101461f86SGreg Roach                'setting_name' => $setting_name,
55201461f86SGreg Roach            ], [
55301461f86SGreg Roach                'setting_value' => $setting_value,
554c1010edaSGreg Roach            ]);
55515d603e7SGreg Roach
556a25f0a04SGreg Roach            $this->preferences[$setting_name] = $setting_value;
557a25f0a04SGreg Roach        }
558a25f0a04SGreg Roach
559a25f0a04SGreg Roach        return $this;
560a25f0a04SGreg Roach    }
561a25f0a04SGreg Roach}
562