Would be good to get some direction on how to use Migrate to include permissions when using the Migrate module. Whether moving a D5/6 site to D7 or using a different CMS, it would be good to do a mapping for roles that would speed up the import process.

I'm figuring this has already been though through, but perhaps not. We're quite enjoying this module!

Comments

mikeryan’s picture

Component: Documentation » Code
Category: support » feature
Status: Active » Postponed (maintainer needs more info)

Can you explain a little more clearly what you mean by "node permissions"? If you mean the permissions associated with a role, we do have a role destination class (MigrateDestinationRole) but it pretty much just migrate the role name - not a bad thought to bring across the permissions as well.

mgifford’s picture

I think in this case it is mostly the role, but there are of course other means to control node permissions. Would be great to be able to provide more flexibility on that front.

I will try to come back with a more detailed requirements list.

StuartDH’s picture

+1 in the interim I was going to use features for permissions, but this would be a good addition to migrate

mikeryan’s picture

Title: Preserving Node Permissions » Importing role permissions
Status: Postponed (maintainer needs more info) » Active

Still don't know what "node permissions" means, but importing the permissions associated with roles makes sense...

SamH’s picture

We could either insert into {role_permission} directly, or use user_role_grant_permissions($rid, array $permissions = array()). Which way makes more sense? The choice will determine what fields we have for the permission destination.

randallknutson’s picture

Issue summary: View changes

For anyone still interested in this, I wrote a quick MigrationDestination class for role_permissions. You will need to include this in a custom module but it does work.

<?php
/**
 * @file
 * Support for role permissions.
 */

/**
 * Destination class implementing migration into role permissions.
 */
class MigrateDestinationRolePermission extends MigrateDestination {

  /**
   * The role.
   *
   * @var string
   */
  protected $role;
  /**
   * Function to get the role.
   */
  public function getRole() {
    return $this->role;
  }

  /**
   * Return the name of the class.
   */
  public function __toString() {
    return t('Role Permission');
  }

  /**
   * Initiate the role_permissions for a particular role.
   *
   * @param string $role_name
   *   The name of the role.
   */
  public function __construct($role_name) {
    $this->role = user_role_load_by_name($role_name);
  }

  /**
   * Set the primary key schema for role permissions.
   *
   * @return array
   *   Return the schema for the key.
   */
  static public function getKeySchema() {
    return array(
      'rid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'ID of destination role',
      ),
      'permission' => array(
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
        'description' => 'key of the destination permission',
      ),
    );
  }

  /**
   * Import a single row.
   *
   * @param object $object
   *   Object object to build. Prefilled with any fields mapped in the
   *   Migration.
   * @param object $row
   *   Raw source data object - passed through to prepare/complete handlers.
   * 
   * @return array
   *   Array of key fields of the object that was saved if
   *   successful. FALSE on failure.
   */
  public function import(stdClass $object, stdClass $row) {
    $modules = user_permission_get_modules();

    migrate_instrument_start('role_permission_grant');

    if ($modules[$object->permission]) {
      user_role_grant_permissions($this->role->rid, array($object->permission));

      migrate_instrument_stop('role_permission_grant');
      return array('rid' => $this->role->rid, 'permission' => $object->permission);
    }
    else {
      // Permission does not exist. Consider this a success.
      migrate_instrument_stop('role_permission_grant');
      return array('rid' => $this->role->rid, 'permission' => $object->permission);
    }

    migrate_instrument_stop('role_permission_grant');
    return FALSE;
  }

  /**
   * Delete a migrated permission.
   *
   * @param array $ids
   *   Array of fields representing the key.
   */
  public function rollback(array $ids) {
    migrate_instrument_start('role_permission_delete');

    user_role_revoke_permissions($ids['destid1'], array($ids['destid2']));

    migrate_instrument_stop('role_permission_delete');

    return TRUE;
  }

  /**
   * Returns a list of fields available to be mapped.
   *
   * @param Migration $migration
   *   Optionally, the migration containing this destination.
   * 
   * @return array
   *   Keys: machine names of the fields (to be passed to addFieldMapping)
   *   Values: Human-friendly descriptions of the fields.
   */
  public function fields($migration = NULL) {
    $fields = array();

    $fields['rid'] = t('Existing Role ID');
    $fields['permission'] = t('Permission Key');
    $fields['module'] = t('Module');

    return $fields;
  }
}
pifagor’s picture

Status: Active » Closed (outdated)