You are here

function eck_update_7013 in Entity Construction Kit (ECK) 7.3

Update global permissions from 2.x to 3.x.

File

./eck.install, line 543
ECK's requirements, schemas, and logic for install and uninstall.

Code

function eck_update_7013() {

  // Lets get all eck related permissions.
  $query = db_select('role_permission', 't');
  $query
    ->fields('t', array(
    'rid',
    'permission',
    'module',
  ));
  $query
    ->condition("module", "eck", "=");
  $permissions = $query
    ->execute();
  foreach ($permissions as $perm_obj) {
    $permission = $perm_obj->permission;

    // Lets parse the permission.
    $pieces = explode(" ", $permission);
    $final_pieces = array();
    $throw_away = array_shift($pieces);

    // The first piece we can ignore, and the second is the action.
    $action_map = array(
      'administer' => "*",
      'add' => 'create',
      'edit' => 'update',
      'view' => 'view',
      'list' => 'list',
      'delete' => 'delete',
    );
    $object_map = array(
      'entity types' => 'entity_type',
      'bundles' => "bundle",
      'entities' => 'entity',
    );
    $action = array_shift($pieces);
    if (array_key_exists($action, $action_map)) {
      $final_pieces[0] = $action_map[$action];
    }

    // We have a permission that breaks all our patters, so lets deal with
    // it right away.
    if (substr_count($permission, 'manage') > 0) {
      $pieces = array();
      $final_pieces = array(
        'update',
        'property',
      );
    }
    $object_id = "";
    while (count($pieces) > 0) {
      $piece = array_shift($pieces);

      // This is a sign that we are talking about an entity type.
      if ($piece == 'entity') {
        $p = array_shift($pieces);
        $piece = "{$piece} {$p}";
      }
      if (in_array($piece, array(
        'entities',
        'bundles',
        'entity types',
      ))) {
        if (!empty($object_id)) {
          $final_pieces[1] = "{$object_map[$piece]}:{$object_id}|*";
        }
        else {
          $final_pieces[1] = "{$object_map[$piece]}";
        }
      }
      else {
        if (empty($object_id)) {
          $object_id = $piece;
        }
        else {
          $object_id .= "|{$piece}";
        }
      }
    }
    $new_permission = implode(" ", $final_pieces);

    // Lets set it up in eck_permissions module.
    $perm = new ECKPermission();
    $perm->type = 'role';
    $perm->oid = $perm_obj->rid;
    $perm->permission = $new_permission;
    $perm
      ->save();

    // And then delete it from Drupal permissions table.
    $num_deleted = db_delete('role_permission')
      ->condition('rid', $perm_obj->rid)
      ->condition('permission', $perm_obj->permission)
      ->execute();
  }
}