如何在CMS中创建成员时自动生成密码并填写密码字段?

时间:2016-03-10 04:42:42

标签: silverstripe

我创建了一个名为“客户端”的模型管理员。在“安全”选项卡下,我创建了一个名为“客户”的新组。此模型管理员只管理客户端而不管理其他成员。

使用模型管理员在CMS中创建新成员时,我想为他们自动生成一个密码(而不是他们必须创建自己的密码),然后通过电子邮件发送给他们。

我想要发生什么:

工作人员点击"Add member"后,密码和密码确认文本框会自动填充生成的密码。 - 这是我认为最理想的方式。 - 然后,一旦工作人员点击保存,它将向客户端和电子邮件发送用户名和新生成的密码。

enter image description here

问题是你是怎么做到的?

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@gmail.com', 'email@gmail.com', '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();


    }
}

2 个答案:

答案 0 :(得分:3)

您应该在SetPassword字段中设置临时纯文本密码,并在调用onBeforeWrite和onAfterWrite挂钩时管理上下文。

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;
            ));
        }
    }
}

答案 1 :(得分:0)

您可以在会员扩展中使用populateDefaults()函数。

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

但是让我这样说:这是一个坏主意。您不希望将纯文本密码发送到电子邮件不安全的地方。让用户选择自己的密码是目前为止更好的方法。