]> BookStack Code Mirror - bookstack/blob - app/Auth/User.php
Replace node-sass with dart-sass
[bookstack] / app / Auth / User.php
1 <?php namespace BookStack\Auth;
2
3 use BookStack\Api\ApiToken;
4 use BookStack\Model;
5 use BookStack\Notifications\ResetPassword;
6 use BookStack\Uploads\Image;
7 use Carbon\Carbon;
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;
15
16 /**
17  * Class User
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
29  */
30 class User extends Model implements AuthenticatableContract, CanResetPasswordContract
31 {
32     use Authenticatable, CanResetPassword, Notifiable;
33
34     /**
35      * The database table used by the model.
36      * @var string
37      */
38     protected $table = 'users';
39
40     /**
41      * The attributes that are mass assignable.
42      * @var array
43      */
44     protected $fillable = ['name', 'email'];
45
46     /**
47      * The attributes excluded from the model's JSON form.
48      * @var array
49      */
50     protected $hidden = [
51         'password', 'remember_token', 'system_name', 'email_confirmed', 'external_auth_id', 'email',
52         'created_at', 'updated_at', 'image_id',
53     ];
54
55     /**
56      * This holds the user's permissions when loaded.
57      * @var array
58      */
59     protected $permissions;
60
61     /**
62      * This holds the default user when loaded.
63      * @var null|User
64      */
65     protected static $defaultUser = null;
66
67     /**
68      * Returns the default public user.
69      * @return User
70      */
71     public static function getDefault()
72     {
73         if (!is_null(static::$defaultUser)) {
74             return static::$defaultUser;
75         }
76         
77         static::$defaultUser = static::where('system_name', '=', 'public')->first();
78         return static::$defaultUser;
79     }
80
81     /**
82      * Check if the user is the default public user.
83      * @return bool
84      */
85     public function isDefault()
86     {
87         return $this->system_name === 'public';
88     }
89
90     /**
91      * The roles that belong to the user.
92      * @return BelongsToMany
93      */
94     public function roles()
95     {
96         if ($this->id === 0) {
97             return ;
98         }
99         return $this->belongsToMany(Role::class);
100     }
101
102     /**
103      * Check if the user has a role.
104      * @param $role
105      * @return mixed
106      */
107     public function hasRole($role)
108     {
109         return $this->roles->pluck('name')->contains($role);
110     }
111
112     /**
113      * Check if the user has a role.
114      * @param $role
115      * @return mixed
116      */
117     public function hasSystemRole($role)
118     {
119         return $this->roles->pluck('system_name')->contains($role);
120     }
121
122     /**
123      * Attach the default system role to this user.
124      */
125     public function attachDefaultRole(): void
126     {
127         $roleId = setting('registration-role');
128         if ($roleId && $this->roles()->where('id', '=', $roleId)->count() === 0) {
129             $this->roles()->attach($roleId);
130         }
131     }
132
133     /**
134      * Get all permissions belonging to a the current user.
135      * @param bool $cache
136      * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
137      */
138     public function permissions($cache = true)
139     {
140         if (isset($this->permissions) && $cache) {
141             return $this->permissions;
142         }
143         $this->load('roles.permissions');
144         $permissions = $this->roles->map(function ($role) {
145             return $role->permissions;
146         })->flatten()->unique();
147         $this->permissions = $permissions;
148         return $permissions;
149     }
150
151     /**
152      * Check if the user has a particular permission.
153      * @param $permissionName
154      * @return bool
155      */
156     public function can($permissionName)
157     {
158         if ($this->email === 'guest') {
159             return false;
160         }
161         return $this->permissions()->pluck('name')->contains($permissionName);
162     }
163
164     /**
165      * Attach a role to this user.
166      * @param Role $role
167      */
168     public function attachRole(Role $role)
169     {
170         $this->roles()->attach($role->id);
171     }
172
173     /**
174      * Get the social account associated with this user.
175      * @return \Illuminate\Database\Eloquent\Relations\HasMany
176      */
177     public function socialAccounts()
178     {
179         return $this->hasMany(SocialAccount::class);
180     }
181
182     /**
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
186      * @return bool
187      */
188     public function hasSocialAccount($socialDriver = false)
189     {
190         if ($socialDriver === false) {
191             return $this->socialAccounts()->count() > 0;
192         }
193
194         return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
195     }
196
197     /**
198      * Returns the user's avatar,
199      * @param int $size
200      * @return string
201      */
202     public function getAvatar($size = 50)
203     {
204         $default = url('/user_avatar.png');
205         $imageId = $this->image_id;
206         if ($imageId === 0 || $imageId === '0' || $imageId === null) {
207             return $default;
208         }
209
210         try {
211             $avatar = $this->avatar ? url($this->avatar->getThumb($size, $size, false)) : $default;
212         } catch (\Exception $err) {
213             $avatar = $default;
214         }
215         return $avatar;
216     }
217
218     /**
219      * Get the avatar for the user.
220      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
221      */
222     public function avatar()
223     {
224         return $this->belongsTo(Image::class, 'image_id');
225     }
226
227     /**
228      * Get the API tokens assigned to this user.
229      */
230     public function apiTokens(): HasMany
231     {
232         return $this->hasMany(ApiToken::class);
233     }
234
235     /**
236      * Get the url for editing this user.
237      */
238     public function getEditUrl(string $path = ''): string
239     {
240         $uri = '/settings/users/' . $this->id . '/' . trim($path, '/');
241         return url(rtrim($uri, '/'));
242     }
243
244     /**
245      * Get the url that links to this user's profile.
246      */
247     public function getProfileUrl(): string
248     {
249         return url('/user/' . $this->id);
250     }
251
252     /**
253      * Get a shortened version of the user's name.
254      * @param int $chars
255      * @return string
256      */
257     public function getShortName($chars = 8)
258     {
259         if (mb_strlen($this->name) <= $chars) {
260             return $this->name;
261         }
262
263         $splitName = explode(' ', $this->name);
264         if (mb_strlen($splitName[0]) <= $chars) {
265             return $splitName[0];
266         }
267
268         return '';
269     }
270
271     /**
272      * Send the password reset notification.
273      * @param  string  $token
274      * @return void
275      */
276     public function sendPasswordResetNotification($token)
277     {
278         $this->notify(new ResetPassword($token));
279     }
280 }