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) {
66 return $this->belongsToMany(Role::class);
70 * Check if the user has a role.
74 public function hasRole($role)
76 return $this->roles->pluck('name')->contains($role);
80 * Check if the user has a role.
84 public function hasSystemRole($role)
86 return $this->roles->pluck('system_name')->contains($role);
90 * Get all permissions belonging to a the current user.
92 * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
94 public function permissions($cache = true)
96 if (isset($this->permissions) && $cache) {
97 return $this->permissions;
99 $this->load('roles.permissions');
100 $permissions = $this->roles->map(function ($role) {
101 return $role->permissions;
102 })->flatten()->unique();
103 $this->permissions = $permissions;
108 * Check if the user has a particular permission.
109 * @param $permissionName
112 public function can($permissionName)
114 if ($this->email === 'guest') {
117 return $this->permissions()->pluck('name')->contains($permissionName);
121 * Attach a role to this user.
124 public function attachRole(Role $role)
126 $this->attachRoleId($role->id);
130 * Attach a role id to this user.
133 public function attachRoleId($id)
135 $this->roles()->attach($id);
139 * Get the social account associated with this user.
140 * @return \Illuminate\Database\Eloquent\Relations\HasMany
142 public function socialAccounts()
144 return $this->hasMany(SocialAccount::class);
148 * Check if the user has a social account,
149 * If a driver is passed it checks for that single account type.
150 * @param bool|string $socialDriver
153 public function hasSocialAccount($socialDriver = false)
155 if ($socialDriver === false) {
156 return $this->socialAccounts()->count() > 0;
159 return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
163 * Returns the user's avatar,
167 public function getAvatar($size = 50)
169 $default = baseUrl('/user_avatar.png');
170 $imageId = $this->image_id;
171 if ($imageId === 0 || $imageId === '0' || $imageId === null) {
176 $avatar = $this->avatar ? baseUrl($this->avatar->getThumb($size, $size, false)) : $default;
177 } catch (\Exception $err) {
184 * Get the avatar for the user.
185 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
187 public function avatar()
189 return $this->belongsTo(Image::class, 'image_id');
193 * Get the url for editing this user.
196 public function getEditUrl()
198 return baseUrl('/settings/users/' . $this->id);
202 * Get the url that links to this user's profile.
205 public function getProfileUrl()
207 return baseUrl('/user/' . $this->id);
211 * Get a shortened version of the user's name.
215 public function getShortName($chars = 8)
217 if (strlen($this->name) <= $chars) {
221 $splitName = explode(' ', $this->name);
222 if (strlen($splitName[0]) <= $chars) {
223 return $splitName[0];
230 * Send the password reset notification.
231 * @param string $token
234 public function sendPasswordResetNotification($token)
236 $this->notify(new ResetPassword($token));