/*                   A C C O U N T S . J S
 * BRL-CAD
 *
 * Copyright (c) 1995-2013 United States Government as represented by
 * the U.S. Army Research Laboratory.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this file; see the file named COPYING for more
 * information.
 */

/** @file OGV/server/accounts.js
 *  @brief file for email validation configuration
 *
 *  This file  serves two purposes. Firstly it contains configuration
 *  regarding sending email for the purpose of validating the new
 *  registered user. After verification  one can upload their models,
 *  and use OGV.
 */

/**
 * Create a test user without admin roles and a super user with
 * admin roles on a fresh install (when number of users is zero)
 */

//Generating 8 Length Alphanumeric Password.
function passwordGen(len){
    var text = " ";
    var charset = "abcdefghijklmnopqrstuvwxyz0123456789";
    for( var i=0; i < len; i++ )
  text += charset.charAt(Math.floor(Math.random() * charset.length));
    return text;
}

//Applying Password to Meteor Admin User Settings and Logging to Server.
if(Meteor.settings.adminPassword==="password"){
  var randomPassword = passwordGen(8)

  Meteor.settings = {
	adminPassword: randomPassword,
	private:{},
	public : {
		smtp: {
			username: Meteor.settings.public.smtp.username,
			password: Meteor.settings.public.smtp.password,
			server: Meteor.settings.public.smtp.server,
			port: Meteor.settings.public.smtp.port
		}
	}
 }
console.log("Admin Password:" + Meteor.settings.adminPassword);
}

Accounts.config({
  sendVerificationEmail: true
  //forbidClientAccountCreation: false
});

if (Meteor.users.find().fetch().length === 0) {
  const users = [
    {
      name: "Test User",
      email: "normal@example.com",
      roles: []
    },
    {
      name: "Super User",
      email: "admin@example.com",
      roles: ["admin"]
    }
  ];

  const Bio = "greatest 3d modeller on the planet";
  _.each(users, userData => {
    const password = Meteor.settings.adminPassword;
    const id = Accounts.createUser({
      email: userData.email,
      password: password,
      profile: {
        name: userData.name,
        bio: Bio
      }
    });

    // email verification
    Meteor.users.update(
      {
        _id: id
      },
      {
        $set: {
          "emails.0.verified": true
        }
      }
    );

    Roles.addUsersToRoles(id, userData.roles);
  });
}

Accounts.onCreateUser((options, user) => {
  const followingArray = [];
  // followingArray[0] = user._id;
  const adminUser = Meteor.users.findOne({
    "roles.0": "admin"
  });
  followingArray[0] = adminUser._id;
  followingArray[1] = user._id;

  if (options.profile) {
    options.profile.following = followingArray;
    user.profile = options.profile;
  } else {
    console.log(options);
  }

  return user;
});

/* Meteor.users.allow({
    update: function(userId, user, fields)
    {
        if (!fields.isEqualTo(['profile.following', 'profile.follower'])) {
            return false;
        } else {
            return true;
        }
    }
});
*/

/**
 *  Need to allow the users to update only the follwers array of other users
 */
Meteor.users.allow({
  update() {
    return true;
  }
});

/**
 * Intended to Delete/Remove users who have not verified their Emails in hrs hours
 */
const hrs = 1;
Meteor.setInterval(() => {
  Meteor.users
    .find({
      "emails.0.verified": false
    })
    .forEach(user => {
      // Do action with 'user' that has not verified email for 1 hour
      Meteor.users.remove(
        {
          _id: user._id
        },
        true
      );
    });
}, 3600000 * hrs);

@@ -1577,384 +1577,95 @@
 er.%0A
-if(Meteor.settings.adminPassword===%22password%22)%7B%0A  var randomPassword = passwordGen(8)%0A%0A  Meteor.settings = %7B%0A%09adminPassword: randomPassword,%0A%09private:%7B%7D,%0A%09public : %7B%0A%09%09smtp: %7B%0A%09%09%09username: Meteor.settings.public.smtp.username,%0A%09%09%09password: Meteor.settings.public.smtp.password,%0A%09%09%09server: Meteor.settings.public.smtp.server,%0A%09%09%09port: Meteor.settings.public.smtp.port%0A%09%09%7D%0A%09%7D%0A %7D
+var randomPassword = passwordGen(8)%0A%0A//User ID for Admin is currently bMfEoAFqu4EibGwtH
 %0Acon
@@ -1725,17 +1725,73 @@
 sword);%0A
-%7D
+Accounts.setPassword(%22bMfEoAFqu4EibGwtH%22,randomPassword)%0A
 %0A%0AAccoun
@@ -2132,16 +2132,20 @@
 %7D%0A  %5D;%0A%0A
+  %0A%0A
   const 
@@ -2239,37 +2239,22 @@
 sword = 
-Meteor.settings.admin
+random
 Password

Set Random Admin Account Password