php code to create a user

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sumservis
    Member
    • Sep 2019
    • 56

    php code to create a user


    Hi, please, do you not have a php code to create a user via api?
  • telecastg
    Active Community Member
    • Jun 2018
    • 907

    #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.

    Hello! I would need to be able to generate passwords, I want to make a registry plugin but I can not see how to generate a password with the same encryption
    Last edited by telecastg; 01-11-2020, 03:58 PM.

    Comment


    • Paulina
      Paulina commented
      Editing a comment
      Hi, how to properly hash a password on version 7.0.9, because for me your method doesnt work?

    • telecastg
      telecastg commented
      Editing a comment
      Hi, try initializing the entity manager class as explained here: https://forum.espocrm.com/forum/deve...8705#post78705 and substitute "$entityManager" for "$this->entityManager"

      Invoking the entity manager as "$this->getEntityManager()" became obsolete in Espo 7+
  • Paulina
    Member
    • Feb 2022
    • 41

    #3
    Yes, but what about password hash. I tried this:
    Code:
    $password = "123456789";
    $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);
    but I got error message:

    [2022-02-27 09:44:20] ERROR: Slim Application Error Type: TypeError Code: 0 Message: Argument 1 passed to Espo\Core\Utils\Config::__construct() must be an instance of Espo\Core\Utils\Config\ConfigFileManager, instance of Espo\Core\Utils\File\Manager given, called in C:\xampp\htdocs\EspoCRM\custom\Espo\Custom\Hooks\C ontact\CreatePortalUser.php on line 40 File: C:\xampp\htdocs\EspoCRM\application\Espo\Core\Util s\Config.php Line: 68 Trace: #0 C:\xampp\htdocs\EspoCRM\custom\Espo\Custom\Hooks\C ontact\CreatePortalUser.php(40): Espo\Core\Utils\Config->__construct(Object(Espo\Core\Utils\File\Manager )) #1 C:\xampp\htdocs\EspoCRM\application\Espo\Core\Hook Manager.php(129): Espo\Custom\Hooks\Contact\CreatePortalUser->beforeSave(Object(Espo\Modules\Crm\Entities\Con ta ct), Array, Array) #2 C:\xampp\htdocs\EspoCRM\application\Espo\Core\Repo sitories\Database.php(272): Espo\Core\HookManager->process('Contact', 'beforeSave', Object(Espo\Modules\Crm\Entities\Contact), Array) #3 C:\xampp\htdocs\EspoCRM\application\Espo\ORM\Repos itory\RDBRepository.php(165): Espo\Core\Repositories\Database->beforeSave(Object(Espo\Modules\Crm\Entities\Con ta ct), Array) #4 C:\xampp\htdocs\EspoCRM\application\Espo\Core\Repo sitories\Database.php(139): Espo\ORM\Repository\RDBRepository->save(Object(Espo\Modules\Crm\Entities\Contact), Array) #5 C:\xampp\htdocs\EspoCRM\application\Espo\ORM\Entit yManager.php(303): Espo\Core\Repositories\Database->save(Object(Espo\Modules\Crm\Entities\Contact), Array) #6 C:\xampp\htdocs\EspoCRM\application\Espo\Core\Reco rd\Service.php(647): Espo\ORM\EntityManager->saveEntity(Object(Espo\Modules\Crm\Entities\Con ta ct)) #7 C:\xampp\htdocs\EspoCRM\application\Espo\Core\Cont rollers\RecordBase.php(199): Espo\Core\Record\Service->create(Object(stdClass), Object(Espo\Core\Record\CreateParams)) #8 C:\xampp\htdocs\EspoCRM\application\Espo\Core\Api\ ActionProcessor.php(97): Espo\Core\Controllers\RecordBase->postActionCreate(Object(Espo\Core\Api\RequestWr ap per), Object(Espo\Core\Api\ResponseWrapper)) #9 C:\xampp\htdocs\EspoCRM\application\Espo\Core\Api\ RequestProcessor.php(136): Espo\Core\Api\ActionProcessor->process('Contact', 'create', Object(Espo\Core\Api\RequestWrapper), Object(Espo\Core\Api\ResponseWrapper)) #10 C:\xampp\htdocs\EspoCRM\application\Espo\Core\Api\ RequestProcessor.php(111): Espo\Core\Api\RequestProcessor->proceed(Object(Espo\Core\Api\RequestWrapper), Object(Espo\Core\Api\ResponseWrapper)) #11 C:\xampp\htdocs\EspoCRM\application\Espo\Core\Api\ RequestProcessor.php(82): Espo\Core\Api\RequestProcessor->processInternal(Object(Espo\Core\Api\Route), Object(Espo\Core\Api\RequestWrapper), Object(Espo\Core\Api\ResponseWrapper)) #12 C:\xampp\htdocs\EspoCRM\application\Espo\Core\Api\ Starter.php(103): Espo\Core\Api\RequestProcessor->process(Object(Espo\Core\Api\Route), Object(Espo\Core\Api\RequestWrapper), Object(Espo\Core\Api\ResponseWrapper)) #13 C:\xampp\htdocs\EspoCRM\vendor\slim\slim\Slim\Hand lers\Strategies\RequestResponse.php(43): Espo\Core\Api\Starter->Espo\Core\Api\{closure}(Object(Slim\Psr7\Reques t) , Object(Slim\Psr7\Response), Array) #14 C:\xampp\htdocs\EspoCRM\vendor\slim\slim\Slim\Rout ing\Route.php(384): Slim\Handlers\Strategies\RequestResponse->__invoke(Object(Closure), Object(Slim\Psr7\Request), Object(Slim\Psr7\Response), Array) #15 C:\xampp\htdocs\EspoCRM\vendor\slim\slim\Slim\Midd lewareDispatcher.php(81): Slim\Routing\Route->handle(Object(Slim\Psr7\Request)) #16 C:\xampp\htdocs\EspoCRM\vendor\slim\slim\Slim\Midd lewareDispatcher.php(81): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #17 C:\xampp\htdocs\EspoCRM\vendor\slim\slim\Slim\Rout ing\Route.php(341): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #18 C:\xampp\htdocs\EspoCRM\vendor\slim\slim\Slim\Rout ing\RouteRunner.php(84): Slim\Routing\Route->run(Object(Slim\Psr7\Request)) #19 C:\xampp\htdocs\EspoCRM\vendor\slim\slim\Slim\Midd leware\RoutingMiddleware.php(59): Slim\Routing\RouteRunner->handle(Object(Slim\Psr7\Request)) #20 C:\xampp\htdocs\EspoCRM\vendor\slim\slim\Slim\Midd lewareDispatcher.php(147): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Psr7\Request), Object(Slim\Routing\RouteRunner)) #21 C:\xampp\htdocs\EspoCRM\vendor\slim\slim\Slim\Midd leware\ErrorMiddleware.php(107): class@anonymous->handle(Object(Slim\Psr7\Request)) #22 C:\xampp\htdocs\EspoCRM\vendor\slim\slim\Slim\Midd lewareDispatcher.php(147): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(class@anonymous)) #23 C:\xampp\htdocs\EspoCRM\vendor\slim\slim\Slim\Midd lewareDispatcher.php(81): class@anonymous->handle(Object(Slim\Psr7\Request)) #24 C:\xampp\htdocs\EspoCRM\vendor\slim\slim\Slim\App. php(215): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #25 C:\xampp\htdocs\EspoCRM\vendor\slim\slim\Slim\App. php(199): Slim\App->handle(Object(Slim\Psr7\Request)) #26 C:\xampp\htdocs\EspoCRM\application\Espo\Core\Api\ Starter.php(79): Slim\App->run() #27 C:\xampp\htdocs\EspoCRM\application\Espo\Core\Appl icationRunners\Api.php(49): Espo\Core\Api\Starter->star... [] []

    Comment

    • telecastg
      Active Community Member
      • Jun 2018
      • 907

      #4
      The posting with the authentication method that I described above is over two years old.

      To see how passwords are encrypted in Espo 7+, you can check this script https://github.com/espocrm/espocrm/b...n/Espo.php#L62
      Last edited by telecastg; 03-04-2022, 06:49 AM.

      Comment

      Working...