2

I'm using the default register form on a custom page, which I want to use to set a specific role for the user who registers using that custom page.

Now I need to know how I add this specific role when the user registers.

Can I use like, the submit funcion for the custom form function, and then use user_multiple_role_edit()?

But how di I get the $uid?

What's best?

4

Here is my solution based on my favorite Rules module ;)

These 2 rules below automatically assign a predefined role to a user who registers to your website using a promotional link and code.

Important: Before importing these 2 rules:

  1. backup your site :P
  2. replace example.com with your domain name
  3. replace 11 with your role id number
  4. replace code=promo1 with your secret promotional code

You can import rules via this page on your website: /admin/config/workflow/rules/reaction/import

Rule 1:

{ "rules_auto_assign_role_1" : {
    "LABEL" : "Auto assign role to user based on the registration path Part 1",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules" ],
    "ON" : [ "user_presave" ],
    "IF" : [
      { "NOT user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "11" : "11" } } } },
      { "data_is" : {
          "data" : [ "site:current-page:url" ],
          "value" : "http:\/\/example.com\/user\/register?code=promo1"
        }
      }
    ],
    "DO" : [ { "redirect" : { "url" : "user?code=promo1" } } ]
  }
}

Rule 2:

{ "rules_auto_assign_role_2" : {
    "LABEL" : "Auto assign role to user based on the registration path Part 2",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules" ],
    "ON" : [ "user_view" ],
    "IF" : [
      { "AND" : [
          { "NOT user_has_role" : {
              "account" : [ "site:current-user" ],
              "roles" : { "value" : { "11" : "11" } }
            }
          },
          { "data_is" : {
              "data" : [ "site:current-page:url" ],
              "value" : "http:\/\/example.com\/user?code=promo1"
            }
          }
        ]
      }
    ],
    "DO" : [
      { "user_add_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "11" : "11" } }
        }
      }
    ]
  }
}
  • Oh, thank you so, so much! I'll give it a go as fast as I can. Thank you! – Daniel Holm Apr 6 '12 at 12:04
  • Hi again, I don't have any promotional code, I just have a certain URL for this reg. form: example.com/cv-register. And I want the role to be added to users registering using that form. – Daniel Holm Apr 6 '12 at 15:29
  • This is just an example. Instead of the promo code you should modify the url "value" in both rules the way you want so that only users who register via form url as defined in Rule 1 will be redirected to a url as defined in Rule 2. I would suggest to check kind of a token code to make it more secure and to make sure visitors cannot easily hack this solution. This token code should be generated in Rule 1 and checked in Rule2. Otherwise a visitor could just open example.com/user?code=promo1 to get the role assigned. – Refineo Apr 6 '12 at 15:44
0

So I did it like this: 1. I skipped the usage of role 1 and just used the altred register form to redirect the new user to the user page with a secret query in the url. 2. Then I used rule 2 and changed the query to the one I was using, changed the static URL to a relative one, and finished the rule of by redirecting the user to another page so he/she wouldn't see the secret query in the URL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.