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