You are here

function LdapAuthorizationBasicTests::testFlags in Lightweight Directory Access Protocol (LDAP) 8.2

Same name and namespace in other branches
  1. 7.2 ldap_authorization/tests/BasicTests.test \LdapAuthorizationBasicTests::testFlags()
  2. 7 ldap_authorization/tests/BasicTests/BasicTests.test \LdapAuthorizationBasicTests::testFlags()

authorization configuration flags tests clumped together

File

ldap_authorization/tests/BasicTests.test, line 201

Class

LdapAuthorizationBasicTests

Code

function testFlags() {
  $sid = 'activedirectory1';
  $this
    ->prepTestData(LDAP_TEST_LDAP_NAME, array(
    $sid,
  ), 'provisionToDrupal', 'default', 'drupal_role_default');

  /**
   * LDAP_authorz.Flags.status=0: Disable ldap_authorization_drupal_role configuration and make sure no authorizations performed
   */
  $user = $this
    ->drupalCreateUser(array());
  $hpotter = $this->testFunctions
    ->drupalLdapUpdateUser(array(
    'name' => 'hpotter',
    'mail' => 'hpotter@hogwarts.edu',
  ), TRUE, $user);
  list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($hpotter, 'query');

  // just see if the correct ones are derived.
  $roles1 = $new_authorizations['drupal_role'];

  // $consumer_conf_admin = ldap_authorization_get_consumer_admin_object('drupal_role', FALSE);
  $this->consumerAdminConf['drupal_role']->status = 0;
  $this->consumerAdminConf['drupal_role']
    ->save();
  list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($hpotter, 'query', 'drupal_role');

  // just see if the correct ones are derived.
  $roles2 = isset($new_authorizations['drupal_role']) ? $new_authorizations['drupal_role'] : array();
  $correct_roles = count($roles1) > 0 && count($roles2) == 0;

  // not worried about which roles here, just that some are granted
  $this
    ->assertTrue($correct_roles, 'disable consumer configuration disallows authorizations.', 'LDAP_authorz.Flags.status.0');
  if (!$correct_roles) {
    debug('LDAP_authorz.Flags.enable.0 roles with enabled');
    debug($roles1);
    debug('LDAP_authorz.Flags.enable.0 roles with disabled');
    debug($roles2);
  }

  /**
   * LDAP_authorz.onlyLdapAuthenticated=1: create normal user and
   * apply authorization query.  should return no roles
   */
  $this->consumerAdminConf['drupal_role']->onlyApplyToLdapAuthenticated = 1;
  $this->consumerAdminConf['drupal_role']->status = 1;
  $this->consumerAdminConf['drupal_role']
    ->save();
  $user = $this
    ->drupalCreateUser(array());
  $hgrainger = $this->testFunctions
    ->drupalLdapUpdateUser(array(
    'name' => 'hgrainger',
    'mail' => 'hgrainger@hogwarts.edu',
  ), TRUE, $user);

  // remove authmap in case it exists so test will work
  db_delete('authmap')
    ->condition('uid', $user->uid)
    ->condition('module', 'ldap_user')
    ->execute();
  list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($hgrainger, 'query');

  // just see if the correct ones are derived.
  $roles = isset($new_authorizations['drupal_role']) ? $new_authorizations['drupal_role'] : array();
  $success = count($roles) == 0;
  $this
    ->assertTrue($success, ' only apply to ldap authenticated grants no roles for non ldap user.', 'LDAP_authorz.onlyLdapAuthenticated.1');
  if (!$success) {
    debug('LDAP_authorz.onlyLdapAuthenticated.1');
    debug($roles);
    debug($this->testFunctions
      ->ldapUserIsAuthmapped('hgrainger'));
    debug($new_authorizations);
    debug($notifications);
  }

  /**
   * LDAP_authorz.Flags.synchOnLogon - execute logon and check that no roles are applied if disabled
   */
  $this->consumerAdminConf['drupal_role']->synchOnLogon = 0;
  $this->consumerAdminConf['drupal_role']
    ->save();
  $edit = array(
    'name' => 'hgrainger',
    'pass' => 'goodpwd',
  );
  $this
    ->drupalPost('user', $edit, t('Log in'));
  $this
    ->assertText(t('Member for'), 'New Ldap user with good password authenticated.', 'LDAP_authorz.Flags.synchOnLogon.0');
  $this
    ->assertTrue($this->testFunctions
    ->ldapUserIsAuthmapped('hgrainger'), 'Ldap user properly authmapped.', 'LDAP_authorz.Flags.synchOnLogon.0');
  $hgrainger = user_load_by_name('hgrainger');
  $this
    ->drupalGet('user/logout');
  $this->consumerAdminConf['drupal_role']->synchOnLogon = 1;
  $this->consumerAdminConf['drupal_role']
    ->save();
  $edit = array(
    'name' => 'hgrainger',
    'pass' => 'goodpwd',
  );
  $this
    ->drupalPost('user', $edit, t('Log in'));
  $this
    ->assertText(t('Member for'), 'New Ldap user with good password authenticated.', 'LDAP_authorz.Flags.synchOnLogon=1');
  $hgrainger = user_load_by_name('hgrainger');
  $this
    ->drupalGet('user/logout');

  // create a couple roles for next 2 tests
  $troublemaker = new stdClass();
  $troublemaker->name = 'troublemaker';
  user_role_save($troublemaker);
  $troublemaker = user_role_load_by_name('troublemaker');
  $superadmin = new stdClass();
  $superadmin->name = 'superadmin';
  user_role_save($superadmin);
  $superadmin = user_role_load_by_name('superadmin');

  /**
   * LDAP_authorz.Flags.revokeLdapProvisioned: test flag for
   *   removing manually granted roles
   *
   *   $this->revokeLdapProvisioned == 1 : Revoke !consumer_namePlural previously granted by LDAP Authorization but no longer valid.
   *
   *   grant roles via ldap and some not vai ldap manually,
   *   then alter ldap so they are no longer valid,
   *   then logon again and make sure the ldap provided roles are revoked and the drupal ones are not revoked
   *
   */
  $this->consumerAdminConf['drupal_role']->onlyApplyToLdapAuthenticated = 0;
  $this->consumerAdminConf['drupal_role']->revokeLdapProvisioned = 1;
  $this->consumerAdminConf['drupal_role']->createConsumers = 1;
  $this->consumerAdminConf['drupal_role']
    ->save();

  // set correct roles manually
  $hpotter = user_load_by_name('hpotter');
  user_delete($hpotter->uid);
  $user = $this
    ->drupalCreateUser(array());
  $hpotter = $this->testFunctions
    ->drupalLdapUpdateUser(array(
    'name' => 'hpotter',
    'mail' => 'hpotter@hogwarts.edu',
  ), TRUE, $user);
  $edit = array(
    'name' => 'hpotter',
    'pass' => 'goodpwd',
  );
  $this
    ->drupalPost('user', $edit, t('Log in'));
  $this
    ->assertText(t('Member for'), 'New Ldap user with good password authenticated.', 'LDAP_authorz.Flags.revokeLdapProvisioned=1');
  $hpotter = user_load_by_name('hpotter');

  // add an underserved, ldap granted drupal role superadmin
  // and an undeserved, non ldap granted role troublemaker
  $hpotter = user_load($hpotter->uid, TRUE);
  $roles = $hpotter->roles;
  $roles[$troublemaker->rid] = $troublemaker->name;
  $roles[$superadmin->rid] = $superadmin->name;
  $data = array(
    'roles' => $roles,
    'data' => array(
      'ldap_authorizations' => array(
        'drupal_role' => array(
          $superadmin->name => array(
            'date_granted' => 1304216778,
          ),
        ),
      ),
    ),
  );
  $hpotter = user_save($hpotter, $data);

  // apply correct authorizations.  should remove the administrator role but not the manually created 'troublemaker' role
  list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($hpotter, 'set', 'drupal_role', 'logon');
  $hpotter = user_load($hpotter->uid, TRUE);
  $this
    ->assertTrue(!isset($new_authorizations['drupal_role'][$superadmin->rid]), ' revoke superadmin ldap granted roles when no longer deserved.', 'LDAP_authorz.Flags.revokeLdapProvisioned=1');

  /**
   * LDAP_authorz.Flags.regrantLdapProvisioned
   * $this->regrantLdapProvisioned == 1 :
   *   Re grant !consumer_namePlural previously granted
   *   by LDAP Authorization but removed manually.
   *
   * - manually remove ldap granted role
   * - logon
   * - check if regranted
   */
  $this
    ->drupalGet('user/logout');
  $this->consumerAdminConf['drupal_role']->regrantLdapProvisioned = 1;
  $this->consumerAdminConf['drupal_role']
    ->save();
  $hpotter = user_load($hpotter->uid, TRUE);
  $roles = $hpotter->roles;
  unset($roles[$superadmin->rid]);
  user_save($hpotter, array(
    'roles' => $roles,
  ));
  $hpotter = user_load($hpotter->uid, TRUE);
  list($new_authorizations, $notifications) = ldap_authorizations_user_authorizations($hpotter, 'set', 'drupal_role', 'logon');
  $hpotter = user_load($hpotter->uid, TRUE);
  $success = !in_array('administrator', array_values($hpotter->roles));
  $this
    ->assertTrue($success, 'regrant Ldap Provisioned roles that were manually revoked', 'LDAP_authorz.Flags.regrantLdapProvisioned=1');
  if (!$success) {
    debug('LDAP_authorz.Flags.regrantLdapProvisioned=1');
    debug('hpotter roles');
    debug($hpotter->roles);
    debug('new_authorizations');
    debug($new_authorizations);
  }

  /**
   * LDAP_authorz.Flags.createConsumers=1
   */

  //add new mapping to and enable create consumers
  $this
    ->prepTestData(LDAP_TEST_LDAP_NAME, array(
    $sid,
  ), 'provisionToDrupal', 'default', 'drupal_role_default');
  $this
    ->drupalGet('user/logout');
  $new_role = 'oompa-loompas';
  $this->consumerAdminConf['drupal_role']->createConsumers = 1;
  $this->consumerAdminConf['drupal_role']->mappings[] = array(
    'from' => 'cn=students,ou=groups,dc=hogwarts,dc=edu',
    'user_entered' => $new_role,
    'normalized' => $new_role,
    'simplified' => $new_role,
    'valid' => TRUE,
    'error_message' => '',
  );
  $this->consumerAdminConf['drupal_role']
    ->save();

  //  debug('mappings'); debug($this->consumerAdminConf['drupal_role']->mappings);
  $edit = array(
    'name' => 'hpotter',
    'pass' => 'goodpwd',
  );
  $this
    ->drupalPost('user', $edit, t('Log in'));
  $new_role_created = in_array($new_role, array_values(user_roles()));

  // debug("roles"); debug(user_roles());
  $roles_by_name = array_flip(user_roles());
  $hpotter = user_load_by_name('hpotter');
  $hpotter = user_load($hpotter->uid, TRUE);
  $role_granted = isset($hpotter->roles[$roles_by_name[$new_role]]);
  $this
    ->assertTrue($new_role_created && $role_granted, 'create consumers (e.g. roles)', 'LDAP_authorz.Flags.createConsumers=1');
  if (!($new_role_created && $role_granted)) {
    debug('roles');
    debug(user_roles());
    debug('roles by name');
    debug($roles_by_name);
    debug('hpotter->roles');
    debug($hpotter->roles);
    debug("new role desired: {$new_role}");
    debug("{$new_role_created} AND {$role_granted}");
  }
}