1 <?php namespace BookStack\Auth;
4 use BookStack\Notifications\ResetPassword;
5 use BookStack\Uploads\Image;
7 use Illuminate\Auth\Authenticatable;
8 use Illuminate\Auth\Passwords\CanResetPassword;
9 use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
10 use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
11 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
12 use Illuminate\Notifications\Notifiable;
16 * @package BookStack\Auth
17 * @property string $id
18 * @property string $name
19 * @property string $email
20 * @property string $password
21 * @property Carbon $created_at
22 * @property Carbon $updated_at
23 * @property bool $email_confirmed
24 * @property int $image_id
25 * @property string $external_auth_id
26 * @property string $system_name
28 class User extends Model implements AuthenticatableContract, CanResetPasswordContract
30 use Authenticatable, CanResetPassword, Notifiable;
33 * The database table used by the model.
36 protected $table = 'users';
39 * The attributes that are mass assignable.
42 protected $fillable = ['name', 'email'];
45 * The attributes excluded from the model's JSON form.
48 protected $hidden = ['password', 'remember_token'];
51 * This holds the user's permissions when loaded.
54 protected $permissions;
57 * Returns the default public user.
60 public static function getDefault()
62 return static::where('system_name', '=', 'public')->first();
66 * Check if the user is the default public user.
69 public function isDefault()
71 return $this->system_name === 'public';
75 * The roles that belong to the user.
76 * @return BelongsToMany
78 public function roles()
80 if ($this->id === 0) {
83 return $this->belongsToMany(Role::class);
87 * Check if the user has a role.
91 public function hasRole($role)
93 return $this->roles->pluck('name')->contains($role);
97 * Check if the user has a role.
101 public function hasSystemRole($role)
103 return $this->roles->pluck('system_name')->contains($role);
107 * Get all permissions belonging to a the current user.
109 * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
111 public function permissions($cache = true)
113 if (isset($this->permissions) && $cache) {
114 return $this->permissions;
116 $this->load('roles.permissions');
117 $permissions = $this->roles->map(function ($role) {
118 return $role->permissions;
119 })->flatten()->unique();
120 $this->permissions = $permissions;
125 * Check if the user has a particular permission.
126 * @param $permissionName
129 public function can($permissionName)
131 if ($this->email === 'guest') {
134 return $this->permissions()->pluck('name')->contains($permissionName);
138 * Attach a role to this user.
141 public function attachRole(Role $role)
143 $this->attachRoleId($role->id);
147 * Attach a role id to this user.
150 public function attachRoleId($id)
152 $this->roles()->attach($id);
156 * Get the social account associated with this user.
157 * @return \Illuminate\Database\Eloquent\Relations\HasMany
159 public function socialAccounts()
161 return $this->hasMany(SocialAccount::class);
165 * Check if the user has a social account,
166 * If a driver is passed it checks for that single account type.
167 * @param bool|string $socialDriver
170 public function hasSocialAccount($socialDriver = false)
172 if ($socialDriver === false) {
173 return $this->socialAccounts()->count() > 0;
176 return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
180 * Returns the user's avatar,
184 public function getAvatar($size = 50)
186 $default = url('/user_avatar.png');
187 $imageId = $this->image_id;
188 if ($imageId === 0 || $imageId === '0' || $imageId === null) {
193 $avatar = $this->avatar ? url($this->avatar->getThumb($size, $size, false)) : $default;
194 } catch (\Exception $err) {
201 * Get the avatar for the user.
202 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
204 public function avatar()
206 return $this->belongsTo(Image::class, 'image_id');
210 * Get the url for editing this user.
213 public function getEditUrl()
215 return url('/settings/users/' . $this->id);
219 * Get the url that links to this user's profile.
222 public function getProfileUrl()
224 return url('/user/' . $this->id);
228 * Get a shortened version of the user's name.
232 public function getShortName($chars = 8)
234 if (mb_strlen($this->name) <= $chars) {
238 $splitName = explode(' ', $this->name);
239 if (mb_strlen($splitName[0]) <= $chars) {
240 return $splitName[0];
247 * Send the password reset notification.
248 * @param string $token
251 public function sendPasswordResetNotification($token)
253 $this->notify(new ResetPassword($token));