From c0d57fd39950b33e97453a6627d155111d0f2891 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Mon, 27 Jul 2015 11:20:18 +0930 Subject: [PATCH] PERF: further simplify User::hasPermission() --- src/Core/Users/User.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Core/Users/User.php b/src/Core/Users/User.php index dc062dabf..948cb743b 100755 --- a/src/Core/Users/User.php +++ b/src/Core/Users/User.php @@ -61,11 +61,11 @@ class User extends Model ]; /** - * An cache of permissions, and whether or not this user has them. + * An array of permissions that this user has. * - * @var array + * @var array|null */ - protected $hasPermission = []; + protected $permissions = null; /** * The hasher with which to hash passwords. @@ -322,15 +322,11 @@ class User extends Model return true; } - if (! array_key_exists('permissions', $this->relations)) { - $this->setRelation('permissions', $this->permissions()->get()); + if (is_null($this->permissions)) { + $this->permissions = $this->permissions()->lists('permission'); } - if (! isset($this->hasPermissions[$permission])) { - $this->hasPermission[$permission] = (bool) $this->permissions->contains('permission', $permission); - } - - return $this->hasPermission[$permission]; + return in_array($permission, $this->permissions); } /**