dolibarr 18.0.6
server_user.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2006-2016 Laurent Destailleur <eldy@users.sourceforge.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
23if (!defined('NOCSRFCHECK')) {
24 define('NOCSRFCHECK', '1'); // Do not check anti CSRF attack test
25}
26if (!defined('NOTOKENRENEWAL')) {
27 define('NOTOKENRENEWAL', '1'); // Do not check anti POST attack test
28}
29if (!defined('NOREQUIREMENU')) {
30 define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
31}
32if (!defined('NOREQUIREHTML')) {
33 define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
34}
35if (!defined('NOREQUIREAJAX')) {
36 define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
37}
38if (!defined("NOLOGIN")) {
39 define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
40}
41if (!defined("NOSESSION")) {
42 define("NOSESSION", '1');
43}
44
45require '../main.inc.php';
46require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
47require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
48require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
49require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
50require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
51require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
52
53
54dol_syslog("Call User webservices interfaces");
55
56$langs->load("main");
57
58// Enable and test if module web services is enabled
59if (empty($conf->global->MAIN_MODULE_WEBSERVICES)) {
60 $langs->load("admin");
61 dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
62 print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
63 print $langs->trans("ToActivateModule");
64 exit;
65}
66
67// Create the soap Object
68$server = new nusoap_server();
69$server->soap_defencoding = 'UTF-8';
70$server->decode_utf8 = false;
71$ns = 'http://www.dolibarr.org/ns/';
72$server->configureWSDL('WebServicesDolibarrUser', $ns);
73$server->wsdl->schemaTargetNamespace = $ns;
74
75
76// Define WSDL Authentication object
77$server->wsdl->addComplexType(
78 'authentication',
79 'complexType',
80 'struct',
81 'all',
82 '',
83 array(
84 'dolibarrkey' => array('name'=>'dolibarrkey', 'type'=>'xsd:string'),
85 'sourceapplication' => array('name'=>'sourceapplication', 'type'=>'xsd:string'),
86 'login' => array('name'=>'login', 'type'=>'xsd:string'),
87 'password' => array('name'=>'password', 'type'=>'xsd:string'),
88 'entity' => array('name'=>'entity', 'type'=>'xsd:string'),
89 )
90);
91
92// Define WSDL Return object
93$server->wsdl->addComplexType(
94 'result',
95 'complexType',
96 'struct',
97 'all',
98 '',
99 array(
100 'result_code' => array('name'=>'result_code', 'type'=>'xsd:string'),
101 'result_label' => array('name'=>'result_label', 'type'=>'xsd:string'),
102 )
103);
104
105// Define other specific objects
106$server->wsdl->addComplexType(
107 'user',
108 'complexType',
109 'struct',
110 'all',
111 '',
112 array(
113 'element' => array('name'=>'element', 'type'=>'xsd:string'),
114 'id' => array('name'=>'id', 'type'=>'xsd:string'),
115 'lastname' => array('name'=>'lastname', 'type'=>'xsd:string'),
116 'firstname' => array('name'=>'firstname', 'type'=>'xsd:string'),
117 'note' => array('name'=>'note', 'type'=>'xsd:string'),
118 'email' => array('name'=>'email', 'type'=>'xsd:string'),
119 'signature' => array('name'=>'signature', 'type'=>'xsd:string'),
120 'office_phone' => array('name'=>'office_phone', 'type'=>'xsd:string'),
121 'office_fax' => array('name'=>'office_fax', 'type'=>'xsd:string'),
122 'user_mobile' => array('name'=>'user_mobile', 'type'=>'xsd:string'),
123 'admin' => array('name'=>'admin', 'type'=>'xsd:string'),
124 'login' => array('name'=>'login', 'type'=>'xsd:string'),
125 'entity' => array('name'=>'entity', 'type'=>'xsd:string'),
126 'pass_indatabase' => array('name'=>'pass_indatabase', 'type'=>'xsd:string'),
127 'pass_indatabase_crypted' => array('name'=>'pass_indatabase_crypted', 'type'=>'xsd:string'),
128 'datec' => array('name'=>'datec', 'type'=>'xsd:dateTime'),
129 'datem' => array('name'=>'datem', 'type'=>'xsd:dateTime'),
130 'fk_thirdparty' => array('name'=>'fk_thirdparty', 'type'=>'xsd:string'),
131 'fk_contact' => array('name'=>'fk_contact', 'type'=>'xsd:string'),
132 'fk_member' => array('name'=>'fk_member', 'type'=>'xsd:string'),
133 'datelastlogin' => array('name'=>'datelastlogin', 'type'=>'xsd:dateTime'),
134 'datepreviouslogin' => array('name'=>'datepreviouslogin', 'type'=>'xsd:dateTime'),
135 'statut' => array('name'=>'statut', 'type'=>'xsd:string'),
136 'photo' => array('name'=>'photo', 'type'=>'xsd:string'),
137 'lang' => array('name'=>'lang', 'type'=>'xsd:string'),
138 'entrepots' => array('name'=>'entrepots', 'type'=>'xsd:string'),
139 //'rights' => array('name'=>'rights','type'=>'xsd:string'),
140 'canvas' => array('name'=>'canvas', 'type'=>'xsd:string')
141 )
142);
143
144// Define other specific objects
145$server->wsdl->addComplexType(
146 'group',
147 'complexType',
148 'struct',
149 'all',
150 '',
151 array(
152 'name' => array('name'=>'name', 'type'=>'xsd:string'),
153 'id' => array('name'=>'id', 'type'=>'xsd:string'),
154 'datec' => array('name'=>'datec', 'type'=>'xsd:string'),
155 'nb' => array('name'=>'nb', 'type'=>'xsd:string')
156 )
157);
158
159$server->wsdl->addComplexType(
160 'GroupsArray',
161 'complexType',
162 'array',
163 '',
164 'SOAP-ENC:Array',
165 array(),
166 array(
167 array('ref'=>'SOAP-ENC:arrayType', 'wsdl:arrayType'=>'tns:group[]')
168 ),
169 'tns:group'
170);
171
172$thirdpartywithuser_fields = array(
173 // For thirdparty and contact
174 'name' => array('name'=>'name', 'type'=>'xsd:string'),
175 'firstname' => array('name'=>'firstname', 'type'=>'xsd:string'),
176 'name_thirdparty' => array('name'=>'name_thirdparty', 'type'=>'xsd:string'),
177 'ref_ext' => array('name'=>'ref_ext', 'type'=>'xsd:string'),
178 'client' => array('name'=>'client', 'type'=>'xsd:string'),
179 'fournisseur' => array('name'=>'fournisseur', 'type'=>'xsd:string'),
180 'address' => array('name'=>'address', 'type'=>'xsd:string'),
181 'zip' => array('name'=>'zip', 'type'=>'xsd:string'),
182 'town' => array('name'=>'town', 'type'=>'xsd:string'),
183 'country_id' => array('name'=>'country_id', 'type'=>'xsd:string'),
184 'country_code' => array('name'=>'country_code', 'type'=>'xsd:string'),
185 'phone' => array('name'=>'phone', 'type'=>'xsd:string'),
186 'phone_mobile' => array('name'=>'phone_mobile', 'type'=>'xsd:string'),
187 'fax' => array('name'=>'fax', 'type'=>'xsd:string'),
188 'email' => array('name'=>'email', 'type'=>'xsd:string'),
189 'url' => array('name'=>'url', 'type'=>'xsd:string'),
190 'profid1' => array('name'=>'profid1', 'type'=>'xsd:string'),
191 'profid2' => array('name'=>'profid2', 'type'=>'xsd:string'),
192 'profid3' => array('name'=>'profid3', 'type'=>'xsd:string'),
193 'profid4' => array('name'=>'profid4', 'type'=>'xsd:string'),
194 'profid5' => array('name'=>'profid5', 'type'=>'xsd:string'),
195 'profid6' => array('name'=>'profid6', 'type'=>'xsd:string'),
196 'capital' => array('name'=>'capital', 'type'=>'xsd:string'),
197 'tva_assuj' => array('name'=>'tva_assuj', 'type'=>'xsd:string'),
198 'tva_intra' => array('name'=>'tva_intra', 'type'=>'xsd:string'),
199 // For user
200 'login' => array('name'=>'login', 'type'=>'xsd:string'),
201 'password' => array('name'=>'password', 'type'=>'xsd:string'),
202 'group_id' => array('name'=>'group_id', 'type'=>'xsd:string')
203);
204
205$elementtype = 'socpeople';
206
207//Retrieve all extrafield for contact
208// fetch optionals attributes and labels
209$extrafields = new ExtraFields($db);
210$extrafields->fetch_name_optionals_label($elementtype, true);
211$extrafield_array = null;
212if (is_array($extrafields) && count($extrafields) > 0) {
213 $extrafield_array = array();
214}
215if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
216 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
217 $type = $extrafields->attributes[$elementtype]['type'][$key];
218 if ($type == 'date' || $type == 'datetime') {
219 $type = 'xsd:dateTime';
220 } else {
221 $type = 'xsd:string';
222 }
223
224 $extrafield_array['contact_options_'.$key] = array('name'=>'contact_options_'.$key, 'type'=>$type);
225 }
226}
227
228if (is_array($extrafield_array)) {
229 $thirdpartywithuser_fields = array_merge($thirdpartywithuser_fields, $extrafield_array);
230}
231
232
233$server->wsdl->addComplexType(
234 'thirdpartywithuser',
235 'complexType',
236 'struct',
237 'all',
238 '',
239 $thirdpartywithuser_fields
240);
241
242// Define WSDL user short object
243$server->wsdl->addComplexType(
244 'shortuser',
245 'complexType',
246 'struct',
247 'all',
248 '',
249 array(
250 'login' => array('name'=>'login', 'type'=>'xsd:string'),
251 'password' => array('name'=>'password', 'type'=>'xsd:string'),
252 'entity' => array('name'=>'entity', 'type'=>'xsd:string'),
253 )
254);
255
256
257
258// 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
259// Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
260// http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
261$styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
262$styleuse = 'encoded'; // encoded/literal/literal wrapped
263// Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
264
265
266// Register WSDL
267$server->register(
268 'getUser',
269 // Entry values
270 array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'ref'=>'xsd:string', 'ref_ext'=>'xsd:string'),
271 // Exit values
272 array('result'=>'tns:result', 'user'=>'tns:user'),
273 $ns,
274 $ns.'#getUser',
275 $styledoc,
276 $styleuse,
277 'WS to get user'
278);
279
280$server->register(
281 'getListOfGroups',
282 // Entry values
283 array('authentication'=>'tns:authentication'),
284 // Exit values
285 array('result'=>'tns:result', 'groups'=>'tns:GroupsArray'),
286 $ns,
287 $ns.'#getListOfGroups',
288 $styledoc,
289 $styleuse,
290 'WS to get list of groups'
291);
292
293$server->register(
294 'createUserFromThirdparty',
295 // Entry values
296 array('authentication'=>'tns:authentication', 'thirdpartywithuser'=>'tns:thirdpartywithuser'),
297 // Exit values
298 array('result'=>'tns:result', 'id'=>'xsd:string'),
299 $ns,
300 $ns.'#createUserFromThirdparty',
301 $styledoc,
302 $styleuse,
303 'WS to create an external user with thirdparty and contact'
304);
305
306$server->register(
307 'setUserPassword',
308 // Entry values
309 array('authentication'=>'tns:authentication', 'shortuser'=>'tns:shortuser'),
310 // Exit values
311 array('result'=>'tns:result', 'id'=>'xsd:string'),
312 $ns,
313 $ns.'#setUserPassword',
314 $styledoc,
315 $styleuse,
316 'WS to change password of an user'
317);
318
319
320
321
331function getUser($authentication, $id, $ref = '', $ref_ext = '')
332{
333 global $db, $conf;
334
335 dol_syslog("Function: getUser login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
336
337 if ($authentication['entity']) {
338 $conf->entity = $authentication['entity'];
339 }
340
341 // Init and check authentication
342 $objectresp = array();
343 $errorcode = ''; $errorlabel = '';
344 $error = 0;
345 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
346 // Check parameters
347 if (!$error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext))) {
348 $error++;
349 $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
350 }
351
352 if (!$error) {
353 $fuser->getrights();
354
355 if ($fuser->rights->user->user->lire
356 || ($fuser->rights->user->self->creer && $id && $id == $fuser->id)
357 || ($fuser->rights->user->self->creer && $ref && $ref == $fuser->login)
358 || ($fuser->rights->user->self->creer && $ref_ext && $ref_ext == $fuser->ref_ext)) {
359 $user = new User($db);
360 $result = $user->fetch($id, $ref, $ref_ext);
361 if ($result > 0) {
362 // Create
363 $objectresp = array(
364 'result'=>array('result_code'=>'OK', 'result_label'=>''),
365 'user'=>array(
366 'id' => $user->id,
367 'lastname' => $user->lastname,
368 'firstname' => $user->firstname,
369 'note' => $user->note,
370 'email' => $user->email,
371 'signature' => $user->signature,
372 'office_phone' => $user->office_phone,
373 'office_fax' => $user->office_fax,
374 'user_mobile' => $user->user_mobile,
375 'admin' => $user->admin,
376 'login' => $user->login,
377 'entity' => $user->entity,
378 'pass_indatabase' => $user->pass_indatabase,
379 'pass_indatabase_crypted' => $user->pass_indatabase_crypted,
380 'datec' => dol_print_date($user->datec, 'dayhourrfc'),
381 'datem' => dol_print_date($user->datem, 'dayhourrfc'),
382 'fk_thirdparty' => $user->socid,
383 'fk_contact' => $user->contact_id,
384 'fk_member' => $user->fk_member,
385 'datelastlogin' => dol_print_date($user->datelastlogin, 'dayhourrfc'),
386 'datepreviouslogin' => dol_print_date($user->datepreviouslogin, 'dayhourrfc'),
387 'statut' => $user->statut,
388 'photo' => $user->photo,
389 'lang' => $user->lang,
390 //'rights' => $user->rights,
391 'canvas' => $user->canvas
392 )
393 );
394 } else {
395 $error++;
396 $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
397 }
398 } else {
399 $error++;
400 $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
401 }
402 }
403
404 if ($error) {
405 $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
406 }
407
408 return $objectresp;
409}
410
417function getListOfGroups($authentication)
418{
419 global $db, $conf, $user;
420
421 dol_syslog("Function: getListOfGroups login=".$authentication['login']);
422
423 if ($authentication['entity']) {
424 $conf->entity = $authentication['entity'];
425 }
426
427 // Init and check authentication
428 $objectresp = array();
429 $arraygroups = array();
430 $errorcode = ''; $errorlabel = '';
431 $error = 0;
432 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
433 // Check parameters
434
435 if (!$error) {
436 $sql = "SELECT g.rowid, g.nom as name, g.entity, g.datec, COUNT(DISTINCT ugu.fk_user) as nb";
437 $sql .= " FROM ".MAIN_DB_PREFIX."usergroup as g";
438 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."usergroup_user as ugu ON ugu.fk_usergroup = g.rowid";
439 if (isModEnabled('multicompany') && $conf->entity == 1 && (getDolGlobalString('MULTICOMPANY_TRANSVERSE_MODE') || ($user->admin && !$user->entity))) {
440 $sql .= " WHERE g.entity IS NOT NULL";
441 } else {
442 $sql .= " WHERE g.entity IN (0,".$conf->entity.")";
443 }
444 $sql .= " GROUP BY g.rowid, g.nom, g.entity, g.datec";
445 $resql = $db->query($sql);
446 if ($resql) {
447 $num = $db->num_rows($resql);
448
449 $i = 0;
450 while ($i < $num) {
451 $obj = $db->fetch_object($resql);
452 $arraygroups[] = array('id'=>$obj->rowid, 'name'=>$obj->name, 'datec'=>$obj->datec, 'nb'=>$obj->nb);
453 $i++;
454 }
455 } else {
456 $error++;
457 $errorcode = $db->lasterrno();
458 $errorlabel = $db->lasterror();
459 }
460 }
461
462 if ($error) {
463 $objectresp = array(
464 'result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel),
465 'groups'=>$arraygroups
466 );
467 } else {
468 $objectresp = array(
469 'result'=>array('result_code' => 'OK', 'result_label' => ''),
470 'groups'=>$arraygroups
471 );
472 }
473
474 return $objectresp;
475}
476
477
485function createUserFromThirdparty($authentication, $thirdpartywithuser)
486{
487 global $db, $conf, $langs;
488
489 dol_syslog("Function: createUserFromThirdparty login=".$authentication['login']);
490
491 if ($authentication['entity']) {
492 $conf->entity = $authentication['entity'];
493 }
494
495 $objectresp = array();
496 $errorcode = ''; $errorlabel = '';
497 $error = 0;
498
499 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
500
501 if ($fuser->socid) {
502 $socid = $fuser->socid;
503 }
504
505 if (!$error && !$thirdpartywithuser) {
506 $error++;
507 $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter thirdparty must be provided.";
508 }
509
510 if (!$error) {
511 $fuser->getrights();
512
513 if ($fuser->rights->societe->creer) {
514 $thirdparty = new Societe($db);
515
516 // If a contact / company already exists with the email, return the corresponding socid
517 $sql = "SELECT s.rowid as socid FROM ".MAIN_DB_PREFIX."societe as s";
518 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople as sp ON sp.fk_soc = s.rowid";
519 $sql .= " WHERE s.entity=".$conf->entity;
520 $sql .= " AND s.email='".$db->escape($thirdpartywithuser['email'])."'";
521 $sql .= " OR sp.email='".$db->escape($thirdpartywithuser['email'])."'";
522 $sql .= $db->plimit(1);
523
524 $resql = $db->query($sql);
525 if ($resql) {
526 // If a company or contact is found with the same email we return an error
527 $row = $db->fetch_object($resql);
528 if ($row) {
529 $error++;
530 $errorcode = 'ALREADY_EXIST'; $errorlabel = 'Object not create : company or contact exists '.$thirdpartywithuser['email'];
531 } else {
532 $db->begin();
533 /*
534 * Company creation
535 */
536 $thirdparty->name = $thirdpartywithuser['name_thirdparty'];
537 $thirdparty->ref_ext = $thirdpartywithuser['ref_ext'];
538 $thirdparty->address = $thirdpartywithuser['address'];
539 $thirdparty->zip = $thirdpartywithuser['zip'];
540 $thirdparty->town = $thirdpartywithuser['town'];
541 $thirdparty->country_id = $thirdpartywithuser['country_id'];
542 $thirdparty->country_code = $thirdpartywithuser['country_code'];
543
544 // find the country id by code
545 $langs->load("dict");
546
547 $sql = "SELECT rowid";
548 $sql .= " FROM ".MAIN_DB_PREFIX."c_country";
549 $sql .= " WHERE active = 1";
550 $sql .= " AND code='".$db->escape($thirdparty->country_code)."'";
551
552 $resql = $db->query($sql);
553 if ($resql) {
554 $num = $db->num_rows($resql);
555 if ($num) {
556 $obj = $db->fetch_object($resql);
557 $thirdparty->country_id = $obj->rowid;
558 }
559 }
560 $thirdparty->phone = $thirdpartywithuser['phone'];
561 $thirdparty->fax = $thirdpartywithuser['fax'];
562 $thirdparty->email = $thirdpartywithuser['email'];
563 $thirdparty->url = $thirdpartywithuser['url'];
564 $thirdparty->ape = $thirdpartywithuser['ape'];
565 $thirdparty->idprof1 = $thirdpartywithuser['prof1'];
566 $thirdparty->idprof2 = $thirdpartywithuser['prof2'];
567 $thirdparty->idprof3 = $thirdpartywithuser['prof3'];
568 $thirdparty->idprof4 = $thirdpartywithuser['prof4'];
569 $thirdparty->idprof5 = $thirdpartywithuser['prof5'];
570 $thirdparty->idprof6 = $thirdpartywithuser['prof6'];
571
572 $thirdparty->client = $thirdpartywithuser['client'];
573 $thirdparty->fournisseur = $thirdpartywithuser['fournisseur'];
574
575 $socid_return = $thirdparty->create($fuser);
576
577 if ($socid_return > 0) {
578 $thirdparty->fetch($socid_return);
579
580 /*
581 * Contact creation
582 *
583 */
584 $contact = new Contact($db);
585 $contact->socid = $thirdparty->id;
586 $contact->lastname = $thirdpartywithuser['name'];
587 $contact->firstname = $thirdpartywithuser['firstname'];
588 $contact->civility_id = $thirdparty->civility_id;
589 $contact->address = $thirdparty->address;
590 $contact->zip = $thirdparty->zip;
591 $contact->town = $thirdparty->town;
592 $contact->email = $thirdparty->email;
593 $contact->phone_pro = $thirdparty->phone;
594 $contact->phone_mobile = $thirdpartywithuser['phone_mobile'];
595 $contact->fax = $thirdparty->fax;
596 $contact->statut = 1;
597 $contact->country_id = $thirdparty->country_id;
598 $contact->country_code = $thirdparty->country_code;
599
600 $elementtype = 'socpeople';
601
602 //Retrieve all extrafield for thirdsparty
603 // fetch optionals attributes and labels
604 $extrafields = new ExtraFields($db);
605 $extrafields->fetch_name_optionals_label($elementtype, true);
606 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
607 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
608 $key = 'contact_options_'.$key;
609 $key = substr($key, 8); // Remove 'contact_' prefix
610 $contact->array_options[$key] = $thirdpartywithuser[$key];
611 }
612 }
613
614 $contact_id = $contact->create($fuser);
615
616 if ($contact_id > 0) {
617 /*
618 * User creation
619 *
620 */
621 $edituser = new User($db);
622
623 $id = $edituser->create_from_contact($contact, $thirdpartywithuser["login"]);
624 if ($id > 0) {
625 $edituser->setPassword($fuser, trim($thirdpartywithuser['password']));
626
627 if ($thirdpartywithuser['group_id'] > 0) {
628 $edituser->SetInGroup($thirdpartywithuser['group_id'], $conf->entity);
629 }
630 } else {
631 $error++;
632 $errorcode = 'NOT_CREATE'; $errorlabel = 'Object not create : '.$edituser->error;
633 }
634 } else {
635 $error++;
636 $errorcode = 'NOT_CREATE'; $errorlabel = 'Object not create : '.$contact->error;
637 }
638
639 if (!$error) {
640 $db->commit();
641 $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>'SUCCESS'), 'id'=>$socid_return);
642 $error = 0;
643 }
644 } else {
645 $error++;
646 $errorcode = join(', ', ($thirdparty->error ? array($thirdparty->error) : $thirdparty->errors));
647 }
648 }
649 } else {
650 // retour creation KO
651 $error++;
652 $errorcode = 'NOT_CREATE'; $errorlabel = 'Object not create';
653 }
654 } else {
655 $error++;
656 $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
657 }
658 }
659
660 if ($error) {
661 $db->rollback();
662 $objectresp = array(
663 'result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel)
664 );
665 }
666
667 return $objectresp;
668}
669
670
678function setUserPassword($authentication, $shortuser)
679{
680
681 global $db, $conf;
682
683 dol_syslog("Function: setUserPassword login=".$authentication['login']);
684
685 if ($authentication['entity']) {
686 $conf->entity = $authentication['entity'];
687 }
688
689 $objectresp = array();
690 $errorcode = ''; $errorlabel = '';
691 $error = 0;
692
693 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
694
695 if ($fuser->socid) {
696 $socid = $fuser->socid;
697 }
698
699 if (!$error && !$shortuser) {
700 $error++;
701 $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter shortuser must be provided.";
702 }
703
704 if (!$error) {
705 $fuser->getrights();
706
707 if ($fuser->rights->user->user->password || $fuser->rights->user->self->password) {
708 $userstat = new User($db);
709 $res = $userstat->fetch('', $shortuser['login']);
710 if ($res) {
711 $res = $userstat->setPassword($userstat, $shortuser['password']);
712 if (is_int($res) && $res < 0) {
713 $error++;
714 $errorcode = 'NOT_MODIFIED'; $errorlabel = 'Error when changing password';
715 } else {
716 $objectresp = array(
717 'result'=>array('result_code' => 'OK', 'result_label' => ''),
718 );
719 }
720 } else {
721 $error++;
722 $errorcode = 'NOT_FOUND'; $errorlabel = 'User not found';
723 }
724 } else {
725 $error++;
726 $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
727 }
728 }
729
730
731 if ($error) {
732 $objectresp = array(
733 'result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel)
734 );
735 }
736
737 return $objectresp;
738}
739
740// Return the results.
741$server->service(file_get_contents("php://input"));
Class to manage contact/addresses.
Class to manage standard extra fields.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage Dolibarr users.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getUser($authentication, $id, $ref='', $ref_ext='')
Get produt or service.
getListOfGroups($authentication)
getListOfGroups
setUserPassword($authentication, $shortuser)
Set password of an user.
createUserFromThirdparty($authentication, $thirdpartywithuser)
Create an external user with thirdparty and contact.
check_authentication($authentication, &$error, &$errorcode, &$errorlabel)
Check authentication array and set error, errorcode, errorlabel.
Definition ws.lib.php:35