all files / src/utils/ AccessHelper.js

37.89% Statements 36/95
24.19% Branches 15/62
8.33% Functions 2/24
37.23% Lines 35/94
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219                                                      16×     16× 16×         14× 14× 11× 10×                                                                                                                                                                                                                                                                                                              
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SimulationHelper = exports.simulationFunctions = exports.ProjectHelper = exports.projectFunctions = undefined;
exports.userHasAccess = userHasAccess;
 
var _react = require('react');
 
var _react2 = _interopRequireDefault(_react);
 
var _Theme = require('HPCCloudStyle/Theme.mcss');
 
var _Theme2 = _interopRequireDefault(_Theme);
 
var _ImageIcon = require('../panels/ImageIcon');
 
var _ImageIcon2 = _interopRequireDefault(_ImageIcon);
 
var _IconActionList = require('../panels/IconActionList');
 
var _IconActionList2 = _interopRequireDefault(_IconActionList);
 
var _workflows = require('../workflows');
 
var _workflows2 = _interopRequireDefault(_workflows);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
var Workflow = void 0;
Eif (process.env.NODE_ENV !== 'test') {
  Workflow = _workflows2.default;
} else {
  Workflow = { test: { name: 'test', logo: 'my-logo' } };
}
 
/* eslint-disable new-cap */
var SHORT_DESCRIPTION_SIZE = 80;
 
// Girder access levels
var accessTypes = ['READ', 'WRITE', 'ADMIN'];
 
// user(obj): full user object,
// objAccess(obj): access object of the simulation or project
// atLeastAccess(int|string): check that the user has at least this access level
function userHasAccess(user, objAccess, atLeastAccess) {
  var accessLevel = typeof atLeastAccess === 'number' ? atLeastAccess : accessTypes.indexOf(atLeastAccess.toUpperCase());
 
  // check groups
  var groups = objAccess.groups;
  for (var i = 0; i < groups.length; i++) {
    if (user.groups.indexOf(groups[i].id) !== -1 && groups[i].level >= accessLevel) {
      return true;
    }
  }
 
  // check user
  var users = objAccess.users;
  for (var _i = 0; _i < users.length; _i++) {
    if (user._id === users[_i].id && users[_i].level >= accessLevel) {
      return true;
    }
  }
 
  return false;
}
 
var projectFunctions = exports.projectFunctions = {
  getIcon: function getIcon(project) {
    var ret = {
      image: Workflow[project.type] ? Workflow[project.type].logo : ''
    };
    return ret;
  },
  getName: function getName(project) {
    return project.name;
  },
  getDescription: function getDescription(project) {
    var short = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
 
    if (short && project.description.length > SHORT_DESCRIPTION_SIZE) {
      return project.description.substring(0, SHORT_DESCRIPTION_SIZE) + '...';
    }
    return project.description;
  },
  getCreationDate: function getCreationDate(project) {
    var short = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
 
    var str = '' + new Date(Date.parse(project.created)).toUTCString();
    if (short) {
      var dateList = str.split(' ');
      dateList.pop(); // remove time zone
      dateList.pop(); // remove hh:mm:ss
      return dateList.join(' ');
    }
    return str;
  },
  getUpdateDate: function getUpdateDate(project) {
    var short = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
 
    var str = '' + new Date(Date.parse(project.updated)).toUTCString();
    if (short) {
      var dateList = str.split(' ');
      dateList.pop(); // remove time zone
      dateList.pop(); // remove hh:mm:ss
      return dateList.join(' ');
    }
    return str;
  },
  getSimulationCount: function getSimulationCount(project) {
    var count = project.metadata.simulationCount || 0;
    return count + ' ' + (count === 1 ? 'simulation' : 'simulations');
  },
  getActions: function getActions(project) {
    return [{
      icon: _Theme2.default.editIcon,
      name: 'edit:' + project._id
    }];
  },
  getViewLink: function getViewLink(project) {
    return '/View/Project/' + project._id;
  },
  getEditLink: function getEditLink(project) {
    return '/Edit/Project/' + project._id;
  }
};
 
var ProjectHelper = exports.ProjectHelper = {
  columns: ['', 'Name', 'Description', 'Created', 'Updated', 'Simulations', ''],
  cellContentFunctions: [function (item) {
    return _react2.default.createElement(_ImageIcon2.default, { data: projectFunctions.getIcon(item) });
  }, projectFunctions.getName, projectFunctions.getDescription, projectFunctions.getCreationDate, projectFunctions.getUpdateDate, projectFunctions.getSimulationCount],
  actionItem: function actionItem(item, onAction) {
    return _react2.default.createElement(_IconActionList2.default, {
      actions: projectFunctions.getActions(item),
      onAction: onAction
    });
  },
  viewLink: projectFunctions.getViewLink,
  editLink: projectFunctions.getEditLink
};
 
var SIMULATIONS_ICONS = {
  created: _Theme2.default.simulationEditIcon,
  running: _Theme2.default.simulationRunningIcon,
  error: _Theme2.default.simulationErrorIcon,
  terminated: _Theme2.default.simulationTerminateIcon,
  complete: _Theme2.default.simulationDoneIcon
};
 
var simulationFunctions = exports.simulationFunctions = {
  getIcon: function getIcon(simulation) {
    return { icon: SIMULATIONS_ICONS[simulation.metadata.status] };
  },
  getName: function getName(simulation) {
    return simulation.name;
  },
  getDescription: function getDescription(simulation) {
    var short = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
 
    if (short && simulation.description.length > SHORT_DESCRIPTION_SIZE) {
      return simulation.description.substring(0, SHORT_DESCRIPTION_SIZE) + '...';
    }
    return simulation.description;
  },
  getCreationDate: function getCreationDate(simulation) {
    var short = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
 
    var str = '' + new Date(Date.parse(simulation.created)).toUTCString();
    if (short) {
      var dateList = str.split(' ');
      dateList.pop(); // remove time zone
      dateList.pop(); // remove hh:mm:ss
      return dateList.join(' ');
    }
    return str;
  },
  getUpdateDate: function getUpdateDate(simulation) {
    var short = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
 
    var str = '' + new Date(Date.parse(simulation.updated)).toUTCString();
    if (short) {
      var dateList = str.split(' ');
      dateList.pop(); // remove time zone
      dateList.pop(); // remove hh:mm:ss
      return dateList.join(' ');
    }
    return str;
  },
  getStep: function getStep(simulation) {
    return simulation.active;
  },
  getActions: function getActions(simulation) {
    return [{ icon: _Theme2.default.editIcon, name: 'edit:' + simulation._id }];
  },
  getViewLink: function getViewLink(simulation) {
    return '/View/Simulation/' + simulation._id;
  },
  getEditLink: function getEditLink(simulation) {
    return '/Edit/Simulation/' + simulation._id;
  }
};
 
var SimulationHelper = exports.SimulationHelper = {
  columns: ['', 'Name', 'Description', 'Created', 'Updated', 'Step', ''],
  cellContentFunctions: [function (item) {
    return _react2.default.createElement(_ImageIcon2.default, { data: simulationFunctions.getIcon(item) });
  }, simulationFunctions.getName, simulationFunctions.getDescription, simulationFunctions.getCreationDate, simulationFunctions.getUpdateDate, simulationFunctions.getStep],
  actionItem: function actionItem(item, onAction) {
    return _react2.default.createElement(_IconActionList2.default, {
      actions: simulationFunctions.getActions(item),
      onAction: onAction
    });
  },
  viewLink: simulationFunctions.getViewLink,
  editLink: simulationFunctions.getEditLink
};