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