499 global $db,
$conf, $langs;
501 dol_syslog(
"Function: createUserFromThirdparty login=".$authentication[
'login']);
503 if ($authentication[
'entity']) {
504 $conf->entity = $authentication[
'entity'];
507 $objectresp = array();
515 $socid = $fuser->socid;
518 if (!$error && !$thirdpartywithuser) {
520 $errorcode =
'BAD_PARAMETERS';
521 $errorlabel =
"Parameter thirdparty must be provided.";
525 $fuser->loadRights();
527 if ($fuser->hasRight(
'societe',
'creer')) {
528 $thirdparty =
new Societe($db);
531 $sql =
"SELECT s.rowid as socid FROM ".MAIN_DB_PREFIX.
"societe as s";
532 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"socpeople as sp ON sp.fk_soc = s.rowid";
533 $sql .=
" WHERE s.entity=".((int)
$conf->entity);
534 $sql .=
" AND s.email='".$db->escape($thirdpartywithuser[
'email']).
"'";
535 $sql .=
" OR sp.email='".$db->escape($thirdpartywithuser[
'email']).
"'";
536 $sql .= $db->plimit(1);
538 $resql = $db->query($sql);
541 $row = $db->fetch_object($resql);
544 $errorcode =
'ALREADY_EXIST';
545 $errorlabel =
'Object not create : company or contact exists '.$thirdpartywithuser[
'email'];
551 $thirdparty->name = $thirdpartywithuser[
'name_thirdparty'];
552 $thirdparty->ref_ext = $thirdpartywithuser[
'ref_ext'];
553 $thirdparty->address = $thirdpartywithuser[
'address'];
554 $thirdparty->zip = $thirdpartywithuser[
'zip'];
555 $thirdparty->town = $thirdpartywithuser[
'town'];
556 $thirdparty->country_id = (int) $thirdpartywithuser[
'country_id'];
557 $thirdparty->country_code = $thirdpartywithuser[
'country_code'];
560 $langs->load(
"dict");
562 $sql =
"SELECT rowid";
563 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_country";
564 $sql .=
" WHERE active = 1";
565 $sql .=
" AND code = '".$db->escape($thirdparty->country_code).
"'";
567 $resql = $db->query($sql);
569 $num = $db->num_rows($resql);
571 $obj = $db->fetch_object($resql);
572 $thirdparty->country_id = $obj->rowid;
575 $thirdparty->phone = $thirdpartywithuser[
'phone'];
576 $thirdparty->fax = $thirdpartywithuser[
'fax'];
577 $thirdparty->email = $thirdpartywithuser[
'email'];
578 $thirdparty->url = $thirdpartywithuser[
'url'];
579 $thirdparty->idprof1 = $thirdpartywithuser[
'profid1'];
580 $thirdparty->idprof2 = $thirdpartywithuser[
'profid2'];
581 $thirdparty->idprof3 = $thirdpartywithuser[
'profid3'];
582 $thirdparty->idprof4 = $thirdpartywithuser[
'profid4'];
583 $thirdparty->idprof5 = $thirdpartywithuser[
'profid5'];
584 $thirdparty->idprof6 = $thirdpartywithuser[
'profid6'];
586 $thirdparty->client = (int) $thirdpartywithuser[
'client'];
587 $thirdparty->fournisseur = (int) $thirdpartywithuser[
'fournisseur'];
589 $socid_return = $thirdparty->create($fuser);
591 if ($socid_return > 0) {
592 $thirdparty->fetch($socid_return);
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->status = 1;
611 $contact->statut = 1;
612 $contact->country_id = $thirdparty->country_id;
613 $contact->country_code = $thirdparty->country_code;
615 $elementtype =
'socpeople';
620 $extrafields->fetch_name_optionals_label($elementtype,
true);
621 if (isset($extrafields->attributes[$elementtype][
'label']) && is_array($extrafields->attributes[$elementtype][
'label']) && count($extrafields->attributes[$elementtype][
'label'])) {
622 foreach ($extrafields->attributes[$elementtype][
'label'] as $key => $label) {
623 $key =
'contact_options_'.$key;
624 $key = substr($key, 8);
625 $contact->array_options[$key] = $thirdpartywithuser[$key];
629 $contact_id = $contact->create($fuser);
631 if ($contact_id > 0) {
636 $edituser =
new User($db);
638 $id = $edituser->create_from_contact($contact, $thirdpartywithuser[
"login"]);
640 $edituser->setPassword($fuser, trim($thirdpartywithuser[
'password']));
642 if ($thirdpartywithuser[
'group_id'] > 0) {
643 $edituser->SetInGroup((
int) $thirdpartywithuser[
'group_id'],
$conf->entity);
647 $errorcode =
'NOT_CREATE';
648 $errorlabel =
'Object not create : '.$edituser->error;
652 $errorcode =
'NOT_CREATE';
653 $errorlabel =
'Object not create : '.$contact->error;
658 $objectresp = array(
'result' => array(
'result_code' =>
'OK',
'result_label' =>
'SUCCESS'),
'id' => $socid_return);
662 $errorcode = implode(
', ', ($thirdparty->error ? array($thirdparty->error) : $thirdparty->errors));
668 $errorcode =
'NOT_CREATE';
669 $errorlabel =
'Object not create';
673 $errorcode =
'PERMISSION_DENIED';
674 $errorlabel =
'User does not have permission for this request';
681 'result' => array(
'result_code' => $errorcode,
'result_label' => $errorlabel)
700 dol_syslog(
"Function: setUserPassword login=".$authentication[
'login']);
702 if ($authentication[
'entity']) {
703 $conf->entity = $authentication[
'entity'];
706 $objectresp = array();
713 if (!$error && !$shortuser) {
715 $errorcode =
'BAD_PARAMETERS';
716 $errorlabel =
"Parameter shortuser must be provided.";
720 $fuser->loadRights();
722 if ($fuser->hasRight(
'user',
'user',
'password')
723 || ($fuser->hasRight(
'user',
'self',
'password') && $fuser->login == $shortuser[
'login'])) {
724 $userstat =
new User($db);
725 $res = $userstat->fetch(0, $shortuser[
'login']);
727 $res = $userstat->setPassword($userstat, $shortuser[
'password']);
728 if (is_int($res) && $res < 0) {
730 $errorcode =
'NOT_MODIFIED';
731 $errorlabel =
'Error when changing password';
734 'result' => array(
'result_code' =>
'OK',
'result_label' =>
''),
739 $errorcode =
'NOT_FOUND';
740 $errorlabel =
'User not found';
744 $errorcode =
'PERMISSION_DENIED';
745 $errorlabel =
'User does not have permission for this request';
752 'result' => array(
'result_code' => $errorcode,
'result_label' => $errorlabel)