1 <?php namespace BookStack\Auth;
3 use BookStack\Actions\Activity;
4 use BookStack\Api\ApiToken;
5 use BookStack\Interfaces\Loggable;
7 use BookStack\Notifications\ResetPassword;
8 use BookStack\Uploads\Image;
10 use Illuminate\Auth\Authenticatable;
11 use Illuminate\Auth\Passwords\CanResetPassword;
12 use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
13 use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
14 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
15 use Illuminate\Database\Eloquent\Relations\HasMany;
16 use Illuminate\Database\Eloquent\Relations\HasOne;
17 use Illuminate\Notifications\Notifiable;
21 * @property string $id
22 * @property string $name
23 * @property string $email
24 * @property string $password
25 * @property Carbon $created_at
26 * @property Carbon $updated_at
27 * @property bool $email_confirmed
28 * @property int $image_id
29 * @property string $external_auth_id
30 * @property string $system_name
32 class User extends Model implements AuthenticatableContract, CanResetPasswordContract, Loggable
34 use Authenticatable, CanResetPassword, Notifiable;
37 * The database table used by the model.
40 protected $table = 'users';
43 * The attributes that are mass assignable.
46 protected $fillable = ['name', 'email'];
49 * The attributes excluded from the model's JSON form.
53 'password', 'remember_token', 'system_name', 'email_confirmed', 'external_auth_id', 'email',
54 'created_at', 'updated_at', 'image_id',
58 * This holds the user's permissions when loaded.
61 protected $permissions;
64 * This holds the default user when loaded.
67 protected static $defaultUser = null;
70 * Returns the default public user.
73 public static function getDefault()
75 if (!is_null(static::$defaultUser)) {
76 return static::$defaultUser;
79 static::$defaultUser = static::where('system_name', '=', 'public')->first();
80 return static::$defaultUser;
84 * Check if the user is the default public user.
87 public function isDefault()
89 return $this->system_name === 'public';
93 * The roles that belong to the user.
94 * @return BelongsToMany
96 public function roles()
98 if ($this->id === 0) {
101 return $this->belongsToMany(Role::class);
105 * Check if the user has a role.
107 public function hasRole($roleId): bool
109 return $this->roles->pluck('id')->contains($roleId);
113 * Check if the user has a role.
117 public function hasSystemRole($role)
119 return $this->roles->pluck('system_name')->contains($role);
123 * Attach the default system role to this user.
125 public function attachDefaultRole(): void
127 $roleId = setting('registration-role');
128 if ($roleId && $this->roles()->where('id', '=', $roleId)->count() === 0) {
129 $this->roles()->attach($roleId);
134 * Get all permissions belonging to a the current user.
136 * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
138 public function permissions($cache = true)
140 if (isset($this->permissions) && $cache) {
141 return $this->permissions;
143 $this->load('roles.permissions');
144 $permissions = $this->roles->map(function ($role) {
145 return $role->permissions;
146 })->flatten()->unique();
147 $this->permissions = $permissions;
152 * Check if the user has a particular permission.
153 * @param $permissionName
156 public function can($permissionName)
158 if ($this->email === 'guest') {
161 return $this->permissions()->pluck('name')->contains($permissionName);
165 * Attach a role to this user.
167 public function attachRole(Role $role)
169 $this->roles()->attach($role->id);
173 * Get the social account associated with this user.
174 * @return \Illuminate\Database\Eloquent\Relations\HasMany
176 public function socialAccounts()
178 return $this->hasMany(SocialAccount::class);
182 * Check if the user has a social account,
183 * If a driver is passed it checks for that single account type.
184 * @param bool|string $socialDriver
187 public function hasSocialAccount($socialDriver = false)
189 if ($socialDriver === false) {
190 return $this->socialAccounts()->count() > 0;
193 return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
197 * Returns the user's avatar,
201 public function getAvatar($size = 50)
203 $default = url('/user_avatar.png');
204 $imageId = $this->image_id;
205 if ($imageId === 0 || $imageId === '0' || $imageId === null) {
210 $avatar = $this->avatar ? url($this->avatar->getThumb($size, $size, false)) : $default;
211 } catch (\Exception $err) {
218 * Get the avatar for the user.
219 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
221 public function avatar()
223 return $this->belongsTo(Image::class, 'image_id');
227 * Get the API tokens assigned to this user.
229 public function apiTokens(): HasMany
231 return $this->hasMany(ApiToken::class);
235 * Get the latest activity instance for this user.
237 public function latestActivity(): HasOne
239 return $this->hasOne(Activity::class)->latest();
243 * Get the url for editing this user.
245 public function getEditUrl(string $path = ''): string
247 $uri = '/settings/users/' . $this->id . '/' . trim($path, '/');
248 return url(rtrim($uri, '/'));
252 * Get the url that links to this user's profile.
254 public function getProfileUrl(): string
256 return url('/user/' . $this->id);
260 * Get a shortened version of the user's name.
264 public function getShortName($chars = 8)
266 if (mb_strlen($this->name) <= $chars) {
270 $splitName = explode(' ', $this->name);
271 if (mb_strlen($splitName[0]) <= $chars) {
272 return $splitName[0];
279 * Send the password reset notification.
280 * @param string $token
283 public function sendPasswordResetNotification($token)
285 $this->notify(new ResetPassword($token));
291 public function logDescriptor(): string
293 return "({$this->id}) {$this->name}";