Match an entity 'user' with actual signed in user

Hi, dear everyone. I don’t know if it’s more a question or idea, but…

First of all thank you for developing such an amazing product. I’m new to it but I already have a few questions. First things first:

  • sample project with orders products and customers. How could I (and could I) match a customer with a user that should log in to studio? Or I could force my users to login and get meta info about them on some screen in app? If the second option is the only option - how could I group my users? I mean group name is not enough, I need to have more information about a group.

Having this feature done - I’ll get an access to versioning any entities in my project “from the box”. That would be awesome.
Thanks again. Ivan

Hi Ivan,

>How could I (and could I) match a customer with a user that should log in to studio?

Probably you mean “log in to the application”. So in your Customer entity you can create a reference to sec$User entity, and when you need to find a Customer instance corresponding to the currently logged in user execute a query:

select c from sample$Customer c where c.user.id = :userId

with the parameter value obtained from UserSession.getUser().getId().

>Having this feature done - I’ll get an access to versioning any entities in my project “from the box”.

Sorry, I don’t understand what you mean.

Yes, I meant from the application of course.

The theme of versioning is completely out of this topic so I will ask it later. Thank you

I don’t know if I have to create another topic for my question, but I have one more question with that ‘user’ thing, if you don’t mind.

I don’t know if I have to create another topic for my question, but I have one more question with that ‘user’ thing, if you don’t mind.

How could I automatically add information about customer (matched to user) into order? E.g. When I create an order it asks me to choose a customer (even if I’m logged in as customer, not administrator) I want my app to identify a customer and automatically add information about him into order, as an order is created (or if it’s edited I want a user to be added to a special field ‘last edited by’ (not just view, but database too).

As I do the request to load context on entity edition - I get JPQLException: problem compiling [select c from usr$Employee c where c.user.id = :userId]. [35, 44] The state field path ‘c.user.id’ cannot be resolved to a valid type.

The simplest way is to initialize the reference to Customer in the initNewItem() method of the Order editor screen, see https://doc.cuba-platform.com/manual-6.5/init_values_in_initNewItem.html for details. Load the customer using DataManager with the query that I mentioned above and set it to new Order instance.

As I do

@Override 
protected void initNewItem(Event item) { 
    LoadContext loadContext = LoadContext.create(Employee.class) 
        .setQuery(LoadContext.createQuery("select c from usr$Employee c where c.user.id = :userId") 
            .setParameter("userId", userSssion.getUser().getId())); 
    employee = (Employee) dataManager.load(loadContext); 
} 

I get a NullPointerException on a line with getuser.getid() method invocation. I guess I’m doing something wrong but can’t understand what

And wh i set a breakpoint here on the initNewItem() method it doesn’t stop executing on a method start but still throws a NPE. Maybe I have to get an user session in some other way?

How do you get your “userSssion” variable?

Now I have no NPE but the value is still not set. Should I load some additional data sources or do additional updates?

@Inject 
private UserSession userSession; 
@Override  
protected void initNewItem(Event item) {  
    LoadContext loadContext = LoadContext.create(Employee.class)  
        .setQuery(LoadContext.createQuery("select c from usr$Employee c where c.user.id = :userId")  
            .setParameter("userId", userSession.getUser().getId()));  
    employee = (Employee) dataManager.load(loadContext);  
    Item.setIsCreatedBy(employee); 
} 

// isCreatedBy is a method that should hold and association to Employee.
As I create a new Event and it is shown on a browser screen - I can’t see who created it

Can you debug your code and confirm that your query returns an instance of Employee?

It doesn’t, but I assume I can do anything else wrong

IMG_0396

So it looks like there is no Employee with “user” attribute that matches to the current user. Check your database.

But I’m sure i did it. As you told me in your first answer. I can attach my project… The only thing that i found strange - the name of column on ‘events’ view is ‘userid’ but every field is named ‘user’. I double-checked it.

Usr.zip (23.5K)

It turned out that EclipseLink does not execute JPQL with condition on an attribute with name “user”. It is actually an SQL reserved word. After renaming the attribute to “systemUser” your code works as expected - see the project attached.

We’ll think how to avoid such problems in the future.

Usr.zip (31.9K)

1 Like

in MySQL the reserved words should be written in single quotes like “select c from usr$Employee where c.‘user’.id = :userId”… maybe we should try something like that?

I think it won’t work.