0

How can I set up default roles in jhipster ? (using angularjs and spring).

I explain myself

in the registration page I want to specify the role for the registred user. let's say by a checkbox or a list. (for exemple human and animal )

How can I do that in the angular controller and in spring ?

What I can do now ?

I added the roles I need in the database and in angular and I can specify the roles for the new registred users , only through the Admin's users management page.

Youssef El Behi
  • 3,042
  • 2
  • 13
  • 22

1 Answers1

1

There is some work to do, to achieve that, so I will paste just the right parts with some small samples..

In general you must extend the API to become aware of a role selection, so this information can be provided explicitly. Then you change your angularJS frontend as you need.

for the backend

a registration happens by POSTing a ManagedUserVM to /api/account/register, so the first thing is to tell AccountResource.registerAccount(...) to pass a set of of strings (your roles) as additional parameter to userService.createUser

@Timed
public ResponseEntity registerAccount(@Valid @RequestBody ManagedUserVM managedUserVM) {

    HttpHeaders textPlainHeaders = new HttpHeaders();
    ///...
                User user = userService
                    .createUser(managedUserVM.getLogin(), 
                                managedUserVM.getPassword(),
                                managedUserVM.getFirstName(), 
                                managedUserVM.getLastName(),
                                managedUserVM.getEmail().toLowerCase(), 
                                managedUserVM.getImageUrl(), 
                                managedUserVM.getLangKey(),
                                //add authorities here
                                managedUserVM.getAuthorities()
                                );

                mailService.sendActivationEmail(user);
                //...
}

Then in UserService.createUser, you apply the set and add them to the user before saving it, by adding the Set<String> authorities to its parameters and

    if (authorities != null) {
        Set<Authority> authorities = new HashSet<>();
        authorities.forEach(
            authority -> authorities.add(authorityRepository.findOne(authority))
        );
        user.setAuthorities(authorities);
    }

and this should be sufficient to pass authorities to /api/register/ and save them. You should be aware of users forbid to register themselves with ADMIN roles, but the security consideration is up to you and not part my answer.

apply to frontend

Knowing your API now can process also authorities, you could just pass them.

You just add some checkbox or selectbox with ng-model="vm.registerAccount.authorities" to src/main/webapp/app/account/register/register.html (if angularJS1) or [(ngModel)]="registerAccount.authorities" tosrc/main/webapp/app/account/register/register.component.html` (if angular2).

AFAIK this should lead automatically to the angular services passing these authorities/roles to the API.

I hope my brief answer helps you to find the proper places! Feel free to ask in comments if you stuck

David Steiman
  • 3,055
  • 2
  • 16
  • 22
  • thanks a lot David! I already found a way to do it and it works perfectly, I don't know if it's the optimal best practice way tho. I'll post it as soon as possible. – Youssef El Behi Feb 04 '17 at 00:00