Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

Commit

Permalink
Controllers refactored to use the API.
Browse files Browse the repository at this point in the history
- Moved session handling into its own class.
- A LOT of TODO comments added
- Refactored the 3 controllers

Due to the state of the codebase, I'm not sure if any of this works.
  • Loading branch information
whymarrh committed Nov 4, 2013
1 parent b8f0768 commit 48ee90a
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 269 deletions.
2 changes: 1 addition & 1 deletion module/AbaLookup/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
'users' => [
'type' => 'Segment',
'options' => [
'route' => '/users/:id/:action[/:mode]',
'route' => '/users/:id/:action',
'constraints' => [
'id' => '[0-9]*',
'action' => '[a-zA-Z0-9_-]+',
Expand Down
103 changes: 31 additions & 72 deletions module/AbaLookup/src/AbaLookup/AbaLookupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,119 +2,78 @@

namespace AbaLookup;

use
Zend\Mvc\Controller\AbstractActionController,
Zend\Session\Container,
Zend\View\Model\ViewModel
;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

/**
* Base controller class
*/
abstract class AbaLookupController extends AbstractActionController
{
/**
* The key used to store the user in session
*/
const SESSION_USER_NAMESPACE = 'user';
const SESSION_USER_ID_KEY = 'id';

/**
* 3 months in seconds
*/
const SECONDS_3_MONTHS = 7884000;

/**
* Returns the API object for the given name
*
* Gets from the API factory the appropriate API and returns
* an instance.
*
* @param string $name The name of the API class.
* @return mixed
*/
protected function getApi($name)
{
// TODO - Does the API factory throw an exception?
return $this->serviceLocator('Lookup\ApiFactory')->get($name);
}

/**
* Returns a redirect to the login route
*
* @return ViewModel
*/
protected function redirectToLoginPage()
{
return $this->redirect()->toRoute('auth/login');
}

/**
* Sets the session for the given user
*
* @param int $id The user id.
* @param boolean $remember Whether to set an explicit TTL for the user session.
* @return ViewModel Redirect to the home route.
*/
protected function setUserSession($id, $remember = FALSE)
protected function redirectHome()
{
$session = new Container(self::SESSION_USER_NAMESPACE);
$session->getManager()
->getConfig()
->setCookieHttpOnly(TRUE); // As per issue #87
if (isset($remember) && $remember) {
$session->getManager()
->rememberMe(self::SECONDS_3_MONTHS);
}
$session->offsetSet(self::SESSION_USER_ID_KEY, $id);
return $this->redirect()->toRoute('home');
}

/**
* Unsets the session
* @return ViewModel Redirect to the login route.
*/
protected function unsetUserSession()
{
$session = new Container(self::SESSION_USER_NAMESPACE);
$session->offsetUnset(self::SESSION_USER_ID_KEY);
}

/**
* Returns whether a user is currently in session
*
* @return bool
*/
protected function isUserInSession()
protected function redirectToLoginPage()
{
$session = new Container(self::SESSION_USER_NAMESPACE);
return $session->offsetExists(self::SESSION_USER_ID_KEY);
return $this->redirect()->toRoute('auth/login');
}

/**
* Returns the id of the current user session
* Redirects to the users route given a user ID and an action
*
* @return Lookup\Entity\User|NULL
* @param int $id The user ID.
* @param string $action The route action.
* @return ViewModel Redirect to the users route.
*/
protected function currentSessionUser()
protected function redirectToUsersRoute($id, $action = 'profile')
{
if (!$this->isUserInSession()) {
return NULL;
}
$session = new Container(self::SESSION_USER_NAMESPACE);
return $this->getApi('UserAccount')
->get($session->offsetGet(self::SESSION_USER_ID_KEY));
$params = ['id' => $id, 'action' => $action];
return $this->redirect()->toRoute('users', $params);
}

/**
* Prepares the given layout to be displayed for the given user
* Prepares the layout to be displayed for the given user
*
* Nests the footer widget into the layout, and attaches the current
* to the layout as a variable.
* user to the layout as a variable.
*
* @param ViewModel $layout The layout for the view.
* @param Lookup\Entity\User $user The user currently in session.
* @param Lookup\Entity\User $user The user in session.
* @return void
*/
protected function prepareLayout($layout, Lookup\Entity\User $user = NULL)
protected function prepareLayout(Lookup\Entity\User $user = NULL)
{
// Add the footer
// Add the footer template
$layout = $this->layout();
$footer = new ViewModel();
$footer->setTemplate('widget/footer');
$layout->addChild($footer, 'footer');
// Add the user's URL
if (!isset($user)) {
if (is_null($user)) {
return;
}
// Attach the user to the view
$layout->setVariable('user', $user);
}
}
82 changes: 20 additions & 62 deletions module/AbaLookup/src/AbaLookup/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,34 @@

namespace AbaLookup;

/**
* Controller for home actions
*/
use AbaLookup\Session\Session;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\MvcEvent;

class HomeController extends AbaLookupController
{
/**
* Displays the home page
*
* @return array
*/
public function indexAction()
public function setEventManager(EventManagerInterface $events)
{
parent::setEventManager($events);
$events->attach(MvcEvent::EVENT_DISPATCH, [$this, 'beforeAction'], 100);
}

public function beforeAction()
{
// Set the home layout
$this->layout('layout/home');
$layout = $this->layout();
$user = $this->currentSessionUser();
$uid = Session::getUserId();
try {
$user = $this->getApi('UserAccount')
->get($uid);
} catch (Lookup\Api\Exception\InvalidDataException $e) {
// TODO - Handle this
$user = NULL;
}
// Prepare the layout
$this->prepareLayout($layout, $user);
$this->prepareLayout($user);
return [
'user' => $user,
];
}

/**
* Displays the privacy policy
*
* @return array
*/
public function privacyAction()
{
return $this->indexAction();
}

/**
* Displays the about page
*
* @return array
*/
public function aboutAction()
{
return $this->indexAction();
}

/**
* Displays the terms of service
*
* @return array
*/
public function termsAction()
{
return $this->indexAction();
}

/**
* Displays information about the sponsors
*
* @return array
*/
public function sponsorsAction()
{
return $this->indexAction();
}

/**
* Displays site colophon
*
* @return array
*/
public function colophonAction()
{
return $this->indexAction();
}
}
57 changes: 57 additions & 0 deletions module/AbaLookup/src/AbaLookup/Session/Session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace AbaLookup\Session;

use Zend\Session\Container;

/**
* Wrapper class around Zend\Session\Container
*/
class Session
{
/**
* 3 months in seconds
*/
const SECONDS_3_MONTHS = 7884000;

/**
* Namespace and keys for the user
*/
const USER_NAMESPACE = 'user';
const USER_KEY_ID = 'id';

/**
* Sets the user ID for the session
*
* @param int $id The user ID.
* @param bool $remember Whether to set an explicit TTL for the user session.
* @return void
*/
public static function setUserId($id, $remember = FALSE)
{
$session = new Container(Session::SESSION_USER_NAMESPACE);
$session->getManager()
->getConfig()
->setCookieHttpOnly(TRUE) // As per issue #87
->rememberMe((is_bool($remember) && $remember) ? Session::SECONDS_3_MONTHS : 0);
$session->offsetSet(Session::SESSION_USER_KEY_ID, $id);
}

/**
* @return int|NULL The ID of the user in session.
*/
public static function getUserId()
{
return (new Container(Session::SESSION_USER_NAMESPACE))->offsetGet(Session::SESSION_USER_ID_KEY);
}

/**
* Unsets the user ID for the session
*
* @return void
*/
public static function unsetUserId()
{
(new Container(Session::SESSION_USER_NAMESPACE))->offsetUnset(Session::SESSION_USER_ID_KEY);
}
}
Loading

0 comments on commit 48ee90a

Please sign in to comment.