You are here

function protected_node_update_7103 in Protected Node 7

Same name and namespace in other branches
  1. 1.0.x protected_node.install \protected_node_update_7103()

Manage reorganisation of permissions.

File

./protected_node.install, line 144
Install, update and uninstall functions for the protected_node module.

Code

function protected_node_update_7103() {

  // Permission: access protected content.
  // Get the rids with "access protected content".
  $rids = db_select('role_permission')
    ->fields('role_permission', array(
    'rid',
  ))
    ->condition('permission', 'access protected content')
    ->condition('module', 'protected_node')
    ->execute()
    ->fetchCol();
  foreach ($rids as $rid) {

    // Give "access protected node password form" to roles with
    // "access protected content".
    user_role_grant_permissions($rid, array(
      'access protected node password form',
    ));

    // Remove "access protected content" from database.
    user_role_revoke_permissions($rid, array(
      'access protected content',
    ));
  }

  // Get all permissions in the form of $permission => $module array.
  $permissions = user_permission_get_modules();

  // This case treats users that were not affected in protected_node_update_7102
  // because 'bypass password protection' does no more exists in code.
  if (!isset($permissions['bypass password protection'])) {

    // Permission: view protected content.
    // Get the rids with "view protected content".
    $rids = db_select('role_permission')
      ->fields('role_permission', array(
      'rid',
    ))
      ->condition('permission', 'view protected content')
      ->condition('module', 'protected_node')
      ->execute()
      ->fetchCol();
    foreach ($rids as $rid) {

      // Give "access protected node overview page" to roles with
      // "view protected content".
      user_role_grant_permissions($rid, array(
        'access protected node overview page',
      ));

      // Give "edit protected content" to roles with "view protected content".
      user_role_grant_permissions($rid, array(
        'edit protected content',
      ));

      // Remove "bypass password protection" from database.
      user_role_revoke_permissions($rid, array(
        'bypass password protection',
      ));
    }
  }

  // Permission: bypass password protection.
  // Get the rids with "bypass password protection".
  $rids = db_select('role_permission')
    ->fields('role_permission', array(
    'rid',
  ))
    ->condition('permission', 'bypass password protection')
    ->condition('module', 'protected_node')
    ->execute()
    ->fetchCol();
  foreach ($rids as $rid) {

    // Give "access protected node overview page" to roles with
    // "bypass password protection".
    user_role_grant_permissions($rid, array(
      'access protected node overview page',
    ));

    // Give "view protected content" to roles with "bypass password protection".
    user_role_grant_permissions($rid, array(
      'view protected content',
    ));

    // Give "edit protected content" to roles with "bypass password protection".
    user_role_grant_permissions($rid, array(
      'edit protected content',
    ));

    // Remove "bypass password protection" from database.
    user_role_revoke_permissions($rid, array(
      'bypass password protection',
    ));
  }

  // Permission: edit any password.
  // Get the rids with "edit any password".
  $rids = db_select('role_permission')
    ->fields('role_permission', array(
    'rid',
  ))
    ->condition('permission', 'edit any password')
    ->condition('module', 'protected_node')
    ->execute()
    ->fetchCol();
  foreach ($rids as $rid) {

    // Give "edit any protected node password" to roles with
    // "edit any password".
    user_role_grant_permissions($rid, array(
      'edit any protected node password',
    ));

    // Remove "edit any password" from database.
    user_role_revoke_permissions($rid, array(
      'edit any password',
    ));
  }
}