Check profile directive
by Michael Hunziker
HTML
<div ng-controller="TestCtrl">
<div check-profile map-cp-allow-profile="variable">
DIV
</div>
</div>
JavaScript
angular.module('myApp', [])
.controller('TestCtrl', function($scope) {
$scope.variable = "CSU";
})
.directive('checkProfile', function(ngIfDirective, CheckProfileService) {
var ngIf = ngIfDirective[0];
return {
multiElement: ngIf.multiElement,
transclude: ngIf.transclude,
priority: ngIf.priority - 1,
terminal: ngIf.terminal,
restrict: ngIf.restrict,
$$tlb: true,
link: function($scope, $element, $attr) {
var linkFunctionArguments = arguments;
var allowProfiles = CheckProfileService.toProfilesArray($attr.mapCpAllowProfile, $scope),
denyProfiles = CheckProfileService.toProfilesArray($attr.mapCpDenyProfile, $scope);
$attr.ngIf = function() {
return CheckProfileService.allowedAndNotDenied(allowProfiles, denyProfiles);
};
ngIf.link.apply(ngIf, linkFunctionArguments);
}
};
})
.factory('CheckProfileService', function(ProfileService) {
var service = {
toProfilesArray: function(input, $scope) {
var values,
flatValues = [];
if (!(input)) {
return flatValues;
}
values = input.split(',').map(function(profile) {
var value = $scope.$eval(profile);
return value === undefined ? [profile] : value;
});
return flatValues.concat.apply(flatValues, values);
},
allowedAndNotDenied: function(allowProfiles, denyProfiles) {
var allowed = true,
notDenied;
if (allowProfiles.length) {
console.log(allowProfiles);
allowed = ProfileService.isMember.apply(this, allowProfiles);
}
notDenied = !ProfileService.isMember.apply(this, denyProfiles);
return allowed && notDenied;
}
};
return service;
})
.factory('ProfileService', function() {
var profileModelProfiles = ['CSU'];
var service = {
isMember: function() {
for (var i = 0; i < arguments.length; i++) {
if (profileModelProfiles && profileModelProfiles.indexOf(arguments[i]) >...