<commit_before>/* https://jira.codenvycorp.com/browse/CODENVY-446 */
var createPermissions = function(){
  var organizationDb = db.getSiblingDB("organization");
  var workersCollection = organizationDb.getCollection("workers");
  var workspaceIterator = organizationDb.getCollection("workspaces2").find();
  var workersCount = 0;
  while (workspaceIterator.hasNext()) {
    var workspace = workspaceIterator.next();

    if (workersCollection.findOne({"user": workspace.namespace, "workspace": workspace._id}) == null) {
      workersCollection.update({
        "user": workspace.namespace,
        "workspace": workspace._id
      },
      {
        "user": workspace.namespace,
        "workspace": workspace._id,
        "actions" : ["read", "run", "use", "configure", "setPermissions", "readPermissions", "delete"]
      },
      {
        upsert: true
      });
      workersCount++;
    }
  }

  if (workersCount == 0) {
    print("All workspaces owners have permissions");
  } else {
    print("Permissions for " + workersCount +" workspaces owners have been created");
  }
}

createPermissions();

// ---------------------------------------------
var checkActions = function(actions, objectType, id) {
  var result = [];
  for (var i = 0; i < actions.length; i++) {
    var action = actions[i];
    if (action == "search") {
      //ignore
    } else if (action != "read" && action != "update" && action != "delete") {
      print("Unknown action " + action + " in " + objectType + " with id " + id + ". It'll be ignored.");
    } else {
      result.push(action);
    }
  }
  return result;
};

var createAcl = function(collectionName, objectType){
  var organizationDb = db.getSiblingDB("organization");
  var collection = organizationDb.getCollection(collectionName);
  var updateNumber = 0;

  var objectIterator = collection.find();
  while (objectIterator.hasNext()) {
    var object = objectIterator.next();
    var permissions = object.permissions;

    var acl = [];

    if(permissions == null || permissions == undefined) {
      continue;
    }

    permissions.groups.forEach(function(group) {
      if (group.name != "public") {
        print("Unknown group name " + group.name + " in "+objectType+" with id "
        + object._id+". It will be ignored.");
      } else {
        acl.push({"user":"*", "actions": checkActions(group.acl)});
      }
    });

    for (var user in permissions.users) {
      var actions = checkActions(permissions.users[user]);
      acl.push({"user": user, "actions": actions});
    }

    delete object.permissions;
    object.acl = acl;

    collection.update({"_id" : object._id}, object);
    updateNumber++;
  }

  print("Added acl for " + updateNumber + " " + collectionName + ". Total number " + collection.count());
}

createAcl("recipes", "recipe");
createAcl("stacks", "stack");

// ---------------------------------------------
var updateOrganization = function(){
    var organizationDb = db.getSiblingDB("organization");
    var ssh = organizationDb.getCollection("ssh");

    ssh.dropIndex( { "owner": 1, "service": 1, "name": 1 } )
    ssh.update({service:"git"}, {$set : {service:"vcs"}}, {multi:true});<commit_msg>CODENVY-446: fix script to migrate Codenvy 4.2.2 to 4.3.2<commit_after>/* https://jira.codenvycorp.com/browse/CODENVY-446 */
var createPermissions = function(){
  var organizationDb = db.getSiblingDB("organization");
  var workersCollection = organizationDb.getCollection("workers");
  var workspaceIterator = organizationDb.getCollection("workspaces2").find();
  var workersCount = 0;
  while (workspaceIterator.hasNext()) {
    var workspace = workspaceIterator.next();

    if (workersCollection.findOne({"user": workspace.namespace, "workspace": workspace._id}) == null) {
      workersCollection.update({
        "user": workspace.namespace,
        "workspace": workspace._id
      },
      {
        "user": workspace.namespace,
        "workspace": workspace._id,
        "actions" : ["read", "run", "use", "configure", "setPermissions", "readPermissions", "delete"]
      },
      {
        upsert: true
      });
      workersCount++;
    }
  }

  if (workersCount == 0) {
    print("All workspaces owners have permissions");
  } else {
    print("Permissions for " + workersCount +" workspaces owners have been created");
  }
}

createPermissions();

// ---------------------------------------------

var checkActions = function(actions, objectType, id) {
  var result = [];
  for (var i = 0; i < actions.length; i++) {
    var action = actions[i];
    if (action == "search") {
      //ignore
    } else if (action != "read" && action != "update" && action != "delete") {
      print("Unknown action " + action + " in " + objectType + " with id " + id + ". It'll be ignored.");
    } else {
      result.push(action);
    }
  }
  return result;
};

var createAcl = function(collectionName, objectType){
  var organizationDb = db.getSiblingDB("organization");
  var collection = organizationDb.getCollection(collectionName);
  var updateNumber = 0;

  var objectIterator = collection.find();
  while (objectIterator.hasNext()) {
    var object = objectIterator.next();
    var permissions = object.permissions;

    var acl = [];

    if(permissions == null || permissions == undefined) {
      continue;
    }

    permissions.groups.forEach(function(group) {
      if (group.name != "public") {
        print("Unknown group name " + group.name + " in " + objectType + " with id "
        + object._id + ". It will be ignored.");
      } else if (group.acl.indexOf("search") > -1) {
          //public search is similar to 'public' (user:*) read in 4.3
          //we don't support other public actions in 4.3
          acl.push({"user":"*", "actions": ["read"]});
        }
      }
    );

    for (var user in permissions.users) {
      var actions = checkActions(permissions.users[user]);
      acl.push({"user": user, "actions": actions});
    }

    //remove old creator's permissions
    var creator = object.creator;
    for (var i = 0; i < acl.length; i++) {
      var aclEntry = acl[i];
      if (aclEntry.user == creator) {
        acl.splice(i, 1);
      }
    }

    //add creator permissions
    acl.push({"user":creator, "actions":["read", "update", "delete", "setPermissions"]});

    delete object.permissions;
    object.acl = acl;

    collection.update({"_id" : object._id}, object);
    updateNumber++;
  }

  print("Added acl for " + updateNumber + " " + collectionName + ". Total number " + collection.count());
}

createAcl("recipes", "recipe");
createAcl("stacks", "stack");

// ---------------------------------------------

var updateOrganization = function(){
    var organizationDb = db.getSiblingDB("organization");
    var ssh = organizationDb.getCollection("ssh");

    ssh.dropIndex( { "owner": 1, "service": 1, "name": 1 } )
    ssh.update({service:"git"}, {$set : {service:"vcs"}}, {multi:true});
{'fraction_non_alphanumeric': 0.18380017326017903, 'fraction_numerical': 0.005486572336124747, 'mean_word_length': 2.8291873963515752, 'pattern_counts': {'":': 28, '<': 6, '<?xml version=': 0, '>': 4, 'https://': 2, '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': '3115382', 'n_tokens_mistral': 2065, 'n_tokens_neox': 1994, 'n_words': 486}