Skip to content

Instantly share code, notes, and snippets.

@lsolesen
Created January 4, 2011 15:46
Show Gist options
  • Select an option

  • Save lsolesen/764935 to your computer and use it in GitHub Desktop.

Select an option

Save lsolesen/764935 to your computer and use it in GitHub Desktop.
Controller for the Zend Framework - making it testable?
<?php
/**
* This holds the auth controller class
*
* @package Insight
* @subpackage Controllers
*/
/**
* The auth controller class
*
* @package Insight
* @subpackage Controllers
*/
class AuthController extends Zend_Controller_Action
{
public function indexAction()
{
$this->_redirect('/auth/login');
}
public function loginAction()
{
$view = Zend_Registry::get("view");
$msg = Zend_Registry::get("message");
$step = 1;
if ($this->_request->isPost()) {
$filters['*'] = 'StringTrim';
$validators['username'] = new Zend_Validate_Regex('/^[a-zA-Z0-9_-]{1,40}$/');
$validators['password'] = new Zend_Validate_Regex('/^[a-zA-Z0-9_-]{1,40}$/');
$validators['smscode'] = new Zend_Validate_Regex('/^[0-9]{4}$/');
$input = new Zend_Filter_Input($filters, $validators, $_POST);
if (!$input->isValid('username')) {
$message = "Ugyldigt brugernavn";
} elseif (!$input->isValid('password')) {
$message = "Ugyldigt kodeord";
} else {
if ($input->isValid('smscode')) {
$smscode = $input->smscode;
} else {
$smscode = null;
}
$authada = new Zend_Auth_Adapter_Insight($input->username, $input->password, $_SERVER['REMOTE_ADDR'], $smscode);
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($authada);
if ($result->isValid()) {
$_SESSION['user'] = $result->getIdentity();
$this->_redirect('/');
} else {
$authmsg = $result->getMessages();
$authmsg = $authmsg[0];
if ($authmsg == "user not found") {
$message = "Forkert brugernavn eller kodeord";
} elseif ($authmsg == "user disabled") {
$message = "Brugeren er disabled, kontakt IT på xxxxxxx";
} elseif ($authmsg == "ip not allowed, no phone") {
$message = "IP ".$_SERVER['REMOTE_ADDR']." ikke tiladt, der kunne ikke findes et telefonnr på brugeren så der kunne ikke sendes en smskode";
} elseif ($authmsg == "ip not allowed, smscode sent") {
$resdata = $result->getIdentity();
$message = "IP ".$_SERVER['REMOTE_ADDR']." ikke tiladt, der er sendt en smskode til telefon: ".$resdata['phone'];
$step = 2;
$view->assign('username', $input->username);
$view->assign('password', $input->password);
} elseif ($authmsg == "ip not allowed, wrong smscode") {
$message = "Forkert smskode, start venligst forfra";
$step = 1;
} else {
$message = "Kunne ikke foretage login";
}
}
}
}
$view->assign('step', $step);
if (isset($message)) {
$view->assign('message', $message);
}
echo $view->render("auth/login.tpl");
}
public function logoutAction()
{
$auth = Zend_Auth::getInstance()->clearIdentity();
$this->_redirect('/auth/login');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment