我一直在关注敏捷工具包"书",我已经达到: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');
问题是显示散列密码(显然是呵呵),添加->系统(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();
}
}
}
这为密码创建了一个空字段,只有在您放入某些内容时才会更新。
我认为这是正确的方法,尽管很难确定。它确实有效,我看不出任何安全问题。
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();
}
}
}