1

We had a method to generate the random passwords for the customers in Magento1 in following class.

app\code\core\Mage\Customer\Model\Customer.php

method >>

 /**
     * Retrieve random password
     *
     * @param   int $length
     * @return  string
     */
    public function generatePassword($length = 8)
    {
        $chars = Mage_Core_Helper_Data::CHARS_PASSWORD_LOWERS
            . Mage_Core_Helper_Data::CHARS_PASSWORD_UPPERS
            . Mage_Core_Helper_Data::CHARS_PASSWORD_DIGITS
            . Mage_Core_Helper_Data::CHARS_PASSWORD_SPECIALS;
        return Mage::helper('core')->getRandomString($length, $chars);
    }

What is the similar method in Magento2?

2

I used the following code to generate the passwords for the customers.

<?php
namespace Muk\Common\Helper;

class CustomerPasswordGeneration extends \Magento\Framework\App\Helper\AbstractHelper
{

    /**
     * @var \Magento\Framework\Math\Random
     */
    protected $mathRandom;

    public function __construct(
        Context $context,
        \Magento\Framework\Math\Random $mathRandom
    ) {        
        $this->mathRandom = $mathRandom;
        parent::__construct($context);
    }

    /**
     * Retrieve random password
     *
     * @param   int $length
     * @return  string
     */
    public function generatePassword($length = 10)
    {
        $chars = \Magento\Framework\Math\Random::CHARS_LOWERS
            . \Magento\Framework\Math\Random::CHARS_UPPERS
            . \Magento\Framework\Math\Random::CHARS_DIGITS;

        return $password = $this->mathRandom->getRandomString($length, $chars);
    }
1

Magento use function like this in the setup script

vendor/magento/magento2-base/setup/src/Magento/Setup/Model/AdminAccount.php

 /**
     * Generate password string
     *
     * @return string
     */
    protected function generatePassword()
    {
        return $this->encryptor->getHash($this->data[self::KEY_PASSWORD], true);
    }
  • 1
    I know about this. But in my case I am not passing self::KEY_PASSWORD to generate the password. – Mukesh Jul 25 '17 at 9:04
  • 1
    also check Magento/Customer/Model/AccountManagement.php I think you have to generate password string by your own if you have not it – bxN5 Jul 25 '17 at 9:13
  • yes, for random strings I can use class Magento\Framework\Math\Random – Mukesh Jul 25 '17 at 11:10
1

I have made one custom url (new custom controller) to generate customer's password .

<?php
namespace Company\Module\Controller\Index;

use \Magento\Framework\Encryption\EncryptorInterface as Encryptor;

class Index extends \Magento\Framework\App\Action\Action {

    protected $_resultPageFactory;
    protected $_indexer;

    public function __construct(\Magento\Framework\App\Action\Context $context,Encryptor $encryptor, \Magento\Framework\View\Result\PageFactory $resultPageFactory) {
        $this->_resultPageFactory = $resultPageFactory;
        $this->encryptor = $encryptor;
        return parent::__construct($context);
    }

    public function execute() {
        $password = '123456'; //or you can use php random function rand() to generate string
        $hash = $this->encryptor->hash($password);
        $newhash = $this->encryptor->getHash($password, true);
        echo $newhash; // this will be customer's new password, which need to store in 'password_hash' column of "customer_entity" db table.
    }
}

you can integrate same code in your functional method / file .

0

I used this code simply and got result

  <?php 
   function generateRandomString($length) {
        $characters = 
       '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@#$%&';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }
?>

<?php echo generateRandomString(5);  ?>
0

I used the following method

    <?php

 function randomPassword()
        {
            $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
            $password = [];
            $alphaLength = strlen($alphabet) - 1;
            for ($i = 0; $i < 8; $i++) {
                $n = rand(0, $alphaLength);
                $password[] = $alphabet[$n];
            }
            return implode($password);
        }

randomPassword();

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.