I want to call a rest API if a new role is created. My original Idea was to add an event listener for ResourceRole Entity, but it seems that it is not accessible. I want so send the role code as data for API.
Any help would be appreciated.
If you mean with not accessible → it does not show up in the entity dropdown when creating a new entity event through the studio GUI. You are right it does not show up.
But if I create my listener from scratch it works fine.
The only thing to keep in mind is that io.jmix.security.model.ResourceRole
is just a POJO serving as a DTO. The actual entity is io.jmix.securitydata.entity.ResourceRoleEntity
.
Following code works, i tested it
package mypackage;
import io.jmix.core.event.EntitySavingEvent;
import io.jmix.securitydata.entity.ResourceRoleEntity;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component("octo_ResourceRoleEventListener")
public class ResourceRoleEventListener {
@EventListener
public void onTemplateSaving(final EntitySavingEvent<ResourceRoleEntity> event) {
System.out.println("ResourceRoleEventListener.onTemplateSaving");
}
}
HI @tom.monnier
Thanks for your response.
I just have one question, when you say that this code works, you mean that similar would be the case if I want to access its superclass BaseRoleEntity…? I mean I want to access the role code that someone would give in UI… and send it to keycloak via REST API… So that I do not need to go and create roles in both the places. Also in my case all the roles can be realm roles in Keycloak Side. There is no segregation for roles on Keycloak side.
I mean I understand there might be a better way to accomplish what I am trying to do… but at this point any help is welcomed and appreciated.
Thanks in advance.
Hi John,
BaseRole is a simple POJO and I do not think a BaseRoleEntity exists, so I do not think you can do what you want done with that.
@tom.monnier gave the right Idea, and if you wish to capture event for both, Resource Roles and Row-Level Roles… you can create different methods for them. You can go about it like so:
package com.xxx.xxx.listener;
import io.jmix.core.event.EntitySavingEvent;
import io.jmix.securitydata.entity.ResourceRoleEntity;
import io.jmix.securitydata.entity.RowLevelRoleEntity;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component("octo_RoleEventListener")
public class RoleEventListener {
@EventListener
public void onTemplateSaving(final EntitySavingEvent<ResourceRoleEntity> event) {
String roleCode = event.getEntity().getCode();
String roleDescription = event.getEntity().getDescription();
System.out.println("BaseRoleEventListener.onTemplateSaving : -> " + roleCode + ": " + roleDescription);
}
@EventListener
public void onTemplateSavingRowLevelRole(final EntitySavingEvent<RowLevelRoleEntity> event) {
String roleCode = event.getEntity().getCode();
String roleDescription = event.getEntity().getDescription();
System.out.println("BaseRoleEventListener.onTemplateSaving Row Level: -> " + roleCode + ": " + roleDescription);
}
}
I have tested it and it works. Now you can do your REST calls to Keycloak using role code and description.