0

我一直在关注敏捷工具包“书”,并且已经达到: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();
        }
    }
}

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

4

1 回答 1

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();
        }
    }
}
于 2012-08-24T20:38:17.840 回答