5 use Illuminate\Auth\Authenticatable;
6 use Illuminate\Database\Eloquent\Model;
7 use Illuminate\Auth\Passwords\CanResetPassword;
8 use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
9 use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
11 class User extends Model implements AuthenticatableContract, CanResetPasswordContract
13 use Authenticatable, CanResetPassword;
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 a default guest user.
42 public static function getDefault()
51 * The roles that belong to the user.
53 public function roles()
55 return $this->belongsToMany('BookStack\Role');
59 * Check if the user has a role.
63 public function hasRole($role)
65 return $this->roles->pluck('name')->contains($role);
69 * Get all permissions belonging to a the current user.
71 * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
73 public function permissions($cache = true)
75 if(isset($this->permissions) && $cache) return $this->permissions;
76 $this->load('roles.permissions');
77 $permissions = $this->roles->map(function($role) {
78 return $role->permissions;
79 })->flatten()->unique();
80 $this->permissions = $permissions;
85 * Check if the user has a particular permission.
86 * @param $permissionName
89 public function can($permissionName)
91 if ($this->email === 'guest') return false;
92 return $this->permissions()->pluck('name')->contains($permissionName);
96 * Attach a role to this user.
99 public function attachRole(Role $role)
101 $this->attachRoleId($role->id);
105 * Attach a role id to this user.
108 public function attachRoleId($id)
110 $this->roles()->attach($id);
114 * Get the social account associated with this user.
115 * @return \Illuminate\Database\Eloquent\Relations\HasMany
117 public function socialAccounts()
119 return $this->hasMany('BookStack\SocialAccount');
123 * Check if the user has a social account,
124 * If a driver is passed it checks for that single account type.
125 * @param bool|string $socialDriver
128 public function hasSocialAccount($socialDriver = false)
130 if ($socialDriver === false) {
131 return $this->socialAccounts()->count() > 0;
134 return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
138 * Returns the user's avatar,
142 public function getAvatar($size = 50)
144 if ($this->image_id === 0 || $this->image_id === '0' || $this->image_id === null) return '/user_avatar.png';
145 return $this->avatar->getThumb($size, $size, false);
149 * Get the avatar for the user.
150 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
152 public function avatar()
154 return $this->belongsTo('BookStack\Image', 'image_id');
158 * Get the url for editing this user.
161 public function getEditUrl()
163 return '/settings/users/' . $this->id;