1 <?php namespace BookStack\Auth;
3 use BookStack\Api\ApiToken;
5 use BookStack\Notifications\ResetPassword;
6 use BookStack\Uploads\Image;
8 use Illuminate\Auth\Authenticatable;
9 use Illuminate\Auth\Passwords\CanResetPassword;
10 use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
11 use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
12 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
13 use Illuminate\Database\Eloquent\Relations\HasMany;
14 use Illuminate\Notifications\Notifiable;
18 * @package BookStack\Auth
19 * @property string $id
20 * @property string $name
21 * @property string $email
22 * @property string $password
23 * @property Carbon $created_at
24 * @property Carbon $updated_at
25 * @property bool $email_confirmed
26 * @property int $image_id
27 * @property string $external_auth_id
28 * @property string $system_name
30 class User extends Model implements AuthenticatableContract, CanResetPasswordContract
32 use Authenticatable, CanResetPassword, Notifiable;
35 * The database table used by the model.
38 protected $table = 'users';
41 * The attributes that are mass assignable.
44 protected $fillable = ['name', 'email'];
47 * The attributes excluded from the model's JSON form.
50 protected $hidden = ['password', 'remember_token', 'system_name', 'email_confirmed', 'external_auth_id', 'email'];
53 * This holds the user's permissions when loaded.
56 protected $permissions;
59 * This holds the default user when loaded.
62 protected static $defaultUser = null;
65 * Returns the default public user.
68 public static function getDefault()
70 if (!is_null(static::$defaultUser)) {
71 return static::$defaultUser;
74 static::$defaultUser = static::where('system_name', '=', 'public')->first();
75 return static::$defaultUser;
79 * Check if the user is the default public user.
82 public function isDefault()
84 return $this->system_name === 'public';
88 * The roles that belong to the user.
89 * @return BelongsToMany
91 public function roles()
93 if ($this->id === 0) {
96 return $this->belongsToMany(Role::class);
100 * Check if the user has a role.
104 public function hasRole($role)
106 return $this->roles->pluck('name')->contains($role);
110 * Check if the user has a role.
114 public function hasSystemRole($role)
116 return $this->roles->pluck('system_name')->contains($role);
120 * Get all permissions belonging to a the current user.
122 * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
124 public function permissions($cache = true)
126 if (isset($this->permissions) && $cache) {
127 return $this->permissions;
129 $this->load('roles.permissions');
130 $permissions = $this->roles->map(function ($role) {
131 return $role->permissions;
132 })->flatten()->unique();
133 $this->permissions = $permissions;
138 * Check if the user has a particular permission.
139 * @param $permissionName
142 public function can($permissionName)
144 if ($this->email === 'guest') {
147 return $this->permissions()->pluck('name')->contains($permissionName);
151 * Attach a role to this user.
154 public function attachRole(Role $role)
156 $this->attachRoleId($role->id);
160 * Attach a role id to this user.
163 public function attachRoleId($id)
165 $this->roles()->attach($id);
169 * Get the social account associated with this user.
170 * @return \Illuminate\Database\Eloquent\Relations\HasMany
172 public function socialAccounts()
174 return $this->hasMany(SocialAccount::class);
178 * Check if the user has a social account,
179 * If a driver is passed it checks for that single account type.
180 * @param bool|string $socialDriver
183 public function hasSocialAccount($socialDriver = false)
185 if ($socialDriver === false) {
186 return $this->socialAccounts()->count() > 0;
189 return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
193 * Returns the user's avatar,
197 public function getAvatar($size = 50)
199 $default = url('/user_avatar.png');
200 $imageId = $this->image_id;
201 if ($imageId === 0 || $imageId === '0' || $imageId === null) {
206 $avatar = $this->avatar ? url($this->avatar->getThumb($size, $size, false)) : $default;
207 } catch (\Exception $err) {
214 * Get the avatar for the user.
215 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
217 public function avatar()
219 return $this->belongsTo(Image::class, 'image_id');
223 * Get the API tokens assigned to this user.
225 public function apiTokens(): HasMany
227 return $this->hasMany(ApiToken::class);
231 * Get the url for editing this user.
233 public function getEditUrl(string $path = ''): string
235 $uri = '/settings/users/' . $this->id . '/' . trim($path, '/');
236 return url(rtrim($uri, '/'));
240 * Get the url that links to this user's profile.
242 public function getProfileUrl(): string
244 return url('/user/' . $this->id);
248 * Get a shortened version of the user's name.
252 public function getShortName($chars = 8)
254 if (mb_strlen($this->name) <= $chars) {
258 $splitName = explode(' ', $this->name);
259 if (mb_strlen($splitName[0]) <= $chars) {
260 return $splitName[0];
267 * Send the password reset notification.
268 * @param string $token
271 public function sendPasswordResetNotification($token)
273 $this->notify(new ResetPassword($token));