1 <?php namespace BookStack\Auth;
3 use BookStack\Api\ApiToken;
5 use BookStack\Notifications\ResetPassword;
6 use BookStack\Uploads\Image;
8 use Illuminate\Auth\Authenticatable;
9 use Illuminate\Auth\Passwords\CanResetPassword;
10 use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
11 use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
12 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
13 use Illuminate\Database\Eloquent\Relations\HasMany;
14 use Illuminate\Notifications\Notifiable;
18 * @package BookStack\Auth
19 * @property string $id
20 * @property string $name
21 * @property string $email
22 * @property string $password
23 * @property Carbon $created_at
24 * @property Carbon $updated_at
25 * @property bool $email_confirmed
26 * @property int $image_id
27 * @property string $external_auth_id
28 * @property string $system_name
30 class User extends Model implements AuthenticatableContract, CanResetPasswordContract
32 use Authenticatable, CanResetPassword, Notifiable;
35 * The database table used by the model.
38 protected $table = 'users';
41 * The attributes that are mass assignable.
44 protected $fillable = ['name', 'email'];
47 * The attributes excluded from the model's JSON form.
50 protected $hidden = ['password', 'remember_token', 'system_name', 'email_confirmed', 'external_auth_id', 'email'];
53 * This holds the user's permissions when loaded.
56 protected $permissions;
59 * This holds the default user when loaded.
62 protected static $defaultUser = null;
65 * Returns the default public user.
68 public static function getDefault()
70 if (!is_null(static::$defaultUser)) {
71 return static::$defaultUser;
74 static::$defaultUser = static::where('system_name', '=', 'public')->first();
75 return static::$defaultUser;
79 * Check if the user is the default public user.
82 public function isDefault()
84 return $this->system_name === 'public';
88 * The roles that belong to the user.
89 * @return BelongsToMany
91 public function roles()
93 if ($this->id === 0) {
96 return $this->belongsToMany(Role::class);
100 * Check if the user has a role.
104 public function hasRole($role)
106 return $this->roles->pluck('name')->contains($role);
110 * Check if the user has a role.
114 public function hasSystemRole($role)
116 return $this->roles->pluck('system_name')->contains($role);
120 * Attach the default system role to this user.
122 public function attachDefaultRole(): void
124 $roleId = setting('registration-role');
125 if ($roleId && $this->roles()->where('id', '=', $roleId)->count() === 0) {
126 $this->roles()->attach($roleId);
131 * Get all permissions belonging to a the current user.
133 * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
135 public function permissions($cache = true)
137 if (isset($this->permissions) && $cache) {
138 return $this->permissions;
140 $this->load('roles.permissions');
141 $permissions = $this->roles->map(function ($role) {
142 return $role->permissions;
143 })->flatten()->unique();
144 $this->permissions = $permissions;
149 * Check if the user has a particular permission.
150 * @param $permissionName
153 public function can($permissionName)
155 if ($this->email === 'guest') {
158 return $this->permissions()->pluck('name')->contains($permissionName);
162 * Attach a role to this user.
165 public function attachRole(Role $role)
167 $this->roles()->attach($role->id);
171 * Get the social account associated with this user.
172 * @return \Illuminate\Database\Eloquent\Relations\HasMany
174 public function socialAccounts()
176 return $this->hasMany(SocialAccount::class);
180 * Check if the user has a social account,
181 * If a driver is passed it checks for that single account type.
182 * @param bool|string $socialDriver
185 public function hasSocialAccount($socialDriver = false)
187 if ($socialDriver === false) {
188 return $this->socialAccounts()->count() > 0;
191 return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
195 * Returns the user's avatar,
199 public function getAvatar($size = 50)
201 $default = url('/user_avatar.png');
202 $imageId = $this->image_id;
203 if ($imageId === 0 || $imageId === '0' || $imageId === null) {
208 $avatar = $this->avatar ? url($this->avatar->getThumb($size, $size, false)) : $default;
209 } catch (\Exception $err) {
216 * Get the avatar for the user.
217 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
219 public function avatar()
221 return $this->belongsTo(Image::class, 'image_id');
225 * Get the API tokens assigned to this user.
227 public function apiTokens(): HasMany
229 return $this->hasMany(ApiToken::class);
233 * Get the url for editing this user.
235 public function getEditUrl(string $path = ''): string
237 $uri = '/settings/users/' . $this->id . '/' . trim($path, '/');
238 return url(rtrim($uri, '/'));
242 * Get the url that links to this user's profile.
244 public function getProfileUrl(): string
246 return url('/user/' . $this->id);
250 * Get a shortened version of the user's name.
254 public function getShortName($chars = 8)
256 if (mb_strlen($this->name) <= $chars) {
260 $splitName = explode(' ', $this->name);
261 if (mb_strlen($splitName[0]) <= $chars) {
262 return $splitName[0];
269 * Send the password reset notification.
270 * @param string $token
273 public function sendPasswordResetNotification($token)
275 $this->notify(new ResetPassword($token));