X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/8e274a5a84cc11c20c0d62f9ecc0b08cd9410305..refs/pull/139/head:/app/User.php diff --git a/app/User.php b/app/User.php index 2d14c6e6e..74aec7e3a 100644 --- a/app/User.php +++ b/app/User.php @@ -1,9 +1,6 @@ -belongsToMany('BookStack\Role'); + return $this->belongsToMany(Role::class); } /** @@ -67,11 +64,12 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon /** * Get all permissions belonging to a the current user. + * @param bool $cache * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough */ - public function permissions() + public function permissions($cache = true) { - if(isset($this->permissions)) return $this->permissions; + if(isset($this->permissions) && $cache) return $this->permissions; $this->load('roles.permissions'); $permissions = $this->roles->map(function($role) { return $role->permissions; @@ -106,7 +104,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon */ public function attachRoleId($id) { - $this->roles()->attach([$id]); + $this->roles()->attach($id); } /** @@ -115,7 +113,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon */ public function socialAccounts() { - return $this->hasMany('BookStack\SocialAccount'); + return $this->hasMany(SocialAccount::class); } /** @@ -150,7 +148,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon */ public function avatar() { - return $this->belongsTo('BookStack\Image', 'image_id'); + return $this->belongsTo(Image::class, 'image_id'); } /** @@ -161,4 +159,19 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon { return '/settings/users/' . $this->id; } + + /** + * Get a shortened version of the user's name. + * @param int $chars + * @return string + */ + public function getShortName($chars = 8) + { + if (strlen($this->name) <= $chars) return $this->name; + + $splitName = explode(' ', $this->name); + if (strlen($splitName[0]) <= $chars) return $splitName[0]; + + return ''; + } }