I'm trying to setup a rule to email a new user (of the role 'Supplier') when an account is setup for them by an admin. I've set it all up with a condition that the registered user has the role 'Supplier' and to email the registered user with the custom message, but when I test it, I get the typical Drupal email with the login details, but not my custom Rules email...
I've attached an export of the rule in case that helps.
| Comment | File | Size | Author |
|---|---|---|---|
| #24 | rules-517396-24.patch | 1.04 KB | fuerst |
| #22 | user_created.rule_.txt | 1.67 KB | fuerst |
| #16 | rules-517396-16.patch | 1.03 KB | fuerst |
| #14 | rules-517396-14.patch | 1 KB | fuerst |
| #12 | rules-517396-12.patch | 1.09 KB | fuerst |
Comments
Comment #1
mitchell commentedFor testing purposes, please try your rule without any conditions. If that doesn't work, try getting rules to send an email on another event.
Comment #2
Anonymous (not verified) commentedRemoving the condition worked; the email came through. Must be something wrong with the condition then...
Comment #3
mitchell commentedI'm not sure how to fix this.. "Role has been added" event is currently in discussion here #521266: role/permission rules integration. In the meantime, you could use role_change_notify, though of course it would be much better if this feature came from rules.
Comment #4
fago@mitchell: Hm, this doesn't really relate to each other. This is about fixing existing stuff, not about adding new one.
@issue:
The cause is probably that the passed $account object doesn't include the rules yet. To fix this special case we might look into adding in the roles properly when invoking the event, probably they are already in $edit['roles'].
Comment #5
tayzlor commentedcan confirm i also have this problem, the $user->roles array has not yet been fully populated with the added roles, so when rules_condition_user_hasrole() fires to check for additional roles it does not find any extra, even although you selected some via the admin interface to add to the user. therefore this condition does not work on account creation (it will only work on the authenticated user role type).
@fago: how would we go about patching this? need to add something to the 'insert' clause on rules_user() in rules.events.inc ??
Comment #6
marc.groth commentedSubscribing as experiencing the same issue.
Comment #7
tayzlor commentedpatch supplied to add the user roles on to the $account object before the event is invoked so the has role condition works on any roles that have been added at account creation
Comment #8
fagoYou broke the existing code to add back new roles to $edit['roles'], which of course needs to run after the rules. Else it looks fine.
Comment #9
tayzlor commented@fago is this the correction you mean -
does the $account->roles += $edit['roles'] have to be wrapped in an if $op == 'insert also?
Comment #10
betamos commentedSubscribing! This is an important feature
Comment #11
fuerst commentedAt account creation you have the assigned Role IDs available in the
$editarray, not the Role names. The attached patch converts the IDs to the names and appends it to a copy of$account->roles. This only makes sense for the insert operation ofhook_user()so it is limited to this operation. Is this a way we can achieve the missing Has role condition at account creation?Comment #12
fuerst commentedTwo problems with my patch in #11:
- By using a cloned $account object it can not be modfied longer by Rules. I removed the cloning part.
- The Role ID must be used as key in the $account->roles array
The attached patch corrects this. It is for Rules 6.x-1.1 by the way.
Comment #13
fago>+ if (in_array($op, array('insert'))) {
Use a usual == comparison instead.
>+ // Only allow numbers, there is at least a value TRUE in the array
+ if (!is_numeric($role_id)) {
+ continue;
+ }
Just test with isset() whether there is an entry in $system_roles and if it is, assign it. That's much easier to read and understand.
Comment #14
fuerst commentedCorrected patch attached.
One word to the "test and assign" approach: I usually try to first exclude the non-matching condition(s) and do the assigning after that. That avoids ugly cascades of many
if's in case of many conditions. In the case of a single condition this is overhead of course. And finally - it's your code so your wish is my command :)Comment #16
fuerst commentedPatch applies now from within the Rules modules root directory and for 6.x-1.x-dev too.
Comment #17
Bilmar commentedPatch applied smoothly but there seems to be an issue with Negate option in the Condition.
IF condition User has role(s) works!
IF NOT condition User has role(s) does not work(!)
The rules action gets executed even if the user has the role set in this "if not role" condition.
Thanks for the great work and I look forward to helping with further testing.
Regards
Comment #18
rburgundy commented+1 subscribing
Comment #19
YK85 commentedafter hours of frustration - i realized that this issue is a bug and found this post =)
the patch worked! in my case I am just checking If User Has Role and not If User Does Not Have Role
Comment #20
YK85 commentedI realized I am in need of the condition "If NOT role x" so tested it with this patch and as reported above it does not yet work. Any development on this patch? Thanks
Comment #21
Bilmar commentedHello fuerst,
I was wondering if you may have time to look into the issue regarding the "Negate" option for the "Has role" condition. Please let me know if more information is required. Thank you very much in advance.
Regards
Comment #22
fuerst commentedI tried to test it by creating a Rule with condition User has role(s), Arguments configuration registered user, negated it and selected an available role. To check the Rule evaluation I added a Show a configurable message on the site action.
Now when adding a user without the role set above the message got displayed. When adding a user with the role the message got not displayed. That's what I would expect.
Since the patch is not related to the evaluation of conditions I doubt I can be helpful to your problems with negated "User has role(s)" conditions.
I attached the Rule used for testing. Modify the User has role(s) condition to meet your needs.
Comment #23
Bilmar commentedHello fuerst,
Thank you for testing this. This is weird as I used your attached rule, selected the role type "non-authenticated", and when creating a new account the message "Rule action fired!" showed on the screen. Users are automatically given role "non-authenticated" via the LoginToboggan module upon creating an account, then later this role is removed after the user validates their account via a link sent to their email.
Could this be a problem with LoginToboggan module of adding the "non-authenticated" role? Maybe, in the order of things, it is added after the rule event is triggered and conditions are checked? If you think this may be the case I will open an issue with LoginToboggan to ask for more details. But from initial testing it just seemed that this "Has Role" condition at event "User account has been created" was not working properly.
I appreciate your kind support.
Comment #24
fuerst commentedInteresting: The standard user creation routine puts the Role information as
array(rid => rid)(means: key and value are the Role ID) into the User module's$editarray. The LoginToboggan module usesarray(rid => TRUE). Since my patch used the value to get the Role ID it failed with the Role added by LoginToboggan.The attached patch corrects that by using the array key instead of the value to get the Role ID. Please test and tell me if it works for you now.
Comment #25
Bilmar commentedhello fuerst,
I tested your new patch several times and it now detects the IF NOT has role at account creation.
Thank you very much for looking into my issue.
I really appreciate your hard work!
Comment #26
fuerst commentedNice, thanks for feedback! Please now set this issue status to reviewed & tested by the community to let the maintainer know about it.
Comment #27
Bilmar commentedsetting the status to RTBC
thanks again for your support!
Comment #28
YK85 commentedtested and works great! many thanks
Comment #29
jannalexx commentedUnsupported operand types in rules.events.inc on line 45 with phpbb integration module http://drupal.org/project/phpbbforum
when existed user in phpbb synced to drupal accounts and of course rules used
works ok
Comment #30
fuerst commented#29: You are obviously using an unpatched rules.events.inc. Line 45 there contains code which can trigger that kind of error. Please open a new bug for this since the error is not related to this issue.
Note: The line in question is
$edit['roles'] += $account->roles. If$account->rolesis not set or is not an array the error mentioned above will occur. It can be avoided by using typecasting like this:$edit['roles'] += (array) $account->rolesComment #31
YK85 commentedIs #29 a problem in committing this patch? If not, would it be possible to see this in the latest dev?
Thanks!
Comment #32
klausiCommitted patch from #24, thanks.
Comment #34
jvieille commentedIf NOT still does not work
I had to reverse the condition to make it working.
The exactly same conditions / action works without any problem with the account update event.
Comment #35
mitchell commentedPlease don't reopen year-old issues. Make a new issue and reference any related ones.