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