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