8

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();
MOHD TAHIR
  • 91
  • 1
  • 1
  • 7

6 Answers6

17

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

Teja Bhagavan Kollepara
  • 3,816
  • 5
  • 32
  • 69
Vinod Kumar
  • 2,045
  • 1
  • 23
  • 67
4

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!';
Yogesh Agarwal
  • 1,162
  • 11
  • 22
3

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();
?>
Vaibhav Ahalpara
  • 5,249
  • 4
  • 42
  • 81
3

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

Konrad Siamro
  • 1,457
  • 1
  • 17
  • 29
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();
Gopal Patel
  • 3,139
  • 2
  • 15
  • 32
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