0

I'm using Drupal 8 rest service to register a new anonymous user and it works fine; the user is created in Drupal.

Then I tried to specify a custom role sending the request below

{
    "name": { "value": "FooBar" },
    "mail": { "value": "foo@bar.com" },
    "pass": { "value": "secretSauce" },
    "roles": [
        "my_role"
    ]
}

but I receive the error

{
    "message": "Access denied on creating field 'roles'."
}

I checked permissions but I'm not getting what is missing.

Mario Catena
  • 575
  • 1
  • 8
  • 20

2 Answers2

0

An anonymous user cannot assign themselves a role. Drupal automatically assigns a user the role of 'Authenticated user'. You can alter the permissions of that role accordingly if that suits your needs.

*If you are using the excellent Simple oAuth module to authenticate, then you can also assign the user being created via the REST API a particular 'Scope' (role) based on the consumer ID used when creating the account. On your app you can then create the logic for the particular role to be assigned to a particular user.

*This all assumes that you are using a headless approach and the users are being created from a JS front-end.

EDIT

  • Create custom field for Drupal Users
  • Install the Rules Module (https://www.drupal.org/project/rules)
  • Create a rule to act on a new user and assign the appropriate role per the value of the custom field.
  • Pass in the desired value for the custom field from your front end (like your example above)
  • Thank you for your comment but I'm not getting how to assign the role. I'm using a headless approach where Drupal serves my js fe and I would like to assign a specific role (I have different roles) during the registration. I can create a new user invoking "localhost:8888/user/register?_format=json" and login invoking "localhost:8888/user/login?_format=hal_json". As you pointed the user is created with the role 'Authenticated user' and has no grants to assign a new role and from my fe layer I'm not getting how to orchestrate some logic without imagine some custom module – Mario Catena Jun 19 '19 at 07:22
  • How many roles will you be using? Is there a specific criteria on who gets a particular one? – briangonzalezmia Jun 20 '19 at 02:07
  • Ideally 3 initially will be 2; each role matches a value I would pass from the registration call. Imagine to select a value from the registration form and then convert this value into a specific role you are assigned to during the registration process. The registration is done from anonymous users, I'm using Drupal headless and I would like to avoid to create a custom module for this. – Mario Catena Jun 20 '19 at 08:06
  • Updated answer above – briangonzalezmia Jun 20 '19 at 13:00
  • I did it using rules – Mario Catena Jun 21 '19 at 09:37
0

I know that that's could very stupid but, actually I had the same mistake and the problem was that I send a GET request instead of POST

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 16 '22 at 14:53