/** * @param $username * @param $password * * @return bool * @throws exceptions\DBException */ public function login($username, $password) { if ($this->validateLogin($username, $password) === true) { $repo = new UserRepository($this->db); $user = $repo->where('name', '=', $username)->findSingle(true); $this->db->query('UPDATE users SET date_last_login = NOW() WHERE id = ' . $user->getId() . ';'); session_regenerate_id(true); $_SESSION['user'] = $user; $_SESSION['last_activity'] = time(); return true; } else { return false; } }
/** * @param Request $request * * @return string * @throws \Exception * @throws exceptions\NotFoundException */ public function run(Request $request) { if ($request->post('action')) { $method = $request->post('action'); if (method_exists($this, $method)) { $this->{$method}($request); } else { throw new BadMethodCallException(); } } $repo = new UserRepository($this->db); $user = $repo->where('id', '=', $this->user->getId())->findSingle(true); if (!$user) { throw new NotFoundException('user not found'); } $view = new UserView($user, $this->errors); return $view->display(); }
/** @noinspection PhpUnusedPrivateMethodInspection * @param Request $request * * @return bool */ private function sendNewPassword(Request $request) { $user_id = Validator::sanitizeText($request->post('user_id')); if (!$user_id) { throw new UnexpectedValueException(); } $repo = new UserRepository($this->db); $user = $repo->where('id', '=', $user_id)->findSingle(); $password = $this->auth->generatePassword(); //print_r('new pw is '.$password); // TODO remove this $this->auth->setPassword($user->getName(), $password); $subject = 'Your password at ' . Request::createUrl() . ' was reset!'; $message = 'Greetings,' . "\n\n" . $this->auth->getCurrentUser()->getName() . ' has reset your password for you at ' . Request::createUrl() . '.' . "\n\n"; $message .= 'The new temporary password is: ' . $password . "\n"; $message .= "\n" . 'Please change your password soon at ' . Request::createUrl(array('p' => 'user')) . '!'; if (MailHandler::sendMail($user->getMail(), $subject, $message)) { return true; } else { $this->errors[] = 'The mail to the user could not be sent'; return false; } }