Creating Users Programatically

Hi,

I am importing the user base of my old app in my JMIX application, and am having some issues while importing them. I am being able to create the user entities and generate the encrypted passwords. However none of the users will be able to login.

I am using a JMX Bean to perform the import, with the following code as the main code snippet:

User u = dataManager.create(User.class);
            u.setFirstName(data[1]);
            u.setLastName(data[2] + " " + data[3]);
            u.setActive(true);
            u.setEmail(data[1]+"."+data[2]+"@domain.com");

            String pwd = pe.encode("00000");
            u.setPassword(pwd);

            u.setUsername((data[1]+data[2]).replace(" ", ""));
            u.setVersion(1);

            dataManager.save(u);

Any help is greatly appreciated.

Thanks in advance.
Carlos.

have you assigned any role to the users? at least role ui-minimal?

1 Like

That’s very likely what I am missing. I reached to get working the following but still doesn’t work:

AnnotatedResourceRoleProvider arrp = (AnnotatedResourceRoleProvider) 
applicationContext.getBean("sec_AnnotatedResourceRoleProvider");

RoleGrantedAuthority o = RoleGrantedAuthority.ofResourceRole(arrp.getRoleByCode("ui-minimal"));
u.setAuthorities(Arrays.asList(new RoleGrantedAuthority[]{o}));

But still need to set ui-minimal manually through the role management screens. Have a big bunch of users. So I would appreciate being able to code that one step.

Many thanks in advance,
Carlos.

Hi,
Here is the working example: https://github.com/Haulmont/jmix-quickstart/blob/security-advanced-complete/src/main/java/com/company/jmixpm/app/RegistrationService.java#L130

Just fill and save a RoleAssignmentEntity instance.

1 Like

And note that in Jmix everything is not permitted by default.
For quick initial testing you can assign “system-full-access” resource role.
In real scenario you should design some roles for imported users beforehand.

Thanks Alexander. Ammended as pointed in the example.

Works like a charm.
Regards,
Carlos.