protected function _edit_agent() { $t = $this->_template; $t->add('_jsapps', 'agent_form'); $storage = \Core\Storage::container()->get_storage('Agent'); $mapper = \Trouble\Agent::mapper()->attach_storage($storage); $t->errors = array(); if ($this->_args['alias']) { $t->title = "Edit Agent"; $agent = \Trouble\Agent::container()->get_by_alias($this->_args['alias']); $this->_auth->check_admin('agent', $agent->id); } else { try { $t->title = "Edit Yourself"; $agent = \Trouble\Agent::container()->get_by_id($this->_auth->user_id()); } catch (\Core\AuthNotLoggedInError $e) { $t->title = "Agent Application"; $t->new = True; $agent = \Trouble\Agent::create(); } } $t->agent = $agent; return $t->render('forms/agent.php'); }
/** * TODO: restrict agent to logged in user or admin */ public function save_agent() { import('core.validation'); import('trouble.agent'); $validator = \Core\Validator::validator('\\Trouble\\Agent'); $editing = $_POST['id'] > 0 ? True : False; try { if ($editing) { if ($_POST['id'] != $this->_auth->user_id()) { $this->_auth->check_admin('agent', $_POST['id']); } $agent = \Trouble\Agent::container()->get_by_id($_POST['id']); $validator->set_id($agent->id); $_POST['alias'] = $agent->alias; $agent->overwrite($_POST, True); } else { if (empty($_POST['password'])) { throw new \Core\ValidationError(array('Password must be set on creation.')); } $agent = \Trouble\Agent::create($_POST, True); } $validator->validate($_POST); try { \Core\Auth::hash($agent, 'password'); } catch (\Core\AuthEmptyPasswordError $e) { $agent->remove('password'); } \Core\Storage::container()->get_storage('Agent')->save($agent); if ($editing) { $this->_auth->user_data($agent); echo $this->_return_message("Success", "Saved."); } else { echo $this->_return_message("Success", "Created agent. You may now log in."); } } catch (\Core\ValidationError $e) { echo $this->_return_message("Fail", "Validation error(s):", $e->get_errors()); } catch (\Core\AuthNotLoggedInError $e) { $this->_not_logged_in(); } catch (\Core\AuthDeniedError $e) { $this->_access_denied(); } catch (\Exception $e) { $this->_unhandled_exception($e); } }