<commit_before>/*
 * athz
 * https://github.com/aleksandar-micic/athz
 *
 * Copyright (c) 2014 <NAME>
 * Licensed under the MIT license.
 */

'use strict';

var AuthorizationError = require('./error.js').AuthorizationError;

function authorize(roles) {
  return function(req, res, next) {
    if (!roles) { return next(); }

    if (!req.user) { throw new AuthorizationError('User not authenticated'); }
    if (!req.user.role && !req.user.roles) { throw new AuthorizationError('User does not have a role'); }

    var ur = req.user.roles || req.user.role,
      authorized = false;

    if (Array.isArray(ur)) {
      authorized = roles.some(function(item) {
        return ur.some(function(role) {
          return item.role === role;
        });
      });
    }
    else {
      authorized = roles.some(function(item) {
        return item.role === ur;
      });
    }

    if (!authorized) {
      throw new AuthorizationError('Not authorized');
    }

    next();
  };
}

module.exports = authorize;<commit_msg>Error messages now more consistent<commit_after>/*
 * athz
 * https://github.com/aleksandar-micic/athz
 *
 * Copyright (c) 2014 <NAME>
 * Licensed under the MIT license.
 */

'use strict';

var AuthorizationError = require('./error.js').AuthorizationError;

function authorize(roles) {
  return function(req, res, next) {
    if (!roles) { return next(); }

    if (!req.user) { throw new AuthorizationError('User not authenticated'); }
    if (!req.user.role && !req.user.roles) { throw new AuthorizationError('User does not have a role'); }

    var ur = req.user.roles || req.user.role,
      authorized = false;

    if (Array.isArray(ur)) {
      authorized = roles.some(function(item) {
        return ur.some(function(role) {
          return item.role === role;
        });
      });
    }
    else {
      authorized = roles.some(function(item) {
        return item.role === ur;
      });
    }

    if (!authorized) {
      throw new AuthorizationError('User not authorized');
    }

    next();
  };
}

module.exports = authorize;
{'fraction_non_alphanumeric': 0.15063168124392615, 'fraction_numerical': 0.003887269193391642, 'mean_word_length': 2.716606498194946, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '7509945', 'n_tokens_mistral': 650, 'n_tokens_neox': 628, 'n_words': 154}