First of all: This project is awesome.. Thanks!!
The only part I dislike is the "accessLevel" directive. There, you show or hide elements according the authorize method. However, in terms of security (which is the key in this project) delegate the visibility of elements to CSS is not the better choice.
My workaround was use the ngIf directive to give a more professional behaviour.
.directive('accessLevel', ['ngIfDirective', 'Auth', function(ngIfDirective, Auth) {
var ngIf = ngIfDirective[0];
return {
transclude: ngIf.transclude,
priority: ngIf.priority,
terminal: ngIf.terminal,
restrict: ngIf.restrict,
link: function($scope, $element, $attr) {
var visibility = false,
userRole, accessLevel;
$scope.$watch('user', function(user) {
if(user.role){
userRole = user.role;
}
updateVisibility();
}, true);
$attr.$observe('accessLevel', function(al) {
if(al){
accessLevel = $scope.$eval(al);
}
updateVisibility();
});
function updateVisibility() {
if(userRole && accessLevel) {
visibility = Auth.authorize(accessLevel, userRole);
$attr.ngIf = function() { return visibility; };
}
}
$attr.ngIf = function() { return visibility; };
ngIf.link.apply(ngIf, arguments);
}
};
As you can see, the logic is pretty close your original directive and the directive structure is delegated to ngIf.
Btw, this solution is based on this: http://stackoverflow.com/questions/20325480/angularjs-whats-the-best-practice-to-add-ngif-to-a-directive-programmatically
Let me know what do you think.
First of all: This project is awesome.. Thanks!!
The only part I dislike is the "accessLevel" directive. There, you show or hide elements according the authorize method. However, in terms of security (which is the key in this project) delegate the visibility of elements to CSS is not the better choice.
My workaround was use the ngIf directive to give a more professional behaviour.
.directive('accessLevel', ['ngIfDirective', 'Auth', function(ngIfDirective, Auth) { var ngIf = ngIfDirective[0]; return { transclude: ngIf.transclude, priority: ngIf.priority, terminal: ngIf.terminal, restrict: ngIf.restrict, link: function($scope, $element, $attr) { var visibility = false, userRole, accessLevel; $scope.$watch('user', function(user) { if(user.role){ userRole = user.role; } updateVisibility(); }, true); $attr.$observe('accessLevel', function(al) { if(al){ accessLevel = $scope.$eval(al); } updateVisibility(); }); function updateVisibility() { if(userRole && accessLevel) { visibility = Auth.authorize(accessLevel, userRole); $attr.ngIf = function() { return visibility; }; } } $attr.ngIf = function() { return visibility; }; ngIf.link.apply(ngIf, arguments); } };As you can see, the logic is pretty close your original directive and the directive structure is delegated to ngIf.
Btw, this solution is based on this: http://stackoverflow.com/questions/20325480/angularjs-whats-the-best-practice-to-add-ngif-to-a-directive-programmatically
Let me know what do you think.