X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/709c182bda5fac45f7a1cb38ec8cd23b3544d67b..refs/pull/5280/head:/app/Users/Models/User.php diff --git a/app/Users/Models/User.php b/app/Users/Models/User.php index 2479521a2..3797e7cb0 100644 --- a/app/Users/Models/User.php +++ b/app/Users/Models/User.php @@ -12,7 +12,8 @@ use BookStack\Api\ApiToken; use BookStack\App\Model; use BookStack\App\Sluggable; use BookStack\Entities\Tools\SlugGenerator; -use BookStack\Translation\LanguageManager; +use BookStack\Translation\LocaleDefinition; +use BookStack\Translation\LocaleManager; use BookStack\Uploads\Image; use Carbon\Carbon; use Exception; @@ -88,36 +89,29 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon */ protected string $avatarUrl = ''; - /** - * This holds the default user when loaded. - */ - protected static ?User $defaultUser = null; - /** * Returns the default public user. + * Fetches from the container as a singleton to effectively cache at an app level. */ - public static function getDefault(): self + public static function getGuest(): self { - if (!is_null(static::$defaultUser)) { - return static::$defaultUser; - } - - static::$defaultUser = static::query()->where('system_name', '=', 'public')->first(); - - return static::$defaultUser; + return app()->make('users.default'); } - public static function clearDefault(): void + /** + * Check if the user is the default public user. + */ + public function isGuest(): bool { - static::$defaultUser = null; + return $this->system_name === 'public'; } /** - * Check if the user is the default public user. + * Check if the user has general access to the application. */ - public function isDefault(): bool + public function hasAppAccess(): bool { - return $this->system_name === 'public'; + return !$this->isGuest() || setting('app-public'); } /** @@ -166,10 +160,6 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon */ public function can(string $permissionName): bool { - if ($this->email === 'guest') { - return false; - } - return $this->permissions()->contains($permissionName); } @@ -250,7 +240,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon } try { - $avatar = $this->avatar ? url($this->avatar->getThumb($size, $size, false)) : $default; + $avatar = $this->avatar?->getThumb($size, $size, false) ?? $default; } catch (Exception $err) { $avatar = $default; } @@ -349,11 +339,11 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon } /** - * Get the system language for this user. + * Get the locale for this user. */ - public function getLanguage(): string + public function getLocale(): LocaleDefinition { - return app()->make(LanguageManager::class)->getLanguageForUser($this); + return app()->make(LocaleManager::class)->getForUser($this); } /** @@ -381,7 +371,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon */ public function refreshSlug(): string { - $this->slug = app(SlugGenerator::class)->generate($this); + $this->slug = app()->make(SlugGenerator::class)->generate($this); return $this->slug; }