You are here

private function ServerTestForm::testWritableGroup in Lightweight Directory Access Protocol (LDAP) 8.4

Same name and namespace in other branches
  1. 8.3 ldap_servers/src/Form/ServerTestForm.php \Drupal\ldap_servers\Form\ServerTestForm::testWritableGroup()

Test writable groups.

Parameters

string $new_group: The CN of the group to test.

string $member: The CN of the member to test.

1 call to ServerTestForm::testWritableGroup()
ServerTestForm::submitForm in ldap_servers/src/Form/ServerTestForm.php
This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state…

File

ldap_servers/src/Form/ServerTestForm.php, line 618

Class

ServerTestForm
Use Drupal\Core\Form\FormBase;.

Namespace

Drupal\ldap_servers\Form

Code

private function testWritableGroup(string $new_group, string $member) : void {
  if (!$this->ldapGroupManager
    ->setServerById($this->ldapServer
    ->id())) {
    return;
  }
  $writableGroupAttributes = [
    'objectClass' => [
      $this->ldapServer
        ->get('grp_object_cat'),
      'top',
    ],
  ];
  $openLdap = FALSE;

  // This empty is needed for OpenLDAP, otherwise it won't get created.
  if (strtolower($this->ldapServer
    ->get('grp_object_cat')) === 'groupofnames') {
    $openLdap = TRUE;
    $writableGroupAttributes['member'] = [
      '',
    ];
  }

  // Delete test group if it exists.
  if ($this->ldapGroupManager
    ->checkDnExists($new_group)) {
    $this->ldapGroupManager
      ->groupRemoveGroup($new_group, FALSE);
  }
  $this->resultsTables['group1'][] = [
    $this
      ->t('Starting test without group (group was deleted if present): @group', [
      '@group' => $new_group,
    ]),
    $this
      ->booleanResult(!$this->ldapGroupManager
      ->checkDnExists($new_group)),
  ];

  // Make sure there are no entries being a member of it.
  $this->resultsTables['group1'][] = [
    $this
      ->t('Are there no members in the writable group?', [
      '@group' => $new_group,
    ]),
    $this
      ->booleanResult(!$this->ldapGroupManager
      ->groupMembers($new_group)),
  ];

  // Add group.
  $attr = json_encode($writableGroupAttributes);
  $this->resultsTables['group1'][] = [
    $this
      ->t('Add group @group with attributes @attributes', [
      '@group' => $new_group,
      '@attributes' => $attr,
    ]),
    $this
      ->booleanResult($this->ldapGroupManager
      ->groupAddGroup($new_group, $writableGroupAttributes)),
  ];

  // Call to all members in an empty group returns empty array, not FALSE.
  $result = $this->ldapGroupManager
    ->groupMembers($new_group);
  if ($openLdap) {
    array_shift($result);
  }
  $this->resultsTables['group1'][] = [
    $this
      ->t('Call to all members in an empty group returns an empty array for group', [
      '@group' => $new_group,
    ]),
    $this
      ->booleanResult($result === []),
  ];

  // Add member to group.
  $this->ldapGroupManager
    ->groupAddMember($new_group, $member);
  $result = $this->ldapGroupManager
    ->groupMembers($new_group);
  if ($openLdap) {
    array_shift($result);
  }
  $this->resultsTables['group1'][] = [
    $this
      ->t('Add member to group @group with DN @dn', [
      '@group' => $new_group,
      '@dn' => $member,
    ]),
    $this
      ->booleanResult(is_array($result) && !empty($result)),
  ];

  // Try to remove group with member in it.
  $result = $this->ldapGroupManager
    ->groupRemoveGroup($new_group);
  $this->resultsTables['group1'][] = [
    $this
      ->t('Remove group @group with member in it (not allowed)', [
      '@group' => $new_group,
    ]),
    $this
      ->booleanResult(!$result),
  ];

  // Remove group member.
  $this->ldapGroupManager
    ->groupRemoveMember($new_group, $member);
  $result = $this->ldapGroupManager
    ->groupMembers($new_group);
  if ($openLdap) {
    array_shift($result);
  }
  $this->resultsTables['group1'][] = [
    $this
      ->t('Remove group member @dn from @group', [
      '@group' => $new_group,
      '@dn' => $member,
    ]),
    $this
      ->booleanResult($result === []),
  ];
  if ($openLdap) {
    $this->ldapGroupManager
      ->groupRemoveGroup($new_group, FALSE);
    $this->resultsTables['group1'][] = [
      $this
        ->t('Forced group removal of @group because this OpenLDAP configuration does not allow for safe removal.', [
        '@group' => $new_group,
      ]),
      $this
        ->booleanResult(!$this->ldapGroupManager
        ->checkDnExists($new_group)),
    ];
  }
  else {
    $this->ldapGroupManager
      ->groupRemoveGroup($new_group);
    $this->resultsTables['group1'][] = [
      $this
        ->t('Remove group @group if empty', [
        '@group' => $new_group,
      ]),
      $this
        ->booleanResult(!$this->ldapGroupManager
        ->checkDnExists($new_group)),
    ];
  }
}