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;
18 use Illuminate\Support\Collection;
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 * Check if the user has a particular permission.
137 public function can(string $permissionName): bool
139 if ($this->email === 'guest') {
143 return $this->permissions()->contains($permissionName);
147 * Get all permissions belonging to a the current user.
149 protected function permissions(): Collection
151 if (isset($this->permissions)) {
152 return $this->permissions;
155 $this->permissions = $this->newQuery()->getConnection()->table('role_user', 'ru')
156 ->select('role_permissions.name as name')->distinct()
157 ->leftJoin('permission_role', 'ru.role_id', '=', 'permission_role.role_id')
158 ->leftJoin('role_permissions', 'permission_role.permission_id', '=', 'role_permissions.id')
159 ->where('ru.user_id', '=', $this->id)
163 return $this->permissions;
167 * Clear any cached permissions on this instance.
169 public function clearPermissionCache()
171 $this->permissions = null;
175 * Attach a role to this user.
177 public function attachRole(Role $role)
179 $this->roles()->attach($role->id);
183 * Get the social account associated with this user.
184 * @return \Illuminate\Database\Eloquent\Relations\HasMany
186 public function socialAccounts()
188 return $this->hasMany(SocialAccount::class);
192 * Check if the user has a social account,
193 * If a driver is passed it checks for that single account type.
194 * @param bool|string $socialDriver
197 public function hasSocialAccount($socialDriver = false)
199 if ($socialDriver === false) {
200 return $this->socialAccounts()->count() > 0;
203 return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
207 * Returns the user's avatar,
211 public function getAvatar($size = 50)
213 $default = url('/user_avatar.png');
214 $imageId = $this->image_id;
215 if ($imageId === 0 || $imageId === '0' || $imageId === null) {
220 $avatar = $this->avatar ? url($this->avatar->getThumb($size, $size, false)) : $default;
221 } catch (\Exception $err) {
228 * Get the avatar for the user.
229 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
231 public function avatar()
233 return $this->belongsTo(Image::class, 'image_id');
237 * Get the API tokens assigned to this user.
239 public function apiTokens(): HasMany
241 return $this->hasMany(ApiToken::class);
245 * Get the latest activity instance for this user.
247 public function latestActivity(): HasOne
249 return $this->hasOne(Activity::class)->latest();
253 * Get the url for editing this user.
255 public function getEditUrl(string $path = ''): string
257 $uri = '/settings/users/' . $this->id . '/' . trim($path, '/');
258 return url(rtrim($uri, '/'));
262 * Get the url that links to this user's profile.
264 public function getProfileUrl(): string
266 return url('/user/' . $this->id);
270 * Get a shortened version of the user's name.
274 public function getShortName($chars = 8)
276 if (mb_strlen($this->name) <= $chars) {
280 $splitName = explode(' ', $this->name);
281 if (mb_strlen($splitName[0]) <= $chars) {
282 return $splitName[0];
289 * Send the password reset notification.
290 * @param string $token
293 public function sendPasswordResetNotification($token)
295 $this->notify(new ResetPassword($token));
301 public function logDescriptor(): string
303 return "({$this->id}) {$this->name}";