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 * This holds the default user when loaded.
60 protected static $defaultUser = null;
63 * Returns the default public user.
66 public static function getDefault()
68 if (!is_null(static::$defaultUser)) {
69 return static::$defaultUser;
72 static::$defaultUser = static::where('system_name', '=', 'public')->first();
73 return static::$defaultUser;
77 * Check if the user is the default public user.
80 public function isDefault()
82 return $this->system_name === 'public';
86 * The roles that belong to the user.
87 * @return BelongsToMany
89 public function roles()
91 if ($this->id === 0) {
94 return $this->belongsToMany(Role::class);
98 * Check if the user has a role.
102 public function hasRole($role)
104 return $this->roles->pluck('name')->contains($role);
108 * Check if the user has a role.
112 public function hasSystemRole($role)
114 return $this->roles->pluck('system_name')->contains($role);
118 * Get all permissions belonging to a the current user.
120 * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
122 public function permissions($cache = true)
124 if (isset($this->permissions) && $cache) {
125 return $this->permissions;
127 $this->load('roles.permissions');
128 $permissions = $this->roles->map(function ($role) {
129 return $role->permissions;
130 })->flatten()->unique();
131 $this->permissions = $permissions;
136 * Check if the user has a particular permission.
137 * @param $permissionName
140 public function can($permissionName)
142 if ($this->email === 'guest') {
145 return $this->permissions()->pluck('name')->contains($permissionName);
149 * Attach a role to this user.
152 public function attachRole(Role $role)
154 $this->attachRoleId($role->id);
158 * Attach a role id to this user.
161 public function attachRoleId($id)
163 $this->roles()->attach($id);
167 * Get the social account associated with this user.
168 * @return \Illuminate\Database\Eloquent\Relations\HasMany
170 public function socialAccounts()
172 return $this->hasMany(SocialAccount::class);
176 * Check if the user has a social account,
177 * If a driver is passed it checks for that single account type.
178 * @param bool|string $socialDriver
181 public function hasSocialAccount($socialDriver = false)
183 if ($socialDriver === false) {
184 return $this->socialAccounts()->count() > 0;
187 return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
191 * Returns the user's avatar,
195 public function getAvatar($size = 50)
197 $default = url('/user_avatar.png');
198 $imageId = $this->image_id;
199 if ($imageId === 0 || $imageId === '0' || $imageId === null) {
204 $avatar = $this->avatar ? url($this->avatar->getThumb($size, $size, false)) : $default;
205 } catch (\Exception $err) {
212 * Get the avatar for the user.
213 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
215 public function avatar()
217 return $this->belongsTo(Image::class, 'image_id');
221 * Get the url for editing this user.
224 public function getEditUrl()
226 return url('/settings/users/' . $this->id);
230 * Get the url that links to this user's profile.
233 public function getProfileUrl()
235 return url('/user/' . $this->id);
239 * Get a shortened version of the user's name.
243 public function getShortName($chars = 8)
245 if (mb_strlen($this->name) <= $chars) {
249 $splitName = explode(' ', $this->name);
250 if (mb_strlen($splitName[0]) <= $chars) {
251 return $splitName[0];
258 * Send the password reset notification.
259 * @param string $token
262 public function sendPasswordResetNotification($token)
264 $this->notify(new ResetPassword($token));