glpi-project / glpi

GLPI is a Free Asset and IT Management Software package, Data center management, ITIL Service Desk, licenses tracking and software auditing.
https://glpi-project.org
GNU General Public License v3.0
4.34k stars 1.3k forks source link

API add item #3188

Closed Miminoux closed 3 years ago

Miminoux commented 7 years ago

Hi, I'm using GLPI 9.2.1 (thanks for the late upgrade) and I have had troubles creating a user. Actually, I was lacking the appropriate right to do so. I think I have been lead in error by the return code which was 400 and not 401. Which one is the best in such a case ? I would vote for 401 ^^ Bye bye.

Miminoux commented 7 years ago

By the way, can you tell me what is the field name to set the e-mail address plz ?

btry commented 7 years ago

Hi

When you create a user and want to add him some email addresses you need to send an additional key in your input _useremails. This key is an array associating an numbeed index to an email address. To specify the default email address (the radio button in the GLPI UI) add an other key _default_email containing the numbered index of one of your email addresses provided in _useremails

See in inc/user.class.php User::post_addItem() to understand the implementation of the feature.

About the HTTP error code, I agree with you, only for creation of a single item. For multiple items, the HTTP return code shall be 207. It seems to be easy to implement.

@orthagh how would you vote for this suggestion ?

orthagh commented 7 years ago

for a single item, without right, yes, we should have 401. For multiples items:

Miminoux commented 7 years ago

Hi, It doesn't seem to work for me : { "input": { "realname": "BOVAIS", "firstname": "José", "_profiles_id": 13, "name": "jose.bauvais@tutu.tata", "phone": 332, "_default_email": 0, "_useremails[0]": "jose.bauvais@tutu.tata", "usercategories_id": 1 } }

It creates José but with no email. I've read the PHP file you mentioned which helped me. Is this payload right for you ? I'm not and expert but I don't find in this function any line setting the user name according to input['name'] for example, same with realname, firstname... although it works.

Thank you Bye bye

orthagh commented 7 years ago

Hello @Miminoux This is not the way to do an array with json. Here is a fixed example (Note the [...]):

{
   "input": {
      "realname": "BOVAIS",
      "firstname": "José",
      "_profiles_id": 13,
      "name": "jose.bauvais@tutu.tata",
      "phone": 332,
      "_default_email": 0,
      "_useremails": [
         "jose.bauvais@tutu.tata"
      ],
      "usercategories_id": 1
   }
}
btry commented 7 years ago

This one works too and has the advantage to give you control on indexes for each email address for use with _default_email (tested before sharing it here):

{
"input": {
"realname": "BOVAIS",
"firstname": "José",
"_profiles_id": 13,
"name": "jose.bauvais@tutu.tata",
"phone": 332,
"_default_email": 1,
"_useremails": { "0": "jose.bauvais@tutu.tata", "1": "jose.beauvais2@toto.tata"},
"usercategories_id": 1
}
}
Miminoux commented 7 years ago

Oh gosh, thanks... I'm also in trouble with the groups, I don't get hos to add my newly created user to a specific group. I've tried to pass the group ID in _groups : { "input": {... "_groups": [ "106" ]... I'm trying to use the search webservice to search my group but it does not return the ID field, I'm kinda lost... I search further later. Thanks anyway.

btry commented 7 years ago

If you can read PHP, your best resource is reading the following methods for the classes related to your action:

sometimes (by the way, emails for an added user is a perfect example):

btry commented 7 years ago

To add a group for a user you need

Again, read the code of the methods I pointed.

Miminoux commented 7 years ago

One more thing, when searching a group "/apirest.php.search/group?$search", using search : "criteria[0][field]=14&criteria[0][searchtype]=equals&criteria[0][value]=HAS" I can't find a group named "HAS" but when changing "equals" by "contains" then it works (the group is exactly "HAS"). I then tried the following search : "criteria[0][field]=14&criteria[0][searchtype]=equals&criteria[0][value]=106" And surprisingly, it worked ! What drives me mad is that field 14 is the name, not the ID... I don't know if it's a bug or if I misunderstood something ? Maybe you prefer a new ticket for this ? Just let me know.

And one last thing when searching for groups, the result doesn't show the ID, how can I get it when I only have the group name ? This is just for information because I tried a little bit to search group_user with group contains HAS and it returned some groups, I'm not blocked.

Thanks a lot for your replies, Bye bye

btry commented 7 years ago

@Miminoux

This is normal. the operator Equals is not handled for most fields. If I remember correctly, this operator works for numeric values, not strings. So you need to use the operator contains.

Again, if I remember correctly, you may use regex anchors ^ and $ to have the same effect as equal operator. Try searching for ^HAS$ You should get your group, and it will exclude items like a_HAS a_HAS_b or HAS_B.

orthagh commented 7 years ago

@bry, i think about this recurrent issue with has/contains. It should be good if we detect a numeric or string value for has operator and in case of string value, shortcut with contains '^string$'

btry commented 7 years ago

OK I'll think improve the API this way

github-actions[bot] commented 3 years ago

There has been no activity on this issue for some time and therefore it is considered stale and will be closed automatically in 10 days.

If this issue is related to a bug, please try to reproduce on latest release. If the problem persist, feel free to add a comment to revive this issue. If it is related to a new feature, please open a topic to discuss with community about this enhancement on suggestion website.

You may also consider taking a subscription to get professionnal support or contact GLPI editor team directly.