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