1 <?php namespace BookStack\Auth;
3 use BookStack\Actions\Activity;
4 use BookStack\Api\ApiToken;
5 use BookStack\Interfaces\Loggable;
7 use BookStack\Notifications\ResetPassword;
8 use BookStack\Uploads\Image;
10 use Illuminate\Auth\Authenticatable;
11 use Illuminate\Auth\Passwords\CanResetPassword;
12 use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
13 use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
14 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
15 use Illuminate\Database\Eloquent\Relations\HasMany;
16 use Illuminate\Database\Eloquent\Relations\HasOne;
17 use Illuminate\Notifications\Notifiable;
21 * @package BookStack\Auth
22 * @property string $id
23 * @property string $name
24 * @property string $email
25 * @property string $password
26 * @property Carbon $created_at
27 * @property Carbon $updated_at
28 * @property bool $email_confirmed
29 * @property int $image_id
30 * @property string $external_auth_id
31 * @property string $system_name
33 class User extends Model implements AuthenticatableContract, CanResetPasswordContract, Loggable
35 use Authenticatable, CanResetPassword, Notifiable;
38 * The database table used by the model.
41 protected $table = 'users';
44 * The attributes that are mass assignable.
47 protected $fillable = ['name', 'email'];
50 * The attributes excluded from the model's JSON form.
54 'password', 'remember_token', 'system_name', 'email_confirmed', 'external_auth_id', 'email',
55 'created_at', 'updated_at', 'image_id',
59 * This holds the user's permissions when loaded.
62 protected $permissions;
65 * This holds the default user when loaded.
68 protected static $defaultUser = null;
71 * Returns the default public user.
74 public static function getDefault()
76 if (!is_null(static::$defaultUser)) {
77 return static::$defaultUser;
80 static::$defaultUser = static::where('system_name', '=', 'public')->first();
81 return static::$defaultUser;
85 * Check if the user is the default public user.
88 public function isDefault()
90 return $this->system_name === 'public';
94 * The roles that belong to the user.
95 * @return BelongsToMany
97 public function roles()
99 if ($this->id === 0) {
102 return $this->belongsToMany(Role::class);
106 * Check if the user has a role.
108 public function hasRole($roleId): bool
110 return $this->roles->pluck('id')->contains($roleId);
114 * Check if the user has a role.
118 public function hasSystemRole($role)
120 return $this->roles->pluck('system_name')->contains($role);
124 * Attach the default system role to this user.
126 public function attachDefaultRole(): void
128 $roleId = setting('registration-role');
129 if ($roleId && $this->roles()->where('id', '=', $roleId)->count() === 0) {
130 $this->roles()->attach($roleId);
135 * Get all permissions belonging to a the current user.
137 * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
139 public function permissions($cache = true)
141 if (isset($this->permissions) && $cache) {
142 return $this->permissions;
144 $this->load('roles.permissions');
145 $permissions = $this->roles->map(function ($role) {
146 return $role->permissions;
147 })->flatten()->unique();
148 $this->permissions = $permissions;
153 * Check if the user has a particular permission.
154 * @param $permissionName
157 public function can($permissionName)
159 if ($this->email === 'guest') {
162 return $this->permissions()->pluck('name')->contains($permissionName);
166 * Attach a role to this user.
168 public function attachRole(Role $role)
170 $this->roles()->attach($role->id);
174 * Get the social account associated with this user.
175 * @return \Illuminate\Database\Eloquent\Relations\HasMany
177 public function socialAccounts()
179 return $this->hasMany(SocialAccount::class);
183 * Check if the user has a social account,
184 * If a driver is passed it checks for that single account type.
185 * @param bool|string $socialDriver
188 public function hasSocialAccount($socialDriver = false)
190 if ($socialDriver === false) {
191 return $this->socialAccounts()->count() > 0;
194 return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
198 * Returns the user's avatar,
202 public function getAvatar($size = 50)
204 $default = url('/user_avatar.png');
205 $imageId = $this->image_id;
206 if ($imageId === 0 || $imageId === '0' || $imageId === null) {
211 $avatar = $this->avatar ? url($this->avatar->getThumb($size, $size, false)) : $default;
212 } catch (\Exception $err) {
219 * Get the avatar for the user.
220 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
222 public function avatar()
224 return $this->belongsTo(Image::class, 'image_id');
228 * Get the API tokens assigned to this user.
230 public function apiTokens(): HasMany
232 return $this->hasMany(ApiToken::class);
236 * Get the latest activity instance for this user.
238 public function latestActivity(): HasOne
240 return $this->hasOne(Activity::class)->latest();
244 * Get the url for editing this user.
246 public function getEditUrl(string $path = ''): string
248 $uri = '/settings/users/' . $this->id . '/' . trim($path, '/');
249 return url(rtrim($uri, '/'));
253 * Get the url that links to this user's profile.
255 public function getProfileUrl(): string
257 return url('/user/' . $this->id);
261 * Get a shortened version of the user's name.
265 public function getShortName($chars = 8)
267 if (mb_strlen($this->name) <= $chars) {
271 $splitName = explode(' ', $this->name);
272 if (mb_strlen($splitName[0]) <= $chars) {
273 return $splitName[0];
280 * Send the password reset notification.
281 * @param string $token
284 public function sendPasswordResetNotification($token)
286 $this->notify(new ResetPassword($token));
292 public function logDescriptor(): string
294 return "({$this->id}) {$this->name}";