]> BookStack Code Mirror - bookstack/blob - app/Auth/User.php
Added latest activity into users list view
[bookstack] / app / Auth / User.php
1 <?php namespace BookStack\Auth;
2
3 use BookStack\Actions\Activity;
4 use BookStack\Api\ApiToken;
5 use BookStack\Interfaces\Loggable;
6 use BookStack\Model;
7 use BookStack\Notifications\ResetPassword;
8 use BookStack\Uploads\Image;
9 use Carbon\Carbon;
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
19 /**
20  * Class User
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
32  */
33 class User extends Model implements AuthenticatableContract, CanResetPasswordContract, Loggable
34 {
35     use Authenticatable, CanResetPassword, Notifiable;
36
37     /**
38      * The database table used by the model.
39      * @var string
40      */
41     protected $table = 'users';
42
43     /**
44      * The attributes that are mass assignable.
45      * @var array
46      */
47     protected $fillable = ['name', 'email'];
48
49     /**
50      * The attributes excluded from the model's JSON form.
51      * @var array
52      */
53     protected $hidden = [
54         'password', 'remember_token', 'system_name', 'email_confirmed', 'external_auth_id', 'email',
55         'created_at', 'updated_at', 'image_id',
56     ];
57
58     /**
59      * This holds the user's permissions when loaded.
60      * @var array
61      */
62     protected $permissions;
63
64     /**
65      * This holds the default user when loaded.
66      * @var null|User
67      */
68     protected static $defaultUser = null;
69
70     /**
71      * Returns the default public user.
72      * @return User
73      */
74     public static function getDefault()
75     {
76         if (!is_null(static::$defaultUser)) {
77             return static::$defaultUser;
78         }
79         
80         static::$defaultUser = static::where('system_name', '=', 'public')->first();
81         return static::$defaultUser;
82     }
83
84     /**
85      * Check if the user is the default public user.
86      * @return bool
87      */
88     public function isDefault()
89     {
90         return $this->system_name === 'public';
91     }
92
93     /**
94      * The roles that belong to the user.
95      * @return BelongsToMany
96      */
97     public function roles()
98     {
99         if ($this->id === 0) {
100             return ;
101         }
102         return $this->belongsToMany(Role::class);
103     }
104
105     /**
106      * Check if the user has a role.
107      */
108     public function hasRole($roleId): bool
109     {
110         return $this->roles->pluck('id')->contains($roleId);
111     }
112
113     /**
114      * Check if the user has a role.
115      * @param $role
116      * @return mixed
117      */
118     public function hasSystemRole($role)
119     {
120         return $this->roles->pluck('system_name')->contains($role);
121     }
122
123     /**
124      * Attach the default system role to this user.
125      */
126     public function attachDefaultRole(): void
127     {
128         $roleId = setting('registration-role');
129         if ($roleId && $this->roles()->where('id', '=', $roleId)->count() === 0) {
130             $this->roles()->attach($roleId);
131         }
132     }
133
134     /**
135      * Get all permissions belonging to a the current user.
136      * @param bool $cache
137      * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
138      */
139     public function permissions($cache = true)
140     {
141         if (isset($this->permissions) && $cache) {
142             return $this->permissions;
143         }
144         $this->load('roles.permissions');
145         $permissions = $this->roles->map(function ($role) {
146             return $role->permissions;
147         })->flatten()->unique();
148         $this->permissions = $permissions;
149         return $permissions;
150     }
151
152     /**
153      * Check if the user has a particular permission.
154      * @param $permissionName
155      * @return bool
156      */
157     public function can($permissionName)
158     {
159         if ($this->email === 'guest') {
160             return false;
161         }
162         return $this->permissions()->pluck('name')->contains($permissionName);
163     }
164
165     /**
166      * Attach a role to this user.
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 latest activity instance for this user.
237      */
238     public function latestActivity(): HasOne
239     {
240         return $this->hasOne(Activity::class)->latest();
241     }
242
243     /**
244      * Get the url for editing this user.
245      */
246     public function getEditUrl(string $path = ''): string
247     {
248         $uri = '/settings/users/' . $this->id . '/' . trim($path, '/');
249         return url(rtrim($uri, '/'));
250     }
251
252     /**
253      * Get the url that links to this user's profile.
254      */
255     public function getProfileUrl(): string
256     {
257         return url('/user/' . $this->id);
258     }
259
260     /**
261      * Get a shortened version of the user's name.
262      * @param int $chars
263      * @return string
264      */
265     public function getShortName($chars = 8)
266     {
267         if (mb_strlen($this->name) <= $chars) {
268             return $this->name;
269         }
270
271         $splitName = explode(' ', $this->name);
272         if (mb_strlen($splitName[0]) <= $chars) {
273             return $splitName[0];
274         }
275
276         return '';
277     }
278
279     /**
280      * Send the password reset notification.
281      * @param  string  $token
282      * @return void
283      */
284     public function sendPasswordResetNotification($token)
285     {
286         $this->notify(new ResetPassword($token));
287     }
288
289     /**
290      * @inheritdoc
291      */
292     public function logDescriptor(): string
293     {
294         return "({$this->id}) {$this->name}";
295     }
296 }