Due to problems I was having with the Role Expire module, I decided that the Rules module might be a suitable alternative to the functionality that I required. A site I was working on required a 7 Day Trial user - essentially a user role that would expire after 7 days, resulting in the user being a standard authenticated user.

Preparations

This tutorial follows on from earlier Rules Scheduler tutorials and so take a look at Tutorial 2 and Tutorial 3 if you haven't already done so.

In User management, go to the Roles administration and create a new user role. Call it trial user, for instance.

Creating a rule set as a scheduling action

  • Go to "/admin/config/workflow/rules/components" and select "Rule set" from the "Component plugin " dropdown. Click "Continue"
  • Fill out the label with "Expire user role {rule_set}".
  • In the Arguments fieldset, on the first row select "User" as data type, enter "User {argument}" as the label and "user_argument" as the machine name.
  • Press the "Save" button to continue.
  • You are redirected now to the rule sets overview page and your freshly created rule set is listed.
  • Click on the "Expire user role {rule set}" label and add a new rule by clicking the "Add new rule" tab.
  • Fill out the label of the new rule with "Expire action {rule}" and click on "Save changes".
  • You are now on the edit page of this rule. We want to add an action by clicking on "Add an action".
  • Click on the "Select an action to add" drop down, choose "Remove user role" and then click Next.
  • Select the role you created in the initial preparations - trial user, and click Save.

Creating a triggered rule

  • Our simple rule set is now completed and is ready for scheduling.
  • To trigger the rule set we need to invoke it on a certain event, so we go to the triggered rules page ("admin/rules/trigger") and click on "Add a new rule".
  • Fill out the label with "Expire rule {triggered rule}" and select "User account has been created" from the event drop down box.
  • Hit "Save changes" to create the rule. On the resulting page click "Add an action", where we will schedule our rule set.
  • Select "Schedule Expire user role {rule set}" from the select box and confirm the action by clicking Next.
  • Now we adjust the scheduling information: Append "{action}" to the label at the top.
  • Under "Arguments configuration", select "registered user" from the drop down. The difference between "registered user" and "acting user" is registered user is the actual new user account that is created upon signup whereas acting user is the user account that is used to perform the task. In the case of a new signup, this user is the Anonymous user.
  • You must enter a value in the Identifier text field. This will show up in the scheduled rule sets task list and so you can enter a value that will refer to the user. Enter "User [account:user]" which will result in the new user's login name being listed in the scheduled rules task list. Note: adding a token in the identifier (i.e., the '[account:user]' part) is crucial to making the scheduling work correctly. Without the token, all task identifiers will be identical, and any existing tasks with this identifier will be replaced.
  • Enter a date or value in the "Scheduled evaluation date" text box below. You could put a value such as "+7 days" to schedule the expire role task to run in 7 days time.
  • Click save and your triggered rule is now set up.

Testing

At this point we are ready to do a little test.

  • Either logout of your Drupal installation or fire up another browser and go to the signup page (user/register).
  • Create your new user and return back to your administration account to confirm.
  • go to the rule set scheduling page ("admin/rules/rule_sets/scheduling"). You can see now a task entry in the task list that contains a scheduling date in 7 days and the identifier is set to the username of the new user.

Comments

queryblitz’s picture

Thanks for writing this, I'm excited to implement. For my use-case, I want to expire a "New User" role after a short period of time so I can use that role to display specific blocks (like "Welcome, new user!", etc.). But this also opens up the possibility to set up reminder blocks, and who knows what else! This is the first Rules Scheduler tutorial I actually understand.

welly’s picture

No problems! I'm glad I could offer some help on this site at last!

Anonymous’s picture

I would also like to know how to make a rule to remove a role after user first accessed (not created) the site. So in my case the time period is not starting when the user was created (because users were created using feeds long time ago) but when user first logs in...

ireto’s picture

With the example above I managed to set up a system which boots off a user 30 days after they have been created and warns them via email a week before hand.
However I did run into problems when setting up multiple users using this system.
The problem was only one user at a time could be scheduled in this manner. That is if more than one user had the expire role, then only the latest addition would work.
However I found a solution to this.
The problem is with setting a value for the identifier field.
You must not put a value in. It must be blank.

Then you can use the system with multiple users.

I mentioned that the I also manged to warn users a week before they were being expired. This I achieved by making an extra rules component and adding it to the the list of actions to perform in the rule.

brooke_heaton’s picture

ireto, you mentioned:

I mentioned that the I also manged to warn users a week before they were being expired. This I achieved by making an extra rules component and adding it to the the list of actions to perform in the rule.

Would you be willing to share this action that you created? It would be something that might be helpful to contribute to this module!