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