0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
LoginController.php
Go to the documentation of this file.
1 <?
23 require_once('ProfileTable.php');
24 require_once('Profile.php');
25 
26 //
27 // This controller uses the Authentication facilities of Zend. Visit
28 // http://weierophinney.net/matthew/archives/165-Login-and-Authentication-with-Zend-Framework.html
29 // for a good tutorial.
30 //
31 
32 class LoginController extends Zend_Controller_Action
33 {
34  public function getLoginForm() {
35  require_once('LoginForm.php');
36  return new LoginForm(array(
37  'action' => '/login/process',
38  'method' => 'post',
39  ));
40  }
41 
42  public function getSignupForm() {
43  require_once('SignupForm.php');
44  return new SignupForm(array(
45  'action' => '/login/processSignup',
46  'method' => 'post',
47  ));
48  }
49 
50  public function handleError(Exception $e) {
51  echo "Failed to connect to the database. Please make sure that Hypertable ".
52  "is up and running. See <a href=\"/about\">About</a> for ".
53  "installation instructions.<br />".
54  "<br />".$e->getMessage()."<br />".
55  "<br /><pre>$e</pre>";
56  die ($e->getCode());
57  }
58 
59  public function getAuthAdapter(array $params) {
60  require_once('MyAuthAdapter.php');
61  return new MyAuthAdapter($params['username'], $params['password']);
62  }
63 
64  public function preDispatch() {
65  // if the user is logged in then only allow the 'logout' action;
66  // every other request is redirected to /index/index.
67  if (Zend_Auth::getInstance()->hasIdentity()) {
68  if ('logout' != $this->getRequest()->getActionName())
69  $this->_helper->redirector('index', 'index');
70  }
71  // if user is not logged in then redirect to /index which will
72  // display the login form
73  else {
74  if ('logout' == $this->getRequest()->getActionName())
75  $this->_helper->redirector('index');
76  }
77  }
78 
79  public function indexAction() {
80  $this->view->loginForm = $this->getLoginForm();
81  $this->view->signupForm = $this->getSignupForm();
82  }
83 
84  public function processAction() {
85  $request = $this->getRequest();
86 
87  // Check if we have a POST request
88  if (!$request->isPost())
89  return $this->_helper->redirector('index');
90 
91  // Get our form and validate it; if it's invalid then show the login form
92  $form = $this->getLoginForm();
93  if (!$form->isValid($request->getPost())) {
94  $this->view->loginForm = $form;
95  $this->view->signupForm = $this->getSignupForm();
96  return $this->render('index');
97  }
98 
99  // Get our authentication adapter and check credentials
100  $adapter = $this->getAuthAdapter($form->getValues());
101  $auth = Zend_Auth::getInstance();
102  try {
103  $result = $auth->authenticate($adapter);
104  }
105  catch (Exception $e) {
106  $this->handleError($e);
107  }
108  if (!$result->isValid()) {
109  $form->setDescription('Invalid credentials provided');
110  $this->view->loginForm = $form;
111  $this->view->signupForm = $this->getSignupForm();
112  return $this->render('index');
113  }
114 
115  // We're authenticated! Redirect to the home page
116  $this->_helper->redirector('index', 'index');
117  }
118 
119  public function logoutAction() {
120  Zend_Auth::getInstance()->clearIdentity();
121  $this->_helper->redirector('index'); // back to login page
122  }
123 
124  public function processsignupAction() {
125  $request = $this->getRequest();
126 
127  // Check if we have a POST request
128  if (!$request->isPost())
129  return $this->_helper->redirector('index');
130 
131  // Get our form and validate it; display signup form if entries
132  // are invalid
133  $form = $this->getSignupForm();
134  if (!$form->isValid($request->getPost())) {
135  $this->view->loginForm = $this->getLoginForm();
136  $this->view->signupForm = $form;
137  return $this->render('index');
138  }
139 
140  // create the new profile. this function will return null
141  // if the profile already exists
142  $ar=$form->getValues();
143  $username=$ar['username'];
144  try {
145  $profile = ProfileTable::create($username);
146  }
147  catch (Exception $e) {
148  $this->handleError($e);
149  }
150  if (!$profile) { // already exists
151  $form->setDescription('Username already exists, please choose '.
152  'another one!');
153  $this->view->loginForm = $this->getLoginForm();
154  $this->view->signupForm = $form;
155  return $this->render('index');
156  }
157  else { // initialize with an empty password
158  $profile->setId($username);
159  $profile->setPasswordPlain('');
160  ProfileTable::store($profile);
161 
162  // The user was created! Authenticate the user against his empty
163  // password and redirect him to the profile page.
164  $adapter=$this->getAuthAdapter(array('username' => $username,
165  'password' => ''));
166  $auth=Zend_Auth::getInstance();
167  $result=$auth->authenticate($adapter);
168  if (!$result->isValid())
169  die("This should never happen...");
170  $this->_helper->redirector('index', 'profile');
171  }
172  }
173 }
174 
175 ?>
create($username)
Copyright (C) 2007-2015 Hypertable, Inc.
Definition: SignupForm.php:23
Copyright (C) 2007-2015 Hypertable, Inc.
Copyright (C) 2007-2015 Hypertable, Inc.
Copyright (C) 2007-2015 Hypertable, Inc.
Definition: LoginForm.php:24
handleError(Exception $e)
store($profile)
getAuthAdapter(array $params)