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