5

I am creating a seller management and I want to login seller to admin panel by assigning them a seller role, role is not a problem, But when I am creating user then user created in admin but I cannot login in it because it is generating random password below is my code:

 $model = Mage::getModel('admin/user');

            $model->setUserId($this->getRequest()->getParam('id'))
                    ->setData($data);                
           // print_r($model->getNewPassword());
            if ($model->hasNewPassword() && $model->getNewPassword() === '') {
                $model->unsNewPassword();
            }
            if ($model->hasPasswordConfirmation() && $model->getPasswordConfirmation() === '') {
                $model->unsPasswordConfirmation();
            }                

            $result = $model->validate();

            // print_r($result); die();

            if (is_array($result)) {
                Mage::getSingleton('adminhtml/session')->setUserData($data);
                foreach ($result as $message) {
                    Mage::getSingleton('adminhtml/session')->addError($message);
                }
                $this->_redirect('*/*/edit', array('_current' => true));
                return $this;
            }

            $model->setCurrentPassword('admin@123');
            $model->save();

            $role_id = Mage::helper('marketplace')->getConfig('general', 'vendor_role');

            $model->setRoleIds(array($role_id))
                    ->setRoleUserId($model->getUserId())
                    ->saveRelations();



                    // print_R($model->getData()); exit();
            $model->save();
9

Create a file in root directory. for ex. admin.php

<?php
    $mageFilename = 'app/Mage.php';
    if (!file_exists($mageFilename)) {
        echo $mageFilename." was not found";
        exit;
    }
    require_once $mageFilename;
    Mage::app();

    try {
        //create new user by providing details below
        $user = Mage::getModel('admin/user')
            ->setData(array(
                'username'  => 'admin1',
                'firstname' => 'John',
                'lastname'  => 'Doe',
                'email'     => 'john@example.com',
                'password'  => 'admin123',
                'is_active' => 1
            ))->save();

    } catch (Exception $e) {
        echo $e->getMessage();
        exit;
    }

    try {
        //create new role
        $role = Mage::getModel("admin/roles")
                ->setName('Inchoo')
                ->setRoleType('G')
                ->save();

        //give "all" privileges to role
        Mage::getModel("admin/rules")
                ->setRoleId($role->getId())
                ->setResources(array("all"))
                ->saveRel();

    } catch (Mage_Core_Exception $e) {
        echo $e->getMessage();
        exit;
    } catch (Exception $e) {
        echo 'Error while saving role.';
        exit;
    }

    try {
        //assign user to role
        $user->setRoleIds(array($role->getId()))
            ->setRoleUserId($user->getUserId())
            ->saveRelations();

    } catch (Exception $e) {
        echo $e->getMessage();
        exit;
    }

    echo 'Admin User sucessfully created!<br /><br /><b>THIS FILE WILL NOW TRY TO DELETE ITSELF, BUT PLEASE CHECK TO BE SURE!</b>';
    @unlink(__FILE__);
    ?>

Run the file from root directory. for ex.: www.abc.com/admin.php

  • Thanks for your answer, But I want to do it from admin controller but it is not working you, first you try and then reply. – MOHD TAHIR Feb 21 '17 at 12:23
  • I have tried before comment. and it is working – Vinod Kumar Feb 22 '17 at 4:50
2

Use following php code to create admin user

Run this php code from magento root folder

 <?php 
    require_once('app/Mage.php');
    umask(0);
    Mage::app();
    $user = Mage::getModel('admin/user')->setData(array(
            'username' => 'username',
            'firstname' => 'vaibhav',
            'lastname' => 'lastname',
            'email' => 'vaibhavahalpara@gmail.com',
            'password' => 'password',
            'is_active' => 1
        ))
        ->save();
    $user->setRoleIds(array(1))
        ->setRoleUserId($user->getUserId())
        ->saveRelations();
?>
  • no this is not working – MOHD TAHIR Feb 20 '17 at 9:04
  • what is error.. – Vaibhav Ahalpara Feb 20 '17 at 9:14
  • username is already exists – MOHD TAHIR Feb 20 '17 at 10:10
  • Then try with other user name. user with that username is already in admin user table – Vaibhav Ahalpara Feb 20 '17 at 11:50
  • This code to create new user u need to set id by ->setId(1); – Vaibhav Ahalpara Feb 21 '17 at 5:07
2

Find below code

$user = Mage::getModel('admin/user')
->setData(array(
    'username'  => 'seller1',
    'firstname' => 'Seller',
    'lastname'    => 'Seller',
    'email'     => 'xyz@seller.com',
    'password'  =>'seller123',
    'is_active' => 1
))->save();

$user->setRoleIds(array(1))  //assign seller role id
    ->setRoleUserId($user->getUserId())
    ->saveRelations();
1

You can also use Mysql for create new admin user:

LOCK TABLES `admin_role` WRITE , `admin_user` WRITE;

SET @SALT = "rp";
SET @PASS = CONCAT(MD5(CONCAT( @SALT , "password") ), CONCAT(":", @SALT ));
SELECT @EXTRA := MAX(extra) FROM admin_user WHERE extra IS NOT NULL;

INSERT INTO `admin_user` (firstname,lastname,email,username,password,created,lognum,reload_acl_flag,is_active,extra,rp_token_created_at) 
VALUES ('Firstname','Lastname','email@example.com','myuser',@PASS,NOW(),0,0,1,@EXTRA,NOW());

INSERT INTO `admin_role` (parent_id,tree_level,sort_order,role_type,user_id,role_name) 
VALUES (1,2,0,'U',(SELECT user_id FROM admin_user WHERE username = 'myuser'),'Firstname');

UNLOCK TABLES;

or update existing admin user password:

SELECT * FROM admin_user;

Then, find the username you want to modify in the listing provided - ‘admin’ in this example. Then, to update the password, type:

UPDATE admin_user SET password=CONCAT(MD5('qXpassword'), ':qX') WHERE username='admin';

‘qX’ would be changed to whatever you want it to be and same goes for ‘password’

You can also do this in phpMyAdmin, find the admin_user field and choose MD5 when updating password.

If you want to add a new admin user, you must not only create a new entry in the table ‘admin_user’, but you also have to insert an entry in the table ‘admin_role’ which has to be associated with the user by the field ‘user_id’.

  • Thanks for your help But I need to do it with code. – MOHD TAHIR Feb 21 '17 at 5:00
1

Create a file (ie. test.php) in Magento root and run it from the browser directly.

    <?php
    define('USERNAME','admin');  // Define username
    define('EMAIL','example@abc.com'); // Define a Email
    define('PASSWORD','admin123');    // Define password  


    if(!defined('USERNAME') || !defined('EMAIL') || !defined('PASSWORD')){
        echo 'Define USERNAME, EMAIL and PASSWORD.';
        exit;
    }


    $mageFilename = 'app/Mage.php';
    if (!file_exists($mageFilename)) {
        echo $mageFilename." was not found";
        exit;
    }
    require_once $mageFilename;
    Mage::app();

    try {
        //create admin new user
        $user = Mage::getModel('admin/user')
            ->setData(array(
                'username'  => USERNAME,
                'firstname' => 'Magento',
                'lastname'  => 'Team',
                'email'     => EMAIL,
                'password'  => PASSWORD,
                'is_active' => 1
            ))->save();

    } catch (Exception $e) {
        echo $e->getMessage();
        exit;
    }

    try {
        //create new role
        $role = Mage::getModel("admin/roles")
                ->setName('Develoeprs')
                ->setRoleType('G')
                ->save();

        //give "all" privileges to role
        Mage::getModel("admin/rules")
                ->setRoleId($role->getId())
                ->setResources(array("all"))
                ->saveRel();

    } catch (Mage_Core_Exception $e) {
        echo $e->getMessage();
        exit;
    } catch (Exception $e) {
        echo 'Error while saving role.';
        exit;
    }

    try {
        //assign user to role
        $user->setRoleIds(array($role->getId()))
            ->setRoleUserId($user->getUserId())
            ->saveRelations();

    } catch (Exception $e) {
        echo $e->getMessage();
        exit;
    }

    echo 'Admin User successfully created!';
0

Did you try this way?

// Preparing the user array

  $user = Mage::getModel(‘admin/user’)->setData(array(

        ‘username’  => ‘admin’,
        ‘firstname’ => ‘Admin’,
        ‘lastname’    => ‘Admin’,
        ’email’     => ‘admin@example.com’,
        ‘password’  =>’admin@123′,
        ‘is_active’ => 1
));
 $user->save();

//Assigning Role Id
//Admin role id is 1
$user->setRoleIds(array(1))
->setRoleUserId($user->getUserId())
->saveRelations();

I hope this helps

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.