* @license PHP License * @package WB * @subpackage rest */ WBClass::load('WBRest_Component' , 'WBUser' ); /** * Rest Component: Auth * * * * @version 0.2.0 * @package WB * @subpackage rest */ class WBRest_Component_Auth extends WBRest_Component { /** * Current user * @var WBUser_Auth */ protected $user; /** * 2nd constructor * */ public function init() { WBClass::load( 'WBUser_Auth' ); $this->user = WBUser_Auth::getCurrent(); } /** * login user * * Use "nickname" and "password" to login user */ public function handleLogin() { if ($this->user->isAuthenticated()) { $this->addUserData(); return; } $credential = array(); $this->out['authendicated'] = 0; if (!isset($this->in['nickname']) || empty($this->in['nickname'])) { $this->error[] = array( 'code' => $this->serviceName . ':login:1', 'message' => 'Nickname is required but not given or empty' ); } else { $credential['nickname'] = $this->in['nickname']; } if (!isset($this->in['password']) || empty($this->in['password'])) { $this->error[] = array( 'code' => $this->serviceName . ':login:2', 'message' => 'Password is required but not given or empty' ); } else { $credential['password'] = $this->in['password']; } if (count($credential) < 2) { $this->status = WBRest::STATUS_FAILED; return; } $this->user->login($credential); if ($this->user->isAuthenticated()) { $this->addUserData(); return; } $this->status = WBRest::STATUS_FAILED; return; } /** * logout user * * Logout user in case user is authenticated, otherwise do nothing */ public function handleLogout() { if ($this->user->isAuthenticated()) { $this->user->logout(); } $this->out['authendicated'] = 0; } /** * Implement behavour for empty requests * * either login, logout or display user data * @see WBRestComponent::emptyRequest() */ public function emptyRequest() { // mangle allowed methods $this->allowedMethods = array( WBRest::METHOD_GET, WBRest::METHOD_POST, WBRest::METHOD_DELETE ); switch ($this->method) { case WBRest::METHOD_POST: return $this->handleLogin(); break; case WBRest::METHOD_DELETE: return $this->handleLogout(); break; case WBRest::METHOD_GET: break; default: return; break; } $this->addUserData(); } /** * inject current user's data to output * * Check whether user is logged in an add her data */ protected function addUserData() { $this->out['authendicated'] = 0; if (!$this->user->isAuthenticated()) { return; } $this->out['authendicated'] = 1; $this->out['id'] = $this->user->getId(); $this->out['nickname'] = $this->user->getNickname(); $data = $this->user->getData(); $keys = array( 'forename', 'surname', 'email', 'lang', 'created', 'changed', 'lastlogin' ); foreach ($keys as $k) { $this->out[$k] = $data[$k]; } } } ?>