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 * This holds the user's permissions when loaded.
40 protected $permissions;
43 * Returns a default guest user.
45 public static function getDefault()
54 * Permissions and roles
58 * The roles that belong to the user.
60 public function roles()
62 return $this->belongsToMany('BookStack\Role');
65 public function getRoleAttribute()
67 return $this->roles()->with('permissions')->first();
71 * Loads the user's permissions from thier role.
73 private function loadPermissions()
75 if (isset($this->permissions)) return;
76 $this->load('roles.permissions');
77 $permissions = $this->roles[0]->permissions;
78 $permissionsArray = $permissions->pluck('name')->all();
79 $this->permissions = $permissionsArray;
83 * Check if the user has a particular permission.
84 * @param $permissionName
87 public function can($permissionName)
89 if ($this->email == 'guest') {
92 $this->loadPermissions();
93 return array_search($permissionName, $this->permissions) !== false;
97 * Attach a role to this user.
100 public function attachRole(Role $role)
102 $this->attachRoleId($role->id);
106 * Attach a role id to this user.
109 public function attachRoleId($id)
111 $this->roles()->sync([$id]);
115 * Get the social account associated with this user.
117 * @return \Illuminate\Database\Eloquent\Relations\HasMany
119 public function socialAccounts()
121 return $this->hasMany('BookStack\SocialAccount');
125 * Check if the user has a social account,
126 * If a driver is passed it checks for that single account type.
127 * @param bool|string $socialDriver
130 public function hasSocialAccount($socialDriver = false)
132 if ($socialDriver === false) {
133 return $this->socialAccounts()->count() > 0;
136 return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
140 * Returns the user's avatar,
141 * Uses Gravatar as the avatar service.
146 public function getAvatar($size = 50)
148 $emailHash = md5(strtolower(trim($this->email)));
149 return '//www.gravatar.com/avatar/' . $emailHash . '?s=' . $size . '&d=identicon';
153 * Get the url for editing this user.
156 public function getEditUrl()
158 return '/users/' . $this->id;