在jhipster中设置新的默认角色 [英] Setting up new default Roles in jhipster

查看:109
本文介绍了在jhipster中设置新的默认角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在jhipster中设置默认角色? (使用angularjs和spring).

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

我解释自己

在注册页面中,我想指定注册用户的角色.用一个复选框或一个列表来说. (例如人类和动物)

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 ?

我现在能做什么?

我在数据库中和在angular中添加了所需的角色,并且只能通过管理员的用户管理页面为新注册的用户指定角色.

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.

推荐答案

要实现这一目标,需要做一些工作,所以我将正确的部分粘贴一些小样本.

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

通常,您必须扩展API才能了解角色选择,因此可以显式提供此信息.然后,您可以根据需要更改angularJS前端.

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.

通过将ManagedUserVM张贴到/api/account/register来进行注册,因此第一件事是告诉AccountResource.registerAccount(...)将一组字符串(您的角色)作为附加参数传递给userService.createUser

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

然后在UserService.createUser中,通过将Set<String> authorities添加到其参数,然后套用集并将其添加到用户,然后再保存

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

,这足以将授权传递给/api/register/并保存它们.您应该意识到用户禁止向ADMIN角色注册,但是安全性的考量取决于您自己,而不是我的答案.

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.

知道您的API现在也可以处理权限,您可以通过它们.

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

您只需在src/main/webapp/app/account/register/register.html中添加带有ng-model="vm.registerAccount.authorities"的复选框或选择框(如果angularJS1)或 [(ngModel)]="registerAccount.authorities" to src/main/webapp/app/account/register/register.component.html`(如果为angular2).

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,这将自动导致将这些权限/角色传递给API的角度服务.

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

这篇关于在jhipster中设置新的默认角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆