All files / src/fs/util permission-util.js

100% Statements 17/17
100% Branches 6/6
100% Functions 2/2
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 451x   1x                     1x 196x   196x 174x   174x 20x       176x                 252x 84x   196x 196x 20x       64x    
import * as PathUtil from 'fs/util/path-util';
 
const DEFAULT_PERMISSION = true;
 
/**
 * Checks if a single path can be modified by checking the 'canModify' key held
 * in the path.
 *
 * This does NOT check parents of the path.
 * @param  {Map}     fs   file system
 * @param  {string}  path path to check for modification permission
 * @return {Boolean}      true, if a single path can be modified
 */
const isModificationAllowed = (fs, path) => {
  const directory = fs.get(path, null);
 
  if (directory) {
    const canModify = directory.get('canModify', DEFAULT_PERMISSION);
 
    if (!canModify) {
      return false;
    }
  }
 
  return true;
};
 
/**
 * Checks if a path and its parents can be modified.
 * @param  {Map}     fs   file systems
 * @param  {String}  path path to a directory or file
 * @return {Boolean}      true, if the path and its parents can be modified
 */
export const canModifyPath = (fs, path) => {
  const breadCrumbPaths = PathUtil.getPathBreadCrumbs(path);
 
  for (const breadCrumbPath of breadCrumbPaths) {
    if (!isModificationAllowed(fs, breadCrumbPath)) {
      return false;
    }
  }
 
  return true;
};