You are here

public function LdapAuthorizationConsumerDrupalRole::createConsumer in Lightweight Directory Access Protocol (LDAP) 8.2

Same name and namespace in other branches
  1. 7.2 ldap_authorization/ldap_authorization_drupal_role/LdapAuthorizationConsumerRole.class.php \LdapAuthorizationConsumerDrupalRole::createConsumer()

Overrides LdapAuthorizationConsumerAbstract::createConsumer

See also

LdapAuthorizationConsumerAbstract::createConsumer

1 call to LdapAuthorizationConsumerDrupalRole::createConsumer()
LdapAuthorizationConsumerDrupalRole::populateConsumersFromConsumerIds in ldap_authorization/ldap_authorization_drupal_role/LdapAuthorizationConsumerRole.class.php

File

ldap_authorization/ldap_authorization_drupal_role/LdapAuthorizationConsumerRole.class.php, line 36

Class

LdapAuthorizationConsumerDrupalRole

Code

public function createConsumer($consumer_id, $consumer) {
  $roles_by_consumer_id = $this
    ->existingRolesByRoleName();
  $existing_role = isset($roles_by_consumer_id[$consumer_id]) ? $roles_by_consumer_id[$consumer_id] : FALSE;
  if ($existing_role) {
    return FALSE;

    // role exists
  }
  elseif (drupal_strlen($consumer_id) > 63) {
    watchdog('ldap_authorization_drupal_role', 'Tried to create drupal role
        with name of over 63 characters (%group_name).  Please correct your
        drupal ldap_authorization settings', array(
      '%group_name' => $consumer_id,
    ));
    return FALSE;
  }
  $new_role = new stdClass();
  $new_role->name = empty($consumer['value']) ? $consumer_id : $consumer['value'];
  if (!($status = user_role_save($new_role))) {

    // if role is not created, remove from array to user object doesn't have it stored as granted
    watchdog('user', 'failed to create drupal role %role in ldap_authorizations module', array(
      '%role' => $new_role->name,
    ));
    return FALSE;
  }
  else {
    $roles_by_consumer_id = $this
      ->existingRolesByRoleName(TRUE);

    // flush existingRolesByRoleName cache after creating new role
    watchdog('user', 'created drupal role %role in ldap_authorizations module', array(
      '%role' => $new_role->name,
    ));
  }
  return TRUE;
}