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; 23b0b72ea4SGreg Roachuse stdClass; 2460bc3e3fSGreg Roach 25a25f0a04SGreg Roach/** 2676692c8bSGreg Roach * Provide an interface to the wt_user table. 27a25f0a04SGreg Roach */ 28e5a6b4d4SGreg Roachclass User implements UserInterface 29c1010edaSGreg Roach{ 30c04dd3c1SGreg Roach /** @var int The primary key of this user. */ 31a25f0a04SGreg Roach private $user_id; 32a25f0a04SGreg Roach 33a25f0a04SGreg Roach /** @var string The login name of this user. */ 34a25f0a04SGreg Roach private $user_name; 35a25f0a04SGreg Roach 36a25f0a04SGreg Roach /** @var string The real (display) name of this user. */ 37a25f0a04SGreg Roach private $real_name; 38a25f0a04SGreg Roach 39a25f0a04SGreg Roach /** @var string The email address of this user. */ 40a25f0a04SGreg Roach private $email; 41a25f0a04SGreg Roach 4215d603e7SGreg Roach /** @var string[] Cached copy of the wt_user_setting table. */ 4315d603e7SGreg Roach private $preferences = []; 44a25f0a04SGreg Roach 45a25f0a04SGreg Roach /** 46e5a6b4d4SGreg Roach * User constructor. 47f7fb7d41SGreg Roach * 488b67c11aSGreg Roach * @param int $user_id 498b67c11aSGreg Roach * @param string $user_name 508b67c11aSGreg Roach * @param string $real_name 518b67c11aSGreg Roach * @param string $email 52f7fb7d41SGreg Roach */ 53e5a6b4d4SGreg Roach public function __construct(int $user_id, string $user_name, string $real_name, string $email) 54c1010edaSGreg Roach { 558b67c11aSGreg Roach $this->user_id = $user_id; 568b67c11aSGreg Roach $this->user_name = $user_name; 578b67c11aSGreg Roach $this->real_name = $real_name; 588b67c11aSGreg Roach $this->email = $email; 598b67c11aSGreg Roach } 608b67c11aSGreg Roach 618b67c11aSGreg Roach /** 62e5a6b4d4SGreg Roach * The user‘s internal identifier. 63a25f0a04SGreg Roach * 64c04dd3c1SGreg Roach * @return int 65a25f0a04SGreg Roach */ 66895230eeSGreg Roach public function id(): int 67c1010edaSGreg Roach { 68a25f0a04SGreg Roach return $this->user_id; 69a25f0a04SGreg Roach } 70a25f0a04SGreg Roach 71a25f0a04SGreg Roach /** 72e5a6b4d4SGreg Roach * The users email address. 73a25f0a04SGreg Roach * 74a25f0a04SGreg Roach * @return string 75a25f0a04SGreg Roach */ 76e5a6b4d4SGreg Roach public function email(): string 77c1010edaSGreg Roach { 78a25f0a04SGreg Roach return $this->email; 79a25f0a04SGreg Roach } 80a25f0a04SGreg Roach 81a25f0a04SGreg Roach /** 82a25f0a04SGreg Roach * Set the email address of this user. 83a25f0a04SGreg Roach * 84a25f0a04SGreg Roach * @param string $email 85a25f0a04SGreg Roach * 86a25f0a04SGreg Roach * @return User 87a25f0a04SGreg Roach */ 888f53f488SRico Sonntag public function setEmail($email): User 89c1010edaSGreg Roach { 90a25f0a04SGreg Roach if ($this->email !== $email) { 91a25f0a04SGreg Roach $this->email = $email; 9201461f86SGreg Roach 9301461f86SGreg Roach DB::table('user') 9401461f86SGreg Roach ->where('user_id', '=', $this->user_id) 9501461f86SGreg Roach ->update([ 9601461f86SGreg Roach 'email' => $email, 97c1010edaSGreg Roach ]); 98a25f0a04SGreg Roach } 99a25f0a04SGreg Roach 100a25f0a04SGreg Roach return $this; 101a25f0a04SGreg Roach } 102a25f0a04SGreg Roach 103a25f0a04SGreg Roach /** 104e5a6b4d4SGreg Roach * The user‘s real name. 105a25f0a04SGreg Roach * 106e5a6b4d4SGreg Roach * @return string 107e5a6b4d4SGreg Roach */ 108e5a6b4d4SGreg Roach public function realName(): string 109e5a6b4d4SGreg Roach { 110e5a6b4d4SGreg Roach return $this->real_name; 111e5a6b4d4SGreg Roach } 112e5a6b4d4SGreg Roach 113e5a6b4d4SGreg Roach /** 114e5a6b4d4SGreg Roach * Set the real name of this user. 115e5a6b4d4SGreg Roach * 116e5a6b4d4SGreg Roach * @param string $real_name 117a25f0a04SGreg Roach * 118a25f0a04SGreg Roach * @return User 119a25f0a04SGreg Roach */ 120e5a6b4d4SGreg Roach public function setRealName($real_name): User 121c1010edaSGreg Roach { 122e5a6b4d4SGreg Roach if ($this->real_name !== $real_name) { 123e5a6b4d4SGreg Roach $this->real_name = $real_name; 124e5a6b4d4SGreg Roach 12501461f86SGreg Roach DB::table('user') 12601461f86SGreg Roach ->where('user_id', '=', $this->user_id) 12701461f86SGreg Roach ->update([ 128e5a6b4d4SGreg Roach 'real_name' => $real_name, 129015c99a2SGreg Roach ]); 130e5a6b4d4SGreg Roach } 131e5a6b4d4SGreg Roach 132e5a6b4d4SGreg Roach return $this; 133e5a6b4d4SGreg Roach } 134e5a6b4d4SGreg Roach 135e5a6b4d4SGreg Roach /** 136e5a6b4d4SGreg Roach * The user‘s login name. 137e5a6b4d4SGreg Roach * 138e5a6b4d4SGreg Roach * @return string 139e5a6b4d4SGreg Roach */ 140e5a6b4d4SGreg Roach public function userName(): string 141e5a6b4d4SGreg Roach { 142e5a6b4d4SGreg Roach return $this->user_name; 143e5a6b4d4SGreg Roach } 144e5a6b4d4SGreg Roach 145e5a6b4d4SGreg Roach /** 146e5a6b4d4SGreg Roach * Set the login name for this user. 147e5a6b4d4SGreg Roach * 148e5a6b4d4SGreg Roach * @param string $user_name 149e5a6b4d4SGreg Roach * 150e5a6b4d4SGreg Roach * @return $this 151e5a6b4d4SGreg Roach */ 152e5a6b4d4SGreg Roach public function setUserName($user_name): self 153e5a6b4d4SGreg Roach { 154e5a6b4d4SGreg Roach if ($this->user_name !== $user_name) { 155e5a6b4d4SGreg Roach $this->user_name = $user_name; 156e5a6b4d4SGreg Roach 157e5a6b4d4SGreg Roach DB::table('user') 158e5a6b4d4SGreg Roach ->where('user_id', '=', $this->user_id) 159e5a6b4d4SGreg Roach ->update([ 160e5a6b4d4SGreg Roach 'user_name' => $user_name, 161e5a6b4d4SGreg Roach ]); 162e5a6b4d4SGreg Roach } 163a25f0a04SGreg Roach 164a25f0a04SGreg Roach return $this; 165a25f0a04SGreg Roach } 166a25f0a04SGreg Roach 167a25f0a04SGreg Roach /** 168a25f0a04SGreg Roach * Fetch a user option/setting from the wt_user_setting table. 169a25f0a04SGreg Roach * Since we'll fetch several settings for each user, and since there aren’t 170a25f0a04SGreg Roach * that many of them, fetch them all in one database query 171a25f0a04SGreg Roach * 172a25f0a04SGreg Roach * @param string $setting_name 17315d603e7SGreg Roach * @param string $default 174a25f0a04SGreg Roach * 17515d603e7SGreg Roach * @return string 176a25f0a04SGreg Roach */ 177e5a6b4d4SGreg Roach public function getPreference(string $setting_name, string $default = ''): string 178c1010edaSGreg Roach { 17983c7613eSGreg Roach $preferences = app('cache.array')->rememberForever('user_setting' . $this->user_id, function () { 18083c7613eSGreg Roach if ($this->user_id) { 18183c7613eSGreg Roach return DB::table('user_setting') 18201461f86SGreg Roach ->where('user_id', '=', $this->user_id) 18301461f86SGreg Roach ->pluck('setting_value', 'setting_name') 18401461f86SGreg Roach ->all(); 18583c7613eSGreg Roach } else { 18683c7613eSGreg Roach return []; 187a25f0a04SGreg Roach } 18883c7613eSGreg Roach }); 189a25f0a04SGreg Roach 19083c7613eSGreg Roach return $preferences[$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') 248*3e1b7b6eSGreg 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 } 261*3e1b7b6eSGreg Roach 262*3e1b7b6eSGreg Roach /** 263*3e1b7b6eSGreg Roach * A closure which will create an object from a database row. 264*3e1b7b6eSGreg Roach * 265*3e1b7b6eSGreg Roach * @return Closure 266*3e1b7b6eSGreg Roach */ 267*3e1b7b6eSGreg Roach public static function rowMapper(): Closure 268*3e1b7b6eSGreg Roach { 269*3e1b7b6eSGreg Roach return function (stdClass $row): User { 270*3e1b7b6eSGreg Roach return new static((int) $row->user_id, $row->user_name, $row->real_name, $row->email); 271*3e1b7b6eSGreg Roach }; 272*3e1b7b6eSGreg Roach } 273a25f0a04SGreg Roach} 274