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 208b67c11aSGreg Roachuse Closure; 21e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 2201461f86SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 232b7831a1SGreg Roachuse Illuminate\Support\Collection; 24b0b72ea4SGreg Roachuse stdClass; 2560bc3e3fSGreg Roach 26a25f0a04SGreg Roach/** 2776692c8bSGreg Roach * Provide an interface to the wt_user table. 28a25f0a04SGreg Roach */ 29e5a6b4d4SGreg Roachclass User implements UserInterface 30c1010edaSGreg Roach{ 31c04dd3c1SGreg Roach /** @var int The primary key of this user. */ 32a25f0a04SGreg Roach private $user_id; 33a25f0a04SGreg Roach 34a25f0a04SGreg Roach /** @var string The login name of this user. */ 35a25f0a04SGreg Roach private $user_name; 36a25f0a04SGreg Roach 37a25f0a04SGreg Roach /** @var string The real (display) name of this user. */ 38a25f0a04SGreg Roach private $real_name; 39a25f0a04SGreg Roach 40a25f0a04SGreg Roach /** @var string The email address of this user. */ 41a25f0a04SGreg Roach private $email; 42a25f0a04SGreg Roach 4315d603e7SGreg Roach /** @var string[] Cached copy of the wt_user_setting table. */ 4415d603e7SGreg Roach private $preferences = []; 45a25f0a04SGreg Roach 46a25f0a04SGreg Roach /** 47e5a6b4d4SGreg Roach * User constructor. 48f7fb7d41SGreg Roach * 498b67c11aSGreg Roach * @param int $user_id 508b67c11aSGreg Roach * @param string $user_name 518b67c11aSGreg Roach * @param string $real_name 528b67c11aSGreg Roach * @param string $email 53f7fb7d41SGreg Roach */ 54e5a6b4d4SGreg Roach public function __construct(int $user_id, string $user_name, string $real_name, string $email) 55c1010edaSGreg Roach { 568b67c11aSGreg Roach $this->user_id = $user_id; 578b67c11aSGreg Roach $this->user_name = $user_name; 588b67c11aSGreg Roach $this->real_name = $real_name; 598b67c11aSGreg Roach $this->email = $email; 608b67c11aSGreg Roach } 618b67c11aSGreg Roach 628b67c11aSGreg Roach /** 63e5a6b4d4SGreg Roach * The user‘s internal identifier. 64a25f0a04SGreg Roach * 65c04dd3c1SGreg Roach * @return int 66a25f0a04SGreg Roach */ 67895230eeSGreg Roach public function id(): int 68c1010edaSGreg Roach { 69a25f0a04SGreg Roach return $this->user_id; 70a25f0a04SGreg Roach } 71a25f0a04SGreg Roach 72a25f0a04SGreg Roach /** 73e5a6b4d4SGreg Roach * The users email address. 74a25f0a04SGreg Roach * 75a25f0a04SGreg Roach * @return string 76a25f0a04SGreg Roach */ 77e5a6b4d4SGreg Roach public function email(): string 78c1010edaSGreg Roach { 79a25f0a04SGreg Roach return $this->email; 80a25f0a04SGreg Roach } 81a25f0a04SGreg Roach 82a25f0a04SGreg Roach /** 83a25f0a04SGreg Roach * Set the email address of this user. 84a25f0a04SGreg Roach * 85a25f0a04SGreg Roach * @param string $email 86a25f0a04SGreg Roach * 87a25f0a04SGreg Roach * @return User 88a25f0a04SGreg Roach */ 898f53f488SRico Sonntag public function setEmail($email): User 90c1010edaSGreg Roach { 91a25f0a04SGreg Roach if ($this->email !== $email) { 92a25f0a04SGreg Roach $this->email = $email; 9301461f86SGreg Roach 9401461f86SGreg Roach DB::table('user') 9501461f86SGreg Roach ->where('user_id', '=', $this->user_id) 9601461f86SGreg Roach ->update([ 9701461f86SGreg Roach 'email' => $email, 98c1010edaSGreg Roach ]); 99a25f0a04SGreg Roach } 100a25f0a04SGreg Roach 101a25f0a04SGreg Roach return $this; 102a25f0a04SGreg Roach } 103a25f0a04SGreg Roach 104a25f0a04SGreg Roach /** 105e5a6b4d4SGreg Roach * The user‘s real name. 106a25f0a04SGreg Roach * 107e5a6b4d4SGreg Roach * @return string 108e5a6b4d4SGreg Roach */ 109e5a6b4d4SGreg Roach public function realName(): string 110e5a6b4d4SGreg Roach { 111e5a6b4d4SGreg Roach return $this->real_name; 112e5a6b4d4SGreg Roach } 113e5a6b4d4SGreg Roach 114e5a6b4d4SGreg Roach /** 115e5a6b4d4SGreg Roach * Set the real name of this user. 116e5a6b4d4SGreg Roach * 117e5a6b4d4SGreg Roach * @param string $real_name 118a25f0a04SGreg Roach * 119a25f0a04SGreg Roach * @return User 120a25f0a04SGreg Roach */ 121e5a6b4d4SGreg Roach public function setRealName($real_name): User 122c1010edaSGreg Roach { 123e5a6b4d4SGreg Roach if ($this->real_name !== $real_name) { 124e5a6b4d4SGreg Roach $this->real_name = $real_name; 125e5a6b4d4SGreg Roach 12601461f86SGreg Roach DB::table('user') 12701461f86SGreg Roach ->where('user_id', '=', $this->user_id) 12801461f86SGreg Roach ->update([ 129e5a6b4d4SGreg Roach 'real_name' => $real_name, 130015c99a2SGreg Roach ]); 131e5a6b4d4SGreg Roach } 132e5a6b4d4SGreg Roach 133e5a6b4d4SGreg Roach return $this; 134e5a6b4d4SGreg Roach } 135e5a6b4d4SGreg Roach 136e5a6b4d4SGreg Roach /** 137e5a6b4d4SGreg Roach * The user‘s login name. 138e5a6b4d4SGreg Roach * 139e5a6b4d4SGreg Roach * @return string 140e5a6b4d4SGreg Roach */ 141e5a6b4d4SGreg Roach public function userName(): string 142e5a6b4d4SGreg Roach { 143e5a6b4d4SGreg Roach return $this->user_name; 144e5a6b4d4SGreg Roach } 145e5a6b4d4SGreg Roach 146e5a6b4d4SGreg Roach /** 147e5a6b4d4SGreg Roach * Set the login name for this user. 148e5a6b4d4SGreg Roach * 149e5a6b4d4SGreg Roach * @param string $user_name 150e5a6b4d4SGreg Roach * 151e5a6b4d4SGreg Roach * @return $this 152e5a6b4d4SGreg Roach */ 153e5a6b4d4SGreg Roach public function setUserName($user_name): self 154e5a6b4d4SGreg Roach { 155e5a6b4d4SGreg Roach if ($this->user_name !== $user_name) { 156e5a6b4d4SGreg Roach $this->user_name = $user_name; 157e5a6b4d4SGreg Roach 158e5a6b4d4SGreg Roach DB::table('user') 159e5a6b4d4SGreg Roach ->where('user_id', '=', $this->user_id) 160e5a6b4d4SGreg Roach ->update([ 161e5a6b4d4SGreg Roach 'user_name' => $user_name, 162e5a6b4d4SGreg Roach ]); 163e5a6b4d4SGreg Roach } 164a25f0a04SGreg Roach 165a25f0a04SGreg Roach return $this; 166a25f0a04SGreg Roach } 167a25f0a04SGreg Roach 168a25f0a04SGreg Roach /** 169a25f0a04SGreg Roach * Fetch a user option/setting from the wt_user_setting table. 170a25f0a04SGreg Roach * Since we'll fetch several settings for each user, and since there aren’t 171a25f0a04SGreg Roach * that many of them, fetch them all in one database query 172a25f0a04SGreg Roach * 173a25f0a04SGreg Roach * @param string $setting_name 17415d603e7SGreg Roach * @param string $default 175a25f0a04SGreg Roach * 17615d603e7SGreg Roach * @return string 177a25f0a04SGreg Roach */ 178e5a6b4d4SGreg Roach public function getPreference(string $setting_name, string $default = ''): string 179c1010edaSGreg Roach { 1802b7831a1SGreg Roach $preferences = app('cache.array')->rememberForever('user_setting' . $this->user_id, function (): Collection { 18183c7613eSGreg Roach if ($this->user_id) { 18283c7613eSGreg Roach return DB::table('user_setting') 18301461f86SGreg Roach ->where('user_id', '=', $this->user_id) 1842b7831a1SGreg Roach ->pluck('setting_value', 'setting_name'); 185a25f0a04SGreg Roach } 186*e364afe4SGreg Roach 187*e364afe4SGreg Roach return new Collection(); 18883c7613eSGreg Roach }); 189a25f0a04SGreg Roach 1902b7831a1SGreg Roach return $preferences->get($setting_name) ?? $default; 191a25f0a04SGreg Roach } 192a25f0a04SGreg Roach 193a25f0a04SGreg Roach /** 194a25f0a04SGreg Roach * Update a setting for the user. 195a25f0a04SGreg Roach * 196a25f0a04SGreg Roach * @param string $setting_name 197a25f0a04SGreg Roach * @param string $setting_value 198a25f0a04SGreg Roach * 199e5a6b4d4SGreg Roach * @return UserInterface 200a25f0a04SGreg Roach */ 201e5a6b4d4SGreg Roach public function setPreference(string $setting_name, string $setting_value): UserInterface 202c1010edaSGreg Roach { 203c04dd3c1SGreg Roach if ($this->user_id !== 0 && $this->getPreference($setting_name) !== $setting_value) { 20401461f86SGreg Roach DB::table('user_setting')->updateOrInsert([ 20501461f86SGreg Roach 'user_id' => $this->user_id, 20601461f86SGreg Roach 'setting_name' => $setting_name, 20701461f86SGreg Roach ], [ 20801461f86SGreg Roach 'setting_value' => $setting_value, 209c1010edaSGreg Roach ]); 21015d603e7SGreg Roach 211a25f0a04SGreg Roach $this->preferences[$setting_name] = $setting_value; 212a25f0a04SGreg Roach } 213a25f0a04SGreg Roach 214611d1316SGreg Roach app('cache.array')->forget('user_setting' . $this->user_id); 215611d1316SGreg Roach 216a25f0a04SGreg Roach return $this; 217a25f0a04SGreg Roach } 218e5a6b4d4SGreg Roach 219e5a6b4d4SGreg Roach /** 220e5a6b4d4SGreg Roach * Set the password of this user. 221e5a6b4d4SGreg Roach * 222e5a6b4d4SGreg Roach * @param string $password 223e5a6b4d4SGreg Roach * 224e5a6b4d4SGreg Roach * @return User 225e5a6b4d4SGreg Roach */ 226e5a6b4d4SGreg Roach public function setPassword(string $password): User 227e5a6b4d4SGreg Roach { 228e5a6b4d4SGreg Roach DB::table('user') 229e5a6b4d4SGreg Roach ->where('user_id', '=', $this->user_id) 230e5a6b4d4SGreg Roach ->update([ 231e5a6b4d4SGreg Roach 'password' => password_hash($password, PASSWORD_DEFAULT), 232e5a6b4d4SGreg Roach ]); 233e5a6b4d4SGreg Roach 234e5a6b4d4SGreg Roach return $this; 235e5a6b4d4SGreg Roach } 236e5a6b4d4SGreg Roach 237e5a6b4d4SGreg Roach 238e5a6b4d4SGreg Roach /** 239e5a6b4d4SGreg Roach * Validate a supplied password 240e5a6b4d4SGreg Roach * 241e5a6b4d4SGreg Roach * @param string $password 242e5a6b4d4SGreg Roach * 243e5a6b4d4SGreg Roach * @return bool 244e5a6b4d4SGreg Roach */ 245e5a6b4d4SGreg Roach public function checkPassword(string $password): bool 246e5a6b4d4SGreg Roach { 247e5a6b4d4SGreg Roach $password_hash = DB::table('user') 2483e1b7b6eSGreg Roach ->where('user_id', '=', $this->id()) 249e5a6b4d4SGreg Roach ->value('password'); 250e5a6b4d4SGreg Roach 251e5a6b4d4SGreg Roach if ($password_hash !== null && password_verify($password, $password_hash)) { 252e5a6b4d4SGreg Roach if (password_needs_rehash($password_hash, PASSWORD_DEFAULT)) { 253e5a6b4d4SGreg Roach $this->setPassword($password); 254e5a6b4d4SGreg Roach } 255e5a6b4d4SGreg Roach 256e5a6b4d4SGreg Roach return true; 257e5a6b4d4SGreg Roach } 258e5a6b4d4SGreg Roach 259e5a6b4d4SGreg Roach return false; 260e5a6b4d4SGreg Roach } 2613e1b7b6eSGreg Roach 2623e1b7b6eSGreg Roach /** 2633e1b7b6eSGreg Roach * A closure which will create an object from a database row. 2643e1b7b6eSGreg Roach * 2653e1b7b6eSGreg Roach * @return Closure 2663e1b7b6eSGreg Roach */ 2673e1b7b6eSGreg Roach public static function rowMapper(): Closure 2683e1b7b6eSGreg Roach { 2693e1b7b6eSGreg Roach return function (stdClass $row): User { 2703e1b7b6eSGreg Roach return new static((int) $row->user_id, $row->user_name, $row->real_name, $row->email); 2713e1b7b6eSGreg Roach }; 2723e1b7b6eSGreg Roach } 273a25f0a04SGreg Roach} 274