【问题标题】:Agile Toolkit: Password Change FormAndSave敏捷工具包:密码更改表格和保存
【发布时间】:2012-08-24 13:57:43
【问题描述】:

我一直在关注敏捷工具包“书”,我已经到达:http://agiletoolkit.org/learn/app/auth

我尝试使用提供的代码:

class page_account extends Page {
    function init(){
        parent::init();

        $this->api->auth->check();

        $model = $this->add('Model_Customer');
        $model->getField('email')->system(true);
        $this->add('FormAndSave')->setModel($model)->loadData($this->api->auth->get('id'));
    }
}

但这只是给了我一个模型未设置错误,所以知道 FormAndSave 从哪里派生我将代码更改为:

class page_account extends Page {
    function init(){
        parent::init();

        $this->api->auth->check();

        $model = $this->add('Model_Customer');

        $saveForm=$this->add('Form');

        $saveForm->setModel($model)->loadData($this->api->auth->get('id'));

        $saveForm->addSubmit();

        $saveForm->onSubmit(function($saveForm) {

        try {
            $saveForm->update()->js()->univ()->successMessage('Saved changes.')->execute();
        } catch(Exception $e) {
            $saveForm->js()->univ()->alert('Failed to save.')->execute();
        }
});

    }
}

这至少可以让我保存数据,但我无法显示密码字段。我可以通过以下方式将其添加到模型中:

$model = $this->add('Model_Customer');
$model->addField('password', 'password');

问题在于显示散列密码(很明显,呵呵)并添加 ->system(true) 只会使其不可见。这是 Model_Customer:

class Model_Customer extends Model_Table {
    public $table='customer';

    function init() {
        parent::init();

        $this->addField('name');
        $this->addField('email');
    }
}

非常感谢您的帮助 - 有一些解释会很棒,我正在学习这个框架,我学得越多越好。

目前表单没有显示密码字段供用户编辑他/她的密码 - 我该如何实现该功能?就像我说的那样,如果我再次将它添加到模型中,我可以显示该字段,但它显示的哈希密码确实不是你想要的。伙计们,我该如何正确地做到这一点?

谢谢!

更新:我有它的工作,但不确定这是否是正确或安全的方式:

    class page_account extends Page {
    function init(){
        parent::init();

        $this->api->auth->check();

        $auth=$this->api->auth;

        $model = $this->add('Model_Customer');

        $model->addField('password')->type('password');

        $saveForm=$this->add('MVCForm');

        $saveForm->setModel($model)->loadData($this->api->auth->get('id'));

        $saveForm->set('password', '');

        $saveForm->addSubmit();

        if($saveForm->isSubmitted()){

            // Short-cuts
            $auth=$this->api->auth;
            $l=$saveForm->get('email');
            $p=$saveForm->get('password');

            if ($p) {
                // Manually encrypt password
                $enc_p = $auth->encryptPassword($p,$l);
                $saveForm->set('password', $enc_p);
            } else {
                $saveForm->set('password', $model->get('password'));
            }

            $saveForm->update()->js()->univ()->successMessage('Saved user information. ')->execute();
        }
    }
}

这会为密码创建一个空字段,仅当您在其中输入内容时才会更新。

【问题讨论】:

  • 我看到一些类似于隐含问题的内容,但需要明确提出。提及您希望代码执行的操作也会有所帮助。
  • 也很抱歉@BenBarden 的问题现在已经说得更清楚了。

标签: php frameworks atk4


【解决方案1】:

我认为这是正确的做法,但很难确定。它确实有效,我看不到任何安全问题。

class page_account extends Page {
    function init(){
        parent::init();

        $this->api->auth->check();

        $auth=$this->api->auth;

        $model = $this->add('Model_Customer');

        $model->addField('password')->type('password');

        $saveForm=$this->add('MVCForm');

        $saveForm->setModel($model)->loadData($this->api->auth->get('id'));

        $saveForm->set('password', '');

        $saveForm->addSubmit();

        if($saveForm->isSubmitted()){

            // Short-cuts
            $auth=$this->api->auth;
            $l=$saveForm->get('email');
            $p=$saveForm->get('password');

            if ($p) {
                // Manually encrypt password
                $enc_p = $auth->encryptPassword($p,$l);
                $saveForm->set('password', $enc_p);
            } else {
                $saveForm->set('password', $model->get('password'));
            }

            $saveForm->update()->js()->univ()->successMessage('Saved user information. ')->execute();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多