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 * Check if the user has a role.
82 public function hasSystemRole($role)
84 return $this->roles->pluck('system_name')->contains('admin');
88 * Get all permissions belonging to a the current user.
90 * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
92 public function permissions($cache = true)
94 if(isset($this->permissions) && $cache) return $this->permissions;
95 $this->load('roles.permissions');
96 $permissions = $this->roles->map(function($role) {
97 return $role->permissions;
98 })->flatten()->unique();
99 $this->permissions = $permissions;
104 * Check if the user has a particular permission.
105 * @param $permissionName
108 public function can($permissionName)
110 if ($this->email === 'guest') return false;
111 return $this->permissions()->pluck('name')->contains($permissionName);
115 * Attach a role to this user.
118 public function attachRole(Role $role)
120 $this->attachRoleId($role->id);
124 * Attach a role id to this user.
127 public function attachRoleId($id)
129 $this->roles()->attach($id);
133 * Get the social account associated with this user.
134 * @return \Illuminate\Database\Eloquent\Relations\HasMany
136 public function socialAccounts()
138 return $this->hasMany(SocialAccount::class);
142 * Check if the user has a social account,
143 * If a driver is passed it checks for that single account type.
144 * @param bool|string $socialDriver
147 public function hasSocialAccount($socialDriver = false)
149 if ($socialDriver === false) {
150 return $this->socialAccounts()->count() > 0;
153 return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
157 * Returns the user's avatar,
161 public function getAvatar($size = 50)
163 $default = baseUrl('/user_avatar.png');
164 $imageId = $this->image_id;
165 if ($imageId === 0 || $imageId === '0' || $imageId === null) return $default;
168 $avatar = $this->avatar ? baseUrl($this->avatar->getThumb($size, $size, false)) : $default;
169 } catch (\Exception $err) {
176 * Get the avatar for the user.
177 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
179 public function avatar()
181 return $this->belongsTo(Image::class, 'image_id');
185 * Get the url for editing this user.
188 public function getEditUrl()
190 return baseUrl('/settings/users/' . $this->id);
194 * Get the url that links to this user's profile.
197 public function getProfileUrl()
199 return baseUrl('/user/' . $this->id);
203 * Get a shortened version of the user's name.
207 public function getShortName($chars = 8)
209 if (strlen($this->name) <= $chars) return $this->name;
211 $splitName = explode(' ', $this->name);
212 if (strlen($splitName[0]) <= $chars) return $splitName[0];
218 * Send the password reset notification.
219 * @param string $token
222 public function sendPasswordResetNotification($token)
224 $this->notify(new ResetPassword($token));