/**
* Creates a new RoleDraft.
*
* @since 6.0
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a RoleDraft
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* if the name of the role already exists or if limitation of the same type
* is repeated in the policy create struct or if limitation is not allowed on module/function
* @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a policy limitation in the $roleCreateStruct is not valid
*
* @param \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $roleCreateStruct
*
* @return \eZ\Publish\API\Repository\Values\User\RoleDraft
*/
public function createRole(APIRoleCreateStruct $roleCreateStruct)
{
if (!is_string($roleCreateStruct->identifier) || empty($roleCreateStruct->identifier)) {
throw new InvalidArgumentValue('identifier', $roleCreateStruct->identifier, 'RoleCreateStruct');
}
if ($this->repository->hasAccess('role', 'create') !== true) {
throw new UnauthorizedException('role', 'create');
}
try {
$existingRole = $this->loadRoleByIdentifier($roleCreateStruct->identifier);
throw new InvalidArgumentException('$roleCreateStruct', "Role '{$existingRole->id}' with the specified identifier '{$roleCreateStruct->identifier}' " . 'already exists');
} catch (APINotFoundException $e) {
// Do nothing
}
$limitationValidationErrors = $this->validateRoleCreateStruct($roleCreateStruct);
if (!empty($limitationValidationErrors)) {
throw new LimitationValidationException($limitationValidationErrors);
}
$spiRoleCreateStruct = $this->roleDomainMapper->buildPersistenceRoleCreateStruct($roleCreateStruct);
$this->repository->beginTransaction();
try {
$spiRole = $this->userHandler->createRole($spiRoleCreateStruct);
$this->repository->commit();
} catch (Exception $e) {
$this->repository->rollback();
throw $e;
}
return $this->roleDomainMapper->buildDomainRoleDraftObject($spiRole);
}