Problem/Motivation
REST registration is currently broken because users created with core's REST endpoint are blocked by default; this prevents users from logging in after creating their accounts in decoupled apps.
Steps to reproduce
1. Enable the setting "Require email verification when a user creates an account."
2. Create a user via core's REST endpoint.
3. Receive the email link to log in.
4. Attempt to log in. Access will be denied because the account is blocked.
Proposed resolution
Users registered via REST should have the account status set to active initially.
Remaining tasks
User interface changes
None.
API changes
User accounts registered via REST will have a status of active instead of blocked.
Data model changes
User accounts registered via REST will have a status of active instead of blocked.
Original Issue
Hi all.
I enabled the “Require email verification when a visitor creates an account” flag to validate email address as part of registration process.
I also enabled “RESTful Web Services” module enabled to trigger registration flow in my headless Drupal ContentaCMS installation through the /user/register endpoint.
Issue description
I found the following issue:
- when a user is created via /user/register?_format=json REST endpoint, the status after creation is “Blocked”
- the onetime password is generated and the email with the one time link is correctly sent to the user
- BUT, after clicking on the link sent via email, the user is getting 403 Access Denied pageI went through the code and I found this ‘if’ in the getResetPassForm(Request $request, $uid) in User.Controller.php
if ($user === NULL || !$user->isActive()) {
// Blocked or invalid user ID, so deny access. The parameters will be in
// the watchdog's URL for the administrator to check.
throw new AccessDeniedHttpException();
}
It looks like Blocked user cannot execute password reset (which makes perfectly sense to me).
I checked the same scenario by creating a user through standard Drupal login page (no REST).
After the creation, the user status is set to Active and the email verification process can be completed without issues.
Conclusion
- only user created via /user/register?_format=json cannot finalize the email verification, since their account is blocked.
Workaround
To overcome this issue I enabled Rules module to trigger the following rule
- event: After Saving a new User
- action: unBlock User
This rule is executed every time a new user is created and moves the status from Blocked -> Active
| Comment | File | Size | Author |
|---|
Issue fork drupal-3055807
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
axel80 commentedComment #3
axel80 commentedComment #4
cilefen commentedSee https://www.drupal.org/core/issue-priority about issue priorities.
Comment #5
vladimir.krupin commentedRequired email verification should affect the user password, not the user status.
Patch attached
Comment #7
vladimir.krupin commentedComment #8
vladimir.krupin commentedComment #9
rensingh99 commentedHello,
I have reviewed the patch #7 and It worked as designed. Below is my update.
1). I have given permission to "Visitors" users to create an account with an email verification option.
2 I have created a user account using the drupal register form and it was an active user.
3 I have created a user account using Rest API then it was a blocked user.
Rest API:

User:

4 I have created users using Rest API after applying the patch then the user was active.
Rest API:

User:

The patch is working great.
Thanks,
Ren
Comment #10
alexpottThis change matches what happens on \Drupal\user\AccountForm::form() and \Drupal\user\RegisterForm - nice catch.
One question is if they've sent a password in the request should we error here? Because if they have email verification on I think we need to generate a new password and whatever they've sent should be ignored.
If we decide to stick with the current approach we should have test coverage of sending a password and ignoring it when we are verifying email.
Comment #11
wim leersIdeally @marthinal & @tedbow would also review this — they were the most active participants in #2291055: REST resources for anonymous users: register, which brought
\Drupal\user\Plugin\rest\resource\UserRegistrationResourceto Drupal core.Comment #12
vladimir.krupin commented#10 Now is not possible to send pass data. This case checked before temporary password generation inside the
protected function ensureAccountCanRegisterAlso,
\Drupal\Tests\user\Functional\RestRegisterUserTesthave related case insidetestRegisterUser()Is this the answer to your question?
Comment #13
vladimir.krupin commentedComment #14
Kloport commentedHello,
Thank you for this welcome patch.
Silly question: what is the module name needed to apply the patch via Composer? Is it User?
Thank you!
Comment #15
vladimir.krupin commented@kloport this is
drupal/coreComment #18
mrunwal commentedI am currently using 8.9.7 This still not actually resolved.
When ever account is created using rest user still showing blocked
REST JSON
{
'name' : [{"value": 'abc' }],
'mail' : [{"value": 'pr'}],
}
Some of the other forum saying I should pass additional key as "status": [{"value":"1"}] but then its giving "no authentication credentials provided" and if I don't have status and user got registered with status is "Blocked"
Comment #19
ptmkenny commentedIt would be great if this could get a review as it breaks registration for headless sites.
Comment #20
vikashsoni commentedApply 7.patch and working fine now sharing the screenshot
Comment #21
mrunwal commentedApplying 7. Patch works for me.
Comment #23
kevinquillen commentedRan into this problem as well. Applying the patch from #7 allowed the account to be created with a status of Active. Note that you do not need to actually pass 'status' in the request when creating an account, you will still get the access denied error.
We still require users to click the registration link in their email, but now they actually can do that without hitting a 403.
Comment #25
ptmkenny commentedRe-rolling the patch in #7 to apply to Drupal 9.3.2.
Comment #26
Kojo Unsui commentedI tested #7 patch as well and confirm it's working properly. Same steps as #9:
Last, it seems that @Vladimir #12 comment answers @Alexpott, so should this be reviewed yet another time or could it be committed ? It's a pretty annoying issue in decoupled context. I'll mark it as RTBC for now.
Comment #27
murilohp commentedHey @Kojo, thanks for testing and moving this issue to RTBC, looking at #25, the patch looks good, I just have one minor suggestion:
Don't you think password generator should be injected? I mean, I know it's just one call, but this way I think we keep things consistent.
The following patch has the suggestion implemented, I'll move this back to needs review to see what you think.
Thanks
Comment #29
murilohp commentedMy bad, here's a new patch with the tests fixed.
Comment #30
ptmkenny commented@murilohp Yes, it's better to inject the class. Thanks for redoing the patch. Setting back to RTBC.
Comment #31
alexpottWe need to do the deprecation dance here - in case someone has extended this class.
Here's an example from \Drupal\jsonapi\Context\FieldResolver::__construct
Comment #32
ankithashettyHere is an updated patch from #29 addressing the changes suggested in #31. Thanks for the reference @alexpott.
Comment #33
murilohp commentedThanks for addressing #31 @ankithashetty!. Since it's a deprecation message, we should have tests for this, see
core/tests/Drupal/KernelTests/Core/File/FileSystemDeprecationTest.phpas example.Comment #34
murilohp commentedHey, I made a new patch with the test, I think now we can move it to needs review.
Comment #35
rdworianyn commentedI have applied this patch and tested the REST API vs Drupal 9.3.6 and successfully created a user with active status. Thank you all for your work on this!
Comment #36
ptmkenny commentedYeah, I've been using this patch in production for a couple months with no issues. Moving to RTBC.
Comment #37
cilefen commentedThe patch didn't apply in January. How can this be RTBC?
Comment #38
ptmkenny commentedI'm sorry, I forgot to re-queue the tests here before moving to RTBC. It did pass the test in January on 9.3, it just seems to be failing on 9.4.
Comment #39
yogeshmpawarReroll for 9.4.x & added reroll diff as well.
Comment #40
murilohp commentedThanks for the reroll @yogeshmpawar, the testbot is happy and it looks good to me, so moving back to RTBC.
Comment #41
alexpottWe have an interesting problem the hal module is removed from 10.0.x but what this means is that there is no real test coverage of the \Drupal\user\Plugin\rest\resource\UserRegistrationResource class in Drupal 10. We only have unit coverage and no functional coverage. Ideally we need to re-instate the coverage and not rely on hal (because it does not exist in D10).
Comment #42
alexpottThis is blocked on #3268105: Bring back RestRegisterUserTest into user module (without HAL)
Comment #43
ptmkenny commentedSetting to Postponed based on #42.
Comment #44
murilohp commented#3268105: Bring back RestRegisterUserTest into user module (without HAL) landed! So we can move this on. I've just updated #39 and created a new patch for D10 only.
Comment #46
borisson_The last patch uploaded here is a 9.x with deprecation dance and a 10.x withouth, I think this is too short to 10.x being release, so it should be 11.x without and 10.x with the deprecations? Means we also have to update the version numbers in the message.
In the higher version patch we also remove the deprecation dance without actually remove the
= nullfrom the constructor, we should change this also.Comment #47
ravi.shankar commentedAdded Drupal 9.5.x patch of comment #44 on Drupal 10.1.x, with changes of #46 as suggested, please review.
Comment #48
alanmunoz commentedPatch #25 works on Core version 9.4.5
Comment #50
pingwin4egPatch #47 applies and works perfectly with 9.4.8.
Comment #51
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #need-reveiw-queue. We are working to keep the size of Needs Review queue [2700+ issues] to around 200, following Review a patch or merge require as a guide.
Thank you for the patch @ravi.shankar please include an interdiff also so we can easily see what was changed. It does look like the deprecated calls were put in. But believe we missed the 10.0 window so those probably should be updated for deprecated in 10.1
Tagging for IS update as it doesn't clearly say what is being accomplished in the patch.
Thanks.
Comment #53
ptmkenny commentedComment #54
ptmkenny commentedI have re-rolled patch #47 as an MR, updated the deprecation messages to 10.3/11, and rewrote the issue summary. Setting back to needs review.
Comment #56
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue.
While you are making the above changes, we recommend that you convert this patch to a merge request. Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)
Comment #57
ptmkenny commentedI created a draft change record and updated the MR to link to it.
I've never written a change record before so someone should take a careful look at it.
Comment #58
smustgrave commentedSeems to have a failing test.
Comment #59
ptmkenny commentedI fixed the test by using the correct deprecation message.
Comment #60
smustgrave commentedSorry to do this but we will need 2 MRs now
1 with the deprecations (current MR) for 10.3.x
1 without the deprecations, can probably add promotion too for 11.x
Comment #64
ptmkenny commented@smustgrave Ok, I split the MR in two, one for 10.x with deprecations, one for 11.x without.
What did you mean by "promotion"? I'm happy to keep working on this but I don't know what that is.
Comment #65
smustgrave commentedHard to explain so pushed what I meant to the 11.x branch. Reduces code by a ton. Also updated MR to not include default NULL as the deprecation makes seem like it's required.
But both MRs appear to be good and address the issue at hand.
Comment #66
alexpottCommitted d349f1d and pushed to 11.x. Thanks!
Committed e6fae86 and pushed to 10.4.x. Thanks!
Committed 68715a6 and pushed to 10.3.x. Thanks!
Backported to 10.3.x as a major bug fix.