. */ declare(strict_types=1); namespace Fisharebest\Webtrees; use Closure; use Fisharebest\Webtrees\Contracts\UserInterface; use Illuminate\Database\Capsule\Manager as DB; use stdClass; /** * Provide an interface to the wt_user table. */ class User implements UserInterface { /** @var int The primary key of this user. */ private $user_id; /** @var string The login name of this user. */ private $user_name; /** @var string The real (display) name of this user. */ private $real_name; /** @var string The email address of this user. */ private $email; /** @var string[] Cached copy of the wt_user_setting table. */ private $preferences = []; /** * User constructor. * * @param int $user_id * @param string $user_name * @param string $real_name * @param string $email */ public function __construct(int $user_id, string $user_name, string $real_name, string $email) { $this->user_id = $user_id; $this->user_name = $user_name; $this->real_name = $real_name; $this->email = $email; } /** * The user‘s internal identifier. * * @return int */ public function id(): int { return $this->user_id; } /** * The users email address. * * @return string */ public function email(): string { return $this->email; } /** * Set the email address of this user. * * @param string $email * * @return User */ public function setEmail($email): User { if ($this->email !== $email) { $this->email = $email; DB::table('user') ->where('user_id', '=', $this->user_id) ->update([ 'email' => $email, ]); } return $this; } /** * The user‘s real name. * * @return string */ public function realName(): string { return $this->real_name; } /** * Set the real name of this user. * * @param string $real_name * * @return User */ public function setRealName($real_name): User { if ($this->real_name !== $real_name) { $this->real_name = $real_name; DB::table('user') ->where('user_id', '=', $this->user_id) ->update([ 'real_name' => $real_name, ]); } return $this; } /** * The user‘s login name. * * @return string */ public function userName(): string { return $this->user_name; } /** * Set the login name for this user. * * @param string $user_name * * @return $this */ public function setUserName($user_name): self { if ($this->user_name !== $user_name) { $this->user_name = $user_name; DB::table('user') ->where('user_id', '=', $this->user_id) ->update([ 'user_name' => $user_name, ]); } return $this; } /** * Fetch a user option/setting from the wt_user_setting table. * Since we'll fetch several settings for each user, and since there aren’t * that many of them, fetch them all in one database query * * @param string $setting_name * @param string $default * * @return string */ public function getPreference(string $setting_name, string $default = ''): string { $preferences = app('cache.array')->rememberForever('user_setting' . $this->user_id, function () { if ($this->user_id) { return DB::table('user_setting') ->where('user_id', '=', $this->user_id) ->pluck('setting_value', 'setting_name') ->all(); } else { return []; } }); return $preferences[$setting_name] ?? $default; } /** * Update a setting for the user. * * @param string $setting_name * @param string $setting_value * * @return UserInterface */ public function setPreference(string $setting_name, string $setting_value): UserInterface { if ($this->user_id !== 0 && $this->getPreference($setting_name) !== $setting_value) { DB::table('user_setting')->updateOrInsert([ 'user_id' => $this->user_id, 'setting_name' => $setting_name, ], [ 'setting_value' => $setting_value, ]); $this->preferences[$setting_name] = $setting_value; } app('cache.array')->forget('user_setting' . $this->user_id); return $this; } /** * Set the password of this user. * * @param string $password * * @return User */ public function setPassword(string $password): User { DB::table('user') ->where('user_id', '=', $this->user_id) ->update([ 'password' => password_hash($password, PASSWORD_DEFAULT), ]); return $this; } /** * Validate a supplied password * * @param string $password * * @return bool */ public function checkPassword(string $password): bool { $password_hash = DB::table('user') ->where('user_id', '=', $this->id()) ->value('password'); if ($password_hash !== null && password_verify($password, $password_hash)) { if (password_needs_rehash($password_hash, PASSWORD_DEFAULT)) { $this->setPassword($password); } return true; } return false; } /** * A closure which will create an object from a database row. * * @return Closure */ public static function rowMapper(): Closure { return function (stdClass $row): User { return new static((int) $row->user_id, $row->user_name, $row->real_name, $row->email); }; } }