【问题标题】:routeprovider resolve not restricting access with parse.comrouteprovider 解析不使用 parse.com 限制访问
【发布时间】:2018-02-23 18:13:45
【问题描述】:

我想将页面限制为仅限管理员。我发现我的身份验证工厂在解析返回路由答案之前没有完成。当我关注/观看代码路径页面在工厂完成之前加载时。 我在想我需要在承诺和/或“.then()”方面做得更好。

任何帮助将不胜感激。谢谢

我的路线代码:

.when("/admin", {
    templateUrl : "templates/admin.htm",
    controller: 'AdminCtrl',
    resolve : {
            'auth' : function(AuthService){
                return AuthService.isAdmin();
            }
    }
}).run(function($rootScope, $location){
    $rootScope.$on('$routeChangeError', function(event, current, previous, rejection){
        if(rejection === 'Not Administrator'){
            $location.path('/404');
        }
        if(rejection === 'Not Authenticated'){
            $location.path('/404');
        }
    });
});

我的工厂:

    app.factory('AuthService', function($q){
    var isAuthenticated = Parse.User.current();
    var isAdmin = undefined;
    var currentUser = Parse.User.current();
        return {
            authenticate : function(){
                if(isAuthenticated){
                    return true;
                } else {
                    return $q.reject('Not Authenticated');
                }
            },
            isAdmin : function(){
                if(isAuthenticated){
                    userHasRole(currentUser, "Administrator").then(function(isRole) {
                        console.log((isRole)? "user is admin" : "user is not admin");
                        isAdmin = isRole;
                        return isAdmin;
                    }).then(function(isAdmin){
                        if(isAdmin){
                            return true;
                        } else {
                            return $q.reject('Not Administrator');
                        }
                    });
                } else {
                    return $q.reject('Not Authenticated');
                }
            }
        };
});  

function userHasRole(user, roleName) {
    var query = new Parse.Query(Parse.Role);
    query.equalTo("name", roleName);
    query.equalTo("users", user);
    return query.find().then(function(roles) {
        if(roles.length > 0){
            return true;
        }else{
            return false;
        }
    });
}

【问题讨论】:

  • 我想我的安全角色可能会导致这种情况,有什么想法吗?

标签: angularjs parse-platform roles route-provider


【解决方案1】:

如此接近。

isAdmin 正在返回 undefined,因为您在 userHasRole 之前缺少 return 语句,因此您正在调用它,但随后 isAdmin 在没有返回值的情况下到达 if( isAuthenticated ) 块的末尾,因为它没有'不知道它应该返回userHasRole的结果。

【讨论】:

  • 你的意思是我需要 if(isAuthenticated){ return { userHasRole......} }else{..... 吗?这给了我 lint 错误意外令牌??
  • 你不会将它包装在{ }s 中,只是return userHasRole... 该函数在主线程继续运行时被异步触发,因为主线程不知道它应该等待返回值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-10
  • 2015-10-29
  • 2014-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多