<commit_before>'use strict';

const _ = require('lodash');
let extend = require('util')._extend;

module.exports = {
  /**
   * Generates the proper scope for an endpoint based on the model routeOptions
   * @param model: A mongoose model
   * @param type: The scope CRUD type. Valid values are 'create', 'read', 'update', 'delete', and 'associate'.
   * @param Log: A logging object
   * @returns {Array}: A list of authorization scopes for the endpoint.
   */
  generateScopeForEndpoint: function(model, type, Log) {

    //NOTE: As of v0.29.0 routeOptions.scope is replaced with routeOptions.routeScope and
    //routeOptions.scope.scope is replaced with routeOptions.routeScope.rootScope
    let routeScope = model.routeOptions.routeScope || {};
    let rootScope = routeScope.rootScope;
    let scope = [];

    let additionalScope = null;

    switch (type) {
      case 'create':
        additionalScope = routeScope.createScope;
        break;
      case 'read':
        additionalScope = routeScope.readScope;
        break;
      case 'update':
        additionalScope = routeScope.updateScope;
        break;
      case 'delete':
        additionalScope = routeScope.deleteScope;
        break;
      case 'associate':
        additionalScope = routeScope.associateScope;
        break;
      default:
        if (routeScope[type]) {
          scope = routeScope[type];
          if (!_.isArray(scope)) {
            scope = [scope];
          }
        }
        return scope;
        break;
    }

    if (rootScope && _.isArray(rootScope)) {
      scope = scope.concat(rootScope);
    }
    else if (rootScope) {
      scope.push(rootScope);
    }

    if (additionalScope && _.isArray(additionalScope)) {
      scope = scope.concat(additionalScope);
    }
    else if (additionalScope) {
      scope.push(additionalScope);
    }

    return scope;
  },

  generateScopeForModel: function(model, Log) {
    const modelName = model.collectionName[0].toUpperCase() + model.collectionName.slice(1);

    //NOTE: As of v0.29.0 routeOptions.scope is replaced with routeOptions.routeScope and
    //routeOptions.scope.scope is replaced with routeOptions.routeScope.rootScope
    let routeScope = model.routeOptions.routeScope || model.routeOptions.scope || {};
    routeScope.rootScope = routeScope.rootScope || routeScope.scope;
    delete routeScope.scope;
    if (!routeScope.rootScope) {
      delete routeScope.rootScope;
    }

    const scope = {};

    scope.rootScope = ["root", model.collectionName];
    scope.createScope = ["create", "create" + modelName];
    scope.readScope = ["read", "read" + modelName];
    scope.updateScope = ["update", "update" + modelName];
    scope.deleteScope = ["delete", "delete" + modelName];
    scope.associateScope = ["associate", "associate" + modelName];

    const associations = model.routeOptions.associations;

    for (const key in associations) {
      const associationName = key[0].toUpperCase() + key.slice(1);
      scope["add" + modelName + associationName + "Scope"] = "add" + modelName + associationName;
      scope["remove" + modelName + associationName + "Scope"] = "remove" + modelName + associationName;
      scope["get" + modelName + associationName + "Scope"] = "get" + modelName + associationName;
    }

    //EXPL: merge any existing scope fields with the generated scope
    for (const key in routeScope) {
      if (scope[key]) {
        if (!_.isArray(scope[key])) {
          scope[key] = [scope[key]];
        }
        if (routeScope[key] && _.isArray(routeScope[key])) {
          scope[key] = scope[key].concat(routeScope[key]);
        }
        else {
          scope[key].push(routeScope[key]);
        }
      }
    }

    model.routeOptions.routeScope = scope;
  }
};<commit_msg>Adding missing permissions.<commit_after>'use strict';

const _ = require('lodash');
let extend = require('util')._extend;

module.exports = {
  /**
   * Generates the proper scope for an endpoint based on the model routeOptions
   * @param model: A mongoose model
   * @param type: The scope CRUD type. Valid values are 'create', 'read', 'update', 'delete', and 'associate'.
   * @param Log: A logging object
   * @returns {Array}: A list of authorization scopes for the endpoint.
   */
  generateScopeForEndpoint: function(model, type, Log) {

    //NOTE: As of v0.29.0 routeOptions.scope is replaced with routeOptions.routeScope and
    //routeOptions.scope.scope is replaced with routeOptions.routeScope.rootScope
    let routeScope = model.routeOptions.routeScope || {};
    let rootScope = routeScope.rootScope;
    let scope = [];

    let additionalScope = null;

    switch (type) {
      case 'create':
        additionalScope = routeScope.createScope;
        break;
      case 'read':
        additionalScope = routeScope.readScope;
        break;
      case 'update':
        additionalScope = routeScope.updateScope;
        break;
      case 'delete':
        additionalScope = routeScope.deleteScope;
        break;
      case 'associate':
        additionalScope = routeScope.associateScope;
        break;
      default:
        if (routeScope[type]) {
          scope = routeScope[type];
          if (!_.isArray(scope)) {
            scope = [scope];
          }
        }
        return scope;
        break;
    }

    if (rootScope && _.isArray(rootScope)) {
      scope = scope.concat(rootScope);
    }
    else if (rootScope) {
      scope.push(rootScope);
    }

    if (additionalScope && _.isArray(additionalScope)) {
      scope = scope.concat(additionalScope);
    }
    else if (additionalScope) {
      scope.push(additionalScope);
    }

    return scope;
  },

  generateScopeForModel: function(model, Log) {
    const modelName = model.collectionName[0].toUpperCase() + model.collectionName.slice(1);

    //NOTE: As of v0.29.0 routeOptions.scope is replaced with routeOptions.routeScope and
    //routeOptions.scope.scope is replaced with routeOptions.routeScope.rootScope
    let routeScope = model.routeOptions.routeScope || model.routeOptions.scope || {};
    routeScope.rootScope = routeScope.rootScope || routeScope.scope;
    delete routeScope.scope;
    if (!routeScope.rootScope) {
      delete routeScope.rootScope;
    }

    const scope = {};

    scope.rootScope = ["root", model.collectionName, "!-root", "!-" + model.collectionName];
    scope.createScope = ["create", "create" + modelName, "!-create", "!-create" + modelName];
    scope.readScope = ["read", "read" + modelName, "!-read", "!-read" + modelName];
    scope.updateScope = ["update", "update" + modelName, "!-update", "!-update" + modelName];
    scope.deleteScope = ["delete", "delete" + modelName, "!-delete", "!-delete" + modelName];
    scope.associateScope = ["associate", "associate" + modelName, "!-associate", "!-associate" + modelName];

    const associations = model.routeOptions.associations;

    for (const key in associations) {
      const associationName = key[0].toUpperCase() + key.slice(1);
      scope["add" + modelName + associationName + "Scope"] = ["add" + modelName + associationName, "!-add" + modelName + associationName];
      scope["remove" + modelName + associationName + "Scope"] = ["remove" + modelName + associationName, "!-remove" + modelName + associationName];
      scope["get" + modelName + associationName + "Scope"] = ["get" + modelName + associationName, "!-get" + modelName + associationName];
    }

    //EXPL: merge any existing scope fields with the generated scope
    for (const key in routeScope) {
      if (scope[key]) {
        if (!_.isArray(scope[key])) {
          scope[key] = [scope[key]];
        }
        if (routeScope[key] && _.isArray(routeScope[key])) {
          scope[key] = scope[key].concat(routeScope[key]);
        }
        else {
          scope[key].push(routeScope[key]);
        }
      }
    }

    model.routeOptions.routeScope = scope;
  }
};
{'fraction_non_alphanumeric': 0.13089667383331224, 'fraction_numerical': 0.0030352851903376753, 'mean_word_length': 2.885995085995086, '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': '6478532', 'n_tokens_mistral': 2291, 'n_tokens_neox': 2248, 'n_words': 562}