X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/07b889547d28e68e5fc8f923c166bd607da17ad7..refs/pull/2023/head:/app/Auth/User.php diff --git a/app/Auth/User.php b/app/Auth/User.php index 12f022b06..a581d9993 100644 --- a/app/Auth/User.php +++ b/app/Auth/User.php @@ -1,15 +1,32 @@ first(); + if (!is_null(static::$defaultUser)) { + return static::$defaultUser; + } + + static::$defaultUser = static::where('system_name', '=', 'public')->first(); + return static::$defaultUser; } /** @@ -88,6 +119,17 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon return $this->roles->pluck('system_name')->contains($role); } + /** + * Attach the default system role to this user. + */ + public function attachDefaultRole(): void + { + $roleId = setting('registration-role'); + if ($roleId && $this->roles()->where('id', '=', $roleId)->count() === 0) { + $this->roles()->attach($roleId); + } + } + /** * Get all permissions belonging to a the current user. * @param bool $cache @@ -125,16 +167,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon */ public function attachRole(Role $role) { - $this->attachRoleId($role->id); - } - - /** - * Attach a role id to this user. - * @param $id - */ - public function attachRoleId($id) - { - $this->roles()->attach($id); + $this->roles()->attach($role->id); } /** @@ -168,14 +201,14 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon */ public function getAvatar($size = 50) { - $default = baseUrl('/user_avatar.png'); + $default = url('/http/source.bookstackapp.com/user_avatar.png'); $imageId = $this->image_id; if ($imageId === 0 || $imageId === '0' || $imageId === null) { return $default; } try { - $avatar = $this->avatar ? baseUrl($this->avatar->getThumb($size, $size, false)) : $default; + $avatar = $this->avatar ? url($this->avatar->getThumb($size, $size, false)) : $default; } catch (\Exception $err) { $avatar = $default; } @@ -191,22 +224,29 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon return $this->belongsTo(Image::class, 'image_id'); } + /** + * Get the API tokens assigned to this user. + */ + public function apiTokens(): HasMany + { + return $this->hasMany(ApiToken::class); + } + /** * Get the url for editing this user. - * @return string */ - public function getEditUrl() + public function getEditUrl(string $path = ''): string { - return baseUrl('/settings/users/' . $this->id); + $uri = '/settings/users/' . $this->id . '/' . trim($path, '/'); + return url(rtrim($uri, '/')); } /** * Get the url that links to this user's profile. - * @return mixed */ - public function getProfileUrl() + public function getProfileUrl(): string { - return baseUrl('/user/' . $this->id); + return url('/http/source.bookstackapp.com/user/' . $this->id); } /** @@ -216,12 +256,12 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon */ public function getShortName($chars = 8) { - if (strlen($this->name) <= $chars) { + if (mb_strlen($this->name) <= $chars) { return $this->name; } $splitName = explode(' ', $this->name); - if (strlen($splitName[0]) <= $chars) { + if (mb_strlen($splitName[0]) <= $chars) { return $splitName[0]; }