dolibarr 24.0.0-beta
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-2026 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024-2026 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' => (int) $user->statut,
397 'status' => (int) $user->status,
398 'photo' => $user->photo,
399 'lang' => $user->lang,
400 //'rights' => $user->rights,
401 'canvas' => $user->canvas
402 )
403 );
404 } else {
405 $error++;
406 $errorcode = 'NOT_FOUND';
407 $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
408 }
409 } else {
410 $error++;
411 $errorcode = 'PERMISSION_DENIED';
412 $errorlabel = 'User does not have permission for this request';
413 }
414 }
415
416 if ($error) {
417 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
418 }
419
420 return $objectresp;
421}
422
429function getListOfGroups($authentication)
430{
431 global $db, $conf, $user;
432
433 dol_syslog("Function: getListOfGroups login=".$authentication['login']);
434
435 if ($authentication['entity']) {
436 $conf->entity = $authentication['entity'];
437 }
438
439 // Init and check authentication
440 $objectresp = array();
441 $arraygroups = array();
442 $errorcode = '';
443 $errorlabel = '';
444 $error = 0;
445 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
446 // Check parameters
447
448 if (!$error) {
449 $sql = "SELECT g.rowid, g.nom as name, g.entity, g.datec, COUNT(DISTINCT ugu.fk_user) as nb";
450 $sql .= " FROM ".MAIN_DB_PREFIX."usergroup as g";
451 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."usergroup_user as ugu ON ugu.fk_usergroup = g.rowid";
452 if (isModEnabled('multicompany') && $conf->entity == 1 && (getDolGlobalString('MULTICOMPANY_TRANSVERSE_MODE') || ($user->admin && !$user->entity))) {
453 $sql .= " WHERE g.entity IS NOT NULL";
454 } else {
455 $sql .= " WHERE g.entity IN (0,".$conf->entity.")";
456 }
457 $sql .= " GROUP BY g.rowid, g.nom, g.entity, g.datec";
458 $resql = $db->query($sql);
459 if ($resql) {
460 $num = $db->num_rows($resql);
461
462 $i = 0;
463 while ($i < $num) {
464 $obj = $db->fetch_object($resql);
465 $arraygroups[] = array('id' => $obj->rowid, 'name' => $obj->name, 'datec' => $obj->datec, 'nb' => $obj->nb);
466 $i++;
467 }
468 } else {
469 $error++;
470 $errorcode = $db->lasterrno();
471 $errorlabel = $db->lasterror();
472 }
473 }
474
475 if ($error) {
476 $objectresp = array(
477 'result' => array('result_code' => $errorcode, 'result_label' => $errorlabel),
478 'groups' => $arraygroups
479 );
480 } else {
481 $objectresp = array(
482 'result' => array('result_code' => 'OK', 'result_label' => ''),
483 'groups' => $arraygroups
484 );
485 }
486
487 return $objectresp;
488}
489
490
498function createUserFromThirdparty($authentication, $thirdpartywithuser)
499{
500 global $db, $conf, $langs;
501
502 dol_syslog("Function: createUserFromThirdparty login=".$authentication['login']);
503
504 if ($authentication['entity']) {
505 $conf->entity = $authentication['entity'];
506 }
507
508 $objectresp = array();
509 $errorcode = '';
510 $errorlabel = '';
511 $error = 0;
512
513 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
514
515 if ($fuser->socid) {
516 $socid = $fuser->socid;
517 }
518
519 if (!$error && !$thirdpartywithuser) {
520 $error++;
521 $errorcode = 'BAD_PARAMETERS';
522 $errorlabel = "Parameter thirdparty must be provided.";
523 }
524
525 if (!$error) {
526 $fuser->loadRights();
527
528 if ($fuser->hasRight('societe', 'creer')) {
529 $thirdparty = new Societe($db);
530
531 // If a contact / company already exists with the email, return the corresponding socid
532 $sql = "SELECT s.rowid as socid FROM ".MAIN_DB_PREFIX."societe as s";
533 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople as sp ON sp.fk_soc = s.rowid";
534 $sql .= " WHERE s.entity=".$conf->entity;
535 $sql .= " AND s.email='".$db->escape($thirdpartywithuser['email'])."'";
536 $sql .= " OR sp.email='".$db->escape($thirdpartywithuser['email'])."'";
537 $sql .= $db->plimit(1);
538
539 $resql = $db->query($sql);
540 if ($resql) {
541 // If a company or contact is found with the same email we return an error
542 $row = $db->fetch_object($resql);
543 if ($row) {
544 $error++;
545 $errorcode = 'ALREADY_EXIST';
546 $errorlabel = 'Object not create : company or contact exists '.$thirdpartywithuser['email'];
547 } else {
548 $db->begin();
549 /*
550 * Company creation
551 */
552 $thirdparty->name = $thirdpartywithuser['name_thirdparty'];
553 $thirdparty->ref_ext = $thirdpartywithuser['ref_ext'];
554 $thirdparty->address = $thirdpartywithuser['address'];
555 $thirdparty->zip = $thirdpartywithuser['zip'];
556 $thirdparty->town = $thirdpartywithuser['town'];
557 $thirdparty->country_id = (int) $thirdpartywithuser['country_id'];
558 $thirdparty->country_code = $thirdpartywithuser['country_code'];
559
560 // find the country id by code
561 $langs->load("dict");
562
563 $sql = "SELECT rowid";
564 $sql .= " FROM ".MAIN_DB_PREFIX."c_country";
565 $sql .= " WHERE active = 1";
566 $sql .= " AND code = '".$db->escape($thirdparty->country_code)."'";
567
568 $resql = $db->query($sql);
569 if ($resql) {
570 $num = $db->num_rows($resql);
571 if ($num) {
572 $obj = $db->fetch_object($resql);
573 $thirdparty->country_id = $obj->rowid;
574 }
575 }
576 $thirdparty->phone = $thirdpartywithuser['phone'];
577 $thirdparty->fax = $thirdpartywithuser['fax'];
578 $thirdparty->email = $thirdpartywithuser['email'];
579 $thirdparty->url = $thirdpartywithuser['url'];
580 $thirdparty->idprof1 = $thirdpartywithuser['profid1'];
581 $thirdparty->idprof2 = $thirdpartywithuser['profid2'];
582 $thirdparty->idprof3 = $thirdpartywithuser['profid3'];
583 $thirdparty->idprof4 = $thirdpartywithuser['profid4'];
584 $thirdparty->idprof5 = $thirdpartywithuser['profid5'];
585 $thirdparty->idprof6 = $thirdpartywithuser['profid6'];
586
587 $thirdparty->client = (int) $thirdpartywithuser['client'];
588 $thirdparty->fournisseur = (int) $thirdpartywithuser['fournisseur'];
589
590 $socid_return = $thirdparty->create($fuser);
591
592 if ($socid_return > 0) {
593 $thirdparty->fetch($socid_return);
594
595 /*
596 * Contact creation
597 *
598 */
599 $contact = new Contact($db);
600 $contact->socid = $thirdparty->id;
601 $contact->lastname = $thirdpartywithuser['name'];
602 $contact->firstname = $thirdpartywithuser['firstname'];
603 $contact->civility_id = $thirdparty->civility_id;
604 $contact->address = $thirdparty->address;
605 $contact->zip = $thirdparty->zip;
606 $contact->town = $thirdparty->town;
607 $contact->email = $thirdparty->email;
608 $contact->phone_pro = $thirdparty->phone;
609 $contact->phone_mobile = $thirdpartywithuser['phone_mobile'];
610 $contact->fax = $thirdparty->fax;
611 $contact->status = 1;
612 $contact->statut = 1;
613 $contact->country_id = $thirdparty->country_id;
614 $contact->country_code = $thirdparty->country_code;
615
616 $elementtype = 'socpeople';
617
618 //Retrieve all extrafield for thirdparties
619 // fetch optionals attributes and labels
620 $extrafields = new ExtraFields($db);
621 $extrafields->fetch_name_optionals_label($elementtype, true);
622 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
623 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
624 $key = 'contact_options_'.$key;
625 $key = substr($key, 8); // Remove 'contact_' prefix
626 $contact->array_options[$key] = $thirdpartywithuser[$key];
627 }
628 }
629
630 $contact_id = $contact->create($fuser);
631
632 if ($contact_id > 0) {
633 /*
634 * User creation
635 *
636 */
637 $edituser = new User($db);
638
639 $id = $edituser->create_from_contact($contact, $thirdpartywithuser["login"]);
640 if ($id > 0) {
641 $edituser->setPassword($fuser, trim($thirdpartywithuser['password']));
642
643 if ($thirdpartywithuser['group_id'] > 0) {
644 $edituser->SetInGroup((int) $thirdpartywithuser['group_id'], $conf->entity);
645 }
646 } else {
647 $error++;
648 $errorcode = 'NOT_CREATE';
649 $errorlabel = 'Object not create : '.$edituser->error;
650 }
651 } else {
652 $error++;
653 $errorcode = 'NOT_CREATE';
654 $errorlabel = 'Object not create : '.$contact->error;
655 }
656
657 if (!$error) {
658 $db->commit();
659 $objectresp = array('result' => array('result_code' => 'OK', 'result_label' => 'SUCCESS'), 'id' => $socid_return);
660 }
661 } else {
662 $error++;
663 $errorcode = implode(', ', ($thirdparty->error ? array($thirdparty->error) : $thirdparty->errors));
664 }
665 }
666 } else {
667 // retour creation KO
668 $error++;
669 $errorcode = 'NOT_CREATE';
670 $errorlabel = 'Object not create';
671 }
672 } else {
673 $error++;
674 $errorcode = 'PERMISSION_DENIED';
675 $errorlabel = 'User does not have permission for this request';
676 }
677 }
678
679 if ($error) {
680 $db->rollback();
681 $objectresp = array(
682 'result' => array('result_code' => $errorcode, 'result_label' => $errorlabel)
683 );
684 }
685
686 return $objectresp;
687}
688
689
697function setUserPassword($authentication, $shortuser)
698{
699 global $db, $conf;
700
701 dol_syslog("Function: setUserPassword login=".$authentication['login']);
702
703 if ($authentication['entity']) {
704 $conf->entity = $authentication['entity'];
705 }
706
707 $objectresp = array();
708 $errorcode = '';
709 $errorlabel = '';
710 $error = 0;
711
712 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
713
714 if ($fuser->socid) {
715 $socid = $fuser->socid;
716 }
717
718 if (!$error && !$shortuser) {
719 $error++;
720 $errorcode = 'BAD_PARAMETERS';
721 $errorlabel = "Parameter shortuser must be provided.";
722 }
723
724 if (!$error) {
725 $fuser->loadRights();
726
727 if ($fuser->hasRight('user', 'user', 'password')
728 || ($fuser->hasRight('user', 'self', 'password') && $fuser->login == $shortuser['login'])) {
729 $userstat = new User($db);
730 $res = $userstat->fetch(0, $shortuser['login']);
731 if ($res) {
732 $res = $userstat->setPassword($userstat, $shortuser['password']);
733 if (is_int($res) && $res < 0) {
734 $error++;
735 $errorcode = 'NOT_MODIFIED';
736 $errorlabel = 'Error when changing password';
737 } else {
738 $objectresp = array(
739 'result' => array('result_code' => 'OK', 'result_label' => ''),
740 );
741 }
742 } else {
743 $error++;
744 $errorcode = 'NOT_FOUND';
745 $errorlabel = 'User not found';
746 }
747 } else {
748 $error++;
749 $errorcode = 'PERMISSION_DENIED';
750 $errorlabel = 'User does not have permission for this request';
751 }
752 }
753
754
755 if ($error) {
756 $objectresp = array(
757 'result' => array('result_code' => $errorcode, 'result_label' => $errorlabel)
758 );
759 }
760
761 return $objectresp;
762}
763
764// Return the results.
765$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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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 product 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