As mentioned in the question here Using user_load() and user_save() to change the status of a logged out user, I want to be able to access the $user object of a logged out user based on a username and set the user status to active with user_save().
As advised, I put my code in a custom module and implemented hook_menu() with a page callback. However, I still don't fetch $user.
UPDATE: I've managed to get what I wanted: I'm indeed fetching the $user object by a username passed as a parameter and I manage to change the user's status. BUT, i'm not doing this the proper way. Here's the code inside mymodule.module:
<?php
function mymodule_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
// set user's status to blocked upon registration
case 'insert' : {
user_save($account, array('status' => 0));
break;
}
}
}
function mymodule_init() {
// needed due to Drupal's menu caching. Otherwise, mymodule_menu() won't be called
menu_rebuild();
}
function mymodule_menu() {
$account = user_load(array( 'name' => $_REQUEST['user'] ));
user_save($account, array('status' => 1));
}
As you can see, I implement hook_menu() but there's no page callback. A working page callback would solve me 2 issues:
- I would like the user activation functionality to be available only from a specific page in Drupal and i'd like to restrict access to that page.
- Performance - currently, the menu_rebuild() is called on each and every page request on Drupal.. not pleasant at all :)
When I tried putting a page callback in the mymodule_menu() function and debugged the code, I saw that the page callback wasn't even fired. Here's what my initial code looked like:
function mymodule_menu() {
$account = user_load(array( 'name' => $_REQUEST['user'] ));
$items['auth'] = array(
'page callback' => 'mymodule_page',
'page arguments' => array($account),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function mymodule_page(&$account) {
menu_rebuild();
user_save($account, array('status' => 1));
}
Why isn't my page callback function fired with this code? How can I tell Drupal to rebuild the menus or to clear the menus cache only for a certain page?
Avner