<commit_before>'use strict';

var bcrypt = require('bcryptjs'),
    logger = require('winston'),
    accountModel = require('../models/account'),
    realmModel = require('../models/realm'),
    ResponseTypes = {
        SUCCESS: 'success',
        DB_ERROR: 'db-error',
        ENCRYPT_ERROR: 'encrypt-error',
        MISSING_FIELD: 'missing-field',
        USER_NOT_FOUND: 'login-user-not-found',
        INVALID_PASSWORD: '<PASSWORD>',
        INVALID_REALM: 'login-invalid-realm',
        REALM_NOT_FOUND: 'login-realm-not-found',
        REALM_TAKEN: 'signup-realm-taken',
        EMAIL_TAKEN: 'signup-email-taken'
    };

var createRealm = function(name, email, callback) {
    var newRealm = {
        name: name,
        creation: new Date(),
        administrator: email
    };

    realmModel.add(newRealm, function(err, dbRealm) {
        if (callback) {
            callback(err, dbRealm);
        }
    });
};

var checkUserMemberOfRealm = function(email, realms) {
    var realmsFound;
    realmsFound = realms.filter(function(realm) {
        if (realm.administrator === email ||
            realm.user1 === email ||
            realm.user2 === email ||
            realm.user3 === email ||
            realm.user4 === email ||
            realm.user5 === email ||
            realm.user6 === email ||
            realm.user7 === email ||
            realm.user8 === email ||
            realm.user9 === email ||
            realm.user10 === email) {
            return true;
        }
    });
    return realmsFound;
};

module.exports.signup = function(req, res) {
    var email = req.param('email'),
        password = req.param('password'),
        firstname = req.param('firstname'),
        lastname = req.param('lastname');

    var checkNotExistingAccount = function(success) {
            accountModel.findOne(email, function(err, account) {
                if (err) {
                    res.json({
                        status: ResponseTypes.DB_ERROR
                    });
                    logger.error(ResponseTypes.DB_ERROR);
                    return;
                }
                if (account) {
                    res.json({
                        status: ResponseTypes.EMAIL_TAKEN
                    });
                    logger.info(ResponseTypes.EMAIL_TAKEN);
                    return;
                }
                success();
            });
        },
        createAccount = function(callback) {
            bcrypt.hash(password, 10, function(err, hash) {
                var account;
                if (err) {
                    res.json({
                        status: ResponseTypes.ENCRYPT_ERROR
                    });
                    logger.error(ResponseTypes.ENCRYPT_ERROR + ': ' + err);
                    return;
                }

                account = {
                    email: email.toLowerCase(),
                    password: <PASSWORD>,
                    firstname: firstname,
                    lastname: lastname
                };
                accountModel.add(account, callback);
            });
        };

    logger.info('Request new account: ' + email);

    if (!email || !password || !firstname || !lastname) {
        res.json({
            status: ResponseTypes.MISSING_FIELD
        });
        logger.info(ResponseTypes.MISSING_FIELD);
        return;
    }

    email = email.toLowerCase();

    checkNotExistingAccount(function() {
        createAccount(function(err) {
            if (err) {
                res.json({
                    status: ResponseTypes.DB_ERROR
                });
                logger.error(ResponseTypes.DB_ERROR + ': ' + err);
                return;
            }
            logger.info('Create realm on the fly for new user ' + email);
            createRealm('__default_', email, function(error, newRealm) {
                if (error) {
                    logger.info('Login failed ' + ResponseTypes.DB_ERROR);
                    res.json({
                        status: ResponseTypes.DB_ERROR
                    });
                    return;
                }
                res.json({
                    account: {
                        firstname: firstname,
                        lastname: lastname,
                        email: email,
                        realm: newRealm
                    },
                    status: ResponseTypes.SUCCESS
                });
            });
        });
    });
};

module.exports.loginDemo = function(req, res) {
    module.exports.login({
        param: function(attr) {
            if (attr == 'email') {
                return '<EMAIL>';
            } else if (attr == 'secretword') {
                return '<PASSWORD>';
            }
        },
        session: req.session
    }, {
        json: function(result) {
            if (result.status == ResponseTypes.SUCCESS) {
                res.redirect('/loggedin');
            } else {
                res.redirect('/logout');
            }
        }
    });
};<commit_msg>Fixed issue on auto login for the demo site<commit_after>'use strict';

var bcrypt = require('bcryptjs'),
    logger = require('winston'),
    accountModel = require('../models/account'),
    realmModel = require('../models/realm'),
    ResponseTypes = {
        SUCCESS: 'success',
        DB_ERROR: 'db-error',
        ENCRYPT_ERROR: 'encrypt-error',
        MISSING_FIELD: 'missing-field',
        USER_NOT_FOUND: 'login-user-not-found',
        INVALID_PASSWORD: '<PASSWORD>',
        INVALID_REALM: 'login-invalid-realm',
        REALM_NOT_FOUND: 'login-realm-not-found',
        REALM_TAKEN: 'signup-realm-taken',
        EMAIL_TAKEN: 'signup-email-taken'
    };

var createRealm = function(name, email, callback) {
    var newRealm = {
        name: name,
        creation: new Date(),
        administrator: email
    };

    realmModel.add(newRealm, function(err, dbRealm) {
        if (callback) {
            callback(err, dbRealm);
        }
    });
};

var checkUserMemberOfRealm = function(email, realms) {
    var realmsFound;
    realmsFound = realms.filter(function(realm) {
        if (realm.administrator === email ||
            realm.user1 === email ||
            realm.user2 === email ||
            realm.user3 === email ||
            realm.user4 === email ||
            realm.user5 === email ||
            realm.user6 === email ||
            realm.user7 === email ||
            realm.user8 === email ||
            realm.user9 === email ||
            realm.user10 === email) {
            return true;
        }
    });
    return realmsFound;
};

module.exports.signup = function(req, res) {
    var email = req.param('email'),
        password = req.param('password'),
        firstname = req.param('firstname'),
        lastname = req.param('lastname');

    var checkNotExistingAccount = function(success) {
            accountModel.findOne(email, function(err, account) {
                if (err) {
                    res.json({
                        status: ResponseTypes.DB_ERROR
                    });
                    logger.error(ResponseTypes.DB_ERROR);
                    return;
                }
                if (account) {
                    res.json({
                        status: ResponseTypes.EMAIL_TAKEN
                    });
                    logger.info(ResponseTypes.EMAIL_TAKEN);
                    return;
                }
                success();
            });
        },
        createAccount = function(callback) {
            bcrypt.hash(password, 10, function(err, hash) {
                var account;
                if (err) {
                    res.json({
                        status: ResponseTypes.ENCRYPT_ERROR
                    });
                    logger.error(ResponseTypes.ENCRYPT_ERROR + ': ' + err);
                    return;
                }

                account = {
                    email: email.toLowerCase(),
                    password: <PASSWORD>,
                    firstname: firstname,
                    lastname: lastname
                };
                accountModel.add(account, callback);
            });
        };

    logger.info('Request new account: ' + email);

    if (!email || !password || !firstname || !lastname) {
        res.json({
            status: ResponseTypes.MISSING_FIELD
        });
        logger.info(ResponseTypes.MISSING_FIELD);
        return;
    }

    email = email.toLowerCase();

    checkNotExistingAccount(function() {
        createAccount(function(err) {
            if (err) {
                res.json({
                    status: ResponseTypes.DB_ERROR
                });
                logger.error(ResponseTypes.DB_ERROR + ': ' + err);
                return;
            }
            logger.info('Create realm on the fly for new user ' + email);
            createRealm('__default_', email, function(error, newRealm) {
                if (error) {
                    logger.info('Login failed ' + ResponseTypes.DB_ERROR);
                    res.json({
                        status: ResponseTypes.DB_ERROR
                    });
                    return;
                }
                res.json({
                    account: {
                        firstname: firstname,
                        lastname: lastname,
                        email: email,
                        realm: newRealm
                    },
                    status: ResponseTypes.SUCCESS
                });
            });
        });
    });
};

module.exports.loginDemo = function(req, res) {
    module.exports.login({
        body: {
            email: '<EMAIL>',
            secretword: '<PASSWORD>'
        },
        session: req.session
    }, {
        json: function(result) {
            if (result.status == ResponseTypes.SUCCESS) {
                res.redirect('/loggedin');
            } else {
                res.redirect('/logout');
            }
        }
    });
};
{'fraction_non_alphanumeric': 0.11737042225335201, 'fraction_numerical': 0.002601560936561937, 'mean_word_length': 1.288756583466911, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '7225726', 'n_tokens_mistral': 2662, 'n_tokens_neox': 2532, 'n_words': 470}