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.
51 'password', 'remember_token', 'system_name', 'email_confirmed', 'external_auth_id', 'email',
52 'created_at', 'updated_at', 'image_id',
56 * This holds the user's permissions when loaded.
59 protected $permissions;
62 * This holds the default user when loaded.
65 protected static $defaultUser = null;
68 * Returns the default public user.
71 public static function getDefault()
73 if (!is_null(static::$defaultUser)) {
74 return static::$defaultUser;
77 static::$defaultUser = static::where('system_name', '=', 'public')->first();
78 return static::$defaultUser;
82 * Check if the user is the default public user.
85 public function isDefault()
87 return $this->system_name === 'public';
91 * The roles that belong to the user.
92 * @return BelongsToMany
94 public function roles()
96 if ($this->id === 0) {
99 return $this->belongsToMany(Role::class);
103 * Check if the user has a role.
105 public function hasRole($roleId): bool
107 return $this->roles->pluck('id')->contains($roleId);
111 * Check if the user has a role.
115 public function hasSystemRole($role)
117 return $this->roles->pluck('system_name')->contains($role);
121 * Attach the default system role to this user.
123 public function attachDefaultRole(): void
125 $roleId = setting('registration-role');
126 if ($roleId && $this->roles()->where('id', '=', $roleId)->count() === 0) {
127 $this->roles()->attach($roleId);
132 * Get all permissions belonging to a the current user.
134 * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
136 public function permissions($cache = true)
138 if (isset($this->permissions) && $cache) {
139 return $this->permissions;
141 $this->load('roles.permissions');
142 $permissions = $this->roles->map(function ($role) {
143 return $role->permissions;
144 })->flatten()->unique();
145 $this->permissions = $permissions;
150 * Check if the user has a particular permission.
151 * @param $permissionName
154 public function can($permissionName)
156 if ($this->email === 'guest') {
159 return $this->permissions()->pluck('name')->contains($permissionName);
163 * Attach a role to this user.
165 public function attachRole(Role $role)
167 $this->roles()->attach($role->id);
171 * Get the social account associated with this user.
172 * @return \Illuminate\Database\Eloquent\Relations\HasMany
174 public function socialAccounts()
176 return $this->hasMany(SocialAccount::class);
180 * Check if the user has a social account,
181 * If a driver is passed it checks for that single account type.
182 * @param bool|string $socialDriver
185 public function hasSocialAccount($socialDriver = false)
187 if ($socialDriver === false) {
188 return $this->socialAccounts()->count() > 0;
191 return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
195 * Returns the user's avatar,
199 public function getAvatar($size = 50)
201 $default = url('/user_avatar.png');
202 $imageId = $this->image_id;
203 if ($imageId === 0 || $imageId === '0' || $imageId === null) {
208 $avatar = $this->avatar ? url($this->avatar->getThumb($size, $size, false)) : $default;
209 } catch (\Exception $err) {
216 * Get the avatar for the user.
217 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
219 public function avatar()
221 return $this->belongsTo(Image::class, 'image_id');
225 * Get the API tokens assigned to this user.
227 public function apiTokens(): HasMany
229 return $this->hasMany(ApiToken::class);
233 * Get the url for editing this user.
235 public function getEditUrl(string $path = ''): string
237 $uri = '/settings/users/' . $this->id . '/' . trim($path, '/');
238 return url(rtrim($uri, '/'));
242 * Get the url that links to this user's profile.
244 public function getProfileUrl(): string
246 return url('/user/' . $this->id);
250 * Get a shortened version of the user's name.
254 public function getShortName($chars = 8)
256 if (mb_strlen($this->name) <= $chars) {
260 $splitName = explode(' ', $this->name);
261 if (mb_strlen($splitName[0]) <= $chars) {
262 return $splitName[0];
269 * Send the password reset notification.
270 * @param string $token
273 public function sendPasswordResetNotification($token)
275 $this->notify(new ResetPassword($token));