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

require_once 'diy-blog/model/BackEndResource.php';

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

/**
 * Backend Resource for displaying a User list.
 * Originally generated by Propel.
 *
 * @package	diy-blog.model
 * @author	Martynas Jusevicius <hide@address.com>
 * @link	http://www.xml.lt
 */

class AdminUserListResource extends BackEndResource
{

	/**
	 * Constructs a new AdminUserListResource class, setting the Type column to BackEndResourcePeer::CLASSKEY_ADMINUSERLISTRESOURCE.
	 */
	public function __construct()
	{

		$this->setType(BackEndResourcePeer::CLASSKEY_ADMINUSERLISTRESOURCE);
	}

	public static function getInstance()
	{
		$c = new Criteria();
		$c->add(BackEndResourcePeer::TYPE, BackEndResourcePeer::CLASSKEY_ADMINUSERLISTRESOURCE);
		return BackEndResourcePeer::doSelectOne($c);
	}

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

		if ($parent != null) $view = $parent;
		else
		try
		{
			$view = new UserListView($this);

			if ($request->getParameter("view") == "new")
			{
				if (!($request->getSession()->getAttribute("user") instanceof AdminUser)) throw new NoPermissionException();

				$view = new CreateUserView($this);
			}
		}
		catch (NoPermissionException $e)
		{
			$view = new ForbiddenView($this);
		}

		return $view;
	}

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

		if ($parent != null) $view = $parent;
		else
		try
		{
			$view = new UserListView($this);

			if ($request->getParameter("action") == "create")
			{
				$errors = array();
				$form = new CreateUserForm($request);
				$errors = $form->validate();

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

					$this->createUser($form->getName(), $form->getPassword(), $form->getType());
					$request->setAttribute("user-result", "success");

					$response->sendRedirect(BackEndMapping::getHost().AdminUserListResource::getInstance()->getURI());
				}
				catch (InvalidFormException $e)
				{
					$view = new CreateUserView($this);
					$request->setAttribute("user-result", "failure");
				}
				catch (ObjectExistsException $e)
				{
					$errors[] = new Error("alreadyExists");
					$view = new CreateUserView($this);
					$request->setAttribute("user-result", "failure");
				}

				$request->setAttribute("user-form", $form);
				$request->setAttribute("user-errors", $errors);
			}
		}
		catch (NoPermissionException $e)
		{
			$view = new ForbiddenView($this);
		}

		return $view;

	}

	private function createUser($name, $password, $type)
	{
		if (UserPeer::retrieveByPk($name)) throw new ObjectExistsException();

		$resource = new AdminUserResource();
		$resource->setURI(AdminUserListResource::getInstance()->getURI().urlencode($name));
		
		if ($type == "admin") $user = new AdminUser();
		else $user = new LimitedUser();
		$user->setName($name);
		$user->setPassword(md5($password));
		$user->setBackEndResource($resource);
		$user->save();
	}

} // AdminUserListResource
Return current item: DIY Blog