Announcement

Collapse
No announcement yet.

php code to create a user

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • php code to create a user


    Hi, please, do you not have a php code to create a user via api?

  • #2
    I don't create Users via api in my application but here's the PHP code that I use to create Portal Users via a hook, but hopefully it will help.

    Create and load a new user entity instance from an existing contact - $contatcObject
    Code:
        
        // initialize an Entity Manager instance
        $entityManager = $this->getEntityManager();    
        // get the contact information
        $contactId = $contactObject->get("id");
        $firstName = $contactObject->get("firstName");
        $lastName = $contactObject->get("lastName");
        // combine the contact's first name and last name to create a new user name - or use any other string
        $usename = strtolower($firstName).".".strtolower($lastName);
        // define a password - use any value
        $password = "my-password";
        // encrypt the password
        $fileM = new \Espo\Core\Utils\File\Manager();
        $config = new \Espo\Core\Utils\Config($fileM);
        $passwordHash = new \Espo\Core\Utils\PasswordHash($config);
        $encodedPassword = $passwordHash->hash($password,true);
        // create a new User instance
        $userObject = $entityManager->getEntity('User');   // create a new user instance
        // load the new user entity instance
        $userObject->set("firstName",$firstName);
        $userObject->set("lastName",$lastName);
        $userObject->set("userName",$usename);            
        $userObject->set("password",$encodedPassword);    
        $userObject->set("confirmPasword",$encodedPassword);
        $userObject->set("contactId",$contactId);            
        $userObject->set("isActive",true);
        $userObject->set("isPortalUser",1);
        $userObject->set("type","portal");
        // persist the new user instance
        $entityManager->saveEntity($userObject);
        // get the new user id
        $newUserObject = $entityManager->getRepository('User')->where(['contactId'=>$contactId])->findOne();
        $userId = $newUserObject->get('id');
    Here's also a link to a VERY HELPFUL posting by dabasystem from which I borrowed the encryption code.

    https://forum.espocrm.com/forum/deve...rate-passwords
    Last edited by telecastg; 01-11-2020, 02:58 PM.

    Comment

    Working...
    X