How to add user role during registration with custom form
I took over a site that uses a custom registration form that directly calls user_register_submit directly, and I need to have a user role added to this user when this form is submitted. However, I can't add the value to $form_state['values']['roles'] because the function errors out if there is a roles array because it is seen as a 'malicious attempt to alter protected user fields.' Since that function then calls user_save, it seems that once the user is saved, I need to 1) load the user I just created, 2) add the role to the object, and 3) save it again. However, in order to call user_load, I need the uid, which isn't returned from user_register_submit. The only logical thing I can think to do is query the users table with the user name I just created (entered as part of the form), get the rid, and then call user_load and do my thing. Is there a better (and easier) way to do this that I'm missing, or am I on the right track? Thanks. Steve
Are you familiar with autoassignrole? It's a pretty capable module and may be useful for you. In D6, you can use hook_user() and modify the roles at insert time, if you want to do it yourself. -Randy On Fri, Feb 26, 2010 at 2:49 PM, Steve Edwards <killshot91@gmail.com> wrote:
I took over a site that uses a custom registration form that directly calls user_register_submit directly, and I need to have a user role added to this user when this form is submitted. However, I can't add the value to $form_state['values']['roles'] because the function errors out if there is a roles array because it is seen as a 'malicious attempt to alter protected user fields.' Since that function then calls user_save, it seems that once the user is saved, I need to 1) load the user I just created, 2) add the role to the object, and 3) save it again. However, in order to call user_load, I need the uid, which isn't returned from user_register_submit. The only logical thing I can think to do is query the users table with the user name I just created (entered as part of the form), get the rid, and then call user_load and do my thing.
Is there a better (and easier) way to do this that I'm missing, or am I on the right track?
Thanks.
Steve
-- Randy Fay Drupal Development, troubleshooting, and debugging randy@randyfay.com +1 970.462.7450
Hi Have a look at autoassignrole module. It does just this, you can also patch profile_role so the two integrate and you get custom registration fields per url/role. Lee -----Original Message----- From: development-bounces@drupal.org [mailto:development-bounces@drupal.org] On Behalf Of Steve Edwards Sent: Saturday, 27 February 2010 7:49 AM To: Drupal Development Subject: [development] How to add user role during registration with custom form I took over a site that uses a custom registration form that directly calls user_register_submit directly, and I need to have a user role added to this user when this form is submitted. However, I can't add the value to $form_state['values']['roles'] because the function errors out if there is a roles array because it is seen as a 'malicious attempt to alter protected user fields.' Since that function then calls user_save, it seems that once the user is saved, I need to 1) load the user I just created, 2) add the role to the object, and 3) save it again. However, in order to call user_load, I need the uid, which isn't returned from user_register_submit. The only logical thing I can think to do is query the users table with the user name I just created (entered as part of the form), get the rid, and then call user_load and do my thing. Is there a better (and easier) way to do this that I'm missing, or am I on the right track? Thanks. Steve = Internal Virus Database is out of date. Checked by AVG - www.avg.com Version: 8.5.406 / Virus Database: 271.1.1/2686 - Release Date: 02/13/10 19:35:00
There's probably a cleaner approach to what you try to accomplish. However, Drupal 5 and 6 both allow not only for uids as parameters but also names, e.g. user_load(array('name' => $name)) If you are using Drupal 7 there's a similar user_load_by_name. Regards, Martin On 2010-02-26 22:49, Steve Edwards wrote:
I took over a site that uses a custom registration form that directly calls user_register_submit directly, and I need to have a user role added to this user when this form is submitted. However, I can't add the value to $form_state['values']['roles'] because the function errors out if there is a roles array because it is seen as a 'malicious attempt to alter protected user fields.' Since that function then calls user_save, it seems that once the user is saved, I need to 1) load the user I just created, 2) add the role to the object, and 3) save it again. However, in order to call user_load, I need the uid, which isn't returned from user_register_submit. The only logical thing I can think to do is query the users table with the user name I just created (entered as part of the form), get the rid, and then call user_load and do my thing.
Is there a better (and easier) way to do this that I'm missing, or am I on the right track?
Thanks.
Steve
You don't need to load the whole user object to do this - way too much potential overhead. $uid = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $name_you_just_got)); db_query("INSERT INTO {users_roles} (uid, rid) VALUES(%d, %d)", $uid, $rid_of_role_to_add); Nancy E. Wichmann, PMP Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr. ________________________________ From: Steve Edwards <killshot91@gmail.com> To: Drupal Development <development@drupal.org> Sent: Fri, February 26, 2010 4:49:02 PM Subject: [development] How to add user role during registration with custom form I took over a site that uses a custom registration form that directly calls user_register_submit directly, and I need to have a user role added to this user when this form is submitted. However, I can't add the value to $form_state['values']['roles'] because the function errors out if there is a roles array because it is seen as a 'malicious attempt to alter protected user fields.' Since that function then calls user_save, it seems that once the user is saved, I need to 1) load the user I just created, 2) add the role to the object, and 3) save it again. However, in order to call user_load, I need the uid, which isn't returned from user_register_submit. The only logical thing I can think to do is query the users table with the user name I just created (entered as part of the form), get the rid, and then call user_load and do my thing. Is there a better (and easier) way to do this that I'm missing, or am I on the right track? Thanks. Steve
Yup, I like the more direct route like that. Works great. Thanks. Steve On Feb 26, 2010, at 3:40 PM, nan wich wrote:
You don't need to load the whole user object to do this - way too much potential overhead.
$uid = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $name_you_just_got)); db_query("INSERT INTO {users_roles} (uid, rid) VALUES(%d, %d)", $uid, $rid_of_role_to_add);
Nancy E. Wichmann, PMP
Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr.
From: Steve Edwards <killshot91@gmail.com> To: Drupal Development <development@drupal.org> Sent: Fri, February 26, 2010 4:49:02 PM Subject: [development] How to add user role during registration with custom form
I took over a site that uses a custom registration form that directly calls user_register_submit directly, and I need to have a user role added to this user when this form is submitted. However, I can't add the value to $form_state['values']['roles'] because the function errors out if there is a roles array because it is seen as a 'malicious attempt to alter protected user fields.' Since that function then calls user_save, it seems that once the user is saved, I need to 1) load the user I just created, 2) add the role to the object, and 3) save it again. However, in order to call user_load, I need the uid, which isn't returned from user_register_submit. The only logical thing I can think to do is query the users table with the user name I just created (entered as part of the form), get the rid, and then call user_load and do my thing.
Is there a better (and easier) way to do this that I'm missing, or am I on the right track?
Thanks.
Steve
Of course, doing a direct operation on the database bypasses the entire Drupal API, so other modules don't get a chance to interact. It assumes full control of the database, and, while often useful, has that downside. Operations like this bypass the capabilities of Role Change Notify and other modules that might be listening for user changes. I generally recommend using the API unless there's a huge performance reason not to. -Randy On Fri, Feb 26, 2010 at 5:29 PM, Steve Edwards <killshot91@gmail.com> wrote:
Yup, I like the more direct route like that. Works great.
Thanks.
Steve
On Feb 26, 2010, at 3:40 PM, nan wich wrote:
You don't need to load the whole user object to do this - way too much potential overhead.
$uid = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $name_you_just_got)); db_query("INSERT INTO {users_roles} (uid, rid) VALUES(%d, %d)", $uid, $rid_of_role_to_add);
*Nancy E. Wichmann, PMP*
Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr.
------------------------------ *From:* Steve Edwards <killshot91@gmail.com> *To:* Drupal Development <development@drupal.org> *Sent:* Fri, February 26, 2010 4:49:02 PM *Subject:* [development] How to add user role during registration with custom form
I took over a site that uses a custom registration form that directly calls user_register_submit directly, and I need to have a user role added to this user when this form is submitted. However, I can't add the value to $form_state['values']['roles'] because the function errors out if there is a roles array because it is seen as a 'malicious attempt to alter protected user fields.' Since that function then calls user_save, it seems that once the user is saved, I need to 1) load the user I just created, 2) add the role to the object, and 3) save it again. However, in order to call user_load, I need the uid, which isn't returned from user_register_submit. The only logical thing I can think to do is query the users table with the user name I just created (entered as part of the form), get the rid, and then call user_load and do my thing.
Is there a better (and easier) way to do this that I'm missing, or am I on the right track?
Thanks.
Steve
-- Randy Fay Drupal Development, troubleshooting, and debugging randy@randyfay.com +1 970.462.7450
I fully understand. I was just having a similar issue on my current site, so had that code handy. I know my site and what is planned for the future, so I am safe in doing this, and performance is an issue for us (we've already hit 150 pages per second in Beta). I have never heard of Role Change Notify and am having trouble figuring out how it could be useful to more than a few people, but I see that 255 are using it. (And, no, you don't need to try to explain it.) Nancy E. Wichmann, PMP Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr.
participants (5)
-
Lee Rowlands -
Martin Afanasjew -
nan wich -
Randy Fay -
Steve Edwards