Entity Attribute Access Control not being picked up by spring (possibly caused by extend and replace of entity)

Following the instructions here:
https://doc.cuba-platform.com/manual-6.8/entity_attribute_access.html

I used the code snippet under “Changes in version 6.8.1” and placed a breakpoint inside the handler class (defined in core module), but when I run the application, this breakpoint is not getting hit. The code appears to be correct per the documentation, it’s just not getting run.

I am using Cuba platform 6.8.9.

Also - the entity I’m creating the entity attribute access handler for is an extend/replace of another entity. I think that may be what’s causing the issue.

Thanks,

Hi,

Due to the problems encountered when access control handlers were implemented as Spring application event listeners, the mechanism was changed in the platform 6.8.1. The above rules are still in place by default, but the alternative way of implementing handlers has been introduced. The new way is turned on by the cuba.useSpringApplicationEventsToSetupAttributeAccess application property and will be the default and only way for version 6.9+

Please setup application property cuba.useSpringApplicationEventsToSetupAttributeAccess = false for using access control handlers in the 6.9 way.

Thank you,
Andrey

It is still not working for me. I have added the application property you mentioned. My breakpoint is still not being hit inside the class when I start the application and access the edit screen of the entity, and it is not setting my test fields either read only or hidden.

I am trying to make certain fields read-only based on entity state. Here is my code (request is an extend/replace of another entity).

@Component(RequestAttributeAccessHandler.NAME)
public class RequestAttributeAccessHandler implements SetupAttributeAccessHandler<Request> {
public static final String NAME = "sample_RequestAttributeAccessHandler";

@Inject
private UserSessionSource userSessionSource;

@Override
public boolean supports(Class clazz) {
    return Request.class.isAssignableFrom(clazz);
}

@Override
public void setupAccess(SetupAttributeAccessEvent<Request> event) {
    Request request = event.getEntity();

    //If user is customer,
    if (userSessionSource.getUserSession().getRoles().equals("TestRole")) {
        //and event request is not in draft,
        if (request.getState() != ParentEntityState.DRAFT) { //request is an extend/replace of the 'Parent' entity for this example
            event.addHidden("field1");
            event.addHidden("field2");
            event.addReadOnly("field3");
        }
    }
}
}

Then, in the Request.java file, I added an entry to @Listeners:

@Listeners({"sample_RequestAttributeAccessHandler","sample_RequestListener"})

Thanks

Could you provide a sample project with Request entity?

1 Like

Adding sample_RequestAttributeAccessHandler to the @Listeners annotation is absolutely not needed, your listeners will be found by the interface.
Please make sure you specified cuba.useSpringApplicationEventsToSetupAttributeAccess = false in the core app.properties.

1 Like

I tried the handler it in a sample project, and the Entity attr access control worked. Right now I am trying to get a sample project that inherits from another sample base so that I can isolate whether the entity extension is relevant to the issue. Thanks for the help, I will try to provide a sample soon.

This problem is fixed for me now. Some modifications were made to the parent project, and I implemented from the parent entity instead when calling attribute handler. The app.properties line was also originally missing, so thanks to Konstantin for the explicit instructions.

1 Like