X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/3acea12f1c0013be4f1e3994cae2ea662e43bb4e..refs/pull/2393/head:/app/Auth/User.php diff --git a/app/Auth/User.php b/app/Auth/User.php index 7ad14d9f0..32179a1fb 100644 --- a/app/Auth/User.php +++ b/app/Auth/User.php @@ -1,5 +1,8 @@ first(); + if (!is_null(static::$defaultUser)) { + return static::$defaultUser; + } + + static::$defaultUser = static::where('system_name', '=', 'public')->first(); + return static::$defaultUser; } /** @@ -85,12 +103,10 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon /** * Check if the user has a role. - * @param $role - * @return mixed */ - public function hasRole($role) + public function hasRole($roleId): bool { - return $this->roles->pluck('name')->contains($role); + return $this->roles->pluck('id')->contains($roleId); } /** @@ -103,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 @@ -136,20 +163,10 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon /** * Attach a role to this user. - * @param Role $role */ 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); } /** @@ -206,20 +223,35 @@ 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 latest activity instance for this user. + */ + public function latestActivity(): HasOne + { + return $this->hasOne(Activity::class)->latest(); + } + /** * Get the url for editing this user. - * @return string */ - public function getEditUrl() + public function getEditUrl(string $path = ''): string { - return url('/http/source.bookstackapp.com/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 url('/http/source.bookstackapp.com/user/' . $this->id); } @@ -252,4 +284,12 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon { $this->notify(new ResetPassword($token)); } + + /** + * @inheritdoc + */ + public function logDescriptor(): string + { + return "({$this->id}) {$this->name}"; + } }