]> BookStack Code Mirror - bookstack/blob - app/User.php
Added initial translation into German (formal)
[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      * Get all permissions belonging to a the current user.
79      * @param bool $cache
80      * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
81      */
82     public function permissions($cache = true)
83     {
84         if(isset($this->permissions) && $cache) return $this->permissions;
85         $this->load('roles.permissions');
86         $permissions = $this->roles->map(function($role) {
87             return $role->permissions;
88         })->flatten()->unique();
89         $this->permissions = $permissions;
90         return $permissions;
91     }
92
93     /**
94      * Check if the user has a particular permission.
95      * @param $permissionName
96      * @return bool
97      */
98     public function can($permissionName)
99     {
100         if ($this->email === 'guest') return false;
101         return $this->permissions()->pluck('name')->contains($permissionName);
102     }
103
104     /**
105      * Attach a role to this user.
106      * @param Role $role
107      */
108     public function attachRole(Role $role)
109     {
110         $this->attachRoleId($role->id);
111     }
112
113     /**
114      * Attach a role id to this user.
115      * @param $id
116      */
117     public function attachRoleId($id)
118     {
119         $this->roles()->attach($id);
120     }
121
122     /**
123      * Get the social account associated with this user.
124      * @return \Illuminate\Database\Eloquent\Relations\HasMany
125      */
126     public function socialAccounts()
127     {
128         return $this->hasMany(SocialAccount::class);
129     }
130
131     /**
132      * Check if the user has a social account,
133      * If a driver is passed it checks for that single account type.
134      * @param bool|string $socialDriver
135      * @return bool
136      */
137     public function hasSocialAccount($socialDriver = false)
138     {
139         if ($socialDriver === false) {
140             return $this->socialAccounts()->count() > 0;
141         }
142
143         return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
144     }
145
146     /**
147      * Returns the user's avatar,
148      * @param int $size
149      * @return string
150      */
151     public function getAvatar($size = 50)
152     {
153         if ($this->image_id === 0 || $this->image_id === '0' || $this->image_id === null) return baseUrl('/user_avatar.png');
154         return baseUrl($this->avatar->getThumb($size, $size, false));
155     }
156
157     /**
158      * Get the avatar for the user.
159      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
160      */
161     public function avatar()
162     {
163         return $this->belongsTo(Image::class, 'image_id');
164     }
165
166     /**
167      * Get the url for editing this user.
168      * @return string
169      */
170     public function getEditUrl()
171     {
172         return baseUrl('/settings/users/' . $this->id);
173     }
174
175     /**
176      * Get the url that links to this user's profile.
177      * @return mixed
178      */
179     public function getProfileUrl()
180     {
181         return baseUrl('/user/' . $this->id);
182     }
183
184     /**
185      * Get a shortened version of the user's name.
186      * @param int $chars
187      * @return string
188      */
189     public function getShortName($chars = 8)
190     {
191         if (strlen($this->name) <= $chars) return $this->name;
192
193         $splitName = explode(' ', $this->name);
194         if (strlen($splitName[0]) <= $chars) return $splitName[0];
195
196         return '';
197     }
198
199     /**
200      * Send the password reset notification.
201      * @param  string  $token
202      * @return void
203      */
204     public function sendPasswordResetNotification($token)
205     {
206         $this->notify(new ResetPassword($token));
207     }
208 }