public function setModel($model, $login_field = 'email', $password_field = 'password')
{
parent::setModel($model);
$this->login_field = $login_field;
$this->password_field = $password_field;
// Load model from session
if ($this->info && $this->recall('id')) {
if ($this->recall('class', false) == get_class($this->model)) {
$this->debug('Loading model from cache');
$this->model->set($this->info);
$this->model->dirty = array();
$this->model->id = $this->recall('id', null);
} else {
// Class changed, re-fetch data from database
$this->debug('Class changed, loading from database');
$this->model->tryLoad($this->recall('id'));
if (!$this->model->loaded()) {
$this->logout();
}
$this->memorizeModel();
}
}
$id = $this->hook('tryLogin', array($model, $login_field, $password_field));
if ($id && is_numeric($id)) {
$this->model->tryLoad($id);
$this->memorizeModel();
}
$t = $this;
// If model is saved, update our cache too, but don't store password
$this->model->addHook('afterSave', function ($m) use($t) {
// after this model is saved, re-cache the info
$tmp = $m->get();
unset($tmp[$t->password_field]);
if ($t->app instanceof App_Web) {
$t->memorize('info', $tmp);
}
});
$this->addEncryptionHook($this->model);
if (strtolower($this->app->page) == 'logout') {
$this->logout();
$this->app->redirect('/');
}
return $this->model;
}