This project is read-only.

Send email when a user is approved - how to catch this event?

Topics: Writing modules
Jun 14, 2011 at 10:38 PM

HI there,

I need to send an email to a User once he has been approved/denied.
How do I subscribe to this event? How can I be notified when this happens?

Do I have to do it via an IContentHandler -> UserPart? If so, which method should I use?  


Jun 14, 2011 at 11:19 PM

IUserEventHandler has a ConfirmedEmail event for when they respond to the challenge email. But if you're talking about manual approval, it happens in the AdminController of Orchard.Users but there's no event. So I guess you'd have to override the controller and add your own hooks.

BTW; I was having a go with your registration module. For some reason the challenge emails aren't getting sent out - have you experienced that? ... Although it could easily be something else causing it, I'll dig a big deeper tomorrow.

Also I noticed you copied the entire AccountController from Orchard.Users; did you know you can inherit from it instead and replace just the actions you need to? At least, according to Piotr, I haven't tried it myself yet ;)

Jun 15, 2011 at 1:17 AM

Hey Pete!

Thanks for the info. I had already noticed that there was no hook in the Users AccountController. Therefore I assumed this could be achieved on a more generic level -> parts. E.g. when a part is modified.
I will take a look at the Orchard source code and investigate the IUserEventHandler later this evening, maybe I can submit a patch to be able to catch this event.

I just tested the challenge emails on a live site and they do work. Both from the admin controller and also when a new user registers.
I did not copy the entire Account controller, basically just the Register action (enhanced to use custom parts) and two methods marked as private: ErrorCodeToString and ValidateRegistration. Ideally these should be outside the controller. If I find some time I'll refactor and submit as a patch.

I don't remember exactly what is was but I did run into some problem when deriving from the AccountController. Then I realized I could just override the one route: 'Account/Register'. I like the approach without inheritance better and just stayed on that path,
But indeed, there is some duplicated code which could be eliminated by refactoring the Orchard Users module. 

Jun 15, 2011 at 9:30 AM

I think the content management generally needs some extra events hooks. An "OnModified" event would be really handy. At the moment you can catch OnCreated and OnPublished; and from a driver you can handle Update, but there's no way to make it run before or after everything else updating. But when the User object is approved, it doesn't go thru the drivers, so you can't catch those events.

I'll have another look at the emails; smtp4dev just wasn't catching anything, when usually it works fine. But there are other modules I've got running so it could be a lot of things!

Jun 15, 2011 at 10:58 PM

Good feedback. there are indeed a few things to improve in the lifecycle of some objects, such as User. Please feel free to file this as a suggestion in the issue tracker. Thanks.

Jun 15, 2011 at 11:48 PM

Actually I took it one step further. ;-) I contributed a patch already. PullRequest is named: Added Approved method to IUserEventHandler

Jun 16, 2011 at 12:00 AM

Cool. Thanks for that. Way to go!