]> BookStack Code Mirror - bookstack/blobdiff - app/Users/Models/User.php
Maintenance: Addressed a range of phpstan level 3 issues
[bookstack] / app / Users / Models / User.php
index 3efbeec70a87bed60ca700551799ebdabc9d04b3..f83e120887b1728d4853bb219fff4c8e9ef787ec 100644 (file)
@@ -10,9 +10,10 @@ use BookStack\Activity\Models\Loggable;
 use BookStack\Activity\Models\Watch;
 use BookStack\Api\ApiToken;
 use BookStack\App\Model;
-use BookStack\App\Sluggable;
+use BookStack\App\SluggableInterface;
 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;
@@ -25,6 +26,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
 use Illuminate\Database\Eloquent\Relations\HasMany;
+use Illuminate\Database\Eloquent\Relations\Relation;
 use Illuminate\Notifications\Notifiable;
 use Illuminate\Support\Collection;
 
@@ -44,8 +46,9 @@ use Illuminate\Support\Collection;
  * @property string     $system_name
  * @property Collection $roles
  * @property Collection $mfaValues
+ * @property ?Image     $avatar
  */
-class User extends Model implements AuthenticatableContract, CanResetPasswordContract, Loggable, Sluggable
+class User extends Model implements AuthenticatableContract, CanResetPasswordContract, Loggable, SluggableInterface
 {
     use HasFactory;
     use Authenticatable;
@@ -62,7 +65,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
     /**
      * The attributes that are mass assignable.
      *
-     * @var array
+     * @var list<string>
      */
     protected $fillable = ['name', 'email'];
 
@@ -71,7 +74,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
     /**
      * The attributes excluded from the model's JSON form.
      *
-     * @var array
+     * @var list<string>
      */
     protected $hidden = [
         'password', 'remember_token', 'system_name', 'email_confirmed', 'external_auth_id', 'email',
@@ -88,49 +91,38 @@ 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');
     }
 
     /**
      * The roles that belong to the user.
      *
-     * @return BelongsToMany
+     * @return BelongsToMany<Role, $this>
      */
-    public function roles()
+    public function roles(): BelongsToMany
     {
-        if ($this->id === 0) {
-            return;
-        }
-
         return $this->belongsToMany(Role::class);
     }
 
@@ -166,10 +158,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 +238,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;
         }
@@ -345,15 +333,15 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
             return $splitName[0];
         }
 
-        return mb_substr($this->name, 0, $chars-3) . '…';
+        return mb_substr($this->name, 0, max($chars - 2, 0)) . '…';
     }
 
     /**
-     * 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 +369,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, $this->name);
 
         return $this->slug;
     }