Form _submit and _validate callbacks should not call user_access() checks. Doing so interferes with form processing and can break hook_form_alter().

Looks like the form itself needs to be rewritten to pass appropriate values to trigger certain conditions.

Comments

agentrickard’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: -D7DX +Novice, +Needs backport to D7

Moving.

starsinmypockets’s picture

Hmm.. this function doesn't seem to be in user.module.

agentrickard’s picture

Title: Remove access check from user_register_form_submit() » Remove access check from user_register_submit()

Looks like it was renamed to user_register_submit().

starsinmypockets’s picture

Can the user_access functions simply be removed or do they need to be replaced by some other form of access validation?

agentrickard’s picture

We should set a #value in the calling form and respond to that.

  $form['access_whatever'] = array(
     '#type' => 'value',
     '#value' => user_access('access whatever'),
  );

Doing so allows form_alter without needing to override the submit handler.

There are related issues in the queue for other submit handlers that need this pattern.

oriol_e9g’s picture

Status: Active » Needs review
StatusFileSize
new1.03 KB

Maybe with something like this?

oriol_e9g’s picture

Assigned: Unassigned » oriol_e9g
oriol_e9g’s picture

StatusFileSize
new1.05 KB

spaces

agentrickard’s picture

Yes, please. The comments need some proofreading for English grammar, though.

Try.

+  // Pass access information to the submit handler. Running an access check
+  // inside the submit function interferes with form processing and breaks hook_form_alter().
agentrickard’s picture

Status: Needs review » Needs work

Overlooked the actual error in the patch. The submit handler should be:

  $admin = $form_state['values']['administer_users']; 

It is not proper to check $form at this stage. And since 'permissions' get stripped, we might want to change the variable name. Perhaps just 'permission'?

oriol_e9g’s picture

Status: Needs work » Needs review
StatusFileSize
new1.06 KB

@agentrickard sorry if you are wasting your time :S Tests are needed/possible?

agentrickard’s picture

Not a waste at all. You saved me from writing the patch.

Does this functionality already have a test? If so, this doesn't break it. If the functionality has no test, then, yes, we probably need one.

dixon_’s picture

subscribing

oriol_e9g’s picture

Assigned: oriol_e9g » Unassigned
moshe weitzman’s picture

Status: Needs review » Needs work

This does not look like right fix to me. The form should set #redirect to where it wants to go and then no special code is needed in submit handler. An alternative is to set destination=foo in the querystring when you get to this form in the case when you want a land on a non-standard page after submission.

agentrickard’s picture

@moshe

That comment seems entirely off-topic here.

tsphethean’s picture

Status: Needs work » Needs review

Agree with @agentrickard - the patch here seems to fix the issue. I've applied the same approach to a patch in http://drupal.org/node/687588#comment-5756978 for a similar issue. Setting back to needs review.

Mike Wacker’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new1.08 KB

The patch is the exact same as in #11, the only difference is the path changed from modules/... to core/modules/...

The patch doesn't introduce a new feature or change anything from a functional perspective, so as long as the unit tests pass, we shouldn't need an additional test for this patch. I did however, insert a drupal_set_message() into user_register_submit() to do some one-off manual testing of the patch, and I confirmed that it works.

webchick’s picture

Status: Reviewed & tested by the community » Needs review

Hm. Isn't the correct way to do this with #access on the form..?

webchick’s picture

Status: Needs review » Fixed

Oh, ok. I get it. The form can be submitted by multiple people, one option of which is an admin. And by moving the access check into the form, it can be altered to some other permission by a module.

Seems reasonable to me, and shouldn't break anything. Committed and pushed to 8.x and 7.x. Thanks!

agentrickard’s picture

Actually, the problem is that you can't form_alter out the admin behavior without rewriting the submit handler from scratch. :-)

Status: Fixed » Closed (fixed)
Issue tags: -Novice, -forms api, -Needs backport to D7

Automatically closed -- issue fixed for 2 weeks with no activity.