<commit_before>/*!
 * Copyright 2012 Sakai Foundation (SF) Licensed under the
 * Educational Community License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 *     http://www.osedu.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

var AuthenticationAPI = require('oae-authentication');
var AuthenticationConstants = require('oae-authentication/lib/constants').AuthenticationConstants;
var Cassandra = require('oae-util/lib/cassandra');
var Context = require('oae-context').Context;
var LoginId = require('oae-authentication/lib/model').LoginId;
var Tenant = require('oae-tenants/lib/model').Tenant;

var PrincipalsAPI = require('oae-principals');
var User = require('oae-principals/lib/model').User;

var log = require('oae-logger').logger('oae-principals');

module.exports = function(config, callback) {
    ensureSchema(function(err) {
        if (err) {
            return callback(err);
        }

        // We'll create/get 1 global admin account in the internal global tenant that can be used
        // to create tenants/other global accounts.
        var globalTenant = new Tenant(config.servers.globalAdminAlias, 'Global admin server', config.servers.globalAdminPort, 'localhost');
        var globalLoginId = new LoginId(globalTenant.alias, AuthenticationConstants.providers.LOCAL, 'administrator', { 'password': '<PASSWORD>' });
        var globalUser = new User(globalTenant.alias, null, 'Global Administrator', {
            visibility: 'private',
            isGlobalAdmin: true
        });
        var globalContext = new Context(globalTenant, globalUser);

        AuthenticationAPI.getUserIdFromLoginId(globalLoginId.tenantAlias, globalLoginId.provider, globalLoginId.externalId, function(err, userId) {
            if (err) {
                return callback(err);
            }

            // the global admin does not exist. create it
            if (!userId) {
                
                AuthenticationAPI.createUser(globalContext, globalLoginId, globalUser.displayName, { visibility: 'private' }, function(err, createdUser) {
                    if (err) {
                        return callback(err);
                    }

                    // promote them to administrator
                    PrincipalsAPI.setGlobalAdmin(globalContext, createdUser.id, true, function(err) {
                        if (err) {
                            return callback(err);
                        }

                        log().info('Admin account created with ID "%s" and username "%s".', createdUser.id, 'administrator');
                        
                        callback();
                    });
                });
            }
        });
    });
};

/**
 * Ensure that the all of the principal-related schemas are created. If they already exist, this method will not
 * do anything
 * 
 * @param  {Function}    callback       A function(err), specifying whether or not there was an error creating the schema.
 * @param  {Object}      callback.err   Error object, containing the error message
 * @api private
 */
var ensureSchema = function(callback) {
    // Both user and group information will be stored inside of the Principals CF
    Cassandra.createColumnFamily('Principals', 'CREATE COLUMNFAMILY Principals (principalId text PRIMARY KEY, alias text, tenant text, visibility text);', callback);
};<commit_msg>(NoTicket) Fixed bug where if admin already existed, server would not continue starting.<commit_after>/*!
 * Copyright 2012 Sakai Foundation (SF) Licensed under the
 * Educational Community License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 *     http://www.osedu.org/licenses/ECL-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

var AuthenticationAPI = require('oae-authentication');
var AuthenticationConstants = require('oae-authentication/lib/constants').AuthenticationConstants;
var Cassandra = require('oae-util/lib/cassandra');
var Context = require('oae-context').Context;
var LoginId = require('oae-authentication/lib/model').LoginId;
var Tenant = require('oae-tenants/lib/model').Tenant;

var PrincipalsAPI = require('oae-principals');
var User = require('oae-principals/lib/model').User;

var log = require('oae-logger').logger('oae-principals');

module.exports = function(config, callback) {
    ensureSchema(function(err) {
        if (err) {
            return callback(err);
        }

        // We'll create/get 1 global admin account in the internal global tenant that can be used
        // to create tenants/other global accounts.
        var globalTenant = new Tenant(config.servers.globalAdminAlias, 'Global admin server', config.servers.globalAdminPort, 'localhost');
        var globalLoginId = new LoginId(globalTenant.alias, AuthenticationConstants.providers.LOCAL, 'administrator', { 'password': '<PASSWORD>' });
        var globalUser = new User(globalTenant.alias, null, 'Global Administrator', {
            visibility: 'private',
            isGlobalAdmin: true
        });
        var globalContext = new Context(globalTenant, globalUser);

        AuthenticationAPI.getUserIdFromLoginId(globalLoginId.tenantAlias, globalLoginId.provider, globalLoginId.externalId, function(err, userId) {
            if (err) {
                return callback(err);
            }

            // the global admin does not exist. create it
            if (!userId) {
                
                AuthenticationAPI.createUser(globalContext, globalLoginId, globalUser.displayName, { visibility: 'private' }, function(err, createdUser) {
                    if (err) {
                        return callback(err);
                    }

                    // promote them to administrator
                    PrincipalsAPI.setGlobalAdmin(globalContext, createdUser.id, true, function(err) {
                        if (err) {
                            return callback(err);
                        }

                        log().info('Admin account created with ID "%s" and username "%s".', createdUser.id, 'administrator');
                        
                        callback();
                    });
                });
            } else {
                callback();
            }
        });
    });
};

/**
 * Ensure that the all of the principal-related schemas are created. If they already exist, this method will not
 * do anything
 * 
 * @param  {Function}    callback       A function(err), specifying whether or not there was an error creating the schema.
 * @param  {Object}      callback.err   Error object, containing the error message
 * @api private
 */
var ensureSchema = function(callback) {
    // Both user and group information will be stored inside of the Principals CF
    Cassandra.createColumnFamily('Principals', 'CREATE COLUMNFAMILY Principals (principalId text PRIMARY KEY, alias text, tenant text, visibility text);', callback);
};
{'fraction_non_alphanumeric': 0.09785171600733561, 'fraction_numerical': 0.002357872674875557, 'mean_word_length': 2.5710944808231995, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '397633', 'n_tokens_mistral': 1928, 'n_tokens_neox': 1867, 'n_words': 624}