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 24 Next »

Contents

Creating a user with a profile

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
// #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("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'
    'roles' => array (
    // The internal role ID is used to assign roles. The following values are examples. 
    // Valid Role ID lists can be provided by a PM.
      array("id" => 98765432), // Student
      array("id" => 12345678), // Reviewer
    ),
  );
  $curl = curl_init("http://your-domain.com/user");
  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
  curl_setopt($curl, CURLOPT_HTTPHEADER, array("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("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.

Updating a user role

User roles may be queried and updated via web services at the user endpoint, which displays information pertaining to the user's account. The user account is separate from the profile, which stores most user data, such as first and last name, location data, and any custom fields. The user account stores the minimum required data for the user, including username, e-mail address, enabled status, and roles.

WARNING

When updating the role array, the transaction is destructive. It will overwrite the entire array of the user. When attempting to make an additive call, it is best practice to query the user endpoint first, to retrieve the current user's roles, and append the new role to the list.

When querying the endpoint, the role attribute will return a one dimensional array of role identifiers (rids), which is the internal system id for a role. However, the update call requires a specific structure, detailed below, where the rids are wrapped in a multidimensional array, with each individual role in it's own array element, keyed by id.

<?php
// Update the roles of UID 100
$curl = curl_init("http://your-domain.com/user/100");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); // An update request requires a PUT call
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "restws_webservice:webservice_password"); // Your web service user credentials
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

// Structure the update array
  'roles' => array (
  // The internal role ID is used to assign roles.
    array("id" => 123123123), // Custom Member
    array("id" => 789789789), // Reviewer
// NOTE the use of id instead of rid
  );
// Set the payload
  curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($roles));
// Send
  $json = curl_exec($curl);
  $response = json_decode($json);
}

Related Pages

Creating an SSO User via Webservice







  • No labels