You are here

function signup_roles_cancel in Signup 7

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

Public function for removing roles if a user cancels.

This function is not automatically invoked by Signup. You should call this function from your own code if you wish to remove roles.

File

modules/signup_roles/signup_roles.module, line 69
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_cancel($account) {

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

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

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

  // Confirm only current roles are being removed.
  $site_roles = user_roles(TRUE);
  $remove_roles = array_intersect_key($site_roles, $remove_roles);
  $remove_roles_string = implode(', ', $remove_roles);

  // Make sure not to remove other roles in the process of removing these.
  $roles = array_diff_key($account->roles, $remove_roles);
  $success = user_save($account, array(
    'roles' => $roles,
  ));
  if ($success) {
    watchdog('signup_roles', 'Removed roles %roles from %name.', array(
      '%roles' => $remove_roles_string,
      '%name' => $account->name,
    ));
    return TRUE;
  }
  else {
    watchdog('signup_roles', 'Error removing roles %roles from %name', array(
      '%roles' => $remove_roles_string,
      '%name' => $account->name,
    ));
    return FALSE;
  }
}