/** * Initializes session, checks is user's info saves in session and gets it from db if it is */ public static function init() { if (!session_id()) { session_start(); } if ($id = self::get('username')) { $model = new SecurityModel(); if ($item = $model->set('id', $id)->getItem()) { $model->update(); self::$_user = $item; } } }
/** * Renders login form and authenticates user after form submitting * * @return string */ public function loginAction() { $this->_redirectIfLoggedIn(); $errors = array(); if (Request::isPost()) { $model = new SecurityModel(); if ($item = $model->set('email', Request::get('email'))->getItem()) { if (0 === strcmp(Token::cryptPassword(Request::get('password'), $item->salt), $item->password)) { Token::setUser($item); $this->redirect('/'); } } array_push($errors, 'Invalid username or password'); } return $this->_renderView('login.html', array('errors' => $errors)); }
/** * Updates user's profile * * @return string */ public function updateAction() { if (!$this->getUser()) { $this->redirect('/login', 'Please, login first!'); } $errors = array(); $model = new SecurityModel(); $model->setItem($this->getUser()); $model->set('email', Request::get('email'))->set('name', Request::get('name')); if ($model->isValid()) { try { $model->update(); $this->redirect('/', 'Data has been saved successfully'); } catch (DatabaseException $e) { $errors['email'] = 'Email already exists!'; } } else { $errors = $model->getErrors(); } return $this->_renderView('form.html', array('user' => $this->getUser(), 'errors' => $errors)); }