2016-09-13 88 views
5

我正在升级到[email protected]并试图找出如果用户没有必要的权限,将用户重定向到其他状态的最佳方式是什么。基于用户角色的UI-Router 1.0重定向

这是我有:

.state('company_dashboard', { 
    url: '/dashboard', 
    templateUrl: 'dashboard.html', 
    controller: 'CompanyDashboardCtrl', 
    resolve: { 
     user: function(UserService) { 
      return UserService.getActiveUser(); 
     }, 
     company: function(UserService) { 
      return CompanyService.getActiveCompany(); 
     }, 
     permissions: function(CompanyService, user, company){ 
      return CompanyService.getUserPermissions(company, user); 
     } 
    }, 
    onEnter: function($state, permissions) { 
     if (permissions.canAccessDashboard === false) 
      return $state.go('company_landing_page'); 
    } 
}) 

上面的例子作品,但它记录了以下错误:TransitionRejection(type: 2, message: The transition has been superseded by a different transition, detail: Transition#1(''{} -> 'company_landing_page'{}))这让我觉得,应该有更好的方式。

旁边的问题,在resolve期间检查权限是否是一个好习惯?

UPDATE:

更改$state.go()$state.target()帮我摆脱控制台错误的。 This comment已帮助。 虽然这个问题的立场,我是否在做这个地方呢?

+0

我用来检查由spring或后端框架构建的模板的权限,我认为检查前端的权限并不好,因为每个人都可以看到代码。 –

回答

1

我将数据属性添加到您的状态如下:

.state('company_dashboard', { 
    data: { 
      requiresAuthentication: true 
    } 
} 

然后:

app.run(function ($rootScope, $state) { 
    $rootScope.$on('$stateChangeStart', function (event, toState, toParams) { 
     // Check if user is authenticated 
     // And route requires authentication 
     if (!toState.data.requiresAuthentication) { 
      $state.go(toState.name, toParams); 
     } else if (user && toState.data.requiresAuthentication) { 
      $state.go(toState.name, toParams); 
     } else { 
      $state.go('login'); 
     } 
    }); 
}); 

这似乎是工作得很好,即使我相信它可以更精致,但我希望这给你一个关于你如何进行的想法。