You are here

function user_external_invite_grant_invite in User External Invite 1.0.x

Same name and namespace in other branches
  1. 7.2 user_external_invite.module \user_external_invite_grant_invite()
  2. 7 user_external_invite.module \user_external_invite_grant_invite()

Grants an invite given a token and mail.

Checks invite key+mail token is valid, Grants role, sends emails, and removes invite from db.

2 calls to user_external_invite_grant_invite()
user_external_invite_accept_invite in ./user_external_invite.module
Page callback for accepting an invite.
user_external_invite_user_login in ./user_external_invite.module
Implements hook_user_login().

File

./user_external_invite.module, line 626
Invites a user to site when connecting via external protocol e.g. LDAP.

Code

function user_external_invite_grant_invite($key, $mail, $account) {
  $grant_rid = _user_external_invite_dehash($key, $mail);

  // Allow for other actions and checks before a role is granted to a user.
  // If any hook returns a message, then the role will not be granted.
  $error_messages = module_invoke_all('user_external_invite_pre_grant_invite', $account, $grant_rid);
  if ($grant_rid && !$error_messages) {

    // Set message to user that role was granted.
    drupal_set_message(t('Invite accepted!'));

    // Check to see if the user already has the role.  Because the email
    // used in the invite is not always = to LDAP, they could already be
    // in the role.
    global $user;
    $role = user_role_load($grant_rid);
    if (in_array($role->name, $user->roles)) {
      return;
    }
    db_insert('users_roles')
      ->fields(array(
      'uid' => $account->uid,
      'rid' => $grant_rid,
    ))
      ->execute();

    // Send acceptance email.
    _user_external_invite_send_invite_accepted_mail($grant_rid, $account->mail);

    // Load the invite to send email to inviter.
    $invite = _user_external_invite_load_invite($mail);

    // Send email to inviter.
    _user_external_invite_send_inviter_confirmation($invite['uid'], $invite['mail'], $invite['rid']);

    // Once granted, change status of invite in database.
    _user_external_invite_change_invite_status($mail, 'Granted');
  }
  elseif ($error_messages) {
    foreach ($error_messages as $message) {
      drupal_set_message($message, 'error');
    }
  }
  else {
    drupal_set_message(t('Invite invalid or has expired! If you feel you have received this in error, please contact a site owner.'), 'error');
  }
}