Skip to content

Commit

Permalink
Merge pull request #323 from Yelldon/master
Browse files Browse the repository at this point in the history
Added permission check against role
  • Loading branch information
andrewelkins committed Aug 7, 2015
2 parents 62c1848 + 23cda54 commit 3aa6e79
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/Entrust/Traits/EntrustRoleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -92,7 +128,7 @@ public function attachPermission($permission)
}

/**
* Detach permission form current role.
* Detach permission from current role.
*
* @param object|array $permission
*
Expand Down

0 comments on commit 3aa6e79

Please sign in to comment.