Hello everyone, I have a problem.
I have created a Customer entity and then a listener with oneBeforeInsert() that loads each user created in the System User.
Next I created a Customers access group and another one Employees.
I am unable to insert the newly created Customer directly into either the Customers or Employees access group.
In some discussions I found this way by setting COMPANY_GROUP_ID but I would need something to insert them in subgroups (Customer or Employees) and not in Company, and also the associated Roles.
I hope someone can help me. Below I post the code. Thanks.
public void onBeforeInsert(Customer entity, EntityManager entityManager) {
/**
* ID of the Group for self-registered users.
*/
final String COMPANY_GROUP_ID = "0fa2b1a5-1d68-4d69-9fbd-dff348347f93";
/**
* ID of the Role to be assigned to self-registered users.
*/
final String DEFAULT_ROLE_ID = "3ec31528-dc0e-c341-7727-7b46771ae9ff";
// Load group and role to be assigned to the new user
Group group = dataManager.load(LoadContext.create(Group.class).setId(UUID.fromString(CUSTOMER_GROUP_ID)));
Role role = dataManager.load(LoadContext.create(Role.class).setId(UUID.fromString(DEFAULT_ROLE_ID)));
User user=metadata.create(com.haulmont.cuba.security.entity.User.class);
user.setPassword(passwordEncryption.getPasswordHash(user.getId(), entity.getPassword()));
user.setName(entity.getName());
user.setLogin(entity.getNameLogin());
user.setGroup(group);
UserRole userRole = metadata.create(UserRole.class);
userRole.setUser(user);
userRole.setRole(role);
dataManager.commit(user); }