PERF: further simplify User::hasPermission()

This commit is contained in:
Toby Zerner 2015-07-27 11:20:18 +09:30
parent 5ae7bfefaa
commit 19ef6ed637

View File

@ -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. * The hasher with which to hash passwords.
@ -322,15 +322,11 @@ class User extends Model
return true; return true;
} }
if (! array_key_exists('permissions', $this->relations)) { if (is_null($this->permissions)) {
$this->setRelation('permissions', $this->permissions()->get()); $this->permissions = $this->permissions()->lists('permission');
} }
if (! isset($this->hasPermissions[$permission])) { return in_array($permission, $this->permissions);
$this->hasPermission[$permission] = (bool) $this->permissions->contains('permission', $permission);
}
return $this->hasPermission[$permission];
} }
/** /**