Silverstripe:ConfirmedPasswordField不保存新密码

时间:2016-08-23 01:55:49

标签: php content-management-system silverstripe

大家好我试图让用户在前端更新密码。除此之外,所有其他字段都会更新。我在Silverstripe版本3.4.0上。这可能不是解决这个问题的最佳方式,但我对SS来说相对较新,所以请保持温和。:

// Edit & Save your details
public function EditMyDetails() {
    //Include JS for updating details
    Requirements::javascript('module-memberprofiles/javascript/MemberProfileUpdate.js');
    Requirements::set_force_js_to_bottom(true);

    $fields = new FieldList(
        $leftCol = CompositeField::create(
            TextField::create('FirstName', 'First Name')
                ->setFieldHolderTemplate('UserDetails_FieldHolder'),
            TextField::create('Surname', 'Surname')
                ->setFieldHolderTemplate('UserDetails_FieldHolder'),
            CompositeField::create(
                TextField::create('Address', ''),
                TextField::create('Suburb', ''),
                CompositeField::create(
                    DropdownField::create('State', '', singleton('Member')->dbObject('State')->enumValues())->setFieldHolderTemplate('UserDetails_StatePostCode'),
                    TextField::create('PostCode', '')->setFieldHolderTemplate('UserDetails_StatePostCode')
                )->addExtraClass('row')
            )
                ->addExtraClass('userdetails-address wrap')
                ->setFieldHolderTemplate('UserDetails_AddressHolder'),
            TextField::create('Phone', 'Phone')
                ->setFieldHolderTemplate('UserDetails_FieldHolder'),
            TextField::create('Email', 'Email')
                ->setFieldHolderTemplate('UserDetails_FieldHolder')
        )->setFieldHolderTemplate('UserDetails_CompositeField'),
        $rightCol = CompositeField::create(
            ConfirmedPasswordField::create('Password', 'Change Password', null, null, $showOnClick = true)
        )->setFieldHolderTemplate('UserDetails_CompositeField')
    );

    $actions = new FieldList(new FormAction('SaveMyDetails', 'Save Profile'));

    $validation = new RequiredFields(array('FirstName','Surname','Email'));

    $form = new Form ( $this, 'EditUserDetails', $fields, $actions, $validation);
    $form->loadDataFrom(Member::currentUser());
    $form->setTemplate('MemberProfilePage_UserDetailsForm');

    return $form;
}

public function SaveMyDetails($data, $form) {
    $table = Member::currentUser();
    $members = Member::get();
    $emailExists = $members->filter(array(
        'Email' => $data['Email'],
        'ID:not' => $table->ID
    ));

    if( $emailExists->count() > 0 ) {
        $form->sessionMessage('Sorry, that email address already exists. Please try again','bad');
        return $this->redirectBack();
    } else {
        $form->sessionMessage('Your details have been updated.','good');
    }

    $form->saveInto($table);
    $table->write();
    $this->redirectBack();

    return $this;
}

1 个答案:

答案 0 :(得分:1)

与任何其他优秀的CMS一样,出于安全原因,SilverStripe不会以明文形式在数据库中存储密码。密码需要进行哈希处理,因此无法直接设置。

这就是为什么您拨打$form->saveInto($table);的电话不会触及密码。

SilverStripe成员类具有changePassword()方法,您可以调用该方法来更新密码。将表单数据保存到Member对象后立即调用它:

$form->saveInto($table);
$table->changePassword($data['Password']['_Password']);
$table->write();

我还没有测试过这段代码,但我很确定它会起作用。

在相关主题上,您可能希望查看Member Profile插件。

希望这有帮助。