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