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

Commit

Permalink
Refactored AbaLookup module.
Browse files Browse the repository at this point in the history
Still to-do:

- Refactor test classes.
- Remove TODO notes from comments and flesh out accordingly
- Update views

In progress:

- Merging profile and profile edit views (MUNComputerScienceSociety#99)
- Add ScheduleForm in place of 'hard-coded' form

Blockers:

- Issues MUNComputerScienceSociety#84, MUNComputerScienceSociety#82, MUNComputerScienceSociety#79, MUNComputerScienceSociety#65

In this commit:

- Moved authentication actions into their own controller
- Moved registration into AuthController
- Merging profile and profile edit views
  • Loading branch information
whymarrh committed Nov 4, 2013
1 parent 48ee90a commit dbea85c
Show file tree
Hide file tree
Showing 15 changed files with 315 additions and 733 deletions.
10 changes: 5 additions & 5 deletions module/AbaLookup/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
return [
'controllers' => [
'invokables' => [
'Auth' => 'AbaLookup\AuthController',
'Home' => 'AbaLookup\HomeController',
'Users' => 'AbaLookup\UsersController',
],
Expand Down Expand Up @@ -89,7 +90,7 @@
'options' => [
'route' => '/users',
'defaults' => [
'controller' => 'Users',
'controller' => 'Auth',
],
],
'may_terminate' => FALSE,
Expand Down Expand Up @@ -138,11 +139,10 @@
realpath(sprintf('%s/../view', __DIR__)),
],
'template_map' => [
'layout/layout' => realpath(sprintf('%s/../view/layout/layout.phtml', __DIR__)),
'layout/home' => realpath(sprintf('%s/../view/layout/home.phtml', __DIR__)),
'error/index' => realpath(sprintf('%s/../view/error/index.phtml', __DIR__)),
'error/404' => realpath(sprintf('%s/../view/error/404.phtml', __DIR__)),
'profile/edit' => realpath(sprintf('%s/../view/aba-lookup/users/profile-edit.phtml', __DIR__)),
'error/index' => realpath(sprintf('%s/../view/error/index.phtml', __DIR__)),
'layout/home' => realpath(sprintf('%s/../view/layout/home.phtml', __DIR__)),
'layout/layout' => realpath(sprintf('%s/../view/layout/layout.phtml', __DIR__)),
'widget/footer' => realpath(sprintf('%s/../view/aba-lookup/widget/footer.phtml', __DIR__)),
],
],
Expand Down
3 changes: 0 additions & 3 deletions module/AbaLookup/src/AbaLookup/AbaLookupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

/**
* Base controller class
*/
abstract class AbaLookupController extends AbstractActionController
{
/**
Expand Down
133 changes: 133 additions & 0 deletions module/AbaLookup/src/AbaLookup/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace AbaLookup;

use AbaLookup\Form\LoginForm;
use AbaLookup\Form\RegisterForm;
use AbaLookup\Session\Session;

class AuthController extends AbaLookupController
{
public function __construct()
{
// Is a user logged in?
$uid = Session::getUserId();
if (!is_null($uid)) {
// Redirect the user to their profile page
$this->redirectToUsersRoute($uid);
return;
}
// Prepare the view layout
$this->prepareLayout();
}

/**
* Registers the user or shows a registration form
*
* Shows the registration form or sends the POST data along to the
* API for validation as needed.
*
* @return array|Zend\Http\Response
*/
public function registerAction()
{
// Get the user type from the URL
$type = $this->params('type');
// Create a registration form for the particular
// type of user that is registering
$form = new RegisterForm($type);
// If the user has NOT submitted a POST request
if (!$this->request->isPost()) {
// Show the registration form
return [
'form' => $form,
'type' => $type,
];
}
// The user has submitted via POST
// TODO - Validate Terms of Service
// TODO - Show previous data to user
$data = $this->params(); // TODO - Is this correct?
try {
$id = $this->getApi('UserAccount')->put(
$data->fromPost($form::ELEMENT_NAME_EMAIL_ADDRESS),
$data->fromPost($form::ELEMENT_NAME_PASSWORD),
$data->fromPost($form::ELEMENT_NAME_DISPLAY_NAME),
$data->fromPost($form::ELEMENT_NAME_USER_TYPE),
$data->fromPost($form::ELEMENT_NAME_POSTAL_CODE),
array_intersect_key(
$data->fromPost(),
// Flip this array to get the keys that are valid
// Only the valid keys remain from the POST data
array_flip([
$form::ELEMENT_NAME_ABA_COURSE,
$form::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT,
$form::ELEMENT_NAME_CERTIFICATE_OF_CONDUCT_DATE,
$form::ELEMENT_NAME_GENDER,
$form::ELEMENT_NAME_PHONE_NUMBER,
])
)
);
} catch (Lookup\Api\Exception\InvalidDataException $e) {
// Show the user the error message
return [
'error' => $e->getMessage(),
'form' => $form,
'type' => $type,
];
}
Session::setUserId($id);
// Redirect the user to their profile page
return $this->redirectToUsersRoute($id);
}

/**
* Logs the user in
*
* Sends the POST data along to the API as needed.
*
* @return array|Zend\Http\Response
*/
public function loginAction()
{
// Create a login form
$form = new LoginForm();
// If the user has NOT submitted a POST request
if (!$this->request->isPost()) {
// Show the login form
return [
'form' => $form,
];
}
// The user has submitted data via POST
$data = $this->params();
try {
$id = $this->getApi('UserAccount')->get([
'email' => $data->fromPost($form::ELEMENT_NAME_EMAIL_ADDRESS),
'password' => $data->fromPost($form::ELEMENT_NAME_PASSWORD),
]);
} catch (Lookup\Api\Exception\InvalidDataException $e) {
return [
'error' => $e->getMessage(),
'form' => $form,
];
}
// Create a session for the user
Session::setUserId($id, (bool) $data->fromPost($form::ELEMENT_NAME_REMEMBER_ME));
return $this->redirectToUsersRoute($id);
}

/**
* Logs the user out
*
* If a user is logged in, log them out. Invalidates the session.
* Reroutes the user to the home page.
*
* @return Zend\Http\Response
*/
public function logoutAction()
{
Session::unsetUserId();
return $this->redirectHome();
}
}
Loading

0 comments on commit dbea85c

Please sign in to comment.