You are here

protected function UserProtectWebTestBase::createProtectionRule in User protect 7

Creates protection rule.

Parameters

int|string $id: The id of the user or role to protect.

array $protections: (optional) The active protections. Defaults to an empty array.

$type: (optional) The protected type. Defaults to "user_role".

2 calls to UserProtectWebTestBase::createProtectionRule()
UserProtectWebTestBase::createProtectedRole in tests/UserProtectWebTestBase.test
Creates a protected role.
UserProtectWebTestBase::createProtectedUser in tests/UserProtectWebTestBase.test
Creates a protected user.

File

tests/UserProtectWebTestBase.test, line 98
Contains \UserProtectWebTestBase.

Class

UserProtectWebTestBase
Base class for User protect web tests.

Code

protected function createProtectionRule($id, array $protections = array(), $type = 'user_role') {

  // Add default values.
  $protections += array(
    'up_name' => 0,
    'up_mail' => 0,
    'up_pass' => 0,
    'up_status' => 0,
    'up_roles' => 0,
    'up_openid' => 0,
    'up_cancel' => 0,
    'up_edit' => 0,
  );
  if ($type == 'user_role') {
    $role_protections = variable_get('userprotect_role_protections', array());
    $role_protections[$id] = $protections;
    variable_set('userprotect_role_protections', $role_protections);
  }
  elseif ($type == 'user') {
    $protections['uid'] = $id;
    $protections['up_type'] = 'user';

    // Check for existing protection rule.
    $saved_uid = db_select('userprotect')
      ->fields('userprotect', array(
      'uid',
    ))
      ->condition('uid', $id)
      ->execute()
      ->fetchField();
    if ($id == $saved_uid) {

      // Overwrite record.
      drupal_write_record('userprotect', $protections, array(
        'uid',
      ));
    }
    else {

      // New record.
      drupal_write_record('userprotect', $protections);
    }
  }
}