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