<commit_before>/**
 *  nacl
 *  copyright(c) 2016 - 2019 Andela Kenya Ltd
 *  MIT Lincensed
 */

'use strict';
let helper = require('./helpers');
const utils = require('./utils');
const unless = require('express-unless');
const _ = require('lodash');
let opt = {};

/**
 * [config Loads the rules from our config file]
 * @param  {[options]} options [defines where the
 * config file is located, and the encoding type]
 *
 */
function config(config, response) {
  let options = config || {};
  let yml = options.yml || false;
  opt.response = response;
  opt.baseUrl = options.baseUrl;
  opt.decodedObjectName = options.decodedObjectName;
  opt.defaultRole = options.defaultRole || 'guest';

  if (options.rules) {
    opt.rules = utils.validate(options.rules);
    return opt.rules;
  }

  /**
   * Get the filename
   */

  let defaultFilename = yml ? 'nacl.yml' : 'nacl.json';
  let filename = options.filename ? options.filename : defaultFilename;

  /**
   * Merge filename and path
   */

  let path = filename && options.path ? (`${options.path}/${filename}`) : filename;

  opt.rules = helper.getRules(path, options.encoding, yml);

  return opt.rules;
}

/**
 * [authorize Express middleware]
 * @param  {[type]}   req  [Th request object]
 * @param  {[type]}   res  [The response object]
 * @param  {Function} next [description]
 * @return {[type]}        [description]
 */

function authorize(req, res, next) {
  let method = req.method;
  let resource = helper.resource(next, req.originalUrl, opt.baseUrl);
  /**
   * if not resource terminate script
   */

  if (!resource || !_.isString(resource)) return;

  /**
   * [group description]
   * @type {[type]}
   */

  let role = helper.getRole(req, res, opt.decodedObjectName, opt.defaultRole);

  if (!_.isString(role) || !role) return;

  /**
   * get resource from the url
   */

  let groupPermissions = opt.rules.get(role);

  /**
   * if no groupPermissions
   */

  if (!groupPermissions || groupPermissions.length === 0) {
    return utils.deny(
      res,
      404,<commit_msg>Fix: Issue#79: called the utils.deny function when the validation whether the provided role is a string or not fails.<commit_after>/**
 *  nacl
 *  copyright(c) 2016 - 2019 Andela Kenya Ltd
 *  MIT Lincensed
 */

'use strict';
let helper = require('./helpers');
const utils = require('./utils');
const unless = require('express-unless');
const _ = require('lodash');
let opt = {};

/**
 * [config Loads the rules from our config file]
 * @param  {[options]} options [defines where the
 * config file is located, and the encoding type]
 *
 */
function config(config, response) {
  let options = config || {};
  let yml = options.yml || false;
  opt.response = response;
  opt.baseUrl = options.baseUrl;
  opt.decodedObjectName = options.decodedObjectName;
  opt.defaultRole = options.defaultRole || 'guest';

  if (options.rules) {
    opt.rules = utils.validate(options.rules);
    return opt.rules;
  }

  /**
   * Get the filename
   */

  let defaultFilename = yml ? 'nacl.yml' : 'nacl.json';
  let filename = options.filename ? options.filename : defaultFilename;

  /**
   * Merge filename and path
   */

  let path = filename && options.path ? (`${options.path}/${filename}`) : filename;

  opt.rules = helper.getRules(path, options.encoding, yml);

  return opt.rules;
}

/**
 * [authorize Express middleware]
 * @param  {[type]}   req  [Th request object]
 * @param  {[type]}   res  [The response object]
 * @param  {Function} next [description]
 * @return {[type]}        [description]
 */

function authorize(req, res, next) {
  let method = req.method;
  let resource = helper.resource(next, req.originalUrl, opt.baseUrl);
  /**
   * if not resource terminate script
   */

  if (!resource || !_.isString(resource)) return;

  /**
   * [group description]
   * @type {[type]}
   */

  let role = helper.getRole(req, res, opt.decodedObjectName, opt.defaultRole);

  /**
   * if no role or role not provided as string
   */
  if (!_.isString(role) || !role) {
    return utils.deny(
      res,
      404,
      'REQUIRED: Role should be provided as a string',
      null
    );
  }

  /**
   * get resource from the url
   */

  let groupPermissions = opt.rules.get(role);

  /**
   * if no groupPermissions
   */

  if (!groupPermissions || groupPermissions.length === 0) {
    return utils.deny(
      res,
      404,
{'fraction_non_alphanumeric': 0.14797899063713177, 'fraction_numerical': 0.006622516556291391, 'mean_word_length': 3.112676056338028, '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': '2581469', 'n_tokens_mistral': 1431, 'n_tokens_neox': 1374, 'n_words': 392}