1 <?php namespace BookStack;
3 use Illuminate\Auth\Authenticatable;
4 use Illuminate\Auth\Passwords\CanResetPassword;
5 use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
6 use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
8 class User extends Model implements AuthenticatableContract, CanResetPasswordContract
10 use Authenticatable, CanResetPassword;
13 * The database table used by the model.
16 protected $table = 'users';
19 * The attributes that are mass assignable.
22 protected $fillable = ['name', 'email', 'image_id'];
25 * The attributes excluded from the model's JSON form.
28 protected $hidden = ['password', 'remember_token'];
31 * This holds the user's permissions when loaded.
34 protected $permissions;
37 * Returns a default guest user.
39 public static function getDefault()
48 * The roles that belong to the user.
50 public function roles()
52 return $this->belongsToMany(Role::class);
56 * Check if the user has a role.
60 public function hasRole($role)
62 return $this->roles->pluck('name')->contains($role);
66 * Get all permissions belonging to a the current user.
68 * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
70 public function permissions($cache = true)
72 if(isset($this->permissions) && $cache) return $this->permissions;
73 $this->load('roles.permissions');
74 $permissions = $this->roles->map(function($role) {
75 return $role->permissions;
76 })->flatten()->unique();
77 $this->permissions = $permissions;
82 * Check if the user has a particular permission.
83 * @param $permissionName
86 public function can($permissionName)
88 if ($this->email === 'guest') return false;
89 return $this->permissions()->pluck('name')->contains($permissionName);
93 * Attach a role to this user.
96 public function attachRole(Role $role)
98 $this->attachRoleId($role->id);
102 * Attach a role id to this user.
105 public function attachRoleId($id)
107 $this->roles()->attach($id);
111 * Get the social account associated with this user.
112 * @return \Illuminate\Database\Eloquent\Relations\HasMany
114 public function socialAccounts()
116 return $this->hasMany(SocialAccount::class);
120 * Check if the user has a social account,
121 * If a driver is passed it checks for that single account type.
122 * @param bool|string $socialDriver
125 public function hasSocialAccount($socialDriver = false)
127 if ($socialDriver === false) {
128 return $this->socialAccounts()->count() > 0;
131 return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
135 * Returns the user's avatar,
139 public function getAvatar($size = 50)
141 if ($this->image_id === 0 || $this->image_id === '0' || $this->image_id === null) return baseUrl('/user_avatar.png');
142 return baseUrl($this->avatar->getThumb($size, $size, false));
146 * Get the avatar for the user.
147 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
149 public function avatar()
151 return $this->belongsTo(Image::class, 'image_id');
155 * Get the url for editing this user.
158 public function getEditUrl()
160 return baseUrl('/settings/users/' . $this->id);
164 * Get the url that links to this user's profile.
167 public function getProfileUrl()
169 return baseUrl('/user/' . $this->id);
173 * Get a shortened version of the user's name.
177 public function getShortName($chars = 8)
179 if (strlen($this->name) <= $chars) return $this->name;
181 $splitName = explode(' ', $this->name);
182 if (strlen($splitName[0]) <= $chars) return $splitName[0];