Exemplo n.º 1
0
}
if (empty($reshook)) {
    // Cancel
    if (GETPOST("cancel") && !empty($backtopage)) {
        header("Location: " . $backtopage);
        exit;
    }
    // Creation utilisateur depuis contact
    if ($action == 'confirm_create_user' && $confirm == 'yes' && $user->rights->user->user->creer) {
        // Recuperation contact actuel
        $result = $object->fetch($id);
        if ($result > 0) {
            $db->begin();
            // Creation user
            $nuser = new User($db);
            $result = $nuser->create_from_contact($object, GETPOST("login"));
            // Do not use GETPOST(alpha)
            if ($result > 0) {
                $result2 = $nuser->setPassword($user, GETPOST("password"), 0, 0, 1);
                // Do not use GETPOST(alpha)
                if ($result2) {
                    $db->commit();
                } else {
                    $error = $nuser->error;
                    $errors = $nuser->errors;
                    $db->rollback();
                }
            } else {
                $error = $nuser->error;
                $errors = $nuser->errors;
                $db->rollback();
Exemplo n.º 2
0
        exit;
    }

	// Creation utilisateur depuis contact
    if ($_POST["action"] == 'confirm_create_user' && $_POST["confirm"] == 'yes' && $user->rights->user->user->creer)
    {
        // Recuperation contact actuel
        $result = $object->fetch($_GET["id"]);

        if ($result > 0)
        {
            $db->begin();

            // Creation user
            $nuser = new User($db);
            $result=$nuser->create_from_contact($object,$_POST["login"]);

            if ($result > 0)
            {
                $result2=$nuser->setPassword($user,$_POST["password"],0,1,1);
                if ($result2)
                {
                    $db->commit();
                }
                else
                {
                    $error=$nuser->error; $errors=$nuser->errors;
                    $db->rollback();
                }
            }
            else
 /**
  *  Load data control
  *
  *  @param	string	$action    Type of action
  *  @param	int		$id			Id of object
  *	@return	void
  */
 function doActions(&$action, $id)
 {
     global $conf, $user, $langs;
     // Creation utilisateur depuis contact
     if ($action == 'confirm_create_user' && GETPOST("confirm") == 'yes') {
         // Recuperation contact actuel
         $result = $this->object->fetch($id);
         if ($result > 0) {
             $this->db->begin();
             // Creation user
             $nuser = new User($this->db);
             $result = $nuser->create_from_contact($this->object, $_POST["login"]);
             if ($result > 0) {
                 $result2 = $nuser->setPassword($user, $_POST["password"], 0, 1, 1);
                 if ($result2) {
                     $this->db->commit();
                 } else {
                     $this->db->rollback();
                 }
             } else {
                 $this->errors = $nuser->error;
                 $this->db->rollback();
             }
         } else {
             $this->errors = $this->object->errors;
         }
     }
     // Creation contact
     if ($action == 'add') {
         $this->assign_post();
         if (!$_POST["name"]) {
             array_push($this->errors, $langs->trans("ErrorFieldRequired", $langs->transnoentities("Lastname") . ' / ' . $langs->transnoentities("Label")));
             $action = 'create';
         }
         if ($_POST["name"]) {
             $id = $this->object->create($user);
             if ($id > 0) {
                 header("Location: " . $_SERVER["PHP_SELF"] . "?id=" . $id);
                 exit;
             } else {
                 $this->errors = $this->object->errors;
                 $action = 'create';
             }
         }
     }
     if ($action == 'confirm_delete' && GETPOST("confirm") == 'yes') {
         $result = $this->object->fetch($id);
         $this->object->old_name = $_POST["old_name"];
         $this->object->old_firstname = $_POST["old_firstname"];
         $result = $this->object->delete();
         if ($result > 0) {
             header("Location: list.php");
             exit;
         } else {
             $this->errors = $this->object->errors;
         }
     }
     if ($action == 'update') {
         if ($_POST["cancel"]) {
             header("Location: " . $_SERVER["PHP_SELF"] . "?id=" . $this->object->id);
             exit;
         }
         if (empty($_POST["name"])) {
             $this->error = array($langs->trans("ErrorFieldRequired", $langs->transnoentities("Name") . ' / ' . $langs->transnoentities("Label")));
             $action = 'edit';
         }
         if (empty($this->error)) {
             $this->object->fetch($_POST["contactid"]);
             $this->object->oldcopy = clone $this->object;
             $this->assign_post();
             $result = $this->object->update($_POST["contactid"], $user);
             if ($result > 0) {
                 header("Location: " . $_SERVER["PHP_SELF"] . "?id=" . $this->object->id);
                 exit;
             } else {
                 $this->errors = $this->object->errors;
                 $action = 'edit';
             }
         }
     }
 }
/**
 * Create an external user with thirdparty and contact
 *
 * @param	array		$authentication		Array of authentication information
 * @param	array		$thirdpartywithuser Datas
 * @return	mixed
 */
function createUserFromThirdparty($authentication, $thirdpartywithuser)
{
    global $db, $conf, $langs;
    dol_syslog("Function: createUserFromThirdparty login="******" id=" . $id . " ref=" . $ref . " ref_ext=" . $ref_ext);
    if ($authentication['entity']) {
        $conf->entity = $authentication['entity'];
    }
    $objectresp = array();
    $errorcode = '';
    $errorlabel = '';
    $error = 0;
    $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
    if ($fuser->societe_id) {
        $socid = $fuser->societe_id;
    }
    if (!$error && !$thirdpartywithuser) {
        $error++;
        $errorcode = 'BAD_PARAMETERS';
        $errorlabel = "Parameter thirdparty must be provided.";
    }
    if (!$error) {
        $fuser->getrights();
        if ($fuser->rights->societe->creer) {
            $thirdparty = new Societe($db);
            // If a contact / company already exists with the email, return the corresponding socid
            $sql = "SELECT s.rowid as societe_id FROM " . MAIN_DB_PREFIX . "societe as s";
            $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "socpeople as sp ON sp.fk_soc = s.rowid";
            $sql .= " WHERE s.entity=" . $conf->entity;
            $sql .= " AND s.email='" . $db->escape($thirdpartywithuser['email']) . "'";
            $sql .= " OR sp.email='" . $db->escape($thirdpartywithuser['email']) . "'";
            $sql .= $db->plimit(1);
            $resql = $db->query($sql);
            if ($resql) {
                // If a company or contact is found with the same email we return an error
                $row = $db->fetch_object($resql);
                if ($row) {
                    $error++;
                    $errorcode = 'ALREADY_EXIST';
                    $errorlabel = 'Object not create : company or contact exists ' . $thirdpartywithuser['email'];
                } else {
                    $db->begin();
                    /*
                     * Company creation
                     */
                    $thirdparty->name = $thirdpartywithuser['name_thirdparty'];
                    $thirdparty->ref_ext = $thirdpartywithuser['ref_ext'];
                    $thirdparty->address = $thirdpartywithuser['address'];
                    $thirdparty->zip = $thirdpartywithuser['zip'];
                    $thirdparty->town = $thirdpartywithuser['town'];
                    $thirdparty->country_id = $thirdpartywithuser['country_id'];
                    $thirdparty->country_code = $thirdpartywithuser['country_code'];
                    // find the country id by code
                    $langs->load("dict");
                    $sql = "SELECT rowid";
                    $sql .= " FROM " . MAIN_DB_PREFIX . "c_pays";
                    $sql .= " WHERE active = 1";
                    $sql .= " AND code='" . $thirdparty->country_code . "'";
                    $resql = $db->query($sql);
                    if ($resql) {
                        $num = $db->num_rows($resql);
                        if ($num) {
                            $obj = $db->fetch_object($resql);
                            $thirdparty->country_id = $obj->rowid;
                        }
                    }
                    $thirdparty->phone = $thirdpartywithuser['phone'];
                    $thirdparty->fax = $thirdpartywithuser['fax'];
                    $thirdparty->email = $thirdpartywithuser['email'];
                    $thirdparty->url = $thirdpartywithuser['url'];
                    $thirdparty->ape = $thirdpartywithuser['ape'];
                    $thirdparty->idprof1 = $thirdpartywithuser['prof1'];
                    $thirdparty->idprof2 = $thirdpartywithuser['prof2'];
                    $thirdparty->idprof3 = $thirdpartywithuser['prof3'];
                    $thirdparty->idprof4 = $thirdpartywithuser['prof4'];
                    $thirdparty->idprof5 = $thirdpartywithuser['prof5'];
                    $thirdparty->idprof6 = $thirdpartywithuser['prof6'];
                    $thirdparty->client = $thirdpartywithuser['client'];
                    $thirdparty->fournisseur = $thirdpartywithuser['fournisseur'];
                    $socid_return = $thirdparty->create($fuser);
                    if ($socid_return > 0) {
                        $thirdparty->fetch($socid_return);
                        /*
                         * Contact creation
                         *
                         */
                        $contact = new Contact($db);
                        $contact->socid = $thirdparty->id;
                        $contact->lastname = $thirdpartywithuser['name'];
                        $contact->firstname = $thirdpartywithuser['firstname'];
                        $contact->civility_id = $thirdparty->civility_id;
                        $contact->address = $thirdparty->address;
                        $contact->zip = $thirdparty->zip;
                        $contact->town = $thirdparty->town;
                        $contact->email = $thirdparty->email;
                        $contact->phone_pro = $thirdparty->phone;
                        $contact->phone_mobile = $thirdpartywithuser['phone_mobile'];
                        $contact->fax = $thirdparty->fax;
                        $contact->statut = 1;
                        $contact->country_id = $thirdparty->country_id;
                        $contact->country_code = $thirdparty->country_code;
                        //Retreive all extrafield for thirdsparty
                        // fetch optionals attributes and labels
                        $extrafields = new ExtraFields($db);
                        $extralabels = $extrafields->fetch_name_optionals_label('socpeople', true);
                        foreach ($extrafields->attribute_label as $key => $label) {
                            $key = 'contact_options_' . $key;
                            $key = substr($key, 8);
                            // Remove 'contact_' prefix
                            $contact->array_options[$key] = $thirdpartywithuser[$key];
                        }
                        $contact_id = $contact->create($fuser);
                        if ($contact_id > 0) {
                            /*
                             * User creation
                             *
                             */
                            $edituser = new User($db);
                            $id = $edituser->create_from_contact($contact, $thirdpartywithuser["login"]);
                            if ($id > 0) {
                                $edituser->setPassword($fuser, trim($thirdpartywithuser['password']));
                                if ($thirdpartywithuser['group_id'] > 0) {
                                    $edituser->SetInGroup($thirdpartywithuser['group_id'], $conf->entity);
                                }
                            } else {
                                $error++;
                                $errorcode = 'NOT_CREATE';
                                $errorlabel = 'Object not create : ' . $edituser->error;
                            }
                        } else {
                            $error++;
                            $errorcode = 'NOT_CREATE';
                            $errorlabel = 'Object not create : ' . $contact->error;
                        }
                        if (!$error) {
                            $db->commit();
                            $objectresp = array('result' => array('result_code' => 'OK', 'result_label' => 'SUCCESS'), 'id' => $socid_return);
                            $error = 0;
                        }
                    } else {
                        $error++;
                        $errors = $thirdparty->error ? array($thirdparty->error) : $thirdparty->errors;
                    }
                }
            } else {
                // retour creation KO
                $error++;
                $errorcode = 'NOT_CREATE';
                $errorlabel = 'Object not create';
            }
        } else {
            $error++;
            $errorcode = 'PERMISSION_DENIED';
            $errorlabel = 'User does not have permission for this request';
        }
    }
    if ($error) {
        $db->rollback();
        $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
    }
    return $objectresp;
}