<commit_before>'use strict';

/**
 * Module dependencies
 */

// Public node modules
const async = require('async');

// Local utilities.
const regex = require('../../node_modules/strapi/util/regex');

/**
 * Creates Routes
 */

exports.create = function () {
  let deferred = Promise.defer();
  let promises = [];
  let newRoutes = [];
  let routesFound;

  async.auto({
    findRoutes: function (callback) {
      // Find all routes.
      strapi.orm
        .collections
        .route
        .find()
        .exec(function (err, routesFound) {
          if (err) {
            callback(err);
          } else {
            callback(null, routesFound);
          }
        })
    },
    deleteRoutes: ['findRoutes', function (callback, results) {
      // Async dependencies.
      routesFound = results.findRoutes;

      // Delete destroyed routes.
      _.forEach(routesFound, function (routeFound) {
        if (!strapi.config.routes[routeFound.name]) {
          promises.push(strapi.orm.collections.route.destroy({id: routeFound.id}));
        }
      });

      callback(null);
    }],
    updateOrCreateRoutes: ['findRoutes', function (callback, results) {
      // Async dependencies.
      routesFound = results.findRoutes;

      // Find or create routes.
      _.forEach(strapi.config.routes, function (route, key) {
        if (_.find(routesFound, {name: key})) {
          promises.push(strapi.orm.collections.route.update({
            name: key
          }, {
            name: key,
            policies: route.policies,
            controller: route.controller,
            action: route.action
          }));
        } else {
          newRoutes.push(key);
          promises.push(strapi.orm.collections.route.create({
            name: key,
            policies: route.policies,
            controller: route.controller,
            action: route.action
          }));
        }
      });

      callback(null);
    }],
    execRoutesModifications: ['deleteRoutes', 'updateOrCreateRoutes', function (callback) {
      // Exec the promises.
      Promise.all(promises)
        .then(function (responses) {
          callback(null, responses);
        })
        .catch(function (err) {
          callback(err);
        });
    }],
    findNewRoutes: ['execRoutesModifications', function (callback) {
      // Find created routes.
      strapi.orm
        .collections
        .route
        .find({
          'name': newRoutes
        })
        .populate('roles')
        .exec(function (err, newRoutesFound) {
          if (err) {
            callback(err);
          } else {
            callback(null, newRoutesFound);
          }
        });
    }],
    updateCreatedRoutes: ['execRoutesModifications', 'findNewRoutes', 'findRoles', function (callback, results) {
      // Async dependencies.
      let newRoutesFound = results.findNewRoutes;
      let roles = results.findRoles;

      const contributorVerbs = ['put', 'patch', 'delete'];
      const userContributorRoutes = [
        'GET /user/:id',
        'PUT /user/:id',
        'DELETE /user/:id'
      ];
      const userRegisteredRoutes = [
        'PUT /user/:id',
        'DELETE /user/:id'
      ];
      let verb;
      let contributorRole = _.find(roles, {name: 'contributor'});
      let registeredRole = _.find(roles, {name: 'registered'});
      let adminRole = _.find(roles, {name: 'admin'});

      _.forEach(newRoutesFound, function (newRoute) {
        if (!_.contains(newRoute.name, '/admin')) {

          // Contributor permissions.
          verb = regex.detectRoute(newRoute.name).verb;
          newRoute.isPublic = false;
          newRoute.registeredAuthorized = false;
          newRoute.contributorsAuthorized = false;

          if (_.contains(newRoute.name, '/auth')) {
            newRoute.isPublic = true;
          } else if (_.contains(newRoute.name, '/user')) {<commit_msg>Add security for dashboard routes<commit_after>'use strict';

/**
 * Module dependencies
 */

// Public node modules
const async = require('async');

// Local utilities.
const regex = require('../../node_modules/strapi/util/regex');

/**
 * Creates Routes
 */

exports.create = function () {
  let deferred = Promise.defer();
  let promises = [];
  let newRoutes = [];
  let routesFound;

  async.auto({
    findRoutes: function (callback) {
      // Find all routes.
      strapi.orm
        .collections
        .route
        .find()
        .exec(function (err, routesFound) {
          if (err) {
            callback(err);
          } else {
            callback(null, routesFound);
          }
        })
    },
    deleteRoutes: ['findRoutes', function (callback, results) {
      // Async dependencies.
      routesFound = results.findRoutes;

      // Delete destroyed routes.
      _.forEach(routesFound, function (routeFound) {
        if (!strapi.config.routes[routeFound.name]) {
          promises.push(strapi.orm.collections.route.destroy({id: routeFound.id}));
        }
      });

      callback(null);
    }],
    updateOrCreateRoutes: ['findRoutes', function (callback, results) {
      // Async dependencies.
      routesFound = results.findRoutes;

      // Find or create routes.
      _.forEach(strapi.config.routes, function (route, key) {
        if (_.find(routesFound, {name: key})) {
          promises.push(strapi.orm.collections.route.update({
            name: key
          }, {
            name: key,
            policies: route.policies,
            controller: route.controller,
            action: route.action
          }));
        } else {
          newRoutes.push(key);
          promises.push(strapi.orm.collections.route.create({
            name: key,
            policies: route.policies,
            controller: route.controller,
            action: route.action
          }));
        }
      });

      callback(null);
    }],
    execRoutesModifications: ['deleteRoutes', 'updateOrCreateRoutes', function (callback) {
      // Exec the promises.
      Promise.all(promises)
        .then(function (responses) {
          callback(null, responses);
        })
        .catch(function (err) {
          callback(err);
        });
    }],
    findNewRoutes: ['execRoutesModifications', function (callback) {
      // Find created routes.
      strapi.orm
        .collections
        .route
        .find({
          'name': newRoutes
        })
        .populate('roles')
        .exec(function (err, newRoutesFound) {
          if (err) {
            callback(err);
          } else {
            callback(null, newRoutesFound);
          }
        });
    }],
    updateCreatedRoutes: ['execRoutesModifications', 'findNewRoutes', 'findRoles', function (callback, results) {
      // Async dependencies.
      let newRoutesFound = results.findNewRoutes;
      let roles = results.findRoles;

      const contributorVerbs = ['put', 'patch', 'delete'];
      const userContributorRoutes = [
        'GET /user/:id',
        'PUT /user/:id',
        'DELETE /user/:id'
      ];
      const userRegisteredRoutes = [
        'PUT /user/:id',
        'DELETE /user/:id'
      ];
      let verb;
      let contributorRole = _.find(roles, {name: 'contributor'});
      let registeredRole = _.find(roles, {name: 'registered'});
      let adminRole = _.find(roles, {name: 'admin'});

      _.forEach(newRoutesFound, function (newRoute) {
        if (!_.contains(newRoute.name, '/dashboard')) {

          // Contributor permissions.
          verb = regex.detectRoute(newRoute.name).verb;
          newRoute.isPublic = false;
          newRoute.registeredAuthorized = false;
          newRoute.contributorsAuthorized = false;

          if (_.contains(newRoute.name, '/auth')) {
            newRoute.isPublic = true;
          } else if (_.contains(newRoute.name, '/user')) {
{'fraction_non_alphanumeric': 0.1340681876441938, 'fraction_numerical': 0.0, 'mean_word_length': 1.9545626656569481, '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': '5500584', 'n_tokens_mistral': 2216, 'n_tokens_neox': 2225, 'n_words': 458}