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.
20 protected $table = 'users';
23 * The attributes that are mass assignable.
27 protected $fillable = ['name', 'email', 'password'];
30 * The attributes excluded from the model's JSON form.
34 protected $hidden = ['password', 'remember_token'];
37 * Returns a default guest user.
39 public static function getDefault()
48 * Permissions and roles
52 * The roles that belong to the user.
54 public function roles()
56 return $this->belongsToMany('BookStack\Role');
59 public function getRoleAttribute()
61 return $this->roles()->first();
65 * Check if the user has a particular permission.
66 * @param $permissionName
69 public function can($permissionName)
71 if($this->email == 'guest') {
74 $permissions = $this->role->permissions()->get();
75 $permissionSearch = $permissions->search(function ($item, $key) use ($permissionName) {
76 return $item->name == $permissionName;
78 return $permissionSearch !== false;
82 * Attach a role to this user.
85 public function attachRole(Role $role)
87 $this->attachRoleId($role->id);
91 * Attach a role id to this user.
94 public function attachRoleId($id)
96 $this->roles()->sync([$id]);
100 * Get the social account associated with this user.
102 * @return \Illuminate\Database\Eloquent\Relations\HasMany
104 public function socialAccounts()
106 return $this->hasMany('BookStack\SocialAccount');
110 * Check if the user has a social account,
111 * If a driver is passed it checks for that single account type.
112 * @param bool|string $socialDriver
115 public function hasSocialAccount($socialDriver = false)
117 if($socialDriver === false) {
118 return $this->socialAccounts()->count() > 0;
121 return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
125 * Returns the user's avatar,
126 * Uses Gravatar as the avatar service.
131 public function getAvatar($size = 50)
133 $emailHash = md5(strtolower(trim($this->email)));
134 return '//www.gravatar.com/avatar/' . $emailHash . '?s=' . $size . '&d=identicon';
138 * Get the url for editing this user.
141 public function getEditUrl()
143 return '/users/' . $this->id;