1

I have a custom module that allows administrator users to review users, and then approve their registration. I can't figure out how to programmatically trigger the 'approve' process.

Changing their role, or doing a regular user_save() with the new status doesn't seem to trigger rules associated with activating the user.

I'm sure there's a simple function for this, but I can't find it.

  • Which "approve process" do you mean? AFAIK, approving a user is nothing more than setting the user status to 1. Can you be more specific about what else you expect to happen (maybe sending an e-mail to the admin?), or which hooks you expect to be invoked? – marcvangend Apr 5 '12 at 8:05
  • Drupal has already a page where the administrator users can approve the registration done from new users. Why do you need a custom module for this? – espero May 5 '12 at 20:15
2

You have to rely on user_load to load the $user object, edit what's appropriate, in your case $user->status, then user_save the modified object.

You could also use db_query to update the users table directly if you know what you're doing.

As for triggering the rule, this post explains how to do it.

1

To expand on Countzero's answer a bit with an example:

If we have a function in a custom module that is passed the UID, for this example we will update a user from 'blocked' to 'active' using Drupal's user_load() and user_save() functions.

function MYMODULE_update_user($uid) {

// Load user object.
// To avoid confusion and to avoid clobbering the global $user object,
// assign the result of this function to a different local variable.
$account = user_load($uid);

// Change user from 'blocked' to 'active'.
user_save($account, array('status'=> 1));
}

The user_save() documentation is as follows:

user_save($account, $edit = array(), $category = 'account')

Parameters

$account: (optional) The user object to modify or add. If you want to modify an existing user account, you will need to ensure that (a) $account is an object, and (b) you have set $account->uid to the numeric user ID of the user account you wish to modify. If you want to create a new user account, you can set $account->is_new to TRUE or omit the $account->uid field.

$edit: An array of fields and values to save. For example array('name' => 'My name'). Key / value pairs added to the $edit['data'] will be serialized and saved in the {users.data} column.

$category: (optional) The category for storing profile information in.

Return value

A fully-loaded $user object upon successful save or FALSE if the save failed.

To determine what part of the $edit array() to modify, you can do something like a hook_form_alter to dpm() out the user object.

-1

If you need to activate all users Programmatically via following methods.

$uids = db_query('SELECT uid FROM {users}')->fetchCol();
foreach (user_load_multiple($uids) as $account) {
  $account->status = 1;
  user_save($account);
}

This will invoke all other activities related to this action such as mail notification, other hook altering etc.

Entity API module:-

$users = entity_load('user');
 foreach ($users as $user) {
  if ($user->uid != 0) {
    $user->status = 1;
    entity_save('user', $user);
  }
}

Query update using db_update

db_update('users')
 ->fields(array('status' => 1))
 ->execute();

Note:- Reference link

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.