ifusion
ifusion

Reputation: 2233

How to generate a password automatically and fill in password fields when creating a member in the CMS?

I have created a Model Admin called 'Clients'. Under the "Security" tab I created a new group called 'clients'. This Model Admin is managing just the clients and not other members.

When creating a new member in the CMS using a model admin, I want to automatically generate a password for them (instead of them having to create their own one) one for them and then email it to them.

What I want to happen:

After the staff member clicks "Add member" the password and password confirmation textboxs are automatically populated with the generated password. - This is the most ideal way I believe. - Then once the staff member clicks save it will send the client and email with the username and newly generated password.

enter image description here

Question is how do you do this?

ClientAdmin.php

<?php

class ClientAdmin extends ModelAdmin {

    private static $menu_icon = 'themes/cadence/images/icons/person.png';

    public $showImportForm = false;

    private static $managed_models = array(
        'Member'
    );

    private static $url_segment = 'clients';

    private static $menu_title = 'Clients';

    public function getList() {
        $list = parent::getList();
        $clientGroup = Group::get()->filter('code', 'clients')->first();
        $list = $list->filter('Groups.ID', $clientGroup->ID);

        return $list;
    }
}


MemberClientExtension.php

<?php

class MemberClientExtension extends DataExtension implements PermissionProvider
{
    private static $db = array(

    );

    public function providePermissions() {
        return array(
            'CLIENTS' => 'Can access the site as a client',
        );
    }

    public function updateCMSFields(FieldList $fields) {

    }

    public function generatePasswordForClient(){

        $plainPassword = $this->owner->create_new_password();
        $encryptedPassword = $this->owner->encryptWithUserSettings($plainPassword);

        // Need to set password in database here?

        return $plainPassword;
    }

    public function sendClientWelcomeEmail() {
        $email = new Email('[email protected]', '[email protected]', 'New member sign up');
        $email->setTemplate('NewClientSignUp');
        $email->populateTemplate(array(
            'Email' => $this->owner->Email,
            'Password' => $this->generatePasswordForClient()
        ));
        return $email->send();
    }

    public function onBeforeWrite()
    {
        parent::onBeforeWrite();
    }

    public function onAfterWrite()
    {
        parent::onAfterWrite();

        // Seems to send 2x emails.. Only want to send one
        $this->sendClientWelcomeEmail();


    }
}

Upvotes: 2

Views: 652

Answers (2)

Greg Smirnov
Greg Smirnov

Reputation: 1592

You should set temporary plain text password in SetPassword field, and manage the context when onBeforeWrite and onAfterWrite hooks are called.

class MemberClientExtension extends DataExtension
{
    protected $sendWelcomeEmail = false;
    ...
    // onBeforeWrite on extension is called after password is encrypted and set
    public function validate(ValidationResult $validationResult) {
       if (!$this->owner->isInDB()) {
           $this->sendWelcomeEmail = true;
       }
    }

    public function onAfterWrite() {
        if ($this->sendWelcomeEmail) {
            // reset for password change
            $this->sendWelcomeEmail = false;
            $password = $this->generatePasswordForClient();
            $this->owner->changePassword($password);
            $this->sendClientWelcomeEmail(array(
                'Email' => $this->owner->Email,
                'Password' => $password;
            ));
        }
    }
}

Upvotes: 3

JayDevlin
JayDevlin

Reputation: 1

You could use the populateDefaults() function in your Member extension.

public function populateDefaults() {
    $this->owner->changePassword($this->generatePasswordForClient());
}

But let me say this: This is a bad idea. You don't want to send plain text passwords over something as insecure as an email. To let the user to choose its own password is by far the better way.

Upvotes: 0

Related Questions