]> BookStack Code Mirror - bookstack/blobdiff - app/User.php
Added indexes, Reduced queries on pages
[bookstack] / app / User.php
index ffe41229b5e74cbb4df1093e95eadf8034d4f014..570789f37423d9f9ab8cdf7c5c82ad932ac2c42b 100644 (file)
@@ -33,6 +33,12 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
      */
     protected $hidden = ['password', 'remember_token'];
 
+    /**
+     * This holds the user's permissions when loaded.
+     * @var array
+     */
+    protected $permissions;
+
     /**
      * Returns a default guest user.
      */
@@ -40,7 +46,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
     {
         return new static([
             'email' => 'guest',
-            'name'  => 'Guest'
+            'name' => 'Guest'
         ]);
     }
 
@@ -58,7 +64,19 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
 
     public function getRoleAttribute()
     {
-        return $this->roles()->first();
+        return $this->roles()->with('permissions')->first();
+    }
+
+    /**
+     * Loads the user's permissions from thier role.
+     */
+    private function loadPermissions()
+    {
+        if (isset($this->permissions)) return;
+        $this->load('roles.permissions');
+        $permissions = $this->roles[0]->permissions;
+        $permissionsArray = $permissions->pluck('name')->all();
+        $this->permissions = $permissionsArray;
     }
 
     /**
@@ -68,14 +86,11 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
      */
     public function can($permissionName)
     {
-        if($this->email == 'guest') {
+        if ($this->email == 'guest') {
             return false;
         }
-        $permissions = $this->role->permissions()->get();
-        $permissionSearch = $permissions->search(function ($item, $key) use ($permissionName) {
-            return $item->name == $permissionName;
-        });
-        return $permissionSearch !== false;
+        $this->loadPermissions();
+        return array_search($permissionName, $this->permissions) !== false;
     }
 
     /**
@@ -114,7 +129,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
      */
     public function hasSocialAccount($socialDriver = false)
     {
-        if($socialDriver === false) {
+        if ($socialDriver === false) {
             return $this->socialAccounts()->count() > 0;
         }