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 20*01461f86SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 21*01461f86SGreg 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. */ 4513abd6f3SGreg Roach private 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 { 74*01461f86SGreg 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); 83*01461f86SGreg Roach 84*01461f86SGreg Roach (new Builder(DB::connection()))->from('block')->insertUsing( 85*01461f86SGreg Roach ['user_id', 'location', 'block_order', 'module_name'], 86*01461f86SGreg Roach function (Builder $query) use ($user): void { 87*01461f86SGreg Roach $query 88*01461f86SGreg Roach ->select([DB::raw($user->getuserId()), 'location', 'block_order', 'module_name']) 89*01461f86SGreg Roach ->from('block') 90*01461f86SGreg Roach ->where('user_id', '=', -1); 91*01461f86SGreg Roach } 92*01461f86SGreg 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 { 104f7fb7d41SGreg Roach // Don't delete the logs. 105f7fb7d41SGreg Roach Database::prepare("UPDATE `##log` SET user_id=NULL WHERE user_id =?")->execute([$this->user_id]); 106f7fb7d41SGreg Roach // Take over the user’s pending changes. (What else could we do with them?) 107f7fb7d41SGreg Roach Database::prepare("DELETE FROM `##change` WHERE user_id=? AND status='rejected'")->execute([$this->user_id]); 108c1010edaSGreg Roach Database::prepare("UPDATE `##change` SET user_id=? WHERE user_id=?")->execute([ 109c1010edaSGreg Roach Auth::id(), 110c1010edaSGreg Roach $this->user_id, 111c1010edaSGreg Roach ]); 112f7fb7d41SGreg Roach Database::prepare("DELETE `##block_setting` FROM `##block_setting` JOIN `##block` USING (block_id) WHERE user_id=?")->execute([$this->user_id]); 113f7fb7d41SGreg Roach Database::prepare("DELETE FROM `##block` WHERE user_id=?")->execute([$this->user_id]); 114f7fb7d41SGreg Roach Database::prepare("DELETE FROM `##user_gedcom_setting` WHERE user_id=?")->execute([$this->user_id]); 1157ff5d33bSGreg Roach Database::prepare("DELETE FROM `##gedcom_setting` WHERE setting_value=? AND setting_name IN ('CONTACT_USER_ID', 'WEBMASTER_USER_ID')")->execute([(string) $this->user_id]); 116f7fb7d41SGreg Roach Database::prepare("DELETE FROM `##user_setting` WHERE user_id=?")->execute([$this->user_id]); 117f7fb7d41SGreg Roach Database::prepare("DELETE FROM `##message` WHERE user_id=?")->execute([$this->user_id]); 118f7fb7d41SGreg Roach Database::prepare("DELETE FROM `##user` WHERE user_id=?")->execute([$this->user_id]); 119f7fb7d41SGreg Roach } 120f7fb7d41SGreg Roach 121f7fb7d41SGreg Roach /** 122a25f0a04SGreg Roach * Find the user with a specified user_id. 123a25f0a04SGreg Roach * 124cbc1590aSGreg Roach * @param int|null $user_id 125a25f0a04SGreg Roach * 126a25f0a04SGreg Roach * @return User|null 127a25f0a04SGreg Roach */ 128c1010edaSGreg Roach public static function find($user_id) 129c1010edaSGreg Roach { 130a25f0a04SGreg Roach if (!array_key_exists($user_id, self::$cache)) { 131*01461f86SGreg Roach $row = DB::table('user') 132*01461f86SGreg Roach ->where('user_id', '=', $user_id) 133*01461f86SGreg Roach ->first(); 134*01461f86SGreg Roach 135a25f0a04SGreg Roach if ($row) { 13606ef8e02SGreg Roach self::$cache[$user_id] = new self($row); 137a25f0a04SGreg Roach } else { 138a25f0a04SGreg Roach self::$cache[$user_id] = null; 139a25f0a04SGreg Roach } 140a25f0a04SGreg Roach } 141a25f0a04SGreg Roach 142a25f0a04SGreg Roach return self::$cache[$user_id]; 143a25f0a04SGreg Roach } 144a25f0a04SGreg Roach 145a25f0a04SGreg Roach /** 146a982a56eSGreg Roach * Find the user with a specified email address. 147a982a56eSGreg Roach * 148a982a56eSGreg Roach * @param string $email 149a982a56eSGreg Roach * 150a982a56eSGreg Roach * @return User|null 151a982a56eSGreg Roach */ 152c1010edaSGreg Roach public static function findByEmail($email) 153c1010edaSGreg Roach { 154*01461f86SGreg Roach $user_id = (int) DB::table('user') 155*01461f86SGreg Roach ->where('email', '=', $email) 156*01461f86SGreg Roach ->value('user_id'); 157a982a56eSGreg Roach 158a982a56eSGreg Roach return self::find($user_id); 159a982a56eSGreg Roach } 160a982a56eSGreg Roach 161a982a56eSGreg Roach /** 162a982a56eSGreg Roach * Find the user with a specified user_name or email address. 163a25f0a04SGreg Roach * 164a25f0a04SGreg Roach * @param string $identifier 165a25f0a04SGreg Roach * 166a25f0a04SGreg Roach * @return User|null 167a25f0a04SGreg Roach */ 168c1010edaSGreg Roach public static function findByIdentifier($identifier) 169c1010edaSGreg Roach { 170*01461f86SGreg Roach $user_id = (int) DB::table('user') 171*01461f86SGreg Roach ->where('user_name', '=', $identifier) 172*01461f86SGreg Roach ->orWhere('email', '=', $identifier) 173*01461f86SGreg Roach ->value('user_id'); 174a25f0a04SGreg Roach 175a25f0a04SGreg Roach return self::find($user_id); 176a25f0a04SGreg Roach } 177a25f0a04SGreg Roach 178a25f0a04SGreg Roach /** 179a25f0a04SGreg Roach * Find the user with a specified genealogy record. 180a25f0a04SGreg Roach * 181a25f0a04SGreg Roach * @param Individual $individual 182a25f0a04SGreg Roach * 183a25f0a04SGreg Roach * @return User|null 184a25f0a04SGreg Roach */ 185c1010edaSGreg Roach public static function findByIndividual(Individual $individual) 186c1010edaSGreg Roach { 187fa4036e8SGreg Roach $user_id = (int) Database::prepare( 188e5588fb0SGreg Roach "SELECT user_id" . 189a25f0a04SGreg Roach " FROM `##user_gedcom_setting`" . 190a5adda01SGreg Roach " WHERE gedcom_id = :tree_id AND setting_name = 'gedcomid' AND setting_value = :xref" 19113abd6f3SGreg Roach )->execute([ 192f4afa648SGreg Roach 'tree_id' => $individual->tree()->id(), 193c0935879SGreg Roach 'xref' => $individual->xref(), 19413abd6f3SGreg Roach ])->fetchOne(); 195a25f0a04SGreg Roach 196a25f0a04SGreg Roach return self::find($user_id); 197a25f0a04SGreg Roach } 198a25f0a04SGreg Roach 199a25f0a04SGreg Roach /** 200f7fb7d41SGreg Roach * Find the user with a specified user_name. 201f7fb7d41SGreg Roach * 202f7fb7d41SGreg Roach * @param string $user_name 203f7fb7d41SGreg Roach * 204f7fb7d41SGreg Roach * @return User|null 205f7fb7d41SGreg Roach */ 206c1010edaSGreg Roach public static function findByUserName($user_name) 207c1010edaSGreg Roach { 208*01461f86SGreg Roach $user_id = (int) DB::table('user') 209*01461f86SGreg Roach ->where('user_name', '=', $user_name) 210*01461f86SGreg Roach ->value('user_id'); 211f7fb7d41SGreg Roach 212f7fb7d41SGreg Roach return self::find($user_id); 213f7fb7d41SGreg Roach } 214f7fb7d41SGreg Roach 215f7fb7d41SGreg Roach /** 216a25f0a04SGreg Roach * Get a list of all users. 217a25f0a04SGreg Roach * 218a25f0a04SGreg Roach * @return User[] 219a25f0a04SGreg Roach */ 220fa4036e8SGreg Roach public static function all(): array 221c1010edaSGreg Roach { 222a25f0a04SGreg Roach $rows = Database::prepare( 223e5588fb0SGreg Roach "SELECT user_id, user_name, real_name, email" . 224a25f0a04SGreg Roach " FROM `##user`" . 225a25f0a04SGreg Roach " WHERE user_id > 0" . 226f7fb7d41SGreg Roach " ORDER BY real_name" 227a25f0a04SGreg Roach )->fetchAll(); 228a25f0a04SGreg Roach 22959f2f229SGreg Roach return array_map(function (stdClass $row): User { 2308d68cabeSGreg Roach return new static($row); 2318d68cabeSGreg Roach }, $rows); 232a25f0a04SGreg Roach } 233a25f0a04SGreg Roach 234a25f0a04SGreg Roach /** 235a25f0a04SGreg Roach * Get a list of all administrators. 236a25f0a04SGreg Roach * 237a25f0a04SGreg Roach * @return User[] 238a25f0a04SGreg Roach */ 239fa4036e8SGreg Roach public static function administrators(): array 240c1010edaSGreg Roach { 241a25f0a04SGreg Roach $rows = Database::prepare( 242e5588fb0SGreg Roach "SELECT user_id, user_name, real_name, email" . 243a25f0a04SGreg Roach " FROM `##user`" . 244a25f0a04SGreg Roach " JOIN `##user_setting` USING (user_id)" . 245f7fb7d41SGreg Roach " WHERE user_id > 0 AND setting_name = 'canadmin' AND setting_value = '1'" . 246f7fb7d41SGreg Roach " ORDER BY real_name" 247a25f0a04SGreg Roach )->fetchAll(); 248a25f0a04SGreg Roach 249c40ccc62SGreg Roach return array_map(function (stdClass $row): User { 2508d68cabeSGreg Roach return new static($row); 2518d68cabeSGreg Roach }, $rows); 252a25f0a04SGreg Roach } 253a25f0a04SGreg Roach 254fa4036e8SGreg Roach /** 255fa4036e8SGreg Roach * Validate a supplied password 256c1010edaSGreg Roach * 257a25f0a04SGreg Roach * @param string $password 258a25f0a04SGreg Roach * 259cbc1590aSGreg Roach * @return bool 260a25f0a04SGreg Roach */ 261fa4036e8SGreg Roach public function checkPassword(string $password): bool 262c1010edaSGreg Roach { 263a25f0a04SGreg Roach $password_hash = Database::prepare( 264a25f0a04SGreg Roach "SELECT password FROM `##user` WHERE user_id = ?" 26513abd6f3SGreg Roach )->execute([$this->user_id])->fetchOne(); 266a25f0a04SGreg Roach 267bbd8bd1bSGreg Roach if ($password_hash !== null && password_verify($password, $password_hash)) { 268015c99a2SGreg Roach if (password_needs_rehash($password_hash, PASSWORD_DEFAULT)) { 269a25f0a04SGreg Roach $this->setPassword($password); 270a25f0a04SGreg Roach } 271cbc1590aSGreg Roach 272a25f0a04SGreg Roach return true; 273a25f0a04SGreg Roach } 274b2ce94c6SRico Sonntag 275b2ce94c6SRico Sonntag return false; 276a25f0a04SGreg Roach } 277a25f0a04SGreg Roach 278a25f0a04SGreg Roach /** 279f7fb7d41SGreg Roach * Get a list of all managers. 280f7fb7d41SGreg Roach * 281f7fb7d41SGreg Roach * @return User[] 282f7fb7d41SGreg Roach */ 283fa4036e8SGreg Roach public static function managers(): array 284c1010edaSGreg Roach { 285f7fb7d41SGreg Roach $rows = Database::prepare( 286e5588fb0SGreg Roach "SELECT user_id, user_name, real_name, email" . 287f7fb7d41SGreg Roach " FROM `##user` JOIN `##user_gedcom_setting` USING (user_id)" . 288f7fb7d41SGreg Roach " WHERE setting_name = 'canedit' AND setting_value='admin'" . 289f7fb7d41SGreg Roach " GROUP BY user_id, real_name" . 290f7fb7d41SGreg Roach " ORDER BY real_name" 291f7fb7d41SGreg Roach )->fetchAll(); 292f7fb7d41SGreg Roach 29359f2f229SGreg Roach return array_map(function (stdClass $row): User { 2948d68cabeSGreg Roach return new static($row); 2958d68cabeSGreg Roach }, $rows); 296f7fb7d41SGreg Roach } 297f7fb7d41SGreg Roach 298f7fb7d41SGreg Roach /** 299f7fb7d41SGreg Roach * Get a list of all moderators. 300f7fb7d41SGreg Roach * 301f7fb7d41SGreg Roach * @return User[] 302f7fb7d41SGreg Roach */ 303fa4036e8SGreg Roach public static function moderators(): array 304c1010edaSGreg Roach { 305f7fb7d41SGreg Roach $rows = Database::prepare( 306e5588fb0SGreg Roach "SELECT user_id, user_name, real_name, email" . 307f7fb7d41SGreg Roach " FROM `##user` JOIN `##user_gedcom_setting` USING (user_id)" . 308f7fb7d41SGreg Roach " WHERE setting_name = 'canedit' AND setting_value='accept'" . 309f7fb7d41SGreg Roach " GROUP BY user_id, real_name" . 310f7fb7d41SGreg Roach " ORDER BY real_name" 311f7fb7d41SGreg Roach )->fetchAll(); 312f7fb7d41SGreg Roach 31359f2f229SGreg Roach return array_map(function (stdClass $row): User { 3148d68cabeSGreg Roach return new static($row); 3158d68cabeSGreg Roach }, $rows); 316f7fb7d41SGreg Roach } 317f7fb7d41SGreg Roach 318f7fb7d41SGreg Roach /** 319f7fb7d41SGreg Roach * Get a list of all verified users. 320f7fb7d41SGreg Roach * 321f7fb7d41SGreg Roach * @return User[] 322f7fb7d41SGreg Roach */ 323fa4036e8SGreg Roach public static function unapproved(): array 324c1010edaSGreg Roach { 325f7fb7d41SGreg Roach $rows = Database::prepare( 326e5588fb0SGreg Roach "SELECT user_id, user_name, real_name, email" . 327f7fb7d41SGreg Roach " FROM `##user` JOIN `##user_setting` USING (user_id)" . 328f7fb7d41SGreg Roach " WHERE setting_name = 'verified_by_admin' AND setting_value = '0'" . 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 unverified(): 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' 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 users who are currently logged in. 358f7fb7d41SGreg Roach * 359f7fb7d41SGreg Roach * @return User[] 360f7fb7d41SGreg Roach */ 361fa4036e8SGreg Roach public static function allLoggedIn(): array 362c1010edaSGreg Roach { 363f7fb7d41SGreg Roach $rows = Database::prepare( 364e5588fb0SGreg Roach "SELECT DISTINCT user_id, user_name, real_name, email" . 365f7fb7d41SGreg Roach " FROM `##user`" . 366f7fb7d41SGreg Roach " JOIN `##session` USING (user_id)" 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 /** 375a25f0a04SGreg Roach * Get the numeric ID for this user. 376a25f0a04SGreg Roach * 377c04dd3c1SGreg Roach * @return int 378a25f0a04SGreg Roach */ 379c1010edaSGreg Roach public function getUserId(): int 380c1010edaSGreg Roach { 381a25f0a04SGreg Roach return $this->user_id; 382a25f0a04SGreg Roach } 383a25f0a04SGreg Roach 384a25f0a04SGreg Roach /** 385a25f0a04SGreg Roach * Get the login name for this user. 386a25f0a04SGreg Roach * 387a25f0a04SGreg Roach * @return string 388a25f0a04SGreg Roach */ 3898f53f488SRico Sonntag public function getUserName(): string 390c1010edaSGreg Roach { 391a25f0a04SGreg Roach return $this->user_name; 392a25f0a04SGreg Roach } 393a25f0a04SGreg Roach 394a25f0a04SGreg Roach /** 395a25f0a04SGreg Roach * Set the login name for this user. 396a25f0a04SGreg Roach * 397a25f0a04SGreg Roach * @param string $user_name 398a25f0a04SGreg Roach * 399a25f0a04SGreg Roach * @return $this 400a25f0a04SGreg Roach */ 4018f53f488SRico Sonntag public function setUserName($user_name): self 402c1010edaSGreg Roach { 403a25f0a04SGreg Roach if ($this->user_name !== $user_name) { 404a25f0a04SGreg Roach $this->user_name = $user_name; 405*01461f86SGreg Roach 406*01461f86SGreg Roach DB::table('user') 407*01461f86SGreg Roach ->where('user_id', '=', $this->user_id) 408*01461f86SGreg Roach ->update([ 409*01461f86SGreg Roach 'user_name' => $user_name, 410c1010edaSGreg Roach ]); 411a25f0a04SGreg Roach } 412a25f0a04SGreg Roach 413a25f0a04SGreg Roach return $this; 414a25f0a04SGreg Roach } 415a25f0a04SGreg Roach 416a25f0a04SGreg Roach /** 417a25f0a04SGreg Roach * Get the real name of this user. 418a25f0a04SGreg Roach * 419a25f0a04SGreg Roach * @return string 420a25f0a04SGreg Roach */ 4218f53f488SRico Sonntag public function getRealName(): string 422c1010edaSGreg Roach { 423a25f0a04SGreg Roach return $this->real_name; 424a25f0a04SGreg Roach } 425a25f0a04SGreg Roach 426a25f0a04SGreg Roach /** 427a25f0a04SGreg Roach * Set the real name of this user. 428a25f0a04SGreg Roach * 429a25f0a04SGreg Roach * @param string $real_name 430a25f0a04SGreg Roach * 431a25f0a04SGreg Roach * @return User 432a25f0a04SGreg Roach */ 4338f53f488SRico Sonntag public function setRealName($real_name): User 434c1010edaSGreg Roach { 435a25f0a04SGreg Roach if ($this->real_name !== $real_name) { 436a25f0a04SGreg Roach $this->real_name = $real_name; 437*01461f86SGreg Roach 438*01461f86SGreg Roach DB::table('user') 439*01461f86SGreg Roach ->where('user_id', '=', $this->user_id) 440*01461f86SGreg Roach ->update([ 441*01461f86SGreg Roach 'real_name' => $real_name, 442c1010edaSGreg Roach ]); 443a25f0a04SGreg Roach } 444a25f0a04SGreg Roach 445a25f0a04SGreg Roach return $this; 446a25f0a04SGreg Roach } 447a25f0a04SGreg Roach 448a25f0a04SGreg Roach /** 449a25f0a04SGreg Roach * Get the email address of this user. 450a25f0a04SGreg Roach * 451a25f0a04SGreg Roach * @return string 452a25f0a04SGreg Roach */ 4538f53f488SRico Sonntag public function getEmail(): string 454c1010edaSGreg Roach { 455a25f0a04SGreg Roach return $this->email; 456a25f0a04SGreg Roach } 457a25f0a04SGreg Roach 458a25f0a04SGreg Roach /** 459a25f0a04SGreg Roach * Set the email address of this user. 460a25f0a04SGreg Roach * 461a25f0a04SGreg Roach * @param string $email 462a25f0a04SGreg Roach * 463a25f0a04SGreg Roach * @return User 464a25f0a04SGreg Roach */ 4658f53f488SRico Sonntag public function setEmail($email): User 466c1010edaSGreg Roach { 467a25f0a04SGreg Roach if ($this->email !== $email) { 468a25f0a04SGreg Roach $this->email = $email; 469*01461f86SGreg Roach 470*01461f86SGreg Roach DB::table('user') 471*01461f86SGreg Roach ->where('user_id', '=', $this->user_id) 472*01461f86SGreg Roach ->update([ 473*01461f86SGreg Roach 'email' => $email, 474c1010edaSGreg Roach ]); 475a25f0a04SGreg Roach } 476a25f0a04SGreg Roach 477a25f0a04SGreg Roach return $this; 478a25f0a04SGreg Roach } 479a25f0a04SGreg Roach 480a25f0a04SGreg Roach /** 481a25f0a04SGreg Roach * Set the password of this user. 482a25f0a04SGreg Roach * 483a25f0a04SGreg Roach * @param string $password 484a25f0a04SGreg Roach * 485a25f0a04SGreg Roach * @return User 486a25f0a04SGreg Roach */ 4878f53f488SRico Sonntag public function setPassword($password): User 488c1010edaSGreg Roach { 489*01461f86SGreg Roach DB::table('user') 490*01461f86SGreg Roach ->where('user_id', '=', $this->user_id) 491*01461f86SGreg Roach ->update([ 492015c99a2SGreg Roach 'password' => password_hash($password, PASSWORD_DEFAULT), 493015c99a2SGreg Roach ]); 494a25f0a04SGreg Roach 495a25f0a04SGreg Roach return $this; 496a25f0a04SGreg Roach } 497a25f0a04SGreg Roach 498a25f0a04SGreg Roach /** 499a25f0a04SGreg Roach * Fetch a user option/setting from the wt_user_setting table. 500a25f0a04SGreg Roach * Since we'll fetch several settings for each user, and since there aren’t 501a25f0a04SGreg Roach * that many of them, fetch them all in one database query 502a25f0a04SGreg Roach * 503a25f0a04SGreg Roach * @param string $setting_name 50415d603e7SGreg Roach * @param string $default 505a25f0a04SGreg Roach * 50615d603e7SGreg Roach * @return string 507a25f0a04SGreg Roach */ 5088f53f488SRico Sonntag public function getPreference($setting_name, $default = ''): string 509c1010edaSGreg Roach { 510c04dd3c1SGreg Roach if (empty($this->preferences) && $this->user_id !== 0) { 511*01461f86SGreg Roach $this->preferences = DB::table('user_setting') 512*01461f86SGreg Roach ->where('user_id', '=', $this->user_id) 513*01461f86SGreg Roach ->pluck('setting_value', 'setting_name') 514*01461f86SGreg Roach ->all(); 515a25f0a04SGreg Roach } 516a25f0a04SGreg Roach 517*01461f86SGreg Roach return $this->preferences[$setting_name] ?? $default; 518a25f0a04SGreg Roach } 519a25f0a04SGreg Roach 520a25f0a04SGreg Roach /** 521a25f0a04SGreg Roach * Update a setting for the user. 522a25f0a04SGreg Roach * 523a25f0a04SGreg Roach * @param string $setting_name 524a25f0a04SGreg Roach * @param string $setting_value 525a25f0a04SGreg Roach * 526a25f0a04SGreg Roach * @return User 527a25f0a04SGreg Roach */ 5288f53f488SRico Sonntag public function setPreference($setting_name, $setting_value): User 529c1010edaSGreg Roach { 530c04dd3c1SGreg Roach if ($this->user_id !== 0 && $this->getPreference($setting_name) !== $setting_value) { 531*01461f86SGreg Roach DB::table('user_setting')->updateOrInsert([ 532*01461f86SGreg Roach 'user_id' => $this->user_id, 533*01461f86SGreg Roach 'setting_name' => $setting_name, 534*01461f86SGreg Roach ], [ 535*01461f86SGreg Roach 'setting_value' => $setting_value, 536c1010edaSGreg Roach ]); 53715d603e7SGreg Roach 538a25f0a04SGreg Roach $this->preferences[$setting_name] = $setting_value; 539a25f0a04SGreg Roach } 540a25f0a04SGreg Roach 541a25f0a04SGreg Roach return $this; 542a25f0a04SGreg Roach } 543a25f0a04SGreg Roach} 544