<commit_before>var USER = 'server.user';

var USER_REGISTRY = 'server.user.registry';

var USER_OPTIONS = 'server.user.options';

var USER_SPACE = 'server.user.space';

var USER_ROLE_PREFIX = 'private_';

var PUBLISHER_ROLE_PREFIX='publisher';

var init = function (options) {
    var role, roles, user,
        server = require('/modules/server.js'),
        um = server.userManager();
    roles = options.roles;
    for (role in roles) {
        if (roles.hasOwnProperty(role)) {
            if (um.roleExists(role)) {
                um.authorizeRole(role, roles[role]);
            } else {
                um.addRole(role, [], roles[role]);
            }
        }
    }
    user = um.getUser(options.user.username);
    if (!user.hasRoles(options.userRoles)) {
        user.addRoles(options.userRoles);
    }
    application.put(USER_OPTIONS, options);
};

var options = function () {
    return application.get(USER_OPTIONS);
};

var login = function (username, password) {
    var user, perm, perms, actions, i, length,
        authorized = false,
        opts = options(),
        carbon = require('carbon'),
        server = require('/modules/server.js'),
        serv = server.server(),
        um = server.userManager();
    if (!serv.authenticate(username, password)) {
        return false;
    }
    user = um.getUser(username);
    perms = opts.permissions.login;
    L1:
        for (perm in perms) {
            if (perms.hasOwnProperty(perm)) {
                actions = perms[perm];
                length = actions.length;
                for (i = 0; i < length; i++) {
                    if (user.isAuthorized(perm, actions[i])) {
                        authorized = true;
                        break L1;
                    }
                }
            }
        }
    if (!authorized) {
        return false;
    }
    session.put(USER, new carbon.user.User(um, username));
    session.put(USER_REGISTRY, new carbon.registry.Registry(serv, {
        username: username,
        tenantId: carbon.server.tenantId()
    }));
    session.put(USER_SPACE, new carbon.user.Space(username, opts.userSpace.space, opts.userSpace.options));
    if (opts.login) {
        opts.login(user, password, session);
    }
    return true;
};

var isAuthorized = function (permission, action) {
    var user = current(),
        server = require('/modules/server.js'),
        um = server.userManager();
    user = user ? user.username : options().user.username;
    return um.getUser(user).isAuthorized(permission, action);
};

var userSpace = function () {
    try {
        return session.get(USER_SPACE);
    } catch (e) {
        return null;
    }
};

var userRegistry = function () {
    try {
        return session.get(USER_REGISTRY);
    } catch (e) {
        return null;
    }
};

var logout = function () {
    var opts = options(),
        user = current();
    if (opts.logout) {
        opts.logout(user, session);
    }
    session.remove(USER);
    session.remove(USER_SPACE);
    session.remove(USER_REGISTRY);
};

var userExists = function (username) {
    var server = require('/modules/server.js');
    return server.userManager().userExists(username);
};

var privateRole = function (username) {
    return USER_ROLE_PREFIX + username;
};

var createPublisherRole=function(){
    return PUBLISHER_ROLE_PREFIX;
};

/*
The function is used to fill the permissions object. The permissions are applied
to the users space e.g. username= test , and the gadget collection in the /_system/governance
then the permissions will be applicable to
/_system/governance/gadget/test.
@username: The username of the account to which the permissions will be attached
@permissions: An object of permissions which will be assigned to the newly created user role
 */
var buildPermissionsList=function(username,permissions,server){

    //Obtain the accessible collections
    var accessible=options().userSpace.accessible;
    //log.debug('Entered permissions list.');
    var id;
    var accessibleContext;
    var accessibleCollections;
    var context;
    var actions;
    var collection;
    var sysRegistry;

    //Go through all of the accessible directives
    for(var index in accessible){

        accessibleContext=accessible[index];

        accessibleCollections=accessibleContext.collections;

        context=accessibleContext.context;     //e.g. /_system/governance/
        actions=accessibleContext.actions;     //read,write

        //Go through all of the collections
        for(var colIndex in accessibleCollections){

            collection=accessibleCollections[colIndex];

            //Create the id used for the permissions
            //id=context+'/'+collection+'/'+username;
            id=context+'/'+collection+'/'+username;
            //log.debug('permission id: '+id);
            //Assign the actions to the id
            permissions[id]=actions;

            //Create a dummy collection, as once permissions are
            //the user will be unable to create assets in the
            //parent collection
            //Thus we create a user collection.<commit_msg>Added changes to user.js to create a collection by the username<commit_after>var USER = 'server.user';

var USER_REGISTRY = 'server.user.registry';

var USER_OPTIONS = 'server.user.options';

var USER_SPACE = 'server.user.space';

var USER_ROLE_PREFIX = 'private_';

var PUBLISHER_ROLE_PREFIX='publisher';

var init = function (options) {
    var role, roles, user,
        server = require('/modules/server.js'),
        um = server.userManager();
    roles = options.roles;
    for (role in roles) {
        if (roles.hasOwnProperty(role)) {
            if (um.roleExists(role)) {
                um.authorizeRole(role, roles[role]);
            } else {
                um.addRole(role, [], roles[role]);
            }
        }
    }
    user = um.getUser(options.user.username);
    if (!user.hasRoles(options.userRoles)) {
        user.addRoles(options.userRoles);
    }
    application.put(USER_OPTIONS, options);
};

var options = function () {
    return application.get(USER_OPTIONS);
};

var login = function (username, password) {
    var user, perm, perms, actions, i, length,
        authorized = false,
        opts = options(),
        carbon = require('carbon'),
        server = require('/modules/server.js'),
        serv = server.server(),
        um = server.userManager();
    if (!serv.authenticate(username, password)) {
        return false;
    }
    user = um.getUser(username);
    perms = opts.permissions.login;
    L1:
        for (perm in perms) {
            if (perms.hasOwnProperty(perm)) {
                actions = perms[perm];
                length = actions.length;
                for (i = 0; i < length; i++) {
                    if (user.isAuthorized(perm, actions[i])) {
                        authorized = true;
                        break L1;
                    }
                }
            }
        }
    if (!authorized) {
        return false;
    }
    session.put(USER, new carbon.user.User(um, username));
    session.put(USER_REGISTRY, new carbon.registry.Registry(serv, {
        username: username,
        tenantId: carbon.server.tenantId()
    }));
    session.put(USER_SPACE, new carbon.user.Space(username, opts.userSpace.space, opts.userSpace.options));
    if (opts.login) {
        opts.login(user, password, session);
    }
    return true;
};

var isAuthorized = function (permission, action) {
    var user = current(),
        server = require('/modules/server.js'),
        um = server.userManager();
    user = user ? user.username : options().user.username;
    return um.getUser(user).isAuthorized(permission, action);
};

var userSpace = function () {
    try {
        return session.get(USER_SPACE);
    } catch (e) {
        return null;
    }
};

var userRegistry = function () {
    try {
        return session.get(USER_REGISTRY);
    } catch (e) {
        return null;
    }
};

var logout = function () {
    var opts = options(),
        user = current();
    if (opts.logout) {
        opts.logout(user, session);
    }
    session.remove(USER);
    session.remove(USER_SPACE);
    session.remove(USER_REGISTRY);
};

var userExists = function (username) {
    var server = require('/modules/server.js');
    return server.userManager().userExists(username);
};

var privateRole = function (username) {
    return USER_ROLE_PREFIX + username;
};

var createPublisherRole=function(){
    return PUBLISHER_ROLE_PREFIX;
};

/*
The function is used to fill the permissions object. The permissions are applied
to the users space e.g. username= test , and the gadget collection in the /_system/governance
then the permissions will be applicable to
/_system/governance/gadget/test.
@username: The username of the account to which the permissions will be attached
@permissions: An object of permissions which will be assigned to the newly created user role
 */
var buildPermissionsList=function(username,permissions,server){

    //Obtain the accessible collections
    var accessible=options().userSpace.accessible;
    //log.debug('Entered permissions list.');
    var id;
    var accessibleContext;
    var accessibleCollections;
    var context;
    var actions;
    var collection;
    var sysRegistry;

    //Go through all of the accessible directives
    for(var index in accessible){

        accessibleContext=accessible[index];

        accessibleCollections=accessibleContext.collections;

        context=accessibleContext.context;     //e.g. /_system/governance/
        actions=accessibleContext.actions;     //read,write

        //Go through all of the collections
        for(var colIndex in accessibleCollections){

            collection=accessibleCollections[colIndex];

            //Create the id used for the permissions
            //id=context+'/'+collection+'/'+username;
            id=context+'/'+collection+'/'+username;
            //log.debug('permission id: '+id);
            //Assign the actions to the id
            permissions[id]=actions;

            //Create a dummy collection, as once permissions are
            //the user will be unable to create assets in the
            //parent collection.
            //Thus we create a user collection.
{'fraction_non_alphanumeric': 0.11494699990275212, 'fraction_numerical': 0.000583487309151026, 'mean_word_length': 2.4177467597208375, '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': '6063946', 'n_tokens_mistral': 2895, 'n_tokens_neox': 2842, 'n_words': 761}