in memory constraint doesnt work


Some background info about project
I’m developing application with two datastore:

  1. PostgreSQL for system Cuba data
  2. additional datastore MSSQL 2000 for data from legacy application.
Cuba version 6.4.4 (because mssql 2000), studio version is 6.5.1.

I’ve extended User with association field Courier (it is transient because User in main db, Courier Entity located in mssql additional datastore, in main database stored only Long Id to Entity from other datastore). Order have association with Courier.

I need to REST / WEB client to see only Orders assigned to it.
I’m used access group to restrict orders to same courierId that is same as in extended field of User Entity. Because of Entities UserExt and Order are from two different datastores - I can not use only database constraint. (I tried but get only errors. May be its possible do it in database constraint)?

Then I tried that groovy code and set in memory and database constraint:

{E} ==

But this doesnt filter out by courierID. And I tried to set groovy code to “false” - but this doesnt work too.
And If I set {E} == 1L (this ID not assignet to any Order or any UserExt) I’ll get all records.

Where is my mistake? Can you help me?

Not sure why in-memory constraints don’t work in your case, but I would recommend implementing constraints on the database level. I think it can be done if you define a session attribute which is set to the courrier ID of the current user.

First, you need to set the attribute upon user login. As long as you need to work both via web UI and REST API, it should be done on the middleware. Unfortunately, there is no public API for intercepting login on the middleware yet (see, so you have to override the UserSessionManager bean. Create a new class in the core module as follows:

package com.sample.sales.core;


import java.util.Locale;
import java.util.UUID;

public class MyUserSessionManager extends UserSessionManager {

    public UserSession createSession(UUID sessionId, User user, Locale locale, boolean system) {
        UserSession session = super.createSession(sessionId, user, locale, system);
        Long courrierId = ((MyUser) user).getCourrierId();
        if (courrierId != null)
            session.setAttribute("courrierId", courrierId);
        return session;

Then register your bean in spring.xml of the core module:

<bean id="cuba_UserSessionManager" class="com.sample.sales.core.MyUserSessionManager"/>

After that, you can use the courrierId session attribute in database-level constraints:

{E} = :session$courrierId
1 Like

Konstantin, thank you! Works as expected!
Thank you very much again.