Pete Posted January 1, 2013 Share Posted January 1, 2013 I've been dealing with how to programatically delete a user account. Oddly, ProcessWire throws an error when you try and put them in the trash - whether you do this through the admin interface or via the API you can't trash users it seems. Deleting them via the API works fine doing something like this in my module: wire('pages')->delete($u); So is there any reason why you can't easily delete a user? I'm guessing it's because there will be a chance that anyone with a user account in ProcessWire would normally have written at least one page, so in this case I guess deleting a user will break ProcessWire somehow...? In my case it's more forum software integration so I want to have it delete user accounts that are spammers or banned (there's a simple way to check this in the forum software) and delete their corresponding user account in ProcessWire as long as they're not linked to any content - uploaded a file or written an article in this case. I guess I should check to see if they've ever created or modified a page as this could break the site too. I think I may have answered my own question, but if someone could confirm it that would be great Link to comment Share on other sites More sharing options...
Pete Posted January 1, 2013 Author Share Posted January 1, 2013 And here's some additional code in case anyone might need to do something similar: It basically runs in a module if the user is logged into the forums already and they're now viewing the site and are marked as either banned or a spammer in the forum software, plus they have a user account in PW (that's all the boring code I didn't include below). $count = 0; $count += wire('pages')->find("authors=" . $u . ", include=all")->count(); $count += wire('pages')->find("dm_owner=" . $u . ", include=all")->count(); $count += wire('pages')->find("created_users_id|modified_users_id=$u")->count(); if ($count == 0) { $this->session->logout(); wire('pages')->delete($u); // Delete as we cannot trash a user } It simply counts 2 fields with Page fieldtype to see if thecurrent user is linked to any content via those fields, as well as checking to see if they've ever created or modified a page. If the count is zero, the user can be deleted, otherwise we must leave it in place to prevent things from breaking. It would be interesting to see if there's a way to find all fields that could link to a user - basically any Page fields I think as users are just pages like everything else - and iterate through them instead of the manual way I've done it for the fields above. That would certainly future-proof the code a bit and I'm sure it's possible, but I don't have the mental capacity to tackle that right now 1 Link to comment Share on other sites More sharing options...
ryan Posted January 4, 2013 Share Posted January 4, 2013 Pete, you can replace your find()->count() lines with just count(), which would be a little more efficient, i.e. $count += wire('pages')->count("authors=" . $u . ", include=all"); 1 Link to comment Share on other sites More sharing options...
Pete Posted January 4, 2013 Author Share Posted January 4, 2013 Thanks - always learning something new with ProcessWire! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now