Hi,
I write authorization in angularjs and I have this problem.
I'm trying to use sections of resolve in routeprovider, but not fully me it
works. Because I do not know how I can get the next level routeprovider
view ACCESS_LEVEL.
app.js
var app = angular.module("app", ["ngRoute", "ngAnimate", "ngResource",
"ui.bootstrap"]);
app.constant('ACCESS_LEVELS', {
guest: 3,
user: 2,
admin: 1
});
app.config(["$routeProvider", "$locationProvider", "ACCESS_LEVELS", function
($routeProvider, $locationProvider, ACCESS_LEVELS) {
$routeProvider
.when("/", {
template: "",
label: "Home",
access_level: ACCESS_LEVELS.guest
})
.when("/edit-user", {
templateUrl: "views/edit-user.html",
label: "Edit User",
access_level: ACCESS_LEVELS.user
})
.when("/users", {
templateUrl: "views/users.html",
label: "Users",
controller: "UsersListController",
access_level: ACCESS_LEVELS.admin,
resolve: {
checkPerm: function (LoginService) {
return LoginService.checkPermission(access_level).then(
function (response) {
return response; // how get actual access level ?
});
}
}
})
.otherwise({
templateUrl: "views/404.html",
label: "404"
});
}]);
app.run(["$rootScope", "$location", "LoginService", function($rootScope ,
$location, LoginService) {
$rootScope.$on('$routeChangeStart', function(e, next, curr) {
if(next.access_level != undefined) {
LoginService.checkSession().then(function(response) {
$rootScope.isLoggedIn = response;
}, function(reject) {
$rootScope.isLoggedIn = reject;
}).then(function() {
if($rootScope.isLoggedIn) {
return LoginService.getUser();
}
}).then(function(response) {
if($rootScope.isLoggedIn) {
$rootScope.firstname = response.firstName;
$rootScope.lastname = response.lastName;
}
/*if(!LoginService.checkPermission(response.roleList,
next.access_level)) {
$location.path("/404");
}*/
}, function(err) {
console.error(err);
});
}
});
}]);
LoginService.js:
app.factory("LoginService", function($resource, $q) {
var LoginService;
LoginService = {
checkPermission: function(actualPermView) {
if(_.find(roleList, function(el) { return el.id <=
actualPermView; }) != undefined) {
return true;
} else {
return false;
}
}
};
return LoginService;
});
UsersListController.js:
app.controller("UsersListController", function($scope, $routeParams, $q,
$location, $timeout, checkPerm ,UserService) {
$scope.checkPermission = checkPerm;
});
and with view i have this code:
<div class="col-sm-12" ng-if="checkPermission == true" ng-init="checkPermission
= false">
Please help, a few days working on that.
Regards,
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.