public function ability($roles, $permissions, $group = null, $options = [])
{
$options = is_array($group) ? $group : $options;
$group = is_array($group) ? null : $group;
// Convert string to array if that's what is passed in.
if (!is_array($roles)) {
$roles = explode(',', $roles);
}
if (!is_array($permissions)) {
$permissions = explode(',', $permissions);
}
// Set up default values and validate options.
if (!isset($options['validate_all'])) {
$options['validate_all'] = false;
} else {
if ($options['validate_all'] !== true && $options['validate_all'] !== false) {
throw new InvalidArgumentException();
}
}
if (!isset($options['return_type'])) {
$options['return_type'] = 'boolean';
} else {
if ($options['return_type'] != 'boolean' && $options['return_type'] != 'array' && $options['return_type'] != 'both') {
throw new InvalidArgumentException();
}
}
// Loop through roles and permissions and check each.
$checkedRoles = [];
$checkedPermissions = [];
foreach ($roles as $role) {
$checkedRoles[$role] = $this->hasRole($role, $group);
}
foreach ($permissions as $permission) {
$checkedPermissions[$permission] = $this->can($permission, $group);
}
// If validate all and there is a false in either
// Check that if validate all, then there should not be any false.
// Check that if not validate all, there must be at least one true.
if ($options['validate_all'] && !(in_array(false, $checkedRoles) || in_array(false, $checkedPermissions)) || !$options['validate_all'] && (in_array(true, $checkedRoles) || in_array(true, $checkedPermissions))) {
$validateAll = true;
} else {
$validateAll = false;
}
// Return based on option
if ($options['return_type'] == 'boolean') {
return $validateAll;
} elseif ($options['return_type'] == 'array') {
return ['roles' => $checkedRoles, 'permissions' => $checkedPermissions];
} else {
return [$validateAll, ['roles' => $checkedRoles, 'permissions' => $checkedPermissions]];
}
}