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\Notifications\Notifiable;
10 class User extends Model implements AuthenticatableContract, CanResetPasswordContract
12 use Authenticatable, CanResetPassword, Notifiable;
15 * The database table used by the model.
18 protected $table = 'users';
21 * The attributes that are mass assignable.
24 protected $fillable = ['name', 'email', 'image_id'];
27 * The attributes excluded from the model's JSON form.
30 protected $hidden = ['password', 'remember_token'];
33 * This holds the user's permissions when loaded.
36 protected $permissions;
39 * Returns a default guest user.
41 public static function getDefault()
50 * The roles that belong to the user.
52 public function roles()
54 return $this->belongsToMany(Role::class);
58 * Check if the user has a role.
62 public function hasRole($role)
64 return $this->roles->pluck('name')->contains($role);
68 * Get all permissions belonging to a the current user.
70 * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
72 public function permissions($cache = true)
74 if(isset($this->permissions) && $cache) return $this->permissions;
75 $this->load('roles.permissions');
76 $permissions = $this->roles->map(function($role) {
77 return $role->permissions;
78 })->flatten()->unique();
79 $this->permissions = $permissions;
84 * Check if the user has a particular permission.
85 * @param $permissionName
88 public function can($permissionName)
90 if ($this->email === 'guest') return false;
91 return $this->permissions()->pluck('name')->contains($permissionName);
95 * Attach a role to this user.
98 public function attachRole(Role $role)
100 $this->attachRoleId($role->id);
104 * Attach a role id to this user.
107 public function attachRoleId($id)
109 $this->roles()->attach($id);
113 * Get the social account associated with this user.
114 * @return \Illuminate\Database\Eloquent\Relations\HasMany
116 public function socialAccounts()
118 return $this->hasMany(SocialAccount::class);
122 * Check if the user has a social account,
123 * If a driver is passed it checks for that single account type.
124 * @param bool|string $socialDriver
127 public function hasSocialAccount($socialDriver = false)
129 if ($socialDriver === false) {
130 return $this->socialAccounts()->count() > 0;
133 return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
137 * Returns the user's avatar,
141 public function getAvatar($size = 50)
143 if ($this->image_id === 0 || $this->image_id === '0' || $this->image_id === null) return baseUrl('/user_avatar.png');
144 return baseUrl($this->avatar->getThumb($size, $size, false));
148 * Get the avatar for the user.
149 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
151 public function avatar()
153 return $this->belongsTo(Image::class, 'image_id');
157 * Get the url for editing this user.
160 public function getEditUrl()
162 return baseUrl('/settings/users/' . $this->id);
166 * Get the url that links to this user's profile.
169 public function getProfileUrl()
171 return baseUrl('/user/' . $this->id);
175 * Get a shortened version of the user's name.
179 public function getShortName($chars = 8)
181 if (strlen($this->name) <= $chars) return $this->name;
183 $splitName = explode(' ', $this->name);
184 if (strlen($splitName[0]) <= $chars) return $splitName[0];
190 * Send the password reset notification.
191 * @param string $token
194 public function sendPasswordResetNotification($token)
196 $this->notify(new ResetPassword($token));