简体   繁体   English

JHipster,AngularJS-登录后,基于用户权限如何将用户带到不同页面

[英]JHipster, AngularJS- After login, based on user authority how to take user to different pages

new to both AngularJS & JHipster and appreciate any help. 是AngularJS和JHipster的新功能,感谢您的帮助。

I currently have two user authorities, ROLE_ADMIN, and ROLE_USER. 我目前有两个用户权限,ROLE_ADMIN和ROLE_USER。 After logging in I would like to be able to direct the user to different pages (that they have access to). 登录后,我希望能够将用户定向到其他页面(他们可以访问)。 I believe I have been able to narrow down the issue to my sign in controller: 我相信我已经能够将问题缩小到我登录控制器的范围:

function login (event) {
event.preventDefault();
Auth.login({
    email: vm.email,
    password: vm.password,
    rememberMe: vm.rememberMe
}).then(function () {
    vm.authenticationError = false;
    $state.go('home', {}, {reload: true});

    $rootScope.$broadcast('authenticationSuccess');

    // previousState was set in the authExpiredInterceptor before being redirected to login modal.
    // since login is successful, go to stored previousState and clear previousState
    if (Auth.getPreviousState()) {
        var previousState = Auth.getPreviousState();
        Auth.resetPreviousState();
        $state.go(previousState.name, previousState.params);
    }
}).catch(function () {
    vm.authenticationError = true;
});}

If I change $state.go('home', {}, {reload: true}); 如果我更改$state.go('home', {}, {reload: true}); to $state.go('trips', {}, {reload: true}); $state.go('trips', {}, {reload: true}); ('home' -> 'trips'), or change that to another page it works fine, so long as that user has the proper privileges, if not it goes to an error page. ('home'->'trips'),或者将其更改为另一个页面,只要该用户具有适当的特权,就可以正常工作;否则,它会转到错误页面。

Intuitively, I would like to add something like: 直观地说,我想添加以下内容:

if (authority === 'ROLE_ADMIN') {
    $state.go('home', {}, {reload: true});
} else if (authority === 'ROLE_USER') {
    $state.go('trips', {}, {reload: true});
}

But I cannot find a way to access what authority I am in/using. 但是我找不到一种方法来访问我正在/正在使用的权限。 I have also looked in auth.service.js and logged about everything I could think of to see if it is lurking in there somewhere. 我还查看了auth.service.js,并记录了我可能想到的所有内容,以查看它是否潜伏在某个地方。

If anyone has any suggestions or can point me in the right direction that would be very helpful! 如果有人有任何建议或可以指出正确的方向,那将非常有帮助! Thanks! 谢谢!

stumbled across a solution with the help of a friend and wanted to share in case anyone else had the same issue... or, even better, a more elegant solution. 在朋友的帮助下偶然发现了一个解决方案,并希望在其他人遇到相同问题的情况下进行分享……甚至更好的是,一个更优雅的解决方案。

Looking into what Principal was doing in some of the other controllers found that result will return true or false 查看Principal在其他一些控制器中的操作后发现result将返回truefalse

Principal.hasAuthority('ROLE_XYZ').then( function(result) {
    if(result) {
        $state.go('trips', {}, {reload: true});
    }
Principal.hasAuthority('ROLE_QWEQWE').then( function(result) {
    if(result) {
        $state.go('home', {}, {reload: true});
    }

As a result I just repeated and replaced ROLE_XYZ with the authority in question and that routes me to the page I am looking to get to. 结果,我只是重复了一次,并用有问题的权限替换了ROLE_XYZ ,这将我带到了我要访问的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AngularJS-在路由到其他控制器之前提示用户以保存更改 - AngularJS- Prompting the user before routing to other controller to save changes AngularJS,登录后将用户重定向到正确的页面 - AngularJS, Redirect user to correct page after login 用户使用angularjs登录后显示隐藏的元素 - show a hidden element after user login with angularjs 登录AngularJS后显示用户信息 - Show user info after login AngularJS Angular.js-如何防止用户不登录就进入内页以及如何在成功登录后存储会话 - Angularjs - How to prevent user from going to inner pages without logging in and how to store session upon successfull login AngularJS基于用户代理的不同模板 - AngularJS different template based on User Agent AngularJS-每个路由和控制器中的登录和身份验证 - AngularJS- Login and Authentication in each route and controller 在 Angularjs 中,如何阻止用户接近某些页面? - In Angularjs, how to block user approaching to some pages? 如何处理$ rootscope并获取AngularJS中登录用户的用户ID? - How to handle $rootscope and take user id of logged in user in AngularJS? 使用Angularjs和Firebase登录用户 - User login with Angularjs and Firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM