13 replies [Last post]
texas-bronius@drupal.org's picture
Offline
Joined: 01/20/2010
Juice: 170
Was this information Helpful?

Has anyone come across the need to automatically delete all Ubercart-created users?

Here's the scenario:

When an anonymous user proceeds through anonymous checkout, Ubercart creates a Drupal user based on the email address provided by the anonymous user. The next time they visit the store, they must use the login credentials generated by Ubercart, or they will be stuck in a space-time vortex of doom and never be able to make their order (if the "User creation" process is sufficiently obfuscated like a current client is asking me to do).

I think a suitable solution is to disable email notification of account, allow the user to be created, then cron delete the Ubercart-created users. I would like to hear some feedback on the best way to accomplish this without deleting real, system users and without deleting a user while the user is on the website still (like, maybe "delete users older than x-minutes...").

Thanks!

Andy's picture
Offline
Administrator
Joined: 08/07/2007
Juice: 1076
Re: cron to automatically delete all users automatically-created

For the record: This whole concept is a bad idea. I assume you know this, but the client is asking/forcing you to do this.
Moving on...
I think the way to designate Ubercart created users is via a role assignment: http://www.ubercart.org/docs/user/10967/selling_site_access_role_assignm... Just make an "Ubercart" role and give it to everyone who checks out. Just be careful not to check out with a regular account, or better yet. Add a role "Don't auto-delete me" which you give to everyone else and check for "Ubercart" and not "Don't auto-delete me".
Ubercart really expects for each order to be tied to a user account. I would test what happens to the order, reports, etc.. when you delete the referenced user before setting up your cron hack.
I think a better option then deleting the account is to just remove the email address from the account. This should result in Ubercart functioning normally, and users not being stuck in the dreaded space-time vortex you mentioned.
Cheers,
Andy

texas-bronius@drupal.org's picture
Offline
Joined: 01/20/2010
Juice: 170
to identify Ubercart users by role or some other flag..

I thought annotating a user on account creation with some flag (like a role) would be pretty clean, but then another idea hit me, and thus far, is panning out: I can identify users automatically created by Ubercart by comparing account creation vs account login using something like:

select uid, name, login - created as timediff from users
where login - created < 10
and uid not in (0,1);

It seems to be clean enough for staging, but I might run into issues when migrating to live bc timediff seems to evaluate to 0 on users migrated in Shocked .

Anyhow-- Looking for a quick fix. I am now looking to determine where to put this snippet of code. I know it will have to be a Drupal cron job, but where does one put php snippets to start hacking away and testing before slipping it into a cron hook? A block? A menu hook?

texas-bronius@drupal.org's picture
Offline
Joined: 01/20/2010
Juice: 170
Re: to identify Ubercart users by role or some other flag..

Join me on my journey.. Please pipe up if I am making any grave errors, but here is my progress thus far. I'm developing within a node with php filter on:

<?php
$uid
= null;
$uname = '';
$result = db_query('select uid, name, mail from {users} where uid not in (0,1) and login - created < 10');
while (
$row = db_fetch_object($result)) {
 
$uid = $row->uid;
 
$uname = $row->name;
 
$umail = $row->mail;
 
/*
  sess_destroy_uid($uid);
  db_query('delete from {users} where uid = %d', $uid);
  db_query('delete from {users_roles} where uid = %d', $uid);
  db_query('delete from {authmap} where uid = %d', $uid);
  */
 
$variables = array('%uid' => $uid, '%name' => $uname, '%email' => $umail);
 
watchdog('user', 'UC Auto-clean deleted user: %uid: %name (%email) from Ubercart.', $variables, WATCHDOG_NOTICE);
}
?>

Missing is a check against created vs now and how to hook into cron.

Andy's picture
Offline
Administrator
Joined: 08/07/2007
Juice: 1076
Re: Re: to identify Ubercart users by role or some other flag..

I recommend creating a small module for your site. So if your site is example.com, make a module called example.module. Put your code in there:
documentation for hook_cron: http://api.drupal.org/api/function/hook_cron
documentation for creating a module: http://drupal.org/node/231276
--Andy

texas-bronius@drupal.org's picture
Offline
Joined: 01/20/2010
Juice: 170
Yes, definitely better in a module than a page

I doubled back here just to report that my user delete solution is pretty cool, pretty effective, and pretty terrible: returning users use the same email address the second go-round, so their auto-created username is the same again, and I am having some sort of conflict either in that user account auto re-creation, auto re-authentication, or just associating their orders... *sigh*

btw, yes, this is in a custom module now: definitely a good tip there. Second in quality only to your previous "this whole concept is really bad idea" caveat! Eye-wink

Andy's picture
Offline
Administrator
Joined: 08/07/2007
Juice: 1076
Re: Yes, definitely better in a module than a page

If the conflict has to do with re-authentication, you might "fix" it by setting the session length shorter in your settings.php file ( www.example.com/sites/default/settings.php). Without more information about the conflict, I don't know what else to say. I'm sorta glad to know it sorta works, sorta Smiling
--Andy

texas-bronius@drupal.org's picture
Offline
Joined: 01/20/2010
Juice: 170
getting ubercart anonymous checkout to ALWAYS create a user

Andy- You have a high tolerance for pain. I appreciate your sticking with this thread even as I have neglected to update it with my "fix" which is to always and always create a new user.. Ubercart checks for existing based on email address, so I break that check:
uc_cart/uc_cart.module ~line 1190:

<?php
..
function
uc_cart_complete_sale($order, $login = FALSE) {
// Client doesn't want account creation for anonymous users.
// POORLY PATCHED.
// The premise is that a new user will, unconditionally, always be created by Ubercart. So let it be written so let it be done.
// Based loosely on: <a href="http://www.ubercart.org/contrib/3992#comment-40610
//" title="http://www.ubercart.org/contrib/3992#comment-40610
//">http://www.ubercart.org/contrib/3992#comment-40610
//</a> bcswebstudio 2/17/2010
//
 
global $user;

 

// Logic to create new user if necessary:
 
if ($order->uid == 0) {
   
// Check for an existing user account with the e-mail address from checkout.
    // HACK: Always fail to find the user.  1 will never equal 2
   
$result = db_query("SELECT uid FROM {users} WHERE mail = '%s' and 1=2", $order->primary_email);
  ..
?>

The goal here is that since I was having such issues with trying to get ubercart to _never_ create users, instead, I have it _always_ create users in anticipation of just deleting them periodically. Preferable, of course, would be completely 100% anonymous checkout baked-in.

Side note: I can't seem to Preview this post before hitting Save. Let's see what happens...

texas-bronius@drupal.org's picture
Offline
Joined: 01/20/2010
Juice: 170
now I'm just fanning the flames

For anyone crazy enough to try this out, consider this unforeseen that has arisen: users who are created but don't log in before this process fires also get deleted! users.login==users.created, even though the user has never logged in! I'll write back with a work-around-work-around soon.

texas-bronius@drupal.org's picture
Offline
Joined: 01/20/2010
Juice: 170
mark a user as created by ubercart role delete users by role

Andy- Thanks for the tip at the top of this thread long ago. It was fun fumbling down the wrong path, but the right way to do this wrong thing is:
- conditional action to assign a role "ubercart created user" at checkout complete (but not for site editor roles and uid=1!!)
- cron to delete uid!=1 and role == 'ubercart created user'

Seems to work more reliably Smiling

Note: I am not using Ubercart Roles module, as that seemed to be inapplicable to me. I thought it might expose some good CA actions, but no, it only exposed an "update roles" for purchased roles. I didn't see how to get it to assign a role on *any* checkout.

pkintu's picture
Offline
Joined: 06/21/2010
Juice: 29
Find any solution ?

Anyone found any solution of this ? , I saw that orders is also linked to user id in database , so if i stop to create new user from uc_cart.module then is it any negative affect on another code ?

How to assign roles in uc_cart.module ?

If i assigned roles then what should be the cron file which delete all reference of created user id without any cross affect and how to run cron file

Or its good if there is better solution of this ?

-
Kintu Designs
http://www.kintudesigns.com

texas-bronius@drupal.org's picture
Offline
Joined: 01/20/2010
Juice: 170
using Conditional Actions to assign a user role at checkout

pkintu- looks like you're hot on my trail!

On checkout, to mark an automatically-created user as having been created by Ubercart, create a CA checkout rule with
* add a Condition that the user is not a site administrator (or other important role to avoid tagging as auto-created)-- see screenshot attached. Note the OR operator.
* add an Action that is custom php code:

<?php
>
if(
$account) {
 
$uid = $account->uid;
 
$role_name = 'ubercart created user';
 
$rid = db_result(db_query("SELECT rid FROM {role} WHERE name = '%s'", $role_name));

 

db_query("INSERT INTO {users_roles} (uid, rid) VALUES(%d, %d)", $uid, $rid);

 

watchdog('user', 'uc ca added role to Ubercart created user');

}

?>
AttachmentSize
conditional action - not already a defined role 69.34 KB
pkintu's picture
Offline
Joined: 06/21/2010
Juice: 29
Re: cron to automatically delete all users automatically-created

Thanks , it works properly to assign role as ubercart user , now I need to run cron to delete such users , i never before run drupal cron , how make it done ?

-
Kintu Designs
http://www.kintudesigns.com

mattwad's picture
Offline
Joined: 01/21/2011
Juice: 14
Re: cron to automatically delete all users automatically-created

Thanks for posting your story - saved me a lot of time!