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

Commit 48ee90a

Browse files
committed
Controllers refactored to use the API.
- 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.
1 parent b8f0768 commit 48ee90a

File tree

5 files changed

+199
-269
lines changed

5 files changed

+199
-269
lines changed

module/AbaLookup/config/module.config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
'users' => [
7474
'type' => 'Segment',
7575
'options' => [
76-
'route' => '/users/:id/:action[/:mode]',
76+
'route' => '/users/:id/:action',
7777
'constraints' => [
7878
'id' => '[0-9]*',
7979
'action' => '[a-zA-Z0-9_-]+',

module/AbaLookup/src/AbaLookup/AbaLookupController.php

Lines changed: 31 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,119 +2,78 @@
22

33
namespace AbaLookup;
44

5-
use
6-
Zend\Mvc\Controller\AbstractActionController,
7-
Zend\Session\Container,
8-
Zend\View\Model\ViewModel
9-
;
5+
use Zend\Mvc\Controller\AbstractActionController;
6+
use Zend\View\Model\ViewModel;
107

8+
/**
9+
* Base controller class
10+
*/
1111
abstract class AbaLookupController extends AbstractActionController
1212
{
13-
/**
14-
* The key used to store the user in session
15-
*/
16-
const SESSION_USER_NAMESPACE = 'user';
17-
const SESSION_USER_ID_KEY = 'id';
18-
19-
/**
20-
* 3 months in seconds
21-
*/
22-
const SECONDS_3_MONTHS = 7884000;
23-
2413
/**
2514
* Returns the API object for the given name
2615
*
16+
* Gets from the API factory the appropriate API and returns
17+
* an instance.
18+
*
2719
* @param string $name The name of the API class.
2820
* @return mixed
2921
*/
3022
protected function getApi($name)
3123
{
24+
// TODO - Does the API factory throw an exception?
3225
return $this->serviceLocator('Lookup\ApiFactory')->get($name);
3326
}
3427

3528
/**
36-
* Returns a redirect to the login route
37-
*
38-
* @return ViewModel
39-
*/
40-
protected function redirectToLoginPage()
41-
{
42-
return $this->redirect()->toRoute('auth/login');
43-
}
44-
45-
/**
46-
* Sets the session for the given user
47-
*
48-
* @param int $id The user id.
49-
* @param boolean $remember Whether to set an explicit TTL for the user session.
29+
* @return ViewModel Redirect to the home route.
5030
*/
51-
protected function setUserSession($id, $remember = FALSE)
31+
protected function redirectHome()
5232
{
53-
$session = new Container(self::SESSION_USER_NAMESPACE);
54-
$session->getManager()
55-
->getConfig()
56-
->setCookieHttpOnly(TRUE); // As per issue #87
57-
if (isset($remember) && $remember) {
58-
$session->getManager()
59-
->rememberMe(self::SECONDS_3_MONTHS);
60-
}
61-
$session->offsetSet(self::SESSION_USER_ID_KEY, $id);
33+
return $this->redirect()->toRoute('home');
6234
}
6335

6436
/**
65-
* Unsets the session
37+
* @return ViewModel Redirect to the login route.
6638
*/
67-
protected function unsetUserSession()
68-
{
69-
$session = new Container(self::SESSION_USER_NAMESPACE);
70-
$session->offsetUnset(self::SESSION_USER_ID_KEY);
71-
}
72-
73-
/**
74-
* Returns whether a user is currently in session
75-
*
76-
* @return bool
77-
*/
78-
protected function isUserInSession()
39+
protected function redirectToLoginPage()
7940
{
80-
$session = new Container(self::SESSION_USER_NAMESPACE);
81-
return $session->offsetExists(self::SESSION_USER_ID_KEY);
41+
return $this->redirect()->toRoute('auth/login');
8242
}
8343

8444
/**
85-
* Returns the id of the current user session
45+
* Redirects to the users route given a user ID and an action
8646
*
87-
* @return Lookup\Entity\User|NULL
47+
* @param int $id The user ID.
48+
* @param string $action The route action.
49+
* @return ViewModel Redirect to the users route.
8850
*/
89-
protected function currentSessionUser()
51+
protected function redirectToUsersRoute($id, $action = 'profile')
9052
{
91-
if (!$this->isUserInSession()) {
92-
return NULL;
93-
}
94-
$session = new Container(self::SESSION_USER_NAMESPACE);
95-
return $this->getApi('UserAccount')
96-
->get($session->offsetGet(self::SESSION_USER_ID_KEY));
53+
$params = ['id' => $id, 'action' => $action];
54+
return $this->redirect()->toRoute('users', $params);
9755
}
9856

9957
/**
100-
* Prepares the given layout to be displayed for the given user
58+
* Prepares the layout to be displayed for the given user
10159
*
10260
* Nests the footer widget into the layout, and attaches the current
103-
* to the layout as a variable.
61+
* user to the layout as a variable.
10462
*
105-
* @param ViewModel $layout The layout for the view.
106-
* @param Lookup\Entity\User $user The user currently in session.
63+
* @param Lookup\Entity\User $user The user in session.
64+
* @return void
10765
*/
108-
protected function prepareLayout($layout, Lookup\Entity\User $user = NULL)
66+
protected function prepareLayout(Lookup\Entity\User $user = NULL)
10967
{
110-
// Add the footer
68+
// Add the footer template
69+
$layout = $this->layout();
11170
$footer = new ViewModel();
11271
$footer->setTemplate('widget/footer');
11372
$layout->addChild($footer, 'footer');
114-
// Add the user's URL
115-
if (!isset($user)) {
73+
if (is_null($user)) {
11674
return;
11775
}
76+
// Attach the user to the view
11877
$layout->setVariable('user', $user);
11978
}
12079
}

module/AbaLookup/src/AbaLookup/HomeController.php

Lines changed: 20 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,34 @@
22

33
namespace AbaLookup;
44

5-
/**
6-
* Controller for home actions
7-
*/
5+
use AbaLookup\Session\Session;
6+
use Zend\EventManager\EventManagerInterface;
7+
use Zend\Mvc\MvcEvent;
8+
89
class HomeController extends AbaLookupController
910
{
10-
/**
11-
* Displays the home page
12-
*
13-
* @return array
14-
*/
15-
public function indexAction()
11+
public function setEventManager(EventManagerInterface $events)
12+
{
13+
parent::setEventManager($events);
14+
$events->attach(MvcEvent::EVENT_DISPATCH, [$this, 'beforeAction'], 100);
15+
}
16+
17+
public function beforeAction()
1618
{
1719
// Set the home layout
1820
$this->layout('layout/home');
19-
$layout = $this->layout();
20-
$user = $this->currentSessionUser();
21+
$uid = Session::getUserId();
22+
try {
23+
$user = $this->getApi('UserAccount')
24+
->get($uid);
25+
} catch (Lookup\Api\Exception\InvalidDataException $e) {
26+
// TODO - Handle this
27+
$user = NULL;
28+
}
2129
// Prepare the layout
22-
$this->prepareLayout($layout, $user);
30+
$this->prepareLayout($user);
2331
return [
2432
'user' => $user,
2533
];
2634
}
27-
28-
/**
29-
* Displays the privacy policy
30-
*
31-
* @return array
32-
*/
33-
public function privacyAction()
34-
{
35-
return $this->indexAction();
36-
}
37-
38-
/**
39-
* Displays the about page
40-
*
41-
* @return array
42-
*/
43-
public function aboutAction()
44-
{
45-
return $this->indexAction();
46-
}
47-
48-
/**
49-
* Displays the terms of service
50-
*
51-
* @return array
52-
*/
53-
public function termsAction()
54-
{
55-
return $this->indexAction();
56-
}
57-
58-
/**
59-
* Displays information about the sponsors
60-
*
61-
* @return array
62-
*/
63-
public function sponsorsAction()
64-
{
65-
return $this->indexAction();
66-
}
67-
68-
/**
69-
* Displays site colophon
70-
*
71-
* @return array
72-
*/
73-
public function colophonAction()
74-
{
75-
return $this->indexAction();
76-
}
7735
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace AbaLookup\Session;
4+
5+
use Zend\Session\Container;
6+
7+
/**
8+
* Wrapper class around Zend\Session\Container
9+
*/
10+
class Session
11+
{
12+
/**
13+
* 3 months in seconds
14+
*/
15+
const SECONDS_3_MONTHS = 7884000;
16+
17+
/**
18+
* Namespace and keys for the user
19+
*/
20+
const USER_NAMESPACE = 'user';
21+
const USER_KEY_ID = 'id';
22+
23+
/**
24+
* Sets the user ID for the session
25+
*
26+
* @param int $id The user ID.
27+
* @param bool $remember Whether to set an explicit TTL for the user session.
28+
* @return void
29+
*/
30+
public static function setUserId($id, $remember = FALSE)
31+
{
32+
$session = new Container(Session::SESSION_USER_NAMESPACE);
33+
$session->getManager()
34+
->getConfig()
35+
->setCookieHttpOnly(TRUE) // As per issue #87
36+
->rememberMe((is_bool($remember) && $remember) ? Session::SECONDS_3_MONTHS : 0);
37+
$session->offsetSet(Session::SESSION_USER_KEY_ID, $id);
38+
}
39+
40+
/**
41+
* @return int|NULL The ID of the user in session.
42+
*/
43+
public static function getUserId()
44+
{
45+
return (new Container(Session::SESSION_USER_NAMESPACE))->offsetGet(Session::SESSION_USER_ID_KEY);
46+
}
47+
48+
/**
49+
* Unsets the user ID for the session
50+
*
51+
* @return void
52+
*/
53+
public static function unsetUserId()
54+
{
55+
(new Container(Session::SESSION_USER_NAMESPACE))->offsetUnset(Session::SESSION_USER_ID_KEY);
56+
}
57+
}

0 commit comments

Comments
 (0)