You are here

function simple_ldap_role_cron in Simple LDAP 7

Same name and namespace in other branches
  1. 7.2 simple_ldap_role/simple_ldap_role.module \simple_ldap_role_cron()

Implements hook_cron().

File

simple_ldap_role/simple_ldap_role.module, line 123
Main simple_ldap_role module file.

Code

function simple_ldap_role_cron() {

  // Get the module configuration.
  $basedn = simple_ldap_role_variable_get('simple_ldap_role_basedn');
  $scope = simple_ldap_role_variable_get('simple_ldap_role_scope');
  $attribute_name = simple_ldap_role_variable_get('simple_ldap_role_attribute_name');

  // Get an LDAP server object.
  $server = SimpleLdapServer::singleton();

  // Get a list of LDAP groups.
  $ldap_groups = $server
    ->search($basedn, SimpleLdapRole::filter(), $scope, array(
    $attribute_name,
  ));

  // Get a list of Drupal roles.
  $drupal_roles = user_roles(TRUE);

  // Make sure each of the LDAP groups is also a Drupal role.
  for ($i = 0; $i < $ldap_groups['count']; $i++) {
    $name = $ldap_groups[$i][$attribute_name][0];
    if (!in_array($name, $drupal_roles)) {
      $role = new stdClass();
      $role->name = $name;
      user_role_save($role);
    }
  }
}