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