You are here

function signup_roles_signup_sign_up in Signup 6

Same name and namespace in other branches
  1. 7 modules/signup_roles/signup_roles.module \signup_roles_signup_sign_up()

Implementation of hook_signup_sign_up().

File

modules/signup_roles/signup_roles.module, line 12
The Signup roles module allows administrators to configure a site so that it grants one or more roles to users when they sign up to nodes.

Code

function signup_roles_signup_sign_up($node, $account) {

  // We can't grant roles to anonymous users.
  if (empty($account->uid)) {
    return;
  }
  $grant_roles = variable_get('signup_roles_grant', array());

  // Filter down to only those roles that are selected.
  $grant_roles = array_filter($grant_roles);

  // If no roles are selected, there's nothing to do here.
  if (empty($grant_roles)) {
    return;
  }

  // Confirm only current roles are being granted.
  $site_roles = user_roles(TRUE);
  $grant_roles = array_intersect_key($site_roles, $grant_roles);
  $grant_roles_string = implode(', ', $grant_roles);

  // Make sure not to remove other roles in the process of granting new ones.
  $roles = $grant_roles;
  $roles += $account->roles;
  $success = user_save($account, array(
    'roles' => $roles,
  ));
  if ($success) {
    watchdog('signup_roles', 'Granted roles %roles to %name.', array(
      '%roles' => $grant_roles_string,
      '%name' => $account->name,
    ));
  }
  else {
    watchdog('signup_roles', 'Error granting roles %roles to %name', array(
      '%roles' => $grant_roles_string,
      '%name' => $account->name,
    ));
  }
}