Looking for an example of recording logged in user id to a record when it is created

Hello,

If anyone has a code sample of recording the user id of the currently logged in user to a record when it is created it would be greatly appreciated.

The scenario is this:

I have extended the user entity to include an association to an Organization entity.

In the main entity in question, I have created two association attributes:
user and organization.

The idea is to set the user id (not username) and the organization id of the user creating the record to the when they create a new item.

I just can’t seem to figure it out.

Any sample of what that code might look like would be appreciated.

a community How-To would be awesome.

Turns out step one was rather easy:

@PostConstruct
protected void init() {
    // Set the 'systemUser' attribute to a currently logged in user
    setSystemUser(AppBeans.get(UserSessionSource.class).getUserSession().getUser());
}

Now on to step 2

Hi!

As I understand, when some new entity is created you want to set the id of the currently logged user?
You can simply get the current user by using UserSession.

In the editor screen, override the initNewItem method and change initial values of a created entity instance.
Code example:

@Inject
private UserSession userSession;

@Override
protected void initNewItem(Company item) {
    item.setUser(userSession.getUser());
}
1 Like

Thank you,

That will be very useful going forward.

I ended up doing something similar at the entity level.

@PostConstruct
protected void init() {
    // Set the 'systemUser' attribute to a currently logged in user
    setSystemUser((ExtUser) AppBeans.get(UserSessionSource.class).getUserSession().getUser());
}

I actually extended the user (ExtUser) to add an additional association attribute. What I would like to do is also record this association attribute to the item being created. For sake of discussion, let’s imagine that attribute is Department. Since it is likely that a user may change departments, we can’t depend on the current department of a user, rather we need to record the depart a user was in at the time an item was created.

Unfortunately, the additional attribute (department) is not in the UserSession.
I think I need to use the data manager to grab this and am trying to find some syntax that will work.

Could you clarify does ExtUser contain required field? If no, you can use DataManager with a query or create some service that will return Organization by the user from UserSession.

1 Like

ExtUser contains only the association attribute for the “department” (Partner).

Update:

I found a previous post that solved the issue entirely for me, that’s for all the help!:

package com.company.sckywfdb.web.referral;

import com.company.sckywfdb.entity.ExtUser;
import com.haulmont.cuba.core.global.DataManager;
import com.haulmont.cuba.core.global.LoadContext;
import com.haulmont.cuba.core.global.UserSessionSource;
import com.haulmont.cuba.gui.components.AbstractEditor;
import com.company.sckywfdb.entity.Referral;
import com.haulmont.cuba.security.entity.User;


import javax.inject.Inject;

public class ReferralEdit extends AbstractEditor<Referral> {

    @Inject
    private UserSessionSource userSessionSource;

    @Inject
    private DataManager dataManager;

    @Override
    protected void initNewItem(Referral item) {
        super.initNewItem(item);

        User user = userSessionSource.getUserSession().getUser();

        LoadContext<ExtUser> loadContext = LoadContext.create(ExtUser.class)
                .setId(user.getId())
                .setView("user.edit");
        ExtUser extUser = dataManager.load(loadContext);

        if (extUser != null) {
            item.setSystemUser(extUser);
            item.setPartner(extUser.getPartner());
        }

    }
}