且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何向jhipster(4.0.3)应用程序添加新的用户角色

更新时间:2022-10-22 21:34:00

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

将登录服务(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.