9.4.3.4.5. Identity and Identity relationships

eXo Platform uses 2 built-in types of identities, including user identities and space identities. An identity is specified by its providerId and remoteId. For user identities, the providerId is "organization", while "space" is for space identities.

You can obviously manage the Social objects via their identities. The source code used in this section is available here for downloading.

Getting users and spaces by identity

To retrieve all users and spaces, use the /rest/v1/social/identities API with a parameter named type that represents the providerId of the identity.

  1. Create a file named SocialIdentityAPIs.xml under the /gadgets/SocialAPIsGadgets/ folder, then add the following script to this file:

    
    <?xml version="1.0" encoding="UTF-8" ?>
    <Module>
        <ModulePrefs title="Social Identity Gadget">
        </ModulePrefs>
        <Content type="html">
            <![CDATA[
                <!--Including platform css-->
                <link href="/eXoSkin/skin/css/Core.css" rel="stylesheet"></link>
                <link href="/eXoSkin/skin/css/sub-core.css" rel="stylesheet"></link>
                <!--Including JQuery library-->
                <script src="jquery-2.1.3.js"></script>
                
                <!--Gadget's main body which will be added by HTML DOM Object later-->
                <div id="main-body" style="max-width: 850px" class="uiComposer uiGrayLightBox">
                        <div class="input-append" style="margin: 20px 30px 20px 30px">
                            //control button for choosing identity type
                            <button id="choose_identity_type_btn" onclick="viewIdentities()" class="btn btn-primary">View identities</button>
                            <select id="identity_type">
                                <option value="organization">organization</option>
                                <option value="space">space</option>
                            </select>
                        </div>
                </div>
                <!--Start calling js function-->
                <script type="text/javascript">
                    function viewIdentities(){
                        //send http request
                        $.ajax({
                             type: "GET",
                             url: "/rest/v1/social/identities?type="+$("#identity_type").val(),
                             success: function (data, status, jqXHR) {
                                //print the result
                                if($("#identity_type").val()=="organization"){
                                    printUsersList(data.identities);
                                }
                                else {
                                    printSpacesList(data.identities);
                                }
                             },
                             error: function(jqXHR, status) {
                                 alert("Cannot retrieve data!");
                             }
                        });
                    }
                    function printUsersList(users){
                        //print a list of users in a table
                        //clean screen
                        $('#main-content').remove();
                        //initialize html content
                        var obj_content="<div id=\"main-content\"><table class=\"uiGrid table table-hover table-striped\"><thead><tr><th>&nbsp;</th><th>Identity</th><th>User name</th><th>First name</th><th>Last name</th><th>Email</th></tr></thead><tbody>";
                            //loop through the list
                            $.each(users, function(key, value) {
                                obj_content+="<tr><td>"+key+"</td><td>"+value.id+"</td><td>"+value.profile.username+"</td><td>"+value.profile.firstname+"</td><td>"+value.profile.lastname+"</td><td>"+value.profile.email+"</td></tr>";
                            });
                        //add closing tag
                        obj_content+="</tbody></table></div>";
                        //add to screen
                        $('#main-body').append(obj_content);
                    }
                    function printSpacesList(spaces){
                        //clear screen
                        $('#main-content').remove();
                        if(spaces.length==0){
                            $('#main-body').append("<div id=\"main-content\">No space was found!</div>");
                        }
                        else {
                            //print a list of spaces in a table
                            //initialize html content
                            var obj_content="<div id=\"main-content\"><table class=\"uiGrid table table-hover table-striped\"><thead><tr><th>&nbsp;</th><th>Name</th><th>Identity</th></tr></thead><tbody>";
                                //loop through the list
                                $.each(spaces, function(key, value) {
                                    obj_content+="<tr><td>"+key+"</td><td>"+value.globalId.localId+"</td><td>"+value.id+"</td></tr>";
                                });
                            //add closing tag
                            obj_content+="</tbody></table></div>";
                            //add to screen
                            $('#main-body').append(obj_content);
                        }
                    }
                </script>]]>
        </Content>
    </Module>
    • If the selected type (providerId) is "organization", the printUsersList() function prints out user profiles.

    • If the selected type (providerId) is "space", the printSpacesList() function prints out basic information of spaces.

  2. Deploy this gadget and test this function:

    • Select the type "organization".

    • Select the type "space".

Getting a specified object by identity

To retrieve an object by its identity, use the /rest/v1/social/identities/{id} API where id is the identity of the object. Implement a getIdentityById() function like this:

function getIdentityById(){
	//send http request
	$.ajax({
		 type: "GET",
		 url: "/rest/v1/social/identities/"+input_identity,
		 success: function (data, status, jqXHR) {
			//print the result
			if(data.providerId=="organization"){
				printUsersList(data);
			}
			else {
				printSpacesList(data);
			}
		 },
		 error: function(jqXHR, status) {
			 alert("Cannot retrieve data!");
		 }
	});
}

Getting relationships of an identity

You can use the /rest/v1/social/relationships API to retrieve all relationships of an identity. For example, passing a user identity and a parameter status=pending to the API call results in connections of this user who are in the "pending" status.

Implement a function like this:

function getIdentityRelationships(){
	//send http request
	$.ajax({
		 type: "GET",
		 url: "/rest/v1/social/identities?identityId="+input_identity+"&status=pending",
		 success: function (data, status, jqXHR) {
			//print the result data.relationships
		 },
		 error: function(jqXHR, status) {
			 alert("Cannot retrieve data!");
		 }
	});
}

Other APIs

The remaining APIs allow to deal with Social objects (Space or User) as in the sections of User and Space APIs.

Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus