...
Table of Contents | ||||
---|---|---|---|---|
|
There are two data points associated to an account when created via the EthosCE user interface, a user entity and a profile entity. Services creating accounts via web service must create both data points.
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.
Verify the user does not exist
...
Send the user creation
...
request
...
Send a profile creation request,
...
assigning it to the
...
user via uid.
...
language | php |
---|
...
Verify
...
the
...
user exists
There are two ways to verify an account exists, checking by e-mail or SSO ID.
GET user.json?mail=newuser@dlc-solutions.com
...
GET authmap.json?authname=EXTERNAL_SSO ID
Create user account
POST user.json
This payload will create a user with the roles “course author” (105847459) and “reviewer” (175031666), by sending the corresponding role ids (rid) as an array of values.
Code Block | ||
---|---|---|
| ||
{
"name": "newuser_username",
"mail": "newuser@dlc-solutions.com",
"status": 1,
"roles": [
{
"id": 105847459
},
{
"id": 175031666
}]
} |
A successful user creation request returns the following:
Code Block |
---|
{"uri":"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); } |
...
/123","id":"123","resource":"user"} |
Create profile demographics
POST profile2.json
Associating a profile to an account requires the user id (uid) to be assigned to the user field in the profile data. In the creation request above the uid value returned is 123.
Code Block | ||
---|---|---|
| ||
{
"user": 123,
"label": "Profile",
"type": "profile",
"field_first_name": "John",
"field_middle_name": "Middle",
"field_last_name": "Smith",
"field_profile_location":
{
"street": "123 S Broad Street",
"additional": "Suite 2260",
"city": "Philadelphia",
"province": "PA",
"postal_code": "19109",
"country": "us"
}
} |
A successful profile creation request returns the following:
...
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.
...
. Only the changed fields need be included in a PUT request.
PUT user/123
Info |
---|
Sending or updating roles is a destructive process. Any PUT updates sent to the user.json endpoint must contain all role ids, both existing and new. The payload below, if sent to the user created on this page, will remove “course author” (105847459), add “report viewer” (92961712), and keep the “reviewer” role (175031666), |
Code Block | ||
---|---|---|
| ||
<?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);"roles": [ { "id": 92961712 }, { "id": 175031666 },] } |
Related Pages
Creating an SSO User via Webservice
...