1 <?php namespace BookStack\Auth;
3 use BookStack\Api\ApiToken;
4 use BookStack\Interfaces\Loggable;
6 use BookStack\Notifications\ResetPassword;
7 use BookStack\Uploads\Image;
9 use Illuminate\Auth\Authenticatable;
10 use Illuminate\Auth\Passwords\CanResetPassword;
11 use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
12 use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
13 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
14 use Illuminate\Database\Eloquent\Relations\HasMany;
15 use Illuminate\Notifications\Notifiable;
19 * @package BookStack\Auth
20 * @property string $id
21 * @property string $name
22 * @property string $email
23 * @property string $password
24 * @property Carbon $created_at
25 * @property Carbon $updated_at
26 * @property bool $email_confirmed
27 * @property int $image_id
28 * @property string $external_auth_id
29 * @property string $system_name
31 class User extends Model implements AuthenticatableContract, CanResetPasswordContract, Loggable
33 use Authenticatable, CanResetPassword, Notifiable;
36 * The database table used by the model.
39 protected $table = 'users';
42 * The attributes that are mass assignable.
45 protected $fillable = ['name', 'email'];
48 * The attributes excluded from the model's JSON form.
52 'password', 'remember_token', 'system_name', 'email_confirmed', 'external_auth_id', 'email',
53 'created_at', 'updated_at', 'image_id',
57 * This holds the user's permissions when loaded.
60 protected $permissions;
63 * This holds the default user when loaded.
66 protected static $defaultUser = null;
69 * Returns the default public user.
72 public static function getDefault()
74 if (!is_null(static::$defaultUser)) {
75 return static::$defaultUser;
78 static::$defaultUser = static::where('system_name', '=', 'public')->first();
79 return static::$defaultUser;
83 * Check if the user is the default public user.
86 public function isDefault()
88 return $this->system_name === 'public';
92 * The roles that belong to the user.
93 * @return BelongsToMany
95 public function roles()
97 if ($this->id === 0) {
100 return $this->belongsToMany(Role::class);
104 * Check if the user has a role.
106 public function hasRole($roleId): bool
108 return $this->roles->pluck('id')->contains($roleId);
112 * Check if the user has a role.
116 public function hasSystemRole($role)
118 return $this->roles->pluck('system_name')->contains($role);
122 * Attach the default system role to this user.
124 public function attachDefaultRole(): void
126 $roleId = setting('registration-role');
127 if ($roleId && $this->roles()->where('id', '=', $roleId)->count() === 0) {
128 $this->roles()->attach($roleId);
133 * Get all permissions belonging to a the current user.
135 * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
137 public function permissions($cache = true)
139 if (isset($this->permissions) && $cache) {
140 return $this->permissions;
142 $this->load('roles.permissions');
143 $permissions = $this->roles->map(function ($role) {
144 return $role->permissions;
145 })->flatten()->unique();
146 $this->permissions = $permissions;
151 * Check if the user has a particular permission.
152 * @param $permissionName
155 public function can($permissionName)
157 if ($this->email === 'guest') {
160 return $this->permissions()->pluck('name')->contains($permissionName);
164 * Attach a role to this user.
166 public function attachRole(Role $role)
168 $this->roles()->attach($role->id);
172 * Get the social account associated with this user.
173 * @return \Illuminate\Database\Eloquent\Relations\HasMany
175 public function socialAccounts()
177 return $this->hasMany(SocialAccount::class);
181 * Check if the user has a social account,
182 * If a driver is passed it checks for that single account type.
183 * @param bool|string $socialDriver
186 public function hasSocialAccount($socialDriver = false)
188 if ($socialDriver === false) {
189 return $this->socialAccounts()->count() > 0;
192 return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
196 * Returns the user's avatar,
200 public function getAvatar($size = 50)
202 $default = url('/user_avatar.png');
203 $imageId = $this->image_id;
204 if ($imageId === 0 || $imageId === '0' || $imageId === null) {
209 $avatar = $this->avatar ? url($this->avatar->getThumb($size, $size, false)) : $default;
210 } catch (\Exception $err) {
217 * Get the avatar for the user.
218 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
220 public function avatar()
222 return $this->belongsTo(Image::class, 'image_id');
226 * Get the API tokens assigned to this user.
228 public function apiTokens(): HasMany
230 return $this->hasMany(ApiToken::class);
234 * Get the url for editing this user.
236 public function getEditUrl(string $path = ''): string
238 $uri = '/settings/users/' . $this->id . '/' . trim($path, '/');
239 return url(rtrim($uri, '/'));
243 * Get the url that links to this user's profile.
245 public function getProfileUrl(): string
247 return url('/user/' . $this->id);
251 * Get a shortened version of the user's name.
255 public function getShortName($chars = 8)
257 if (mb_strlen($this->name) <= $chars) {
261 $splitName = explode(' ', $this->name);
262 if (mb_strlen($splitName[0]) <= $chars) {
263 return $splitName[0];
270 * Send the password reset notification.
271 * @param string $token
274 public function sendPasswordResetNotification($token)
276 $this->notify(new ResetPassword($token));
282 public function logDescriptor(): string
284 return "({$this->id}) {$this->name}";