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