I'm resolving rights in AngularJS application... I would like to use custom directive for resolving what user can see by his role. It's only client side design. If user entry on page what he hasn't access, server return 403 for all requests for data from this page. My actual solution looks like this:
<li ng-repeat="menuItem in navigationItems" class='{{menuItem.css}}' restrict-access="menuItem.restrict">
<a href="#/{{menuItem.url}}">
<div class="label">
<span class="icon">
<span class="title">{{menuItem.title}}</span>
</span>
</div>
</a>
</li>
.directive('restrictAccess', function() {
return {
restrict: 'EA',
prioriry: 100000,
scope: {
restrictAccess: "=restrictAccess"
},
controller: ['$scope', '$element', function($scope, $element) {
var accessDenied = true;
var userRole = App.LoggedUser.userRole;
var attributes = $scope.restrictAccess.split(" ");
for (var i in attributes) {
if (userRole == attributes[i]) {
accessDenied = false;
}
}
if (accessDenied) {
$element.remove();
}
}]
};
})
There are many problems...
Controllers of widgets rendered by ng-include are faster than my directive and some ajax requests are sended to server before I remove widgets from DOM. I need directive what stops controllers in their child elements.
I hope it is understandable... Here is testing Fiddle
Thanks David
I used angularjs authentication interceptor in my last project. It suits what you need..
Inteceptor stores the request on every service request and broadcast message.. You can get broadcasted messages on your controllers. Here is the code I dealt with.. Checkout auth interceptor.
And in your controller :