Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 18 Next »

Creating a user via the web service can be accomplished with 2 requests to the system, with an extra request to verify the user does not already exist.

  1. Verify the user does not exist by sending a GET request to the system, searching by user 'mail' attribute. (see Web Service Responses)
  2. Send the user creation request, via POST request.
  3. Send a profile creation request, via POST, assigning it to the new user via uid.

<?php
// Login to the site and request the access token
$curl = curl_init('http://your-domain.com/restws/session/token');
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "restws_webservice:webservice_password"); // Your web service user credentials goes here.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$token = curl_exec($curl);
// The token looks like "q71OBx05wtECfjA0KmXf6wiktewrywNhkMZv-OcfyOA%"

// #1 Verify the user does not exist by sending a GET request to the system, searching by user 'mail' attribute
$curl = curl_init("http://your-domain.com/user.json?mail=newuser@dlc-solutions.com");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-CSRF-Token: $token", "Content-Type: application/json"));
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "restws_webservice:webservice_password"); //Your credentials goes here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

$json = curl_exec($curl);
$response = json_decode($json);

// A list of users matching criteria will be returned. If this list is empty, a user does not exist in EthosCE with the given e-mail address.
$list = $response->list;

// #2 Send the user creation request, via POST
if (empty($list)) {
  $user = array(
    'name' => 'newuser_username', // Username
    'mail' => 'newuser@dlc-solutions.com',
    'status' => 1, // Enabled. A value of 0 sets the account as 'Blocked'
  );
  $curl = curl_init("http://your-domain.com/user");
  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
  curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-CSRF-Token: $token", "Content-Type: application/json"));
  curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($curl, CURLOPT_USERPWD, "restws_webservice:webservice_password"); //Your credentials goes here
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($user));

  $json = curl_exec($curl);
  // An array detailing the new user entity is returned if successful
  $userInfo = json_decode($json);

// #3 Send a profile creation request, via POST, assigning it to the new user via uid.
  $profile = array(
    'user' => $userInfo->id, // The value sent back is a user entity, which identifies itself by entity id, which is also the Drupal uid
    'label' => 'Profile',
    'type' => 'profile',
    'field_first_name' => 'John',
    'field_middle_name' => 'Middle',
    'field_last_name' => 'Smith',
    'field_profile_location' => array(
      'street' => '1520 Locust Street',
      'additional' => 'Suite 1000',
      'city' => 'Philadelphia',
      'province' => 'PA',
      'postal_code' => '19102',
      'country' => 'us', // A two character country code
    ),
  );

  $curl = curl_init("http://your-domain.com/profile2");
  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
  curl_setopt($curl, CURLOPT_HTTPHEADER, array("X-CSRF-Token: $token", "Content-Type: application/json"));
  curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($curl, CURLOPT_USERPWD, "restws_webservice:webservice_password"); //Your credentials goes here
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($profile));

  $json = curl_exec($curl);
  $response = json_decode($json);
}


The final successful profile creation request returns the following:

{"uri":"http://your-domain.com/profile2/72","id":"72","resource":"profile2"}
For an explanation of the information returned from the verification request, seeĀ Web Service Responses.

Related Pages

Creating an SSO User via Webservice

  • No labels