0

I want to create an admin user programmatically , from the frontend controller my frontend code is:

<?php 
namespace Learning\HelloPage\Controller\Account;
use Magento\Framework\App\Action\Context;
class Add extends \Magento\Backend\App\Action
{ 
    protected $_userFactory;

    public function __construct(
        \Magento\User\Model\UserFactory $userFactory
       ) 
    {
        $this->_userFactory = $userFactory;
    }

    public function execute(){

        $adminInfo = [
            'username'  => 'killer',
            'firstname' => 'admin',
            'lastname'    => 'admin',
            'email'     => 'me@helloworld.com',
            'password'  =>'hello@123',       
            'interface_locale' => 'en_US',
            'is_active' => 1
        ];

        $userModel = $this->_userFactory->create();
        $userModel->setData($adminInfo);
        $userModel->setRoleId(7);
        try{
           $userModel->save(); 
           echo 'saved';
        } catch (\Exception $ex) {
            $ex->getMessage();
        }
    }
}

it is giving me this error. enter image description here

2

For the first thing If you are using this controller on frontend side then you should extend the Magento\Framework\App\Action\Action class Instead of Magento\Backend\App\Action

And you can use below script to create an user for admin

use \Magento\User\Model\UserFactory;
use \Magento\User\Model\ResourceModel\User;

public function __construct(
       ...... 
        UserFactory $userFactory,
        User $userResourceModel,     
        .....
    ) {
        $this->userFactory = $userFactory;
        $this->userResourceModel = $userResourceModel;
    }

Now you can use this like,

    $model = $this->userFactory->create();        
    $model->setRoleId(7);// change with it 
    $data = array(
        'username' => 'killer',
        'firstname' => 'killer',
        'lastname' => 'killer',
        'email' => 'me@helloworld.com',
        'password' => 'hello@123',
        'is_active' => 1,
        'interface_locale' => ' en_US',
        'role_id' => 7
    );
    $model->setData($data);
    $this->userResourceModel->save($model);
  • Fatal error: Uncaught TypeError: Argument 2 passed to Learning\HelloPage\Controller\Account\Add::__construct() must be an instance of Magento\User\Model\ResourceModel\User, none given, called in /var/www/html/magento2/var/generation/Learning/HelloPage/Controller/Account/Add/Interceptor.php on line 14 and defined in /var/www/html/magento2/app/code/Learning/HelloPage/Controller/Account/Add.php:12 Stack trace: #0 – Jay Kapoor Sep 1 '17 at 11:24
  • got this error. – Jay Kapoor Sep 1 '17 at 11:24
  • Try to run di:compile :) @J – Keyur Shah Sep 1 '17 at 11:25
  • Learning\HelloPage\Controller\Account\Add\Interceptor->__construct(Object(Magento\User\Model\UserFactory)) #2 /var/www/html/magento2/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(89): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Learning\\HelloP...', Array) #3 /var/www/html/magen in /var/www/html/magento2/app/code/Learning/HelloPage/Controller/Account/Add.php on line 12 – Jay Kapoor Sep 1 '17 at 11:27
  • again got this error – Jay Kapoor Sep 1 '17 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.