EntityLog in name of specific user

Is there way how to commit changes to entity programatically in the name of specific user so that EntityLog contains name of that user instead of admin for example?

I tried to use this:

private void commitAsUser(User user, CommitContext commitContext){
        if(user != null) {
            Locale locale = user.getLanguage() != null ? Locale.forLanguageTag(user.getLanguage()):Locale.ENGLISH;
            UserSession substitutedUserSession = userSessionManager.createSession(user, locale, true);
            SecurityContext securityContext = new SecurityContext(substitutedUserSession);
            SecurityContext previousSecurityContext = AppContext.getSecurityContext();
            AppContext.setSecurityContext(securityContext);
            try {
                dataManager.commit(commitContext);
            } finally {
                AppContext.setSecurityContext(previousSecurityContext);
            }
        }
    }

but i was getting NoUserSessionException on some EntityChangeListener methods.

Hi,

You can try System Authentication. The Authentication#withUser method allow to execute code on behalf of another user

@Inject
private DataManager dataManager;
@Inject
private Authentication authentication;

@Override
public void createProduct(String name, BigDecimal price, String username) {
    authentication.withUser(username, () -> {
        Product product = dataManager.create(Product.class);
        product.setName(name);
        product.setPrice(price);
        return dataManager.commit(product);
    });
}

Regards,
Gleb