diff --git a/src/Entrust/Traits/EntrustRoleTrait.php b/src/Entrust/Traits/EntrustRoleTrait.php index ef9a9379..93df0d36 100644 --- a/src/Entrust/Traits/EntrustRoleTrait.php +++ b/src/Entrust/Traits/EntrustRoleTrait.php @@ -54,6 +54,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 hasPermission($name, $requireAll = false) + { + if (is_array($name)) { + foreach ($name as $permissionName) { + $hasPermission = $this->hasPermission($permissionName); + + if ($hasPermission && !$requireAll) { + return true; + } elseif (!$hasPermission && $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 $permission) { + if ($permission->name == $name) { + return true; + } + } + } + + return false; + } /** * Save the inputted permissions. @@ -92,7 +128,7 @@ public function attachPermission($permission) } /** - * Detach permission form current role. + * Detach permission from current role. * * @param object|array $permission *