You are not logged in.

Announcement

 Téléchargez la dernière version stable de GLPI      -     Et vous, que pouvez vous faire pour le projet GLPI ? :  Contribuer
 Download last stable version of GLPI                      -     What can you do for GLPI ? :  Contribute

#1 2019-06-18 16:03:13

MrCraac
Member
Registered: 2019-06-18
Posts: 6

How to add user's password and user's email

Hi,

I am able to create new user using the API, however, I am not able to set passwords and email.

First question is , is that possible to do ? if yes, how should I proceed ?
Maybe the user has to be updated once created ?
However, I did not found the fields name for those data.

Here is my request to create user:

$data = [
            'input' => [
                'name' => 'JT',
                'realname' => 'Travolta',
                'firstname' => 'John',
                'language' => 'en_GB',
                'is_active' => 1,
                'entities_id' => 3,
            ]
        ];
$input = json_encode($data);

$url=$api_url . "/User";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS,$input);
$json = curl_exec($ch);
curl_close ($ch);

Thanks for reading me smile

Je suis Francais, si qqun prefere repondre en francais ... !

Last edited by MrCraac (2019-06-18 16:49:50)

Offline

#2 2019-06-19 12:15:54

MrCraac
Member
Registered: 2019-06-18
Posts: 6

Re: How to add user's password and user's email

I found how to add the user's password , it seems it has to use sha1
$name= "test@domain.com";
$pwd = sha1($name);

$data = [
    'input' => [
        'name' => $name,
        "password" => $pwd,
        'realname' => 'Travolta',
        'email' => 'Travolta',
        'firstname' => 'John',
        'language' => 'en_GB',
        'is_active' => 1,
        'entities_id' => 3,
    ]
];

However, I see that the emails are stored on another tables , so I am still looking on how to add it. Any help welcome smile

Offline

#3 2019-06-19 14:20:48

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,145

Re: How to add user's password and user's email

$url='Myserver.com/glpi/apirest.php/User/'.$user_id.'/Useremail';


then post

$url='Myserver.com/glpi/apirest.php/User/'.$user_id.'/Useremail';
$fields='{ "input" : {"users_id": '.$user_id.', "is_default": "1", "is_dynamic":"0" ,"email": '.$newemail.' }}';

Trouver la panne avant de réparer...
GLPI10.0.10 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6

Offline

#4 2019-06-19 14:36:10

MrCraac
Member
Registered: 2019-06-18
Posts: 6

Re: How to add user's password and user's email

Thanks for your answer , it helps me a lot smile
I am now working on the search request to retrieve the user id.

Offline

#5 2019-06-19 14:48:36

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,145

Re: How to add user's password and user's email

$userlogin="ladenree";


$url="/search/User?criteria[0][field]=1&criteria[0][searchtype]=contains&criteria[0][value]=^".$userlogin."$";


Trouver la panne avant de réparer...
GLPI10.0.10 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6

Offline

#6 2019-06-19 16:11:59

MrCraac
Member
Registered: 2019-06-18
Posts: 6

Re: How to add user's password and user's email

Merci encore pour la réponse,
Je ne comprends pas , l'ID de mon user est 8.
lorsque je fait la requête avec l'URL ci dessus, j'obtiens:
Array
(
    [totalcount] => 1
    [count] => 1
    [sort] => 1
    [order] => ASC
    [data] => Array
        (
            [0] => Array
                (
                    [1] => NewUserTest
                    [80] => Root entity
                    [34] =>
                    [5] =>
                    [6] =>
                    [3] =>
                    [8] => 1
                )

        )

    [content-range] => 0-0/1
)
Je ne vois pas comment récupérer l'ID à partir de ca, quelque chose doit m'échapper.

Offline

#7 2019-06-19 16:14:32

LaDenrée
HELPER
Registered: 2012-11-19
Posts: 6,145

Re: How to add user's password and user's email

il faut ajouter un     &forcedisplay[0]=2    pour forcer le retour du champ ID

$url="/search/User?criteria[0][field]=1&criteria[0][searchtype]=contains&criteria[0][value]=^".$userlogin."$&forcedisplay[0]=2";

Trouver la panne avant de réparer...
GLPI10.0.10 (ubuntu 22.04 PHP8.1  Mariadb10.6 ) plugins : comportements 2.7.2 reports 1.16.0 formcreator 2.13.8, datainjection 2.13.4 fields 1.21.6

Offline

#8 2019-06-19 16:55:29

MrCraac
Member
Registered: 2019-06-18
Posts: 6

Re: How to add user's password and user's email

Merci encore, j'étais pas près de le trouver tout seul celui la!
smile

Offline

#9 2019-06-19 18:23:19

MrCraac
Member
Registered: 2019-06-18
Posts: 6

Re: How to add user's password and user's email

Au cas ou ca serve, voici mon code pour ajouter un user et lui set un email:
<?php

// SESSION OPENING
function OpenSession()
{
    try {
        $api_url = "http://localhost/apirest.php";
        $user_token = "QU6a1yy6Fpf2aVxg4rFzjTiZUcpFbGOyB8uBApFA";
        $app_token = "Ia9UgZflvuqO7P1Sg5u4Qz3QsylIGfh6CMFxbp5l";
        $ch = curl_init();
        $url = $api_url . "/initSession?Content-Type=%20application/json&app_token=" . $app_token . "&user_token=" . $user_token;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $json = curl_exec($ch);
        curl_close($ch);
        $obj = json_decode($json, true);
        $sess_token = $obj['session_token'];
        $headers = array(
            'Content-Type: application/json',
            'App-Token: ' . $app_token,
            'Session-Token: ' . $sess_token
        );

        return $headers;
    } catch (Exception $e) {
        echo "session opening error: $e->getMessage()";
    }

}


// USER CREATION

function CreateUser($name,$realname = null,$firstname = null ,$entities_id,$pwd, $headers){
    try {
        $data = [
            'input' => [
                'name' => $name,
                "password" => $pwd,
                'realname' => $realname,
                'firstname' => $firstname,
                'language' => 'en_GB',
                'is_active' => 1,
                'entities_id' => $entities_id,
            ]
        ];
        $input = json_encode($data);

        $api_url = "http://localhost/apirest.php";
        $url=$api_url . "/User";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS,$input);

        $json = curl_exec($ch);
        curl_close ($ch);

        $res = json_decode($json, true);

        if ($res[0]== "ERROR_GLPI_ADD") {
            return ["status" => false, "message" => $res[1], "user" => $name];

        }
        else {
            return ["status" => true];
        }

    }
    catch (Exception $e) {echo "user creation error: $e->getMessage()";}
}


function AddEmail($name, $headers)
{
// GET USER ID
    try {
        $api_url="http://localhost/apirest.php";
        $url = $api_url . "/search/User?criteria[0][field]=1&criteria[0][searchtype]=contains&criteria[0][value]=^" . $name . "$&forcedisplay[0]=2";


        //$url=$api_url . "/listSearchOptions/User";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        //curl_setopt($ch, CURLOPT_POSTFIELDS,$input);
        $json = curl_exec($ch);
        curl_close($ch);


    } catch (Exception $e) {
        echo "get user id error: $e->getMessage()";
    }

    $obj = json_decode($json, true);


// ADD USER EMAIL
    try {
        $user_id = $obj['data']['0']['2'];

        $api_url="http://localhost/apirest.php";
        $url = $api_url . "/User/" . $user_id . "/UserEmail/";
        $fields = '{"input":{"users_id":' . $user_id . ',"is_default":1,"is_dynamic":0,"email":"' . $name . '"}}';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        $json = curl_exec($ch);
        curl_close($ch);

        $res = json_decode($json, true);
//print_r($res);
        return true;

    } catch (Exception $e) {
        echo "email add error: $e->getMessage()";
    }

}

//  USER TEST
$name= "hello2@coucou.fr";
$realname = 'test';
$firstname = 'bbb';
$entities_id = 3;
$pwd = sha1($name);
$headers = OpenSession();

$CreateUser= CreateUser($name,$realname,$firstname,$entities_id,$pwd, $headers);

if ($CreateUser['status']) {

   $AddEmail = AddEmail($name, $headers);
   if (!$AddEmail){ echo "error for user: $name while adding email";}
   else {echo "user: $name added successfuly";}


}

else { echo "error: ". $CreateUser['message']. " User: ". $CreateUser['user'] ;}

Offline

#10 2022-08-31 22:16:01

androB
Member
Registered: 2022-08-31
Posts: 2

Re: How to add user's password and user's email

This code is perfect, however when I am trying to search the user based on name example user login is 3000022 but it doesn't work, if I replace 3000022 with email address then it works,

any idea how to get the user ID # by searching the name/login_name ?

I am trying to get

// GET USER ID

$name = "3000022";

        $api_url="http://192.168.252.102/apirest.php";
        $url = $api_url . "/search/User?criteria[0][field]=1&criteria[0][searchtype]=contains&criteria[0][value]=^". $name. "$&forcedisplay[0]=2";
        //$url=$api_url . "/listSearchOptions/User";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');


        //curl_setopt($ch, CURLOPT_POSTFIELDS,$input);
        $json = curl_exec($ch);
        curl_close($ch);

        $obj = json_decode($json, true);
        $agentbadge = $obj['data']['0']['2'];
        echo "AgentBadge: $agentbadge";

Offline

#11 2023-02-23 16:41:49

Ocotaa
Member
Registered: 2022-10-12
Posts: 2

Re: How to add user's password and user's email

Hello androB,

It is said in the documentation that the id of the inserted objects are returned in the result of the query.

You can get the result of the query using the following code line :

$result = json_decode(curl_exec($curl), true);

This avoids creating a function that retrieves the id of the inserted object.

Link to the API documentation : myglpiserver/apirest.php (if this last one dont work try myglpiserver/glpi/apirest.php)

Last edited by Ocotaa (2023-02-23 16:45:47)

Offline

Board footer

Powered by FluxBB