From 87af3cd5f7a3dfd0deb7bb979a122edb35ef2c88 Mon Sep 17 00:00:00 2001 From: Greg Date: Wed, 10 Jun 2015 15:48:34 -0500 Subject: [PATCH 1/2] Added permission check against role Added a check to see true or false check against a role to see the permissions it has assigned to it. --- src/Entrust/Traits/EntrustRoleTrait.php | 38 ++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Entrust/Traits/EntrustRoleTrait.php b/src/Entrust/Traits/EntrustRoleTrait.php index e9fc5bf0..aef0295d 100644 --- a/src/Entrust/Traits/EntrustRoleTrait.php +++ b/src/Entrust/Traits/EntrustRoleTrait.php @@ -53,6 +53,42 @@ public static function boot() return true; }); } + + /** + * Checks if the role has a permission by its name. + * + * @param string|array $name Permission name or array of permission names. + * @param bool $requireAll All permissions in the array are required. + * + * @return bool + */ + public function hasPerm($name, $requireAll = false) + { + if (is_array($name)) { + foreach ($name as $permName) { + $hasPerm = $this->hasPerm($permName); + + if ($hasPerm && !$requireAll) { + return true; + } elseif (!$hasPerm && $requireAll) { + return false; + } + } + + // If we've made it this far and $requireAll is FALSE, then NONE of the permissions were found + // If we've made it this far and $requireAll is TRUE, then ALL of the permissions were found. + // Return the value of $requireAll; + return $requireAll; + } else { + foreach ($this->perms as $perm) { + if ($perm->name == $name) { + return true; + } + } + } + + return false; + } /** * Save the inputted permissions. @@ -91,7 +127,7 @@ public function attachPermission($permission) } /** - * Detach permission form current role. + * Detach permission from current role. * * @param object|array $permission * From 23cda540bc740f313e9c7b8215f4a3793c9d164f Mon Sep 17 00:00:00 2001 From: Yelldon Date: Thu, 9 Jul 2015 16:01:53 -0500 Subject: [PATCH 2/2] Updated method 'hasPerm' to 'hasPermission' for better consistency. --- src/Entrust/Traits/EntrustRoleTrait.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Entrust/Traits/EntrustRoleTrait.php b/src/Entrust/Traits/EntrustRoleTrait.php index aef0295d..7e31a526 100644 --- a/src/Entrust/Traits/EntrustRoleTrait.php +++ b/src/Entrust/Traits/EntrustRoleTrait.php @@ -62,15 +62,15 @@ public static function boot() * * @return bool */ - public function hasPerm($name, $requireAll = false) + public function hasPermission($name, $requireAll = false) { if (is_array($name)) { - foreach ($name as $permName) { - $hasPerm = $this->hasPerm($permName); + foreach ($name as $permissionName) { + $hasPermission = $this->hasPermission($permissionName); - if ($hasPerm && !$requireAll) { + if ($hasPermission && !$requireAll) { return true; - } elseif (!$hasPerm && $requireAll) { + } elseif (!$hasPermission && $requireAll) { return false; } } @@ -80,8 +80,8 @@ public function hasPerm($name, $requireAll = false) // Return the value of $requireAll; return $requireAll; } else { - foreach ($this->perms as $perm) { - if ($perm->name == $name) { + foreach ($this->perms as $permission) { + if ($permission->name == $name) { return true; } }