class UserNamespaceAuthorizationServiceImpl implements UserNamespaceAuthorizationService {

    @PublishNotificationMessages
    @NamespacePermission(fields = "#request?.userNamespaceAuthorizationKey?.namespace", permissions = NamespacePermissionEnum.GRANT)
    @Override
    public UserNamespaceAuthorization createUserNamespaceAuthorization(UserNamespaceAuthorizationCreateRequest request)
    {
        // Validate and trim the request parameters.
        validateUserNamespaceAuthorizationCreateRequest(request);

        // Get the user namespace authorization key.
        UserNamespaceAuthorizationKey key = request.getUserNamespaceAuthorizationKey();

        // Ensure a user namespace authorization with the specified name doesn't already exist for the specified namespace.
        UserNamespaceAuthorizationEntity userNamespaceAuthorizationEntity = userNamespaceAuthorizationDao.getUserNamespaceAuthorizationByKey(key);
        if (userNamespaceAuthorizationEntity != null)
        {
            throw new AlreadyExistsException(String
                .format("Unable to create user namespace authorization with user id \"%s\" and namespace \"%s\" because it already exists.", key.getUserId(),
                    key.getNamespace()));
        }

        // Retrieve and ensure that namespace exists with the specified user namespace authorization namespace code.
        NamespaceEntity namespaceEntity = namespaceDaoHelper.getNamespaceEntity(key.getNamespace());

        // Create and persist a new user namespace authorization entity from the request information.
        userNamespaceAuthorizationEntity = createUserNamespaceAuthorizationEntity(key.getUserId(), namespaceEntity, request.getNamespacePermissions());

        // Create a user namespace authorization change notification to be sent on create user namespace authorization event.
        messageNotificationEventService.processUserNamespaceAuthorizationChangeNotificationEvent(key);

        // Create and return the user namespace authorization object from the persisted entity.
        return createUserNamespaceAuthorizationFromEntity(userNamespaceAuthorizationEntity);
    }

}