server_user.php 23 KB

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