1a25f0a04SGreg Roach<?php 2a25f0a04SGreg Roach/** 3a25f0a04SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 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 { 104*37b0f34fSGreg Roach // Don't delete the logs, just set the user to null. 105*37b0f34fSGreg Roach DB::table('log') 106*37b0f34fSGreg Roach ->where('user_id', '=', $this->user_id) 107*37b0f34fSGreg Roach ->update(['user_id' => null]); 108*37b0f34fSGreg Roach 109f7fb7d41SGreg Roach // Take over the user’s pending changes. (What else could we do with them?) 110*37b0f34fSGreg Roach DB::table('change') 111*37b0f34fSGreg Roach ->where('user_id', '=', $this->user_id) 112*37b0f34fSGreg Roach ->where('status', '=', 'rejected') 113*37b0f34fSGreg Roach ->delete(); 114*37b0f34fSGreg Roach 115*37b0f34fSGreg Roach DB::table('change') 116*37b0f34fSGreg Roach ->where('user_id', '=', $this->user_id) 117*37b0f34fSGreg Roach ->update(['user_id' => Auth::id()]); 118*37b0f34fSGreg Roach 119*37b0f34fSGreg Roach // Take over the user's contact details 120*37b0f34fSGreg Roach DB::table('gedcom_setting') 121*37b0f34fSGreg Roach ->where('setting_value', '=', $this->user_id) 122*37b0f34fSGreg Roach ->whereIn('setting_name', ['CONTACT_USER_ID', 'WEBMASTER_USER_ID']) 123*37b0f34fSGreg Roach ->update(['setting_value' => Auth::id()]); 124*37b0f34fSGreg Roach 125*37b0f34fSGreg Roach // Delete settings and preferences 126*37b0f34fSGreg Roach DB::table('block_setting') 127*37b0f34fSGreg Roach ->join('block', 'block_setting.block_id', '=', 'block.block_id') 128*37b0f34fSGreg Roach ->where('user_id', '=', $this->user_id) 129*37b0f34fSGreg Roach ->delete(); 130*37b0f34fSGreg Roach 131*37b0f34fSGreg Roach DB::table('block')->where('user_id', '=', $this->user_id)->delete(); 132*37b0f34fSGreg Roach DB::table('user_gedcom_setting')->where('user_id', '=', $this->user_id)->delete(); 133*37b0f34fSGreg Roach DB::table('user_setting')->where('user_id', '=', $this->user_id)->delete(); 134*37b0f34fSGreg Roach DB::table('message')->where('user_id', '=', $this->user_id)->delete(); 135*37b0f34fSGreg Roach DB::table('user')->where('user_id', '=', $this->user_id)->delete(); 136*37b0f34fSGreg Roach 137*37b0f34fSGreg 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 /** 198a25f0a04SGreg Roach * Find the user with a specified genealogy record. 199a25f0a04SGreg Roach * 200a25f0a04SGreg Roach * @param Individual $individual 201a25f0a04SGreg Roach * 202a25f0a04SGreg Roach * @return User|null 203a25f0a04SGreg Roach */ 204c1010edaSGreg Roach public static function findByIndividual(Individual $individual) 205c1010edaSGreg Roach { 206fa4036e8SGreg Roach $user_id = (int) Database::prepare( 207e5588fb0SGreg Roach "SELECT user_id" . 208a25f0a04SGreg Roach " FROM `##user_gedcom_setting`" . 209a5adda01SGreg Roach " WHERE gedcom_id = :tree_id AND setting_name = 'gedcomid' AND setting_value = :xref" 21013abd6f3SGreg Roach )->execute([ 211f4afa648SGreg Roach 'tree_id' => $individual->tree()->id(), 212c0935879SGreg Roach 'xref' => $individual->xref(), 21313abd6f3SGreg Roach ])->fetchOne(); 214a25f0a04SGreg Roach 215a25f0a04SGreg Roach return self::find($user_id); 216a25f0a04SGreg Roach } 217a25f0a04SGreg Roach 218a25f0a04SGreg Roach /** 219f7fb7d41SGreg Roach * Find the user with a specified user_name. 220f7fb7d41SGreg Roach * 221f7fb7d41SGreg Roach * @param string $user_name 222f7fb7d41SGreg Roach * 223f7fb7d41SGreg Roach * @return User|null 224f7fb7d41SGreg Roach */ 225c1010edaSGreg Roach public static function findByUserName($user_name) 226c1010edaSGreg Roach { 22701461f86SGreg Roach $user_id = (int) DB::table('user') 22801461f86SGreg Roach ->where('user_name', '=', $user_name) 22901461f86SGreg Roach ->value('user_id'); 230f7fb7d41SGreg Roach 231f7fb7d41SGreg Roach return self::find($user_id); 232f7fb7d41SGreg Roach } 233f7fb7d41SGreg Roach 234f7fb7d41SGreg Roach /** 235a25f0a04SGreg Roach * Get a list of all users. 236a25f0a04SGreg Roach * 237a25f0a04SGreg Roach * @return User[] 238a25f0a04SGreg Roach */ 239fa4036e8SGreg Roach public static function all(): array 240c1010edaSGreg Roach { 241a25f0a04SGreg Roach $rows = Database::prepare( 242e5588fb0SGreg Roach "SELECT user_id, user_name, real_name, email" . 243a25f0a04SGreg Roach " FROM `##user`" . 244a25f0a04SGreg Roach " WHERE user_id > 0" . 245f7fb7d41SGreg Roach " ORDER BY real_name" 246a25f0a04SGreg Roach )->fetchAll(); 247a25f0a04SGreg Roach 24859f2f229SGreg Roach return array_map(function (stdClass $row): User { 2498d68cabeSGreg Roach return new static($row); 2508d68cabeSGreg Roach }, $rows); 251a25f0a04SGreg Roach } 252a25f0a04SGreg Roach 253a25f0a04SGreg Roach /** 254a25f0a04SGreg Roach * Get a list of all administrators. 255a25f0a04SGreg Roach * 256a25f0a04SGreg Roach * @return User[] 257a25f0a04SGreg Roach */ 258fa4036e8SGreg Roach public static function administrators(): array 259c1010edaSGreg Roach { 260a25f0a04SGreg Roach $rows = Database::prepare( 261e5588fb0SGreg Roach "SELECT user_id, user_name, real_name, email" . 262a25f0a04SGreg Roach " FROM `##user`" . 263a25f0a04SGreg Roach " JOIN `##user_setting` USING (user_id)" . 264f7fb7d41SGreg Roach " WHERE user_id > 0 AND setting_name = 'canadmin' AND setting_value = '1'" . 265f7fb7d41SGreg Roach " ORDER BY real_name" 266a25f0a04SGreg Roach )->fetchAll(); 267a25f0a04SGreg Roach 268c40ccc62SGreg Roach return array_map(function (stdClass $row): User { 2698d68cabeSGreg Roach return new static($row); 2708d68cabeSGreg Roach }, $rows); 271a25f0a04SGreg Roach } 272a25f0a04SGreg Roach 273fa4036e8SGreg Roach /** 274fa4036e8SGreg Roach * Validate a supplied password 275c1010edaSGreg Roach * 276a25f0a04SGreg Roach * @param string $password 277a25f0a04SGreg Roach * 278cbc1590aSGreg Roach * @return bool 279a25f0a04SGreg Roach */ 280fa4036e8SGreg Roach public function checkPassword(string $password): bool 281c1010edaSGreg Roach { 282a25f0a04SGreg Roach $password_hash = Database::prepare( 283a25f0a04SGreg Roach "SELECT password FROM `##user` WHERE user_id = ?" 28413abd6f3SGreg Roach )->execute([$this->user_id])->fetchOne(); 285a25f0a04SGreg Roach 286bbd8bd1bSGreg Roach if ($password_hash !== null && password_verify($password, $password_hash)) { 287015c99a2SGreg Roach if (password_needs_rehash($password_hash, PASSWORD_DEFAULT)) { 288a25f0a04SGreg Roach $this->setPassword($password); 289a25f0a04SGreg Roach } 290cbc1590aSGreg Roach 291a25f0a04SGreg Roach return true; 292a25f0a04SGreg Roach } 293b2ce94c6SRico Sonntag 294b2ce94c6SRico Sonntag return false; 295a25f0a04SGreg Roach } 296a25f0a04SGreg Roach 297a25f0a04SGreg Roach /** 298f7fb7d41SGreg Roach * Get a list of all managers. 299f7fb7d41SGreg Roach * 300f7fb7d41SGreg Roach * @return User[] 301f7fb7d41SGreg Roach */ 302fa4036e8SGreg Roach public static function managers(): array 303c1010edaSGreg Roach { 304f7fb7d41SGreg Roach $rows = Database::prepare( 305e5588fb0SGreg Roach "SELECT user_id, user_name, real_name, email" . 306f7fb7d41SGreg Roach " FROM `##user` JOIN `##user_gedcom_setting` USING (user_id)" . 307f7fb7d41SGreg Roach " WHERE setting_name = 'canedit' AND setting_value='admin'" . 308f7fb7d41SGreg Roach " GROUP BY user_id, real_name" . 309f7fb7d41SGreg Roach " ORDER BY real_name" 310f7fb7d41SGreg Roach )->fetchAll(); 311f7fb7d41SGreg Roach 31259f2f229SGreg Roach return array_map(function (stdClass $row): User { 3138d68cabeSGreg Roach return new static($row); 3148d68cabeSGreg Roach }, $rows); 315f7fb7d41SGreg Roach } 316f7fb7d41SGreg Roach 317f7fb7d41SGreg Roach /** 318f7fb7d41SGreg Roach * Get a list of all moderators. 319f7fb7d41SGreg Roach * 320f7fb7d41SGreg Roach * @return User[] 321f7fb7d41SGreg Roach */ 322fa4036e8SGreg Roach public static function moderators(): array 323c1010edaSGreg Roach { 324f7fb7d41SGreg Roach $rows = Database::prepare( 325e5588fb0SGreg Roach "SELECT user_id, user_name, real_name, email" . 326f7fb7d41SGreg Roach " FROM `##user` JOIN `##user_gedcom_setting` USING (user_id)" . 327f7fb7d41SGreg Roach " WHERE setting_name = 'canedit' AND setting_value='accept'" . 328f7fb7d41SGreg Roach " GROUP BY user_id, real_name" . 329f7fb7d41SGreg Roach " ORDER BY real_name" 330f7fb7d41SGreg Roach )->fetchAll(); 331f7fb7d41SGreg Roach 33259f2f229SGreg Roach return array_map(function (stdClass $row): User { 3338d68cabeSGreg Roach return new static($row); 3348d68cabeSGreg Roach }, $rows); 335f7fb7d41SGreg Roach } 336f7fb7d41SGreg Roach 337f7fb7d41SGreg Roach /** 338f7fb7d41SGreg Roach * Get a list of all verified users. 339f7fb7d41SGreg Roach * 340f7fb7d41SGreg Roach * @return User[] 341f7fb7d41SGreg Roach */ 342fa4036e8SGreg Roach public static function unapproved(): array 343c1010edaSGreg Roach { 344f7fb7d41SGreg Roach $rows = Database::prepare( 345e5588fb0SGreg Roach "SELECT user_id, user_name, real_name, email" . 346f7fb7d41SGreg Roach " FROM `##user` JOIN `##user_setting` USING (user_id)" . 347f7fb7d41SGreg Roach " WHERE setting_name = 'verified_by_admin' AND setting_value = '0'" . 348f7fb7d41SGreg Roach " ORDER BY real_name" 349f7fb7d41SGreg Roach )->fetchAll(); 350f7fb7d41SGreg Roach 35159f2f229SGreg Roach return array_map(function (stdClass $row): User { 3528d68cabeSGreg Roach return new static($row); 3538d68cabeSGreg Roach }, $rows); 354f7fb7d41SGreg Roach } 355f7fb7d41SGreg Roach 356f7fb7d41SGreg Roach /** 357f7fb7d41SGreg Roach * Get a list of all verified users. 358f7fb7d41SGreg Roach * 359f7fb7d41SGreg Roach * @return User[] 360f7fb7d41SGreg Roach */ 361fa4036e8SGreg Roach public static function unverified(): array 362c1010edaSGreg Roach { 363f7fb7d41SGreg Roach $rows = Database::prepare( 364e5588fb0SGreg Roach "SELECT user_id, user_name, real_name, email" . 365f7fb7d41SGreg Roach " FROM `##user` JOIN `##user_setting` USING (user_id)" . 366f7fb7d41SGreg Roach " WHERE setting_name = 'verified' AND setting_value = '0'" . 367f7fb7d41SGreg Roach " ORDER BY real_name" 368f7fb7d41SGreg Roach )->fetchAll(); 369f7fb7d41SGreg Roach 37059f2f229SGreg Roach return array_map(function (stdClass $row): User { 3718d68cabeSGreg Roach return new static($row); 3728d68cabeSGreg Roach }, $rows); 373f7fb7d41SGreg Roach } 374f7fb7d41SGreg Roach 375f7fb7d41SGreg Roach /** 376f7fb7d41SGreg Roach * Get a list of all users who are currently logged in. 377f7fb7d41SGreg Roach * 378f7fb7d41SGreg Roach * @return User[] 379f7fb7d41SGreg Roach */ 380fa4036e8SGreg Roach public static function allLoggedIn(): array 381c1010edaSGreg Roach { 382f7fb7d41SGreg Roach $rows = Database::prepare( 383e5588fb0SGreg Roach "SELECT DISTINCT user_id, user_name, real_name, email" . 384f7fb7d41SGreg Roach " FROM `##user`" . 385f7fb7d41SGreg Roach " JOIN `##session` USING (user_id)" 386f7fb7d41SGreg Roach )->fetchAll(); 387f7fb7d41SGreg Roach 38859f2f229SGreg Roach return array_map(function (stdClass $row): User { 3898d68cabeSGreg Roach return new static($row); 3908d68cabeSGreg Roach }, $rows); 391f7fb7d41SGreg Roach } 392f7fb7d41SGreg Roach 393f7fb7d41SGreg Roach /** 394a25f0a04SGreg Roach * Get the numeric ID for this user. 395a25f0a04SGreg Roach * 396c04dd3c1SGreg Roach * @return int 397a25f0a04SGreg Roach */ 398c1010edaSGreg Roach public function getUserId(): int 399c1010edaSGreg Roach { 400a25f0a04SGreg Roach return $this->user_id; 401a25f0a04SGreg Roach } 402a25f0a04SGreg Roach 403a25f0a04SGreg Roach /** 404a25f0a04SGreg Roach * Get the login name for this user. 405a25f0a04SGreg Roach * 406a25f0a04SGreg Roach * @return string 407a25f0a04SGreg Roach */ 4088f53f488SRico Sonntag public function getUserName(): string 409c1010edaSGreg Roach { 410a25f0a04SGreg Roach return $this->user_name; 411a25f0a04SGreg Roach } 412a25f0a04SGreg Roach 413a25f0a04SGreg Roach /** 414a25f0a04SGreg Roach * Set the login name for this user. 415a25f0a04SGreg Roach * 416a25f0a04SGreg Roach * @param string $user_name 417a25f0a04SGreg Roach * 418a25f0a04SGreg Roach * @return $this 419a25f0a04SGreg Roach */ 4208f53f488SRico Sonntag public function setUserName($user_name): self 421c1010edaSGreg Roach { 422a25f0a04SGreg Roach if ($this->user_name !== $user_name) { 423a25f0a04SGreg Roach $this->user_name = $user_name; 42401461f86SGreg Roach 42501461f86SGreg Roach DB::table('user') 42601461f86SGreg Roach ->where('user_id', '=', $this->user_id) 42701461f86SGreg Roach ->update([ 42801461f86SGreg Roach 'user_name' => $user_name, 429c1010edaSGreg Roach ]); 430a25f0a04SGreg Roach } 431a25f0a04SGreg Roach 432a25f0a04SGreg Roach return $this; 433a25f0a04SGreg Roach } 434a25f0a04SGreg Roach 435a25f0a04SGreg Roach /** 436a25f0a04SGreg Roach * Get the real name of this user. 437a25f0a04SGreg Roach * 438a25f0a04SGreg Roach * @return string 439a25f0a04SGreg Roach */ 4408f53f488SRico Sonntag public function getRealName(): string 441c1010edaSGreg Roach { 442a25f0a04SGreg Roach return $this->real_name; 443a25f0a04SGreg Roach } 444a25f0a04SGreg Roach 445a25f0a04SGreg Roach /** 446a25f0a04SGreg Roach * Set the real name of this user. 447a25f0a04SGreg Roach * 448a25f0a04SGreg Roach * @param string $real_name 449a25f0a04SGreg Roach * 450a25f0a04SGreg Roach * @return User 451a25f0a04SGreg Roach */ 4528f53f488SRico Sonntag public function setRealName($real_name): User 453c1010edaSGreg Roach { 454a25f0a04SGreg Roach if ($this->real_name !== $real_name) { 455a25f0a04SGreg Roach $this->real_name = $real_name; 45601461f86SGreg Roach 45701461f86SGreg Roach DB::table('user') 45801461f86SGreg Roach ->where('user_id', '=', $this->user_id) 45901461f86SGreg Roach ->update([ 46001461f86SGreg Roach 'real_name' => $real_name, 461c1010edaSGreg Roach ]); 462a25f0a04SGreg Roach } 463a25f0a04SGreg Roach 464a25f0a04SGreg Roach return $this; 465a25f0a04SGreg Roach } 466a25f0a04SGreg Roach 467a25f0a04SGreg Roach /** 468a25f0a04SGreg Roach * Get the email address of this user. 469a25f0a04SGreg Roach * 470a25f0a04SGreg Roach * @return string 471a25f0a04SGreg Roach */ 4728f53f488SRico Sonntag public function getEmail(): string 473c1010edaSGreg Roach { 474a25f0a04SGreg Roach return $this->email; 475a25f0a04SGreg Roach } 476a25f0a04SGreg Roach 477a25f0a04SGreg Roach /** 478a25f0a04SGreg Roach * Set the email address of this user. 479a25f0a04SGreg Roach * 480a25f0a04SGreg Roach * @param string $email 481a25f0a04SGreg Roach * 482a25f0a04SGreg Roach * @return User 483a25f0a04SGreg Roach */ 4848f53f488SRico Sonntag public function setEmail($email): User 485c1010edaSGreg Roach { 486a25f0a04SGreg Roach if ($this->email !== $email) { 487a25f0a04SGreg Roach $this->email = $email; 48801461f86SGreg Roach 48901461f86SGreg Roach DB::table('user') 49001461f86SGreg Roach ->where('user_id', '=', $this->user_id) 49101461f86SGreg Roach ->update([ 49201461f86SGreg Roach 'email' => $email, 493c1010edaSGreg Roach ]); 494a25f0a04SGreg Roach } 495a25f0a04SGreg Roach 496a25f0a04SGreg Roach return $this; 497a25f0a04SGreg Roach } 498a25f0a04SGreg Roach 499a25f0a04SGreg Roach /** 500a25f0a04SGreg Roach * Set the password of this user. 501a25f0a04SGreg Roach * 502a25f0a04SGreg Roach * @param string $password 503a25f0a04SGreg Roach * 504a25f0a04SGreg Roach * @return User 505a25f0a04SGreg Roach */ 5068f53f488SRico Sonntag public function setPassword($password): User 507c1010edaSGreg Roach { 50801461f86SGreg Roach DB::table('user') 50901461f86SGreg Roach ->where('user_id', '=', $this->user_id) 51001461f86SGreg Roach ->update([ 511015c99a2SGreg Roach 'password' => password_hash($password, PASSWORD_DEFAULT), 512015c99a2SGreg Roach ]); 513a25f0a04SGreg Roach 514a25f0a04SGreg Roach return $this; 515a25f0a04SGreg Roach } 516a25f0a04SGreg Roach 517a25f0a04SGreg Roach /** 518a25f0a04SGreg Roach * Fetch a user option/setting from the wt_user_setting table. 519a25f0a04SGreg Roach * Since we'll fetch several settings for each user, and since there aren’t 520a25f0a04SGreg Roach * that many of them, fetch them all in one database query 521a25f0a04SGreg Roach * 522a25f0a04SGreg Roach * @param string $setting_name 52315d603e7SGreg Roach * @param string $default 524a25f0a04SGreg Roach * 52515d603e7SGreg Roach * @return string 526a25f0a04SGreg Roach */ 5278f53f488SRico Sonntag public function getPreference($setting_name, $default = ''): string 528c1010edaSGreg Roach { 529c04dd3c1SGreg Roach if (empty($this->preferences) && $this->user_id !== 0) { 53001461f86SGreg Roach $this->preferences = DB::table('user_setting') 53101461f86SGreg Roach ->where('user_id', '=', $this->user_id) 53201461f86SGreg Roach ->pluck('setting_value', 'setting_name') 53301461f86SGreg Roach ->all(); 534a25f0a04SGreg Roach } 535a25f0a04SGreg Roach 53601461f86SGreg Roach return $this->preferences[$setting_name] ?? $default; 537a25f0a04SGreg Roach } 538a25f0a04SGreg Roach 539a25f0a04SGreg Roach /** 540a25f0a04SGreg Roach * Update a setting for the user. 541a25f0a04SGreg Roach * 542a25f0a04SGreg Roach * @param string $setting_name 543a25f0a04SGreg Roach * @param string $setting_value 544a25f0a04SGreg Roach * 545a25f0a04SGreg Roach * @return User 546a25f0a04SGreg Roach */ 5478f53f488SRico Sonntag public function setPreference($setting_name, $setting_value): User 548c1010edaSGreg Roach { 549c04dd3c1SGreg Roach if ($this->user_id !== 0 && $this->getPreference($setting_name) !== $setting_value) { 55001461f86SGreg Roach DB::table('user_setting')->updateOrInsert([ 55101461f86SGreg Roach 'user_id' => $this->user_id, 55201461f86SGreg Roach 'setting_name' => $setting_name, 55301461f86SGreg Roach ], [ 55401461f86SGreg Roach 'setting_value' => $setting_value, 555c1010edaSGreg Roach ]); 55615d603e7SGreg Roach 557a25f0a04SGreg Roach $this->preferences[$setting_name] = $setting_value; 558a25f0a04SGreg Roach } 559a25f0a04SGreg Roach 560a25f0a04SGreg Roach return $this; 561a25f0a04SGreg Roach } 562a25f0a04SGreg Roach} 563