Insert new user directly into his group at controller level

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. :smiley:

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); }

Hi,

It’s not clear from your post - what problems do you meet when trying “to insert the newly created Customer directly into either the Customers or Employees access group.”

P.S.
You should save both new entities: user and userRole at the same time, this code looks wrong:

dataManager.commit(user);

It should be

dataManager.commit(user, userRole);
1 Like

Hi, thanks for your reply and correction.
I try to explain my problem better.
In Access Group I have a hierarchy:

-Company;
-Customers;
-Employees;

And also roles:

-Customers;
-Employees;

I have Customer and Employee entity.

When I create the new Customer user and it is loaded into the system it should be going into the Customers access group. Same for Employee.

image

I would also be interested in assigning from controller the roles of the two different users that are registered in the system without putting the default role.

Thank you, I hope I explained the problem right! :smiley:

Hi,
Unfortunately, we cannot reproduce the problem. We will be able to help you if you send us a small sample project that demonstrates the issue.

BR,
Yulia Maistrenko

Please debug and make sure the association you are updating for Group in new user is correct. May be you have wrong UUID of Group.

final String COMPANY_GROUP_ID = "0fa2b1a5-1d68-4d69-9fbd-dff348347f93";

Verify this.