Store plain text passwords for users
Security warning: Do not store plain-text passwords unless an integration absolutely requires it. Anyone who can read this field in the database or the admin interface can obtain every password captured by this customization. Storing or emailing passwords may also violate your security and compliance requirements.
This customization captures only passwords that users set after it is installed. It cannot recover existing passwords.
In aMember CP, go to Configuration > Add User Fields, click New Field, and create this field:
- Field Name: plain_password
- Field Title: Plain Text Password
- Field Type: SQL
- SQL Field Type: String (VARCHAR(255))
Add this code to site.php file:
Am_Di::getInstance()->hook->add(Am_Event::SET_PASSWORD, function (Am_Event_SetPassword $event) {
$password = $event->getPassword();
if ($password !== '') {
$event->getUser()->updateQuick('plain_password', $password);
}
});
Whenever a user's password is set or changed, aMember copies it to the
plain_password field. You can then use the %user.plain_password%
placeholder in email templates where the %user% placeholder is available.
Optionally you can add the following code snippet to site.php file:
Am_Di::getInstance()->hook->add('gridUserInitForm', function (Am_Event_Grid $event) {
$record = $event->getGrid()->getRecord();
if ($record->isLoaded() && $record->plain_password !== '') {
$password = json_encode(
(string) $record->plain_password,
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
);
$event->getGrid()->getForm()
->addScript()
->setScript(<<<CUT
jQuery(function(){
jQuery('[name=_pass]').closest('.am-element').prepend(
jQuery('<span>', {id: 'plain-password', text: {$password}}).append(' ')
);
jQuery(document).on('click', '.am-change-pass', function(){
jQuery('#plain-password').remove();
});
jQuery('[name=plain_password]').closest('.am-row').hide();
});
CUT
);
}
});
The optional code displays the stored password beside Password on the user edit form and hides the separate Plain Text Password field. The password is removed from the page when the administrator clicks change.