No announcement yet.

php code to create a user

  • 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
        // 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
        // persist the new user instance
        // 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.
    Last edited by telecastg; 01-11-2020, 02:58 PM.