I want to assign new users to a particular role based on their email address

I created the rule below with the trigger when a new user signs up. It will check the new users email address against a Text comparism using reg expressions "/@mycompany\.com*/g" as the filter but it is not working, any idea

here is the code for my rule

{ "rules_assign_to_employee_role" : {
    "LABEL" : "Assign to Employee role",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "user_insert" : [] },
    "IF" : [
      { "OR" : [
          { "text_matches" : {
              "text" : [ "site:current-user:mail" ],
              "match" : "@mycompany\\.com*",
              "operation" : "regex"
            }
          }
		  }
        ]
      }
    ],
    "DO" : [
      { "user_add_role" : { "account" : [ "account" ], "roles" : { "value" : { "5" : "5" } } } }
    ]
  }

Comments

TR’s picture

Priority: Major » Normal
Status: Active » Fixed
Issue tags: -Email filter regex

I guess you've already figured this out (and didn't post a follow-up to let us know what you did wrong), or else you gave up and accomplished this some other way.

Regardless, for documentation, here is a working version of your Rule:

{ "rules_assign_to_employee_role" : {
    "LABEL" : "Assign to Employee role",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "user_insert" : [] },
    "IF" : [
      { "text_matches" : {
          "text" : [ "account:mail" ],
          "match" : "@mycompany\\.com*",
          "operation" : "regex"
        }
      }
    ],
    "DO" : [
      { "user_add_role" : { "account" : [ "account" ], "roles" : { "value" : { "5" : "5" } } } }
    ]
  }
}

The fundamental problem with what you have above is that you used the [site:current-user:mail] token instead of [account:mail]. The account object is created when the new user is created, so [account:mail] is the place that the new user e-mail address is stored. The site object is created upon the page request, so [site:current-user] is still the anonymous user - [site:current-user] is a constant that doesn't change even if a new user is created during the request.

In general, you should always use the context variables provided by the event (account in this case) rather than the global context variables (site), unless the data is only available from the global variable.

As a postscript, when you make a support request, you need to provide good information. "but it is not working" doesn't help much - WHAT is not working? Rules are composed of Events, Conditions, and Actions. Does the Event not trigger? Does the Condition not work? Does the Action not do the right thing? You can figure out on your own which of these three parts is causing a problem by taking simple, logical steps. For example, instead of assigning a role, you can display a system message. Then you will see if your Events and Conditions are working. The system message can and should be configured to print ALL the tokens you are trying to use in your Conditions and Actions. If you did that, you would immediately see that [site:current-user:mail] was empty, and that might lead you to figure out why your Rule wasn't working. Likewise, you can test your condition with a simple action like "User has logged in" - that way you won't have to keep creating and deleting accounts like you would with "After saving a new user account". With this simple event and with the system message action, you can test to see that your condition is properly working. Then you can add the role assignment as an additional action and see that works. By taking simple, logical steps you can solve most of these problems on your own.

As a final note, your Rule export is mangled - it has invalid syntax and won't import (see that } with excess indentation? It shouldn't be there. Likewise, you're missing a } at the end). You need to be careful to post valid information because most people will give up trying to help if what you post is invalid.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

TR’s picture