Passed
Pull Request — master (#2)
by
unknown
26:19
created

getUser()   D

Complexity

Conditions 22
Paths 32

Size

Total Lines 85
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 22
eloc 56
nc 32
nop 4
dl 0
loc 85
rs 4.1666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* Copyright (C) 2006-2016 Laurent Destailleur  <[email protected]>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
/**
19
 *       \file       htdocs/webservices/server_user.php
20
 *       \brief      File that is entry point to call Dolibarr WebServices
21
 */
22
23
if (! defined("NOCSRFCHECK"))    define("NOCSRFCHECK",'1');
24
25
require_once '../master.inc.php';
26
require_once NUSOAP_PATH.'/nusoap.php';		// Include SOAP
27
require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
28
require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
29
require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
30
require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
31
require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
32
33
34
dol_syslog("Call User webservices interfaces");
35
36
$langs->load("main");
37
38
// Enable and test if module web services is enabled
39
if (empty($conf->global->MAIN_MODULE_WEBSERVICES))
40
{
41
    $langs->load("admin");
42
    dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
43
    print $langs->trans("WarningModuleNotActive",'WebServices').'.<br><br>';
44
    print $langs->trans("ToActivateModule");
45
    exit;
46
}
47
48
// Create the soap Object
49
$server = new nusoap_server();
50
$server->soap_defencoding='UTF-8';
51
$server->decode_utf8=false;
52
$ns='http://www.dolibarr.org/ns/';
53
$server->configureWSDL('WebServicesDolibarrUser',$ns);
54
$server->wsdl->schemaTargetNamespace=$ns;
55
56
57
// Define WSDL Authentication object
58
$server->wsdl->addComplexType(
59
    'authentication',
60
    'complexType',
61
    'struct',
62
    'all',
63
    '',
64
    array(
65
        'dolibarrkey' => array('name'=>'dolibarrkey','type'=>'xsd:string'),
66
    	'sourceapplication' => array('name'=>'sourceapplication','type'=>'xsd:string'),
67
    	'login' => array('name'=>'login','type'=>'xsd:string'),
68
    	'password' => array('name'=>'password','type'=>'xsd:string'),
69
        'entity' => array('name'=>'entity','type'=>'xsd:string'),
70
    )
71
);
72
73
// Define WSDL Return object
74
$server->wsdl->addComplexType(
75
    'result',
76
    'complexType',
77
    'struct',
78
    'all',
79
    '',
80
    array(
81
        'result_code' => array('name'=>'result_code','type'=>'xsd:string'),
82
        'result_label' => array('name'=>'result_label','type'=>'xsd:string'),
83
    )
84
);
85
86
// Define other specific objects
87
$server->wsdl->addComplexType(
88
    'user',
89
    'complexType',
90
    'struct',
91
    'all',
92
    '',
93
    array(
94
        'element' => array('name'=>'element','type'=>'xsd:string'),
95
        'id' => array('name'=>'id','type'=>'xsd:string'),
96
        'lastname' => array('name'=>'lastname','type'=>'xsd:string'),
97
        'firstname' => array('name'=>'firstname','type'=>'xsd:string'),
98
        'note' => array('name'=>'note','type'=>'xsd:string'),
99
        'email' => array('name'=>'email','type'=>'xsd:string'),
100
        'signature' => array('name'=>'signature','type'=>'xsd:string'),
101
        'office_phone' => array('name'=>'office_phone','type'=>'xsd:string'),
102
        'office_fax' => array('name'=>'office_fax','type'=>'xsd:string'),
103
        'user_mobile' => array('name'=>'user_mobile','type'=>'xsd:string'),
104
        'admin' => array('name'=>'admin','type'=>'xsd:string'),
105
        'login' => array('name'=>'login','type'=>'xsd:string'),
106
        'entity' => array('name'=>'entity','type'=>'xsd:string'),
107
        'pass_indatabase' => array('name'=>'pass_indatabase','type'=>'xsd:string'),
108
        'pass_indatabase_crypted' => array('name'=>'pass_indatabase_crypted','type'=>'xsd:string'),
109
        'datec' => array('name'=>'datec','type'=>'xsd:dateTime'),
110
        'datem' => array('name'=>'datem','type'=>'xsd:dateTime'),
111
        'fk_thirdparty' => array('name'=>'fk_thirdparty','type'=>'xsd:string'),
112
        'fk_contact' => array('name'=>'fk_contact','type'=>'xsd:string'),
113
        'fk_member' => array('name'=>'fk_member','type'=>'xsd:string'),
114
        'datelastlogin' => array('name'=>'datelastlogin','type'=>'xsd:dateTime'),
115
        'datepreviouslogin' => array('name'=>'datepreviouslogin','type'=>'xsd:dateTime'),
116
        'statut' => array('name'=>'statut','type'=>'xsd:string'),
117
        'photo' => array('name'=>'photo','type'=>'xsd:string'),
118
        'lang' => array('name'=>'lang','type'=>'xsd:string'),
119
        'entrepots' => array('name'=>'entrepots','type'=>'xsd:string'),
120
        //'rights' => array('name'=>'rights','type'=>'xsd:string'),
121
        'canvas' => array('name'=>'canvas','type'=>'xsd:string')
122
    )
123
);
124
125
// Define other specific objects
126
$server->wsdl->addComplexType(
127
	'group',
128
	'complexType',
129
	'struct',
130
	'all',
131
	'',
132
	array(
133
	'name' => array('name'=>'name','type'=>'xsd:string'),
134
	'id' => array('name'=>'id','type'=>'xsd:string'),
135
	'datec' => array('name'=>'datec','type'=>'xsd:string'),
136
	'nb' => array('name'=>'nb','type'=>'xsd:string')
137
	)
138
);
139
140
$server->wsdl->addComplexType(
141
	'GroupsArray',
142
	'complexType',
143
	'array',
144
	'',
145
	'SOAP-ENC:Array',
146
	array(),
147
	array(
148
	array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:group[]')
149
	),
150
	'tns:group'
151
);
152
153
$thirdpartywithuser_fields = array(
154
	// For thirdparty and contact
155
	'name' => array('name'=>'name','type'=>'xsd:string'),
156
	'firstname' => array('name'=>'firstname','type'=>'xsd:string'),
157
	'name_thirdparty' => array('name'=>'name_thirdparty','type'=>'xsd:string'),
158
	'ref_ext' => array('name'=>'ref_ext','type'=>'xsd:string'),
159
	'client' => array('name'=>'client','type'=>'xsd:string'),
160
	'fournisseur' => array('name'=>'fournisseur','type'=>'xsd:string'),
161
	'address' => array('name'=>'address','type'=>'xsd:string'),
162
	'zip' => array('name'=>'zip','type'=>'xsd:string'),
163
	'town' => array('name'=>'town','type'=>'xsd:string'),
164
	'country_id' => array('name'=>'country_id','type'=>'xsd:string'),
165
	'country_code' => array('name'=>'country_code','type'=>'xsd:string'),
166
	'phone' => array('name'=>'phone','type'=>'xsd:string'),
167
	'phone_mobile' => array('name'=>'phone_mobile','type'=>'xsd:string'),
168
	'fax' => array('name'=>'fax','type'=>'xsd:string'),
169
	'email' => array('name'=>'email','type'=>'xsd:string'),
170
	'url' => array('name'=>'url','type'=>'xsd:string'),
171
	'profid1' => array('name'=>'profid1','type'=>'xsd:string'),
172
	'profid2' => array('name'=>'profid2','type'=>'xsd:string'),
173
	'profid3' => array('name'=>'profid3','type'=>'xsd:string'),
174
	'profid4' => array('name'=>'profid4','type'=>'xsd:string'),
175
	'profid5' => array('name'=>'profid5','type'=>'xsd:string'),
176
	'profid6' => array('name'=>'profid6','type'=>'xsd:string'),
177
	'capital' => array('name'=>'capital','type'=>'xsd:string'),
178
	'tva_assuj' => array('name'=>'tva_assuj','type'=>'xsd:string'),
179
	'tva_intra' => array('name'=>'tva_intra','type'=>'xsd:string'),
180
	// 	For user
181
	'login' => array('name'=>'login','type'=>'xsd:string'),
182
	'password' => array('name'=>'password','type'=>'xsd:string'),
183
	'group_id' => array('name'=>'group_id','type'=>'xsd:string')
184
);
185
186
//Retreive all extrafield for contact
187
// fetch optionals attributes and labels
188
$extrafields=new ExtraFields($db);
189
$extralabels=$extrafields->fetch_name_optionals_label('socpeople',true);
190
$extrafield_array=null;
191
if (is_array($extrafields) && count($extrafields)>0) {
192
	$extrafield_array = array();
193
}
194
foreach($extrafields->attribute_label as $key=>$label)
195
{
196
	$type =$extrafields->attribute_type[$key];
197
	if ($type=='date' || $type=='datetime') {$type='xsd:dateTime';}
198
	else {$type='xsd:string';}
199
200
	$extrafield_array['contact_options_'.$key]=array('name'=>'contact_options_'.$key,'type'=>$type);
201
}
202
203
if (is_array($extrafield_array)) $thirdpartywithuser_fields=array_merge($thirdpartywithuser_fields,$extrafield_array);
204
205
206
$server->wsdl->addComplexType(
207
	'thirdpartywithuser',
208
	'complexType',
209
	'struct',
210
	'all',
211
	'',
212
	$thirdpartywithuser_fields
213
);
214
215
// Define WSDL user short object
216
$server->wsdl->addComplexType(
217
	'shortuser',
218
	'complexType',
219
	'struct',
220
	'all',
221
	'',
222
	array(
223
	'login' => array('name'=>'login','type'=>'xsd:string'),
224
	'password' => array('name'=>'password','type'=>'xsd:string'),
225
	'entity' => array('name'=>'entity','type'=>'xsd:string'),
226
	)
227
);
228
229
230
231
// 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
232
// Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
233
// http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
234
$styledoc='rpc';       // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
235
$styleuse='encoded';   // encoded/literal/literal wrapped
236
// Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
237
238
239
// Register WSDL
240
$server->register(
241
    'getUser',
242
    // Entry values
243
    array('authentication'=>'tns:authentication','id'=>'xsd:string','ref'=>'xsd:string','ref_ext'=>'xsd:string'),
244
    // Exit values
245
    array('result'=>'tns:result','user'=>'tns:user'),
246
    $ns,
247
    $ns.'#getUser',
248
    $styledoc,
249
    $styleuse,
250
    'WS to get user'
251
);
252
253
$server->register(
254
	'getListOfGroups',
255
	// Entry values
256
	array('authentication'=>'tns:authentication'),
257
	// Exit values
258
	array('result'=>'tns:result','groups'=>'tns:GroupsArray'),
259
	$ns,
260
	$ns.'#getListOfGroups',
261
	$styledoc,
262
	$styleuse,
263
	'WS to get list of groups'
264
);
265
266
$server->register(
267
	'createUserFromThirdparty',
268
	// Entry values
269
	array('authentication'=>'tns:authentication','thirdpartywithuser'=>'tns:thirdpartywithuser'),
270
	// Exit values
271
	array('result'=>'tns:result','id'=>'xsd:string'),
272
	$ns,
273
	$ns.'#createUserFromThirdparty',
274
	$styledoc,
275
	$styleuse,
276
	'WS to create an external user with thirdparty and contact'
277
);
278
279
$server->register(
280
	'setUserPassword',
281
	// Entry values
282
	array('authentication'=>'tns:authentication','shortuser'=>'tns:shortuser'),
283
	// Exit values
284
	array('result'=>'tns:result','id'=>'xsd:string'),
285
	$ns,
286
	$ns.'#setUserPassword',
287
	$styledoc,
288
	$styleuse,
289
	'WS to change password of an user'
290
);
291
292
293
294
295
/**
296
 * Get produt or service
297
 *
298
 * @param	array		$authentication		Array of authentication information
299
 * @param	int			$id					Id of object
300
 * @param	string		$ref				Ref of object
301
 * @param	string		$ref_ext			Ref external of object
302
 * @return	mixed
303
 */
304
function getUser($authentication,$id,$ref='',$ref_ext='')
305
{
306
    global $db,$conf,$langs;
307
308
    dol_syslog("Function: getUser login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
309
310
    if ($authentication['entity']) $conf->entity=$authentication['entity'];
311
312
    // Init and check authentication
313
    $objectresp=array();
314
    $errorcode='';$errorlabel='';
315
    $error=0;
316
    $fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
317
    // Check parameters
318
    if (! $error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext)))
319
    {
320
        $error++;
321
        $errorcode='BAD_PARAMETERS'; $errorlabel="Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
322
    }
323
324
    if (! $error)
325
    {
326
        $fuser->getrights();
327
328
        if ($fuser->rights->user->user->lire
329
        	|| ($fuser->rights->user->self->creer && $id && $id==$fuser->id)
330
        	|| ($fuser->rights->user->self->creer && $ref && $ref==$fuser->login)
331
        	|| ($fuser->rights->user->self->creer && $ref_ext && $ref_ext==$fuser->ref_ext))
332
        {
333
            $user=new User($db);
334
            $result=$user->fetch($id,$ref,$ref_ext);
335
            if ($result > 0)
336
            {
337
                // Create
338
                $objectresp = array(
339
			    	'result'=>array('result_code'=>'OK', 'result_label'=>''),
340
			        'user'=>array(
341
						'id' => $user->id,
342
						'lastname' => $user->lastname,
343
						'firstname' => $user->firstname,
344
						'note' => $user->note,
345
						'email' => $user->email,
346
						'signature' => $user->signature,
347
						'office_phone' => $user->office_phone,
348
						'office_fax' => $user->office_fax,
349
						'user_mobile' => $user->user_mobile,
350
						'admin' => $user->admin,
351
						'login' => $user->login,
352
						'entity' => $user->entity,
353
						'pass_indatabase' => $user->pass_indatabase,
354
						'pass_indatabase_crypted' => $user->pass_indatabase_crypted,
355
						'datec' => dol_print_date($user->datec,'dayhourrfc'),
356
						'datem' => dol_print_date($user->datem,'dayhourrfc'),
357
						'fk_thirdparty' => $user->societe_id,
358
						'fk_contact' => $user->contact_id,
359
						'fk_member' => $user->fk_member,
360
						'datelastlogin' => dol_print_date($user->datelastlogin,'dayhourrfc'),
361
						'datepreviouslogin' => dol_print_date($user->datepreviouslogin,'dayhourrfc'),
362
						'statut' => $user->statut,
363
						'photo' => $user->photo,
364
						'lang' => $user->lang,
365
						//'rights' => $user->rights,
366
						'canvas' => $user->canvas
367
                    )
368
                );
369
            }
370
            else
371
            {
372
                $error++;
373
                $errorcode='NOT_FOUND'; $errorlabel='Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
374
            }
375
        }
376
        else
377
        {
378
            $error++;
379
            $errorcode='PERMISSION_DENIED'; $errorlabel='User does not have permission for this request';
380
        }
381
    }
382
383
    if ($error)
384
    {
385
        $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
386
    }
387
388
    return $objectresp;
389
}
390
391
/**
392
 * getListOfGroups
393
 *
394
 * @param	array		$authentication		Array of authentication information
395
 * @return	array							Array result
396
 */
397
function getListOfGroups($authentication)
398
{
399
	global $db,$conf,$langs;
400
401
	$now=dol_now();
402
403
	dol_syslog("Function: getListOfGroups login=".$authentication['login']);
404
405
	if ($authentication['entity']) $conf->entity=$authentication['entity'];
406
407
	// Init and check authentication
408
	$objectresp=array();
409
	$arraygroups=array();
410
	$errorcode='';$errorlabel='';
411
	$error=0;
412
	$fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
413
	// Check parameters
414
415
	if (! $error)
416
	{
417
		$sql = "SELECT g.rowid, g.nom as name, g.entity, g.datec, COUNT(DISTINCT ugu.fk_user) as nb";
418
		$sql.= " FROM ".MAIN_DB_PREFIX."usergroup as g";
419
		$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."usergroup_user as ugu ON ugu.fk_usergroup = g.rowid";
420
		if (! empty($conf->multicompany->enabled) && $conf->entity == 1 && ($conf->global->MULTICOMPANY_TRANSVERSE_MODE || ($user->admin && ! $user->entity)))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user does not exist. Did you maybe mean $fuser?
Loading history...
421
		{
422
			$sql.= " WHERE g.entity IS NOT NULL";
423
		}
424
		else
425
		{
426
			$sql.= " WHERE g.entity IN (0,".$conf->entity.")";
427
		}
428
		$sql.= " GROUP BY g.rowid, g.nom, g.entity, g.datec";
429
		$resql=$db->query($sql);
430
		if ($resql)
431
		{
432
			$num=$db->num_rows($resql);
433
434
			$i=0;
435
			while ($i < $num)
436
			{
437
				$obj=$db->fetch_object($resql);
438
				$arraygroups[]=array('id'=>$obj->rowid,'name'=>$obj->name,'datec'=>$obj->datec,'nb'=>$obj->nb);
439
				$i++;
440
			}
441
		}
442
		else
443
		{
444
			$error++;
445
			$errorcode=$db->lasterrno();
446
			$errorlabel=$db->lasterror();
447
		}
448
	}
449
450
	if ($error)
451
	{
452
		$objectresp = array(
453
		'result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel),
454
		'groups'=>$arraygroups
455
		);
456
	}
457
	else
458
	{
459
		$objectresp = array(
460
		'result'=>array('result_code' => 'OK', 'result_label' => ''),
461
		'groups'=>$arraygroups
462
		);
463
	}
464
465
	return $objectresp;
466
}
467
468
469
/**
470
 * Create an external user with thirdparty and contact
471
 *
472
 * @param	array		$authentication		Array of authentication information
473
 * @param	array		$thirdpartywithuser Datas
474
 * @return	mixed
475
 */
476
function createUserFromThirdparty($authentication,$thirdpartywithuser)
477
{
478
	global $db,$conf,$langs;
479
480
	dol_syslog("Function: createUserFromThirdparty login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ref_ext seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $id seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $ref seems to be never defined.
Loading history...
481
482
	if ($authentication['entity']) $conf->entity=$authentication['entity'];
483
484
	$objectresp=array();
485
	$errorcode='';$errorlabel='';
486
	$error=0;
487
488
	$fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
489
490
	if ($fuser->societe_id) $socid=$fuser->societe_id;
491
492
	if (! $error && ! $thirdpartywithuser)
0 ignored issues
show
Bug Best Practice introduced by
The expression $thirdpartywithuser of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
493
	{
494
		$error++;
495
		$errorcode='BAD_PARAMETERS'; $errorlabel="Parameter thirdparty must be provided.";
496
	}
497
498
	if (! $error)
499
	{
500
		$fuser->getrights();
501
502
		if ($fuser->rights->societe->creer)
503
		{
504
			$thirdparty=new Societe($db);
505
506
			// If a contact / company already exists with the email, return the corresponding socid
507
			$sql = "SELECT s.rowid as societe_id FROM ".MAIN_DB_PREFIX."societe as s";
508
			$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople as sp ON sp.fk_soc = s.rowid";
509
			$sql.= " WHERE s.entity=".$conf->entity;
510
			$sql.= " AND s.email='".$db->escape($thirdpartywithuser['email'])."'";
511
			$sql.= " OR sp.email='".$db->escape($thirdpartywithuser['email'])."'";
512
			$sql.= $db->plimit(1);
513
514
			$resql = $db->query($sql);
515
			if ($resql)
516
			{
517
				// If a company or contact is found with the same email we return an error
518
				$row = $db->fetch_object($resql);
519
				if ($row)
520
				{
521
					$error++;
522
					$errorcode='ALREADY_EXIST'; $errorlabel='Object not create : company or contact exists '.$thirdpartywithuser['email'];
523
				}
524
				else
525
				{
526
					$db->begin();
527
					/*
528
					 * Company creation
529
					 */
530
					$thirdparty->name=$thirdpartywithuser['name_thirdparty'];
531
					$thirdparty->ref_ext=$thirdpartywithuser['ref_ext'];
532
					$thirdparty->address=$thirdpartywithuser['address'];
533
					$thirdparty->zip=$thirdpartywithuser['zip'];
534
					$thirdparty->town=$thirdpartywithuser['town'];
535
					$thirdparty->country_id=$thirdpartywithuser['country_id'];
536
					$thirdparty->country_code=$thirdpartywithuser['country_code'];
537
538
					// find the country id by code
539
					$langs->load("dict");
540
541
					$sql = "SELECT rowid";
542
					$sql.= " FROM ".MAIN_DB_PREFIX."c_country";
543
					$sql.= " WHERE active = 1";
544
					$sql.= " AND code='".$thirdparty->country_code."'";
545
546
					$resql=$db->query($sql);
547
					if ($resql)
548
					{
549
						$num = $db->num_rows($resql);
550
						if ($num)
551
						{
552
							$obj = $db->fetch_object($resql);
553
							$thirdparty->country_id      = $obj->rowid;
554
						}
555
					}
556
					$thirdparty->phone=$thirdpartywithuser['phone'];
557
					$thirdparty->fax=$thirdpartywithuser['fax'];
558
					$thirdparty->email=$thirdpartywithuser['email'];
559
					$thirdparty->url=$thirdpartywithuser['url'];
560
					$thirdparty->ape=$thirdpartywithuser['ape'];
561
					$thirdparty->idprof1=$thirdpartywithuser['prof1'];
562
					$thirdparty->idprof2=$thirdpartywithuser['prof2'];
563
					$thirdparty->idprof3=$thirdpartywithuser['prof3'];
564
					$thirdparty->idprof4=$thirdpartywithuser['prof4'];
565
					$thirdparty->idprof5=$thirdpartywithuser['prof5'];
566
					$thirdparty->idprof6=$thirdpartywithuser['prof6'];
567
568
					$thirdparty->client=$thirdpartywithuser['client'];
569
					$thirdparty->fournisseur=$thirdpartywithuser['fournisseur'];
570
571
					$socid_return=$thirdparty->create($fuser);
572
573
					if ($socid_return > 0)
574
					{
575
						$thirdparty->fetch($socid_return);
576
577
						/*
578
						 * Contact creation
579
						*
580
						*/
581
						$contact = new Contact($db);
582
						$contact->socid = $thirdparty->id;
583
						$contact->lastname = $thirdpartywithuser['name'];
584
						$contact->firstname = $thirdpartywithuser['firstname'];
585
						$contact->civility_id = $thirdparty->civility_id;
586
						$contact->address = $thirdparty->address;
587
						$contact->zip = $thirdparty->zip;
588
						$contact->town = $thirdparty->town;
589
						$contact->email = $thirdparty->email;
590
						$contact->phone_pro = $thirdparty->phone;
591
						$contact->phone_mobile = $thirdpartywithuser['phone_mobile'];
592
						$contact->fax = $thirdparty->fax;
593
						$contact->statut = 1;
594
						$contact->country_id = $thirdparty->country_id;
595
						$contact->country_code = $thirdparty->country_code;
596
597
						//Retreive all extrafield for thirdsparty
598
						// fetch optionals attributes and labels
599
						$extrafields=new ExtraFields($db);
600
						$extralabels=$extrafields->fetch_name_optionals_label('socpeople',true);
601
						foreach($extrafields->attribute_label as $key=>$label)
602
						{
603
							$key='contact_options_'.$key;
604
							$key=substr($key,8);   // Remove 'contact_' prefix
605
							$contact->array_options[$key]=$thirdpartywithuser[$key];
606
						}
607
608
						$contact_id =  $contact->create($fuser);
609
610
						if ($contact_id > 0)
611
						{
612
							/*
613
							 * User creation
614
							*
615
							*/
616
							$edituser = new User($db);
617
618
							$id = $edituser->create_from_contact($contact,$thirdpartywithuser["login"]);
619
							if ($id > 0)
620
							{
621
								$edituser->setPassword($fuser,trim($thirdpartywithuser['password']));
622
623
								if($thirdpartywithuser['group_id'] > 0 )
624
									$edituser->SetInGroup($thirdpartywithuser['group_id'],$conf->entity);
625
							}
626
							else
627
							{
628
								$error++;
629
								$errorcode='NOT_CREATE'; $errorlabel='Object not create : '.$edituser->error;
630
							}
631
						}
632
						else
633
						{
634
							$error++;
635
							$errorcode='NOT_CREATE'; $errorlabel='Object not create : '.$contact->error;
636
						}
637
638
						if(!$error) {
639
							$db->commit();
640
							$objectresp=array('result'=>array('result_code'=>'OK', 'result_label'=>'SUCCESS'),'id'=>$socid_return);
641
							$error=0;
642
						}
643
					}
644
					else
645
					{
646
						$error++;
647
						$errors=($thirdparty->error?array($thirdparty->error):$thirdparty->errors);
648
					}
649
				}
650
			}
651
			else
652
			{
653
				// retour creation KO
654
				$error++;
655
				$errorcode='NOT_CREATE'; $errorlabel='Object not create';
656
			}
657
		}
658
		else
659
		{
660
			$error++;
661
			$errorcode='PERMISSION_DENIED'; $errorlabel='User does not have permission for this request';
662
		}
663
	}
664
665
	if ($error)
666
	{
667
		$db->rollback();
668
		$objectresp = array(
669
		'result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel)
670
		);
671
	}
672
673
	return $objectresp;
674
}
675
676
677
/**
678
 * Set password of an user
679
 *
680
 * @param	array		$authentication		Array of authentication information
681
 * @param	array		$shortuser			Array of login/password info
682
 * @return	mixed
683
 */
684
function setUserPassword($authentication,$shortuser)
685
{
686
687
	global $db,$conf,$langs;
688
689
	dol_syslog("Function: setUserPassword login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ref_ext seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $ref seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $id seems to be never defined.
Loading history...
690
691
	if ($authentication['entity']) $conf->entity=$authentication['entity'];
692
693
	$objectresp=array();
694
	$errorcode='';$errorlabel='';
695
	$error=0;
696
697
	$fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
698
699
	if ($fuser->societe_id) $socid=$fuser->societe_id;
700
701
	if (! $error && ! $shortuser)
0 ignored issues
show
Bug Best Practice introduced by
The expression $shortuser of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
702
	{
703
		$error++;
704
		$errorcode='BAD_PARAMETERS'; $errorlabel="Parameter shortuser must be provided.";
705
	}
706
707
	if (! $error)
708
	{
709
		$fuser->getrights();
710
711
		if ($fuser->rights->user->user->password || $fuser->rights->user->self->password)
712
		{
713
			$userstat=new User($db);
714
			$res = $userstat->fetch('',$shortuser['login']);
715
			if($res)
716
			{
717
				$res = $userstat->setPassword($userstat,$shortuser['password']);
718
				if($res)
719
				{
720
					$objectresp = array(
721
						'result'=>array('result_code' => 'OK', 'result_label' => ''),
722
						'groups'=>$arraygroups
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $arraygroups seems to be never defined.
Loading history...
723
					);
724
				}
725
				else
726
				{
727
					$error++;
728
					$errorcode='NOT_MODIFIED'; $errorlabel='Error when changing password';
729
				}
730
			}
731
			else
732
			{
733
				$error++;
734
				$errorcode='NOT_FOUND'; $errorlabel='User not found';
735
			}
736
		}
737
		else
738
		{
739
			$error++;
740
			$errorcode='PERMISSION_DENIED'; $errorlabel='User does not have permission for this request';
741
		}
742
	}
743
744
745
	if ($error)
746
	{
747
		$objectresp = array(
748
			'result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel)
749
		);
750
	}
751
752
	return $objectresp;
753
}
754
755
// Return the results.
756
$server->service(file_get_contents("php://input"));
757