<commit_before>'use strict';

// Module dependencies
var passport = require('passport');
var util = require('@openveo/api/lib/util.js');
var LocalStrategy = require('passport-local').Strategy;
var UserModel = process.require('app/server/models/UserModel.js');
var RoleModel = process.require('app/server/models/RoleModel.js');

var userModel = new UserModel();
var roleModel = new RoleModel();

/**
 * Gets user roles details.
 *
 * @async
 * @method getUserRoles
 * @private
 * @param {Object} user The user
 * @param {Function} callback Function to call when its done
 *  - *Error* An error if something went wrong, null otherwise
 *  - *Object* The upgraded user with roles details
 */
function getUserRoles(user, callback) {

  // Get user permissions by roles
  if (user.roles) {
    roleModel.getByIds(user.roles, function(error, roles) {
      if (error)
        callback(null, false);
      else {
        user.permissions = [];
        for (var i = 0; i < roles.length; i++) {
          user.permissions = util.joinArray(user.permissions, roles[i].permissions);
          delete roles[i].permissions;
        }

        user.roles = roles;
        callback(null, user);
      }
    });
  }

}

// Define a passport authentication strategy.
// "email" and "password" field must be send using a POST
// request to authenticate.
passport.use(new LocalStrategy(
  {
    usernameField: 'email',
    passwordField: 'password'
  },
  function(email, password, done) {
    userModel.getUserByCredentials(email, password, function(error, user) {
      if (error || !user)
        done(null, false);
      else if (user.id == 0)
        done(null, user);
      else
        getUserRoles(user, done);
    });
  }
));

// In order to support login sessions, Passport serialize and
// deserialize user instances to and from the session.
// Only the user ID is serialized to the session.
passport.serializeUser(function(user, done) {
  done(null, user.id);
});

// When subsequent requests are received, the ID is used to find<commit_msg>Correct bug when accessing back end page without roles<commit_after>'use strict';

// Module dependencies
var passport = require('passport');
var util = require('@openveo/api/lib/util.js');
var LocalStrategy = require('passport-local').Strategy;
var UserModel = process.require('app/server/models/UserModel.js');
var RoleModel = process.require('app/server/models/RoleModel.js');

var userModel = new UserModel();
var roleModel = new RoleModel();

/**
 * Gets user roles details.
 *
 * @async
 * @method getUserRoles
 * @private
 * @param {Object} user The user
 * @param {Function} callback Function to call when its done
 *  - *Error* An error if something went wrong, null otherwise
 *  - *Object* The upgraded user with roles details
 */
function getUserRoles(user, callback) {

  // Get user permissions by roles
  if (user.roles) {
    roleModel.getByIds(user.roles, function(error, roles) {
      if (error)
        callback(null, false);
      else {
        user.permissions = [];
        for (var i = 0; i < roles.length; i++) {
          user.permissions = util.joinArray(user.permissions, roles[i].permissions);
          delete roles[i].permissions;
        }

        user.roles = roles;
        callback(null, user);
      }
    });
  } else
    callback(null, user);

}

// Define a passport authentication strategy.
// "email" and "password" field must be send using a POST
// request to authenticate.
passport.use(new LocalStrategy(
  {
    usernameField: 'email',
    passwordField: 'password'
  },
  function(email, password, done) {
    userModel.getUserByCredentials(email, password, function(error, user) {
      if (error || !user)
        done(null, false);
      else if (user.id == 0)
        done(null, user);
      else
        getUserRoles(user, done);
    });
  }
));

// In order to support login sessions, Passport serialize and
// deserialize user instances to and from the session.
// Only the user ID is serialized to the session.
passport.serializeUser(function(user, done) {
  done(null, user.id);
});

// When subsequent requests are received, the ID is used to find
{'fraction_non_alphanumeric': 0.12469792170130498, 'fraction_numerical': 0.0009666505558240696, 'mean_word_length': 3.1639839034205233, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2188013', 'n_tokens_mistral': 1228, 'n_tokens_neox': 1209, 'n_words': 382}