<commit_before>import _ from 'lodash'
import aclFilter from '../rest/acl'
import roles from '../rest/roles'
import * as errors from 'netiam-errors'

function request(spec) {
  const {asserts} = spec
  const acl = aclFilter({settings: spec.settings})

  return function(req) {
    const role = roles.get(req.user ? req.user.role : null)

    // create
    if (req.method === 'POST' && req.is('json')) {
      if (!acl.resource(req.user, role, 'C')) {
        throw errors.forbidden(
          `You have not enough privileges to create this resource as ${role.name}`
        )
      }

      if (_.isArray(req.body)) {
        req.body = _.map(function(node) {
          return acl.filter(req.user, node, role, 'C', asserts)
        })
        return
      }

      if (_.isObject(req.body)) {
        req.body = acl.filter(req.user, req.body, role, 'C', asserts)
        return
      }
    }

    // update
    if (req.method === 'PUT' && req.is('json')) {
      if (!acl.resource(req.user, role, 'U')) {
        throw errors.forbidden(
          `You have not enough privileges to modify this resource as ${role.name}`
        )
      }

      if (_.isArray(req.body)) {
        req.body = _.map(function(node) {
          return acl.filter(req.user, node, role, 'U', asserts)
        })
        return
      }

      if (_.isObject(req.body)) {
        req.body = acl.filter(req.user, req.body, role, 'U', asserts)
      }
    }
  }
}

function response(spec) {
  const {asserts} = spec
  const acl = aclFilter({settings: spec.settings})

  return function(req, res) {
    const role = roles.get(req.user ? req.user.role : null)<commit_msg>fix: resource method DELETE permission checks<commit_after>import _ from 'lodash'
import aclFilter from '../rest/acl'
import roles from '../rest/roles'
import * as errors from 'netiam-errors'

function request(spec) {
  const {asserts} = spec
  const acl = aclFilter({settings: spec.settings})

  return function(req) {
    const role = roles.get(req.user ? req.user.role : null)

    // create
    if (req.method === 'POST' && req.is('json')) {
      if (!acl.resource(req.user, role, 'C')) {
        throw errors.forbidden(
          `You have not enough privileges to create this resource as ${role.name}`
        )
      }

      if (_.isArray(req.body)) {
        req.body = _.map(function(node) {
          return acl.filter(req.user, node, role, 'C', asserts)
        })
        return
      }

      if (_.isObject(req.body)) {
        req.body = acl.filter(req.user, req.body, role, 'C', asserts)
        return
      }
    }

    // update
    if (req.method === 'PUT' && req.is('json')) {
      if (!acl.resource(req.user, role, 'U')) {
        throw errors.forbidden(
          `You have not enough privileges to modify this resource as ${role.name}`
        )
      }

      if (_.isArray(req.body)) {
        req.body = _.map(function(node) {
          return acl.filter(req.user, node, role, 'U', asserts)
        })
        return
      }

      if (_.isObject(req.body)) {
        req.body = acl.filter(req.user, req.body, role, 'U', asserts)
      }
    }

    // delete
    if (req.method === 'DELETE') {
      if (!acl.resource(req.user, role, 'D')) {
        throw errors.forbidden(
          `You have not enough privileges to delete this resource as ${role.name}`
        )
      }
    }

  }
}

function response(spec) {
  const {asserts} = spec
  const acl = aclFilter({settings: spec.settings})

  return function(req, res) {
    const role = roles.get(req.user ? req.user.role : null)
{'fraction_non_alphanumeric': 0.15174549549549549, 'fraction_numerical': 0.0, 'mean_word_length': 2.180841539838854, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1025203', 'n_tokens_mistral': 1170, 'n_tokens_neox': 1149, 'n_words': 289}