如何向jhipster(4.0.3)应用程序添加新的用户角色 [英] How to add new user role to jhipster(4.0.3) application

查看:130
本文介绍了如何向jhipster(4.0.3)应用程序添加新的用户角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 Jhipster 创建了 angular 2 应用程序.我想在登录后使用我的应用程序中的新用户角色,例如 ROLE_MANAGER ,将他路由到其他视图,后端和UI中我需要更改的所有文件是什么,任何人都可以帮助我. /p>

我尝试更改未成功的以下文件,未在数据库表中添加新角色.

src\main\java\com\mycompany\myapp\security\AuthoritiesConstants.java
src\main\resources\config\liquibase\authorities.csv
src\main\resources\config\liquibase\users_authorities.csv

如果有人这样做过,请先说明如何将每个用户转到不同的视图.

解决方案

在当前版本中,可以通过对生成的角度应用程序进行以下更改来解决您的问题

将登录服务(login.service.ts)更改为返回帐户而不是令牌

login (credentials, callback?) {
    let cb = callback || function() {};

    return new Promise((resolve, reject) => {
        this.authServerProvider.login(credentials).subscribe(data => {
            this.principal.identity(true).then(account => {
                // After the login the language will be changed to
                // the language selected by the user during his registration
                if (account !== null) {
                    this.languageService.changeLanguage(account.langKey);
                }
                resolve(data);
            });
            return cb();
        }, err => {
            this.logout();
            reject(err);
            return cb(err);
        });
    });
}

resolve(data)更改为resolve(account)

在login.component.ts中捕获该帐户

在函数登录名中,像这样将帐户添加到then()

login () {
    this.loginService.login({
        username: this.username,
        password: this.password,
        rememberMe: this.rememberMe
    }).then((account: Account) => {
        this.authenticationError = false;
        this.activeModal.dismiss('login success');
        if (this.router.url === '/register' || this.router.url === '/activate' ||
            this.router.url === '/finishReset' || this.router.url === '/requestReset') {
            this.router.navigate(['']);
        }

        this.eventManager.broadcast({
            name: 'authenticationSuccess',
            content: 'Sending Authentication Success'
        });

        // // previousState was set in the authExpiredInterceptor before being redirected to login modal.
        // // since login is succesful, go to stored previousState and clear previousState
        let previousState = this.stateStorageService.getPreviousState();
        //**CHANGED** check if we have are a manager
        let isManager = account.authorities.indexOf("ROLE_MANAGER") > -1;
        if(isManager) { 
            this.stateStorageService.resetPreviousState();
            this.router.navigate(['your-manager-specific-state']);
        } 
        else if (previousState) {
            this.stateStorageService.resetPreviousState();
            this.router.navigate([previousState.name], { queryParams:  previousState.params });
        }
    }).catch(() => {
        this.authenticationError = true;
    });
}

这将检查该帐户是否存在ROLE_MANAGER角色,并覆盖默认行为,该默认行为会将用户重定向到以前的状态.

I've created angular 2 application using Jhipster. i want use new user role like ROLE_MANAGER in my application to route him to different view after logging in, what are all the files i need to change in back-end and in UI can anyone help me.

i tried changing following files not succeded, new role is not adding in database table.

src\main\java\com\mycompany\myapp\security\AuthoritiesConstants.java
src\main\resources\config\liquibase\authorities.csv
src\main\resources\config\liquibase\users_authorities.csv

if anyone done this before please explain how to route each user to different views.

解决方案

With the current version, your problem may solved by performing the following changes to your generated angular application

change the login service (login.service.ts) to return account instead of token

in

login (credentials, callback?) {
    let cb = callback || function() {};

    return new Promise((resolve, reject) => {
        this.authServerProvider.login(credentials).subscribe(data => {
            this.principal.identity(true).then(account => {
                // After the login the language will be changed to
                // the language selected by the user during his registration
                if (account !== null) {
                    this.languageService.changeLanguage(account.langKey);
                }
                resolve(data);
            });
            return cb();
        }, err => {
            this.logout();
            reject(err);
            return cb(err);
        });
    });
}

change resolve(data) to resolve(account)

catch the account in login.component.ts

in the function login, add account to then() like this

login () {
    this.loginService.login({
        username: this.username,
        password: this.password,
        rememberMe: this.rememberMe
    }).then((account: Account) => {
        this.authenticationError = false;
        this.activeModal.dismiss('login success');
        if (this.router.url === '/register' || this.router.url === '/activate' ||
            this.router.url === '/finishReset' || this.router.url === '/requestReset') {
            this.router.navigate(['']);
        }

        this.eventManager.broadcast({
            name: 'authenticationSuccess',
            content: 'Sending Authentication Success'
        });

        // // previousState was set in the authExpiredInterceptor before being redirected to login modal.
        // // since login is succesful, go to stored previousState and clear previousState
        let previousState = this.stateStorageService.getPreviousState();
        //**CHANGED** check if we have are a manager
        let isManager = account.authorities.indexOf("ROLE_MANAGER") > -1;
        if(isManager) { 
            this.stateStorageService.resetPreviousState();
            this.router.navigate(['your-manager-specific-state']);
        } 
        else if (previousState) {
            this.stateStorageService.resetPreviousState();
            this.router.navigate([previousState.name], { queryParams:  previousState.params });
        }
    }).catch(() => {
        this.authenticationError = true;
    });
}

This will check the account for having the ROLE_MANAGER role being present, and overrides the default behavior, which redirects the user to the previous state.

这篇关于如何向jhipster(4.0.3)应用程序添加新的用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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