# Plugin development-- Event fired if email address is changed?

**URL:** https://meta.discourse.org/t/plugin-development-event-fired-if-email-address-is-changed/123899
**Category:** Development
**Created:** [July 24, 2019, 11:06pm UTC](https://meta.discourse.org/t/plugin-development-event-fired-if-email-address-is-changed/123899 "2019-07-24T23:06:51Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [July 24, 2019, 11:06pm UTC](https://meta.discourse.org/t/plugin-development-event-fired-if-email-address-is-changed/123899/1 "2019-07-24T23:06:51Z")

</div>

I’m working on a plugin that will add users to a group if their email address matches a whitelist.

I have it working for new users with:

```plaintext
  DiscourseEvent.on(:user_created) do |user|
    GroupDomain.add_to_group_if_in_whitelisted_domain(user)
  end

```

What I want to do next is have it move users to the group if they change their address to a matching domain. I would think that

```plaintext
  DiscourseEvent.on(:user_updated) do |user|
    GroupDomain.add_to_group_if_in_whitelisted_domain(user)
  end

```

would do the trick, but it doesn’t. (Then I thought that if they changed their email address and then changed anything else in their user record that it would work, and it does!)

Is there some event that I can call/watch (I’m just a caveman) that will fire if their email address is changed?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [July 25, 2019, 6:32pm UTC](https://meta.discourse.org/t/plugin-development-event-fired-if-email-address-is-changed/123899/2 "2019-07-25T18:32:03Z")

</div>

The more I think about it, the more I think that `:user_updated` not being triggered when an email address is changed seems like a bug. Back before we had a `user_email` model, changing the email address would trigger `:user_updated`.

But having `UserEmail` ` do

```
after_update :trigger_user_updated_event

```

and with

```plaintext
  def trigger_user_updated_event
    user = User.find(self.user_id)
    DiscourseEvent.trigger(:user_updated, user)
    true
  end

```

triggers more often than it should (like when logging in via email link) and also causes a

```
PG::UniqueViolation - ERROR: duplicate key value violates unique constraint "index_group_users_on_group_id_and_user_id"

```

For a few minutes, I thought I knew something. 😿

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [July 25, 2019, 6:39pm UTC](https://meta.discourse.org/t/plugin-development-event-fired-if-email-address-is-changed/123899/3 "2019-07-25T18:39:47Z")

</div>

> [@pfaffman](#):
>
> The more I think about it, the more I think that `:user_updated` not being triggered when an email address is changed seems like a bug

It is not because it is a different model, as you found out. Since @LeoMcA changes where merged, User and UserEmail are different models, and changing an instance of UserEmail won’t trigger an watcher in a User instance.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [July 25, 2019, 6:42pm UTC](https://meta.discourse.org/t/plugin-development-event-fired-if-email-address-is-changed/123899/4 "2019-07-25T18:42:04Z")

</div>

Can you tell me the best way to have my conditionally\_add\_to\_group function called when the email address changes?

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [July 25, 2019, 6:54pm UTC](https://meta.discourse.org/t/plugin-development-event-fired-if-email-address-is-changed/123899/5 "2019-07-25T18:54:57Z")

</div>

I think something like this in plugin.rb will do

```ruby
after_initialize do

  add_model_callback(UserEmail, :after_commit, on: :update) do
     # Group.add blablabla
  end

end

```

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [July 25, 2019, 7:19pm UTC](https://meta.discourse.org/t/plugin-development-event-fired-if-email-address-is-changed/123899/6 "2019-07-25T19:19:02Z")

</div>

Hooray! IT works!

```plaintext
  self.add_model_callback(UserEmail, :after_commit, on: :update) do
    puts "#{'-'*50}\nEMAIL YES ADDRESS IS UPDATED for #{self.user_id}!!!!\n#{'-'*50}\n"
    user = User.find(self.user_id)
    GroupDomain.add_to_group_if_in_whitelisted_domain(user)
  end

```

Thanks a million, @Falco!
