Location: PHPKode > projects > DIY Blog > diy-blog/classes/diy-blog/model/BackEndResource.php
<?php

require_once 'diy-blog/model/om/BaseBackEndResource.php';

/**
 * A parent backend Resource, inherited the by specific ones.
 * Originally generated by Propel.
 *
 * @package	diy-blog.model
 * @author	Martynas Jusevicius <hide@address.com>
 * @link	http://www.xml.lt
 */

class BackEndResource extends BaseBackEndResource
{

	public function doGet(Request $request, Response $response)
	{
		$view = parent::doGet($request, $response);

		// GuestUser set in BackEndController
		if ($request->getSession()->getAttribute("user") instanceof GuestUser) $view = new ForbiddenView($this);

		return $view;
	}

	public function doPost(Request $request, Response $response)
	{
		$view = parent::doPost($request, $response);

		if ($request->getParameter("action") == "login")
		{
			$form = new LoginForm($request);
			$errors = $form->validate();

			try
			{
				if (count($errors) != 0) throw new InvalidFormException();

				$user = $this->login($form->getUserName(), $form->getPassword());

				if ($user == null) throw new WrongPasswordException();

				$request->getSession()->setAttribute("user", $user);
				$request->setAttribute("login-result", "success");

			}
			catch (InvalidFormException $e)
			{
				$request->setAttribute("login-result", "failure");
			}
			catch (WrongPasswordException $e)
			{
				$errors[] = new Error("noUser");
				$request->setAttribute("login-result", "failure");
			}

			$request->setAttribute("login-form", $form);
			$request->setAttribute("login-errors", $errors);
		}

		// GuestUser set in BackEndController
		if ($request->getSession()->getAttribute("user") instanceof GuestUser) $view = new ForbiddenView($this);

		return $view;
	}

	private function login($userName, $password)
	{
		/*
		$user = UserPeer::retrieveByPk($userName);
		if ($user != null && $user->getPassword() != md5($password)) throw new WrongPasswordException();
		return $user;
		*/
		$c = new Criteria();
		$criterion = $c->getNewCriterion(UserPeer::NAME, $userName, Criteria::EQUAL);
		$criterion->addAnd($c->getNewCriterion(UserPeer::PASSWORD, md5($password), Criteria::EQUAL));
		$c->add($criterion);
		$user = null;
		$users = UserPeer::doSelect($c);
		if (count($users) == 1) return $users[0];
		else return null;
	}

} // BackEndResource
Return current item: DIY Blog