github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/public/static/js/directives/directives.spawn.js (about)

     1  var directives = directives || {};
     2  
     3  directives.spawn = angular.module('directives.spawn', ['filters.common']);
     4  
     5  directives.spawn.directive('userSpawnModal', function() {
     6    return {
     7      restrict: 'E',
     8      transclude: true,
     9      templateUrl: '/static/partials/user_spawn_modal.html'
    10    }
    11  });
    12  
    13  directives.spawn.directive('userHostOptions', function() {
    14    return {
    15      restrict: 'E',
    16      templateUrl: '/static/partials/user_host_options.html'
    17    }
    18  });
    19  
    20  directives.spawn.directive('userHostDetails', function() {
    21    return {
    22      restrict: 'E',
    23      templateUrl: '/static/partials/user_host_details.html'
    24    }
    25  });
    26  
    27  directives.spawn.directive('userHostTerminate', function() {
    28    return {
    29      restrict: 'E',
    30      templateUrl: '/static/partials/user_host_terminate.html'
    31    }
    32  });
    33  
    34  directives.spawn.directive('userHostUpdate', function() {
    35    return {
    36      restrict: 'E',
    37      templateUrl: '/static/partials/user_host_update.html'
    38    }
    39  });
    40  
    41  // Validation directives
    42  directives.spawn.directive('keyNameUnique', function() {
    43    return {
    44      require: 'ngModel',
    45      link: function(scope, elm, attrs, ctrl) {
    46        ctrl.$parsers.unshift(function(viewValue) {
    47          if (viewValue === '') {
    48            ctrl.$setValidity('keyNameUnique', false);
    49            return viewValue;
    50          }
    51          // ensure the key name isn't taken
    52          for (var i = 0; i < scope.userKeys.length; i++) {
    53            if (scope.userKeys[i].name === viewValue) {
    54              ctrl.$setValidity('keyNameUnique', false);
    55              return viewValue;
    56            }
    57          }
    58          ctrl.$setValidity('keyNameUnique', true);
    59          return viewValue;
    60        });
    61      }
    62    };
    63  });
    64  
    65  directives.spawn.directive('userDataValid', function() {
    66    return {
    67      require: 'ngModel',
    68      link: function(scope, elm, attrs, ctrl) {
    69        ctrl.$parsers.unshift(function(viewValue) {
    70          if (scope.selectedDistro.userDataValidate == 'x-www-form-urlencoded') {
    71            var arr = viewValue.split('&');
    72            if (arr.length == 0) {
    73              ctrl.$setValidity('userDataValid', false);
    74              return viewValue;
    75            }
    76            for(var n = 0; n < arr.length; n++) {
    77              var item = arr[n].split("=");
    78              if (item.length != 2) {
    79                  ctrl.$setValidity('userDataValid', false);
    80                  return viewValue;
    81              }
    82            }
    83          } else if (scope.selectedDistro.userDataValidate == 'json') {
    84            try {
    85              JSON.parse(viewValue);
    86            } catch (e) {
    87              ctrl.$setValidity('userDataValid', false);
    88              return viewValue;
    89            }
    90          } else if (scope.selectedDistro.userDataValidate == 'yaml') {
    91            try {
    92              jsyaml.load(viewValue);
    93            } catch (e) {
    94              ctrl.$setValidity('userDataValid', false);
    95              return viewValue;
    96            }
    97          }
    98          ctrl.$setValidity('userDataValid', true);
    99          return viewValue;
   100        });
   101      }
   102    };
   103  });
   104  
   105  var RSA = 'ssh-rsa';
   106  var DSS = 'ssh-dss';
   107  var BASE64REGEX = new RegExp("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$");
   108  
   109  directives.spawn.directive('keyBaseValid', function() {
   110    return {
   111      require: 'ngModel',
   112      link: function(scope, elm, attrs, ctrl) {
   113        ctrl.$parsers.unshift(function(viewValue) {
   114          // validate the public key
   115          var isRSA = viewValue.substring(0, RSA.length) === RSA;
   116          var isDSS = viewValue.substring(0, DSS.length) === DSS;
   117          if (!isRSA && !isDSS) {
   118            ctrl.$setValidity('keyBaseValid', false);
   119            return viewValue;
   120          }
   121          // do pseudo key validation
   122          var sections = viewValue.split(' ');
   123          if (sections.length < 2) {
   124            ctrl.$setValidity('keyBaseValid', false);
   125            return viewValue;
   126          }
   127          ctrl.$setValidity('keyBaseValid', BASE64REGEX.test(sections[1]));
   128          return viewValue;
   129        });
   130      }
   131    };
   132  });
   133  
   134  directives.spawn.directive('equals', function() {
   135    return {
   136      require: 'ngModel',
   137      link: function(scope, elem, attrs, ctrl) {
   138        // watch own value and re-validate on change
   139        scope.$watch(attrs.ctrl, function() {
   140          ensureEquals();
   141        });
   142  
   143        // observe the other value and re-validate on change
   144        attrs.$observe('equals', function (val) {
   145          ensureEquals();
   146        });
   147  
   148        var ensureEquals = function() {
   149          // values
   150          var password = ctrl.$viewValue;
   151          var cPassword = attrs.equals;
   152          // set validity
   153          scope.updateForm.uPassword.$setValidity("equals", password === cPassword);
   154          scope.updateForm.cPassword.$setValidity("equals", password === cPassword);
   155        };
   156      }
   157    }
   158  });
   159  
   160  // the regex below is used to ensure that a string meets the windows
   161  // password complexity requirements as described at:
   162  // http://technet.microsoft.com/en-us/library/cc786468(v=ws.10).aspx
   163  var passwordRegex = /(?=^.{6,255}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*/;
   164  
   165  directives.spawn.directive('complexity', function() {
   166    return {
   167      require: 'ngModel',
   168      link: function(scope, elm, attrs, ctrl) {
   169        ctrl.$parsers.unshift(function(viewValue) {
   170          // validate the password requirements
   171          ctrl.$setValidity('complexity', passwordRegex.test(viewValue));
   172          return viewValue;
   173        });
   174      }
   175    };
   176  });