cancel
Showing results for 
Search instead for 
Did you mean: 

Change min password length

Change min password length

I am making users programatically (weird system that a company wants, where users send their details to companies email, they then create user in their crm, and upload users data to server, where I pick it up and create user from it), and the problem is, that password shorter than 6 characters is not allowed. I've updated this in administration, and that seems to only work when users register in frontend.
 

How can I set minimum password length for users I am making in the backend?

Example of code that creates user:

 

    $CustomerModel = $objectManager->create('Magento\Customer\Model\Customer');

    $CustomerModel->setWebsiteId(1); 
    $CustomerModel->loadByEmail($email);

    if (!$CustomerModel->getId()) {
        $customer = $customerFactory->create();
        $customer->setWebsiteId($websiteId);

        $customer->setEmail($email);
        $customer->setFirstname($name);
        $customer->setPassword($password);
        $customer->save();
    }

 

8 REPLIES 8

Re: Change min password length

Hello,

 

There is one setting for set min pwd length

 

go to admin >> store >> configuration >> customer >> Customer Configuration

 

 

hope it will help you.

 

if works then mark as solution.


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Change min password length

Sadly it does not work.. Already tried that. It only seems to work for users registering in frontend, but not on ones I make in backend via the code I provided above.

Re: Change min password length

Hello @blaz_p

 

make sure that setting globally.

 

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Change min password length

Where do i set that? here http://prntscr.com/khxul2 ?

Re: Change min password length

Hello,

 

you need to set into default config.


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Change min password length

Hello @blaz_p

 

seems like magento 2 bug

 

please check

 

https://magento.stackexchange.com/questions/186370/magento-2-changing-default-minimum-length-of-cust...

 

/vendor/magento/module-customer/Model/Customer/Attribute/Backend/Password.php 

// constact declared 
const MIN_PASSWORD_LENGTH = 6;

hope it will help you.

 

if works then mark as solution.


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Change min password length

File path:

 

/vendor/magento/module-customer/Model/Customer/Attribute/Backend/Password.php

 

 

const MIN_PASSWORD_LENGTH = 6;

 


You can set your min password length value here.


For create your own custom file :

 

/app/code/VendorName/ModuleName/Model/Customer/Attribute/Backend/Password.php

 

Add this below code in file : 

<?php

namespace VendorName\ModuleName\Model\Customer\Attribute\Backend;

use Magento\Framework\Exception\LocalizedException;

class Password extends \Magento\Customer\Model\Customer\Attribute\Backend\Password
{
/**
* Min password length
*/
const MIN_PASSWORD_LENGTH = 5; // set your custom length of min password

/**
* Magento string lib
*
* @var \Magento\Framework\Stdlib\StringUtils
*/
protected $string;

/**
* @param \Magento\Framework\Stdlib\StringUtils $string
*/
public function __construct(\Magento\Framework\Stdlib\StringUtils $string)
{
$this->string = $string;
}

/**
* check some rule for password & transform temporary attribute 'password' into real attribute 'password_hash' process.
*
*/
public function beforeSave($object)
{
$password = $object->getPassword();

$length = $this->string->strlen($password);
if ($length > 0) {
if ($length < self::MIN_PASSWORD_LENGTH) {
throw new LocalizedException(
__('Please enter a password with at least %1 characters.', self::MIN_PASSWORD_LENGTH)
);
}

if (trim($password) !== $password) {
throw new LocalizedException(__('The password can not begin or end with a space.'));
}

$object->setPasswordHash($object->hashPassword($password));
}
}

/**
* @param \Magento\Framework\DataObject $object
* @return bool
*/
public function validate($object)
{
$password = $object->getPassword();
if ($password && $password === $object->getPasswordConfirm()) {
return true;
}

return parent::validate($object);
}
}

 

 

 

Hope, It will helpful for you.

If issue solved,Click Kudos & Accept as Solution

Re: Change min password length

Create di.xml at 

app/code/Vendor/Module/etc/di.xml file,

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Customer\Model\Customer\Attribute\Backend\Password" type="Vendor\Module\Model\Customer\Attribute\Backend\Password"/>
</config>

In Model file,
app\code\Vendor\Module\Model\Customer\Attribute\Backend\Password.php

<?php
namespace Vendor\Module\Model\Customer\Attribute\Backend;

use Magento\Framework\Exception\LocalizedException;

class Password extends \Magento\Customer\Model\Customer\Attribute\Backend\Password
{
    /**
     * Min password length
     */
    const MIN_PASSWORD_LENGTH = 4; // you can set here value

    /**
     * Special processing before attribute save:
     * a) check some rules for password
     * b) transform temporary attribute 'password' into real attribute 'password_hash'
     *
     * @param \Magento\Framework\DataObject $object
     * @return void
     * @throws \Magento\Framework\Exception\LocalizedException
     */
    public function beforeSave($object)
    {
        $password = $object->getPassword();

        $length = $this->string->strlen($password);
        if ($length > 0) {
            if ($length < self::MIN_PASSWORD_LENGTH) {
                throw new LocalizedException(
                    __('Please enter a password with at least %1 characters.', self::MIN_PASSWORD_LENGTH)
                );
            }

            if (trim($password) !== $password) {
                throw new LocalizedException(__('The password can not begin or end with a space.'));
            }

            $object->setPasswordHash($object->hashPassword($password));
        }
    }

    /**
     * @deprecated 100.2.0
     * @param \Magento\Framework\DataObject $object
     * @return bool
     */
    public function validate($object)
    {
        $password = $object->getPassword();
        if ($password && $password === $object->getPasswordConfirm()) {
            return true;
        }

        return parent::validate($object);
    }
}
If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial