<?php namespace BookStack\Auth;
+use BookStack\Api\ApiToken;
use BookStack\Model;
use BookStack\Notifications\ResetPassword;
use BookStack\Uploads\Image;
+use Carbon\Carbon;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
+use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Notifications\Notifiable;
+/**
+ * Class User
+ * @package BookStack\Auth
+ * @property string $id
+ * @property string $name
+ * @property string $email
+ * @property string $password
+ * @property Carbon $created_at
+ * @property Carbon $updated_at
+ * @property bool $email_confirmed
+ * @property int $image_id
+ * @property string $external_auth_id
+ * @property string $system_name
+ */
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword, Notifiable;
* The attributes excluded from the model's JSON form.
* @var array
*/
- protected $hidden = ['password', 'remember_token'];
+ protected $hidden = [
+ 'password', 'remember_token', 'system_name', 'email_confirmed', 'external_auth_id', 'email',
+ 'created_at', 'updated_at',
+ ];
/**
* This holds the user's permissions when loaded.
*/
protected $permissions;
+ /**
+ * This holds the default user when loaded.
+ * @var null|User
+ */
+ protected static $defaultUser = null;
+
/**
* Returns the default public user.
* @return User
*/
public static function getDefault()
{
- return static::where('system_name', '=', 'public')->first();
+ if (!is_null(static::$defaultUser)) {
+ return static::$defaultUser;
+ }
+
+ static::$defaultUser = static::where('system_name', '=', 'public')->first();
+ return static::$defaultUser;
}
/**
return $this->roles->pluck('system_name')->contains($role);
}
+ /**
+ * Attach the default system role to this user.
+ */
+ public function attachDefaultRole(): void
+ {
+ $roleId = setting('registration-role');
+ if ($roleId && $this->roles()->where('id', '=', $roleId)->count() === 0) {
+ $this->roles()->attach($roleId);
+ }
+ }
+
/**
* Get all permissions belonging to a the current user.
* @param bool $cache
*/
public function attachRole(Role $role)
{
- $this->attachRoleId($role->id);
- }
-
- /**
- * Attach a role id to this user.
- * @param $id
- */
- public function attachRoleId($id)
- {
- $this->roles()->attach($id);
+ $this->roles()->attach($role->id);
}
/**
*/
public function getAvatar($size = 50)
{
- $default = baseUrl('/user_avatar.png');
+ $default = url('/user_avatar.png');
$imageId = $this->image_id;
if ($imageId === 0 || $imageId === '0' || $imageId === null) {
return $default;
}
try {
- $avatar = $this->avatar ? baseUrl($this->avatar->getThumb($size, $size, false)) : $default;
+ $avatar = $this->avatar ? url($this->avatar->getThumb($size, $size, false)) : $default;
} catch (\Exception $err) {
$avatar = $default;
}
return $this->belongsTo(Image::class, 'image_id');
}
+ /**
+ * Get the API tokens assigned to this user.
+ */
+ public function apiTokens(): HasMany
+ {
+ return $this->hasMany(ApiToken::class);
+ }
+
/**
* Get the url for editing this user.
- * @return string
*/
- public function getEditUrl()
+ public function getEditUrl(string $path = ''): string
{
- return baseUrl('/settings/users/' . $this->id);
+ $uri = '/settings/users/' . $this->id . '/' . trim($path, '/');
+ return url(rtrim($uri, '/'));
}
/**
* Get the url that links to this user's profile.
- * @return mixed
*/
- public function getProfileUrl()
+ public function getProfileUrl(): string
{
- return baseUrl('/user/' . $this->id);
+ return url('/user/' . $this->id);
}
/**
*/
public function getShortName($chars = 8)
{
- if (strlen($this->name) <= $chars) {
+ if (mb_strlen($this->name) <= $chars) {
return $this->name;
}
$splitName = explode(' ', $this->name);
- if (strlen($splitName[0]) <= $chars) {
+ if (mb_strlen($splitName[0]) <= $chars) {
return $splitName[0];
}