1 <?php namespace BookStack;
3 use BookStack\Notifications\ResetPassword;
4 use Illuminate\Auth\Authenticatable;
5 use Illuminate\Auth\Passwords\CanResetPassword;
6 use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
7 use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
8 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9 use Illuminate\Notifications\Notifiable;
11 class User extends Model implements AuthenticatableContract, CanResetPasswordContract
13 use Authenticatable, CanResetPassword, Notifiable;
16 * The database table used by the model.
19 protected $table = 'users';
22 * The attributes that are mass assignable.
25 protected $fillable = ['name', 'email', 'image_id'];
28 * The attributes excluded from the model's JSON form.
31 protected $hidden = ['password', 'remember_token'];
34 * This holds the user's permissions when loaded.
37 protected $permissions;
40 * Returns the default public user.
43 public static function getDefault()
45 return static::where('system_name', '=', 'public')->first();
49 * Check if the user is the default public user.
52 public function isDefault()
54 return $this->system_name === 'public';
58 * The roles that belong to the user.
59 * @return BelongsToMany
61 public function roles()
63 if ($this->id === 0) return ;
64 return $this->belongsToMany(Role::class);
68 * Check if the user has a role.
72 public function hasRole($role)
74 return $this->roles->pluck('name')->contains($role);
78 * Get all permissions belonging to a the current user.
80 * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
82 public function permissions($cache = true)
84 if(isset($this->permissions) && $cache) return $this->permissions;
85 $this->load('roles.permissions');
86 $permissions = $this->roles->map(function($role) {
87 return $role->permissions;
88 })->flatten()->unique();
89 $this->permissions = $permissions;
94 * Check if the user has a particular permission.
95 * @param $permissionName
98 public function can($permissionName)
100 if ($this->email === 'guest') return false;
101 return $this->permissions()->pluck('name')->contains($permissionName);
105 * Attach a role to this user.
108 public function attachRole(Role $role)
110 $this->attachRoleId($role->id);
114 * Attach a role id to this user.
117 public function attachRoleId($id)
119 $this->roles()->attach($id);
123 * Get the social account associated with this user.
124 * @return \Illuminate\Database\Eloquent\Relations\HasMany
126 public function socialAccounts()
128 return $this->hasMany(SocialAccount::class);
132 * Check if the user has a social account,
133 * If a driver is passed it checks for that single account type.
134 * @param bool|string $socialDriver
137 public function hasSocialAccount($socialDriver = false)
139 if ($socialDriver === false) {
140 return $this->socialAccounts()->count() > 0;
143 return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
147 * Returns the user's avatar,
151 public function getAvatar($size = 50)
153 if ($this->image_id === 0 || $this->image_id === '0' || $this->image_id === null) return baseUrl('/user_avatar.png');
154 return baseUrl($this->avatar->getThumb($size, $size, false));
158 * Get the avatar for the user.
159 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
161 public function avatar()
163 return $this->belongsTo(Image::class, 'image_id');
167 * Get the url for editing this user.
170 public function getEditUrl()
172 return baseUrl('/settings/users/' . $this->id);
176 * Get the url that links to this user's profile.
179 public function getProfileUrl()
181 return baseUrl('/user/' . $this->id);
185 * Get a shortened version of the user's name.
189 public function getShortName($chars = 8)
191 if (strlen($this->name) <= $chars) return $this->name;
193 $splitName = explode(' ', $this->name);
194 if (strlen($splitName[0]) <= $chars) return $splitName[0];
200 * Send the password reset notification.
201 * @param string $token
204 public function sendPasswordResetNotification($token)
206 $this->notify(new ResetPassword($token));