2

Users on my site generally have several roles. However, every once in awhile I get a spam user, and so I close that account by flagging it with a spam flag, which triggers a rule to block the account.

Since I compile statistics on my site based on the number of users having a given role, I don't want spam users to be included. How can I automatically remove all roles from spam users? (I suppose I could add rules components to check for each role and remove it if found, but there has to be a more efficient way...)

2

This function can help you

/**
 * Remove a role from a user.
 *
 * @param $user
 *   User object or user ID.
 * @param $role_name
 *   String value of role to be removed.
 */
function custom_remove_role_from_user($user, $role_name) {
  // For convenience, we'll allow user ids as well as full user objects.
  if (is_numeric($user)) {
    $user = user_load($user);
  }
  // Only remove the role if the user already has it.
  $key = array_search($role_name, $user->roles);
  if ($key == TRUE) {
    // Get the rid from the roles table.
    $roles = user_roles(TRUE);
    $rid = array_search($role_name, $roles);
    if ($rid != FALSE) {
      // Make a copy of the roles array, without the deleted one.
      $new_roles = array();
      foreach($user->roles as $id => $name) {
        if ($id != $rid) {
          $new_roles[$id] = $name;
        }
      }
      user_save($user, array('roles' => $new_roles));
    }
  }
}

first load user's roles, then call this function something like

$user=user_load($uid);
foreach($user->roles as $role){
custom_remove_role_from_user($user, $role)
}
1

Here are some quick code examples that I have used to add or remove user roles in Drupal 6. This can be very helpful if you are building a module for a specific site that has to manipulate user roles. Using this technique is not the most flexible solution as it limits itself to the roles configured on a specific site (based on role id and role name). However, you could easily add a database table and an administration section and make this more flexible if needed.

Here is an easy way to add a role:

//you will need to enter the uid of the user here, I am just using user 1

$user = user_load(1);

//first we check if the user has the role in question (using the rid of the role)

if (!isset($user->roles[7])) {
    //if not we add the role and set the role name based on what is entered
    //in the drupal role administration section
    $user->roles[7] = 'role name seven';
}

Here is an easy way to remove a role:

//you will need to enter the uid of the user here, I am just using user 1

$user = user_load(1);

//first we check if the user has the first role in question

if (isset($user->roles[8])) {

  //if so, we unset the role
  unset($user->roles[8]);

  $edit = array('roles' => $user->roles);
  user_save($user, $edit);
}

Here is an easy way to do a role swap:

//you will need to enter the uid of the user here, I am just using user 1

$user = user_load(1);



//first we check if the user has the first role in question
if (isset($user->roles[8])) {

  //if so, we unset the role
  unset($user->roles[8]);

  //now we check to see if they have the role we want to swap for
  if (!isset($user->roles[7])) {

    //if not we add this role
    $user->roles[7] = 'role name seven';
  }

  $edit = array('roles' => $user->roles);
  user_save($user, $edit);
}

Helpful? Let me know.

0

Try this I hope this may help you

function user_role_delete($role) {

if (is_int($role)) {

        $role = user_role_load($role);

  }

else {

    $role = user_role_load_by_name($role);
  }


 db_delete('role')->condition('rid', $role->rid)->execute();

 db_delete('role_permission')->condition('rid', $role->rid)->execute();

 // Update the users who have this role set:

 db_delete('users_roles')->condition('rid', $role->rid)->execute();

  module_invoke_all('user_role_delete', $role);

  // Clear the user access cache.

 drupal_static_reset('user_access');

 drupal_static_reset('user_role_permissions');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.