You are here

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

Same name and namespace in other branches
  1. 8.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 37

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) {

    // Role exists.
    return FALSE;
  }
  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', [
      '%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', [
      '%role' => $new_role->name,
    ]);
    return FALSE;
  }
  else {

    // Flush existingRolesByRoleName cache after creating new role.
    $roles_by_consumer_id = $this
      ->existingRolesByRoleName(TRUE);
    watchdog('user', 'created drupal role %role in ldap_authorizations module', [
      '%role' => $new_role->name,
    ]);
  }
  return TRUE;
}