n Register new anonymous user with specific role through rest api in Drupal 8 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

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. or you can use mymodule_entity_presave to assign role to the anonymous users.

use Drupal\Core\Entity\EntityInterface;

function mymodule_entity_presave(EntityInterface $entity)
{
    if ($entity->getEntityTypeId() == 'user') {
        $role = $entity->get('field_role')->value;
        if ($role) {
            $entity->addRole($role);
        }elseif ($role = \Drupal::request()->headers->get("role")) {
            $entity->addRole($role);
        }
    }
}

 

Example 1: add role to body
       
 {
          "name": { "value": "fooBar" },
          "mail": { "value": "foo@bar.com" },
          "pass": { "value": "secretSauce" },
          "field_role": { "value": "editor" }

}

Example 1: add role to header
  Content-type: application/json
  role: editor
 

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook