1
1
<?php namespace BookStack \Auth ;
2
2
3
3
use BookStack \Api \ApiToken ;
4
+ use BookStack \Entities \Tools \SlugGenerator ;
4
5
use BookStack \Interfaces \Loggable ;
6
+ use BookStack \Interfaces \Sluggable ;
5
7
use BookStack \Model ;
6
8
use BookStack \Notifications \ResetPassword ;
7
9
use BookStack \Uploads \Image ;
22
24
* Class User
23
25
* @property string $id
24
26
* @property string $name
27
+ * @property string $slug
25
28
* @property string $email
26
29
* @property string $password
27
30
* @property Carbon $created_at
32
35
* @property string $system_name
33
36
* @property Collection $roles
34
37
*/
35
- class User extends Model implements AuthenticatableContract, CanResetPasswordContract, Loggable
38
+ class User extends Model implements AuthenticatableContract, CanResetPasswordContract, Loggable, Sluggable
36
39
{
37
40
use Authenticatable, CanResetPassword, Notifiable;
38
41
@@ -73,23 +76,21 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
73
76
74
77
/**
75
78
* Returns the default public user.
76
- * @return User
77
79
*/
78
- public static function getDefault ()
80
+ public static function getDefault (): User
79
81
{
80
82
if (!is_null (static ::$ defaultUser )) {
81
83
return static ::$ defaultUser ;
82
84
}
83
85
84
- static ::$ defaultUser = static ::where ('system_name ' , '= ' , 'public ' )->first ();
86
+ static ::$ defaultUser = static ::query ()-> where ('system_name ' , '= ' , 'public ' )->first ();
85
87
return static ::$ defaultUser ;
86
88
}
87
89
88
90
/**
89
91
* Check if the user is the default public user.
90
- * @return bool
91
92
*/
92
- public function isDefault ()
93
+ public function isDefault (): bool
93
94
{
94
95
return $ this ->system_name === 'public ' ;
95
96
}
@@ -116,12 +117,10 @@ public function hasRole($roleId): bool
116
117
117
118
/**
118
119
* Check if the user has a role.
119
- * @param $role
120
- * @return mixed
121
120
*/
122
- public function hasSystemRole ($ role )
121
+ public function hasSystemRole (string $ roleSystemName ): bool
123
122
{
124
- return $ this ->roles ->pluck ('system_name ' )->contains ($ role );
123
+ return $ this ->roles ->pluck ('system_name ' )->contains ($ roleSystemName );
125
124
}
126
125
127
126
/**
@@ -185,9 +184,8 @@ public function attachRole(Role $role)
185
184
186
185
/**
187
186
* Get the social account associated with this user.
188
- * @return HasMany
189
187
*/
190
- public function socialAccounts ()
188
+ public function socialAccounts (): HasMany
191
189
{
192
190
return $ this ->hasMany (SocialAccount::class);
193
191
}
@@ -208,11 +206,9 @@ public function hasSocialAccount($socialDriver = false)
208
206
}
209
207
210
208
/**
211
- * Returns the user's avatar,
212
- * @param int $size
213
- * @return string
209
+ * Returns a URL to the user's avatar
214
210
*/
215
- public function getAvatar ($ size = 50 )
211
+ public function getAvatar (int $ size = 50 ): string
216
212
{
217
213
$ default = url ('/user_avatar.png ' );
218
214
$ imageId = $ this ->image_id ;
@@ -230,9 +226,8 @@ public function getAvatar($size = 50)
230
226
231
227
/**
232
228
* Get the avatar for the user.
233
- * @return BelongsTo
234
229
*/
235
- public function avatar ()
230
+ public function avatar (): BelongsTo
236
231
{
237
232
return $ this ->belongsTo (Image::class, 'image_id ' );
238
233
}
@@ -272,15 +267,13 @@ public function getEditUrl(string $path = ''): string
272
267
*/
273
268
public function getProfileUrl (): string
274
269
{
275
- return url ('/user/ ' . $ this ->id );
270
+ return url ('/user/ ' . $ this ->slug );
276
271
}
277
272
278
273
/**
279
274
* Get a shortened version of the user's name.
280
- * @param int $chars
281
- * @return string
282
275
*/
283
- public function getShortName ($ chars = 8 )
276
+ public function getShortName (int $ chars = 8 ): string
284
277
{
285
278
if (mb_strlen ($ this ->name ) <= $ chars ) {
286
279
return $ this ->name ;
@@ -311,4 +304,13 @@ public function logDescriptor(): string
311
304
{
312
305
return "( {$ this ->id }) {$ this ->name }" ;
313
306
}
307
+
308
+ /**
309
+ * @inheritDoc
310
+ */
311
+ public function refreshSlug (): string
312
+ {
313
+ $ this ->slug = app (SlugGenerator::class)->generate ($ this );
314
+ return $ this ->slug ;
315
+ }
314
316
}
0 commit comments