<commit_before>const validator = require('validator');
const openbadger = require('../lib/openbadger');
const async = require('async');

const Account = require('../models/account')("DATABASE");
const AccountPermission = require('../models/account-permission')("DATABASE");

exports.home = function home (req, res, next) {
  getSystems(res.locals.hasPermission, function(err, data) {
    if (err)
      return res.send(500, err);

    data.issuersUrl = res.locals.url('settings.issuers');
    data.programsUrl = res.locals.url('settings.programs');
    data.systemsUrl = res.locals.url('settings.systems');
    data.usersUrl = res.locals.url('settings.users');

    return res.render('settings/home.html', data);
  });
};

exports.systems = function systems (req, res, next) {
  getSystems(res.locals.hasPermission, function(err, data) {
    if (err)
      return res.send(500, err);

    return res.send(200, data);
  });
}

function getSystems(hasPermission, callback) {
  openbadger.getSystems(function(err, systemData) {
    if (err)
      return callback(err)

    var data = { systems: [] };

    systemData.forEach(function(system) {
      var context = { system: system.slug };
      if (hasPermission(context, 'view')) {
        system.canAdmin = hasPermission(context, 'admin');
        delete system.issuers;
        data.systems.push(system);
      }
    });

    return callback(null, data);
  });
}

exports.issuers = function issuers (req, res, next) {
  const systemSlug = req.query.systemSlug;
  const issuerSlug = req.query.issuerSlug;

  if (typeof issuerSlug === 'undefined' || issuerSlug === null) {
    openbadger.getIssuers( { system: systemSlug }, function(err, issuerData) {
      if (err)
        return res.send(500, err);

      var data = { issuers: [] };

      issuerData.forEach(function(issuer) {
        var context = { system: systemSlug, issuer: issuer.slug };
        if (res.locals.hasPermission(context, 'view')) {
          issuer.canAdmin = res.locals.hasPermission(context, 'admin');
          delete issuer.programs;
          data.issuers.push(issuer);
        }
      });

      return res.send(200, data);
    });
  }
  else {
    var context = { system: systemSlug, issuer: issuerSlug };
    if (res.locals.hasPermission(context, 'view')) {
      openbadger.getIssuer(context, function(err, issuerData) {
        return res.send(200, { issuer: issuerData });
      });
    }
    else {
      return res.send(403, 'Access Denied');
    }
  }
};

exports.programs = function issuers (req, res, next) {
  const systemSlug = req.query.systemSlug;
  const issuerSlug = req.query.issuerSlug;
  const programSlug = req.query.programSlug;

  if (typeof programSlug === 'undefined' || programSlug === null) {
    openbadger.getPrograms( { system: systemSlug, issuer: issuerSlug }, function(err, programData) {
      if (err)
        return res.send(500, err);

      var data = { programs: [] };

      programData.forEach(function(program) {
        var context = { system: systemSlug, issuer: issuerSlug, program: program.slug };
        if (res.locals.hasPermission(context, 'view')) {
          program.canAdmin = res.locals.hasPermission(context, 'admin');
          delete program.programs;
          data.programs.push(program);
        }
      });

      return res.send(200, data);
    });
  }
  else {
    var context = { system: systemSlug, issuer: issuerSlug, program: programSlug };
    if (res.locals.hasPermission(context, 'view')) {
      openbadger.getProgram(context, function(err, programData) {
        return res.send(200, { program: programData });
      });
    }
    else {
      return res.send(403, 'Access Denied');
    }
  }
};

exports.users = function users (req, res, next) {
  const systemSlug = req.query.systemSlug;
  const issuerSlug = req.query.issuerSlug;
  const programSlug = req.query.programSlug;
  const email = req.query.email;

  var query = { system: systemSlug,
                issuer: issuerSlug || null,
                program: programSlug || null };

  if (!res.locals.hasPermission(query, 'admin')) {
    return res.send(403, 'Access Denied');
  }

  if (typeof email === 'undefined' || email === null) {<commit_msg>Sort systems/issuers/programs in settings page<commit_after>const validator = require('validator');
const openbadger = require('../lib/openbadger');
const async = require('async');

const Account = require('../models/account')("DATABASE");
const AccountPermission = require('../models/account-permission')("DATABASE");

exports.home = function home (req, res, next) {
  getSystems(res.locals.hasPermission, function(err, data) {
    if (err)
      return res.send(500, err);

    data.issuersUrl = res.locals.url('settings.issuers');
    data.programsUrl = res.locals.url('settings.programs');
    data.systemsUrl = res.locals.url('settings.systems');
    data.usersUrl = res.locals.url('settings.users');

    return res.render('settings/home.html', data);
  });
};

exports.systems = function systems (req, res, next) {
  getSystems(res.locals.hasPermission, function(err, data) {
    if (err)
      return res.send(500, err);

    return res.send(200, data);
  });
}

function getSystems(hasPermission, callback) {
  openbadger.getSystems(function(err, systemData) {
    if (err)
      return callback(err)

    var data = { systems: [] };

    systemData.forEach(function(system) {
      var context = { system: system.slug };
      if (hasPermission(context, 'view')) {
        system.canAdmin = hasPermission(context, 'admin');
        delete system.issuers;
        data.systems.push(system);
      }
    });

    data.systems.sort(nameSort);
    return callback(null, data);
  });
}

exports.issuers = function issuers (req, res, next) {
  const systemSlug = req.query.systemSlug;
  const issuerSlug = req.query.issuerSlug;

  if (typeof issuerSlug === 'undefined' || issuerSlug === null) {
    openbadger.getIssuers( { system: systemSlug }, function(err, issuerData) {
      if (err)
        return res.send(500, err);

      var data = { issuers: [] };

      issuerData.forEach(function(issuer) {
        var context = { system: systemSlug, issuer: issuer.slug };
        if (res.locals.hasPermission(context, 'view')) {
          issuer.canAdmin = res.locals.hasPermission(context, 'admin');
          delete issuer.programs;
          data.issuers.push(issuer);
        }
      });

      data.issuers.sort(nameSort);
      return res.send(200, data);
    });
  }
  else {
    var context = { system: systemSlug, issuer: issuerSlug };
    if (res.locals.hasPermission(context, 'view')) {
      openbadger.getIssuer(context, function(err, issuerData) {
        return res.send(200, { issuer: issuerData });
      });
    }
    else {
      return res.send(403, 'Access Denied');
    }
  }
};

exports.programs = function issuers (req, res, next) {
  const systemSlug = req.query.systemSlug;
  const issuerSlug = req.query.issuerSlug;
  const programSlug = req.query.programSlug;

  if (typeof programSlug === 'undefined' || programSlug === null) {
    openbadger.getPrograms( { system: systemSlug, issuer: issuerSlug }, function(err, programData) {
      if (err)
        return res.send(500, err);

      var data = { programs: [] };

      programData.forEach(function(program) {
        var context = { system: systemSlug, issuer: issuerSlug, program: program.slug };
        if (res.locals.hasPermission(context, 'view')) {
          program.canAdmin = res.locals.hasPermission(context, 'admin');
          delete program.programs;
          data.programs.push(program);
        }
      });

      data.programs.sort(nameSort);
      return res.send(200, data);
    });
  }
  else {
    var context = { system: systemSlug, issuer: issuerSlug, program: programSlug };
    if (res.locals.hasPermission(context, 'view')) {
      openbadger.getProgram(context, function(err, programData) {
        return res.send(200, { program: programData });
      });
    }
    else {
      return res.send(403, 'Access Denied');
    }
  }
};

exports.users = function users (req, res, next) {
  const systemSlug = req.query.systemSlug;
  const issuerSlug = req.query.issuerSlug;
  const programSlug = req.query.programSlug;
  const email = req.query.email;

  var query = { system: systemSlug,
                issuer: issuerSlug || null,
                program: programSlug || null };

  if (!res.locals.hasPermission(query, 'admin')) {
    return res.send(403, 'Access Denied');
  }

  if (typeof email === 'undefined' || email === null) {
{'fraction_non_alphanumeric': 0.14566514566514566, 'fraction_numerical': 0.008424008424008424, 'mean_word_length': 3.0627376425855513, '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': '7518147', 'n_tokens_mistral': 2821, 'n_tokens_neox': 2733, 'n_words': 567}