public function AddPermissionToRole::execute in JSON-RPC 8
Same name and namespace in other branches
- 2.x modules/jsonrpc_core/src/Plugin/jsonrpc/Method/AddPermissionToRole.php \Drupal\jsonrpc_core\Plugin\jsonrpc\Method\AddPermissionToRole::execute()
Executes the action with the parameters passed in.
Parameters
\Drupal\jsonrpc\Object\ParameterBag $params: The parameters.
Return value
mixed The result of the execution.
Overrides ExecutableWithParamsInterface::execute
File
- modules/
jsonrpc_core/ src/ Plugin/ jsonrpc/ Method/ AddPermissionToRole.php, line 29
Class
- AddPermissionToRole
- A method to add permissions to a role.
Namespace
Drupal\jsonrpc_core\Plugin\jsonrpc\MethodCode
public function execute(ParameterBag $params) {
$permission = $params
->get('permission');
/* @var \Drupal\user\RoleInterface $role */
$role = $params
->get('role');
try {
$role
->grantPermission($permission);
$violations = $role
->getTypedData()
->validate();
if ($violations
->count() !== 0) {
$error = Error::invalidParams(array_map(function (ConstraintViolationInterface $violation) {
return $violation
->getMessage();
}, iterator_to_array($violations)));
throw JsonRpcException::fromError($error);
}
return $role
->save();
} catch (EntityStorageException $e) {
$error = Error::internalError('Unable to save the user role. Error: ' . $e
->getMessage(), $role);
throw JsonRpcException::fromError($error);
}
}