Auto-assign role to user on registration

emergingdzns - 9 months ago

I have a need to make sure that users that register on the site are assigned a specific role. Is it possible to do this in the form or something? Every user gets assigned to the "members" role on this site. But the system doesn't appear to have a default role which is clearly required in the admin panel for the Users module. If it's required shouldn't there be a setting somewhere that we can define the default role assigned on registration?

Answer

ryanthompson - 9 months ago

Try adding .roles(['members']) to your register form builder.

ryanthompson - 9 months ago

Try adding .roles(['members']) to your register form builder.

emergingdzns - 9 months ago

Ok. Does that go inside the .redirect() bit? Here's my current registration form callout: {{ form('register').redirect(request_get('redirect', '/some/page/'))|raw }} I tried it like this: {{ form('register').redirect(request_get('redirect', '/healthguide/view/'~guide_hash)).roles['members']|raw }} But the form wouldn't show up. So I tried it inside the redirect() like this: {{ form('register').redirect(request_get('redirect', '/healthguide/view/'~guide_hash).roles['members'])|raw }} The form showed up but the role didn't get added.

Sorry for being a dunce on this. Appreciate the help!

ryanthompson - 9 months ago

Whoops sorry I updated my answer - was missing the first parenthesis. You can try the literal method setRoles.

emergingdzns - 9 months ago

You sir are a rockstar! Thank you!!

ryanthompson - 9 months ago

Need to install @daviesgeek emoji plugin :-(

daviesgeek - 9 months ago

@ryanthompson yes you do ;)