1 <?php namespace BookStack\Auth;
4 use BookStack\Notifications\ResetPassword;
5 use BookStack\Uploads\Image;
6 use Illuminate\Auth\Authenticatable;
7 use Illuminate\Auth\Passwords\CanResetPassword;
8 use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
9 use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
10 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
11 use Illuminate\Notifications\Notifiable;
13 class User extends Model implements AuthenticatableContract, CanResetPasswordContract
15 use Authenticatable, CanResetPassword, Notifiable;
18 * The database table used by the model.
21 protected $table = 'users';
24 * The attributes that are mass assignable.
27 protected $fillable = ['name', 'email'];
30 * The attributes excluded from the model's JSON form.
33 protected $hidden = ['password', 'remember_token'];
36 * This holds the user's permissions when loaded.
39 protected $permissions;
42 * Returns the default public user.
45 public static function getDefault()
47 return static::where('system_name', '=', 'public')->first();
51 * Check if the user is the default public user.
54 public function isDefault()
56 return $this->system_name === 'public';
60 * The roles that belong to the user.
61 * @return BelongsToMany
63 public function roles()
65 if ($this->id === 0) {
68 return $this->belongsToMany(Role::class);
72 * Check if the user has a role.
76 public function hasRole($role)
78 return $this->roles->pluck('name')->contains($role);
82 * Check if the user has a role.
86 public function hasSystemRole($role)
88 return $this->roles->pluck('system_name')->contains($role);
92 * Get all permissions belonging to a the current user.
94 * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
96 public function permissions($cache = true)
98 if (isset($this->permissions) && $cache) {
99 return $this->permissions;
101 $this->load('roles.permissions');
102 $permissions = $this->roles->map(function ($role) {
103 return $role->permissions;
104 })->flatten()->unique();
105 $this->permissions = $permissions;
110 * Check if the user has a particular permission.
111 * @param $permissionName
114 public function can($permissionName)
116 if ($this->email === 'guest') {
119 return $this->permissions()->pluck('name')->contains($permissionName);
123 * Attach a role to this user.
126 public function attachRole(Role $role)
128 $this->attachRoleId($role->id);
132 * Attach a role id to this user.
135 public function attachRoleId($id)
137 $this->roles()->attach($id);
141 * Get the social account associated with this user.
142 * @return \Illuminate\Database\Eloquent\Relations\HasMany
144 public function socialAccounts()
146 return $this->hasMany(SocialAccount::class);
150 * Check if the user has a social account,
151 * If a driver is passed it checks for that single account type.
152 * @param bool|string $socialDriver
155 public function hasSocialAccount($socialDriver = false)
157 if ($socialDriver === false) {
158 return $this->socialAccounts()->count() > 0;
161 return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
165 * Returns the user's avatar,
169 public function getAvatar($size = 50)
171 $default = baseUrl('/user_avatar.png');
172 $imageId = $this->image_id;
173 if ($imageId === 0 || $imageId === '0' || $imageId === null) {
178 $avatar = $this->avatar ? baseUrl($this->avatar->getThumb($size, $size, false)) : $default;
179 } catch (\Exception $err) {
186 * Get the avatar for the user.
187 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
189 public function avatar()
191 return $this->belongsTo(Image::class, 'image_id');
195 * Get the url for editing this user.
198 public function getEditUrl()
200 return baseUrl('/settings/users/' . $this->id);
204 * Get the url that links to this user's profile.
207 public function getProfileUrl()
209 return baseUrl('/user/' . $this->id);
213 * Get a shortened version of the user's name.
217 public function getShortName($chars = 8)
219 if (mb_strlen($this->name) <= $chars) {
223 $splitName = explode(' ', $this->name);
224 if (mb_strlen($splitName[0]) <= $chars) {
225 return $splitName[0];
232 * Send the password reset notification.
233 * @param string $token
236 public function sendPasswordResetNotification($token)
238 $this->notify(new ResetPassword($token));