Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding basic account registration and login/logout - not finished or …
…secure yet.
- Loading branch information
Showing
13 changed files
with
713 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
/** | ||
* based on http://akrabat.com/zend-auth-tutorial/ | ||
*/ | ||
|
||
class AuthController extends Zend_Controller_Action | ||
{ | ||
|
||
public function indexAction() | ||
{ | ||
$form = new Form_Login(); | ||
$request = $this->getRequest(); | ||
if ($request->isPost()) { | ||
if ($form->isValid($request->getPost())) { | ||
if ($this->_process($form->getValues())) { | ||
// We're authenticated! Redirect to the home page | ||
$this->_helper->redirector('index', 'index'); | ||
} | ||
} | ||
} | ||
$this->view->form = $form; | ||
} | ||
|
||
public function logoutAction() | ||
{ | ||
Zend_Auth::getInstance()->clearIdentity(); | ||
$this->_helper->redirector('index'); // back to login page | ||
} | ||
|
||
protected function _process($values) | ||
{ | ||
// Get our authentication adapter and check credentials | ||
$adapter = $this->_getAuthAdapter(); | ||
$adapter->setIdentity($values['username']); | ||
$adapter->setCredential($values['password']); | ||
|
||
|
||
$auth = Zend_Auth::getInstance(); | ||
$result = $auth->authenticate($adapter); | ||
if ($result->isValid()) { | ||
$user = $adapter->getResultRowObject(); | ||
$auth->getStorage()->write($user); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
protected function _getAuthAdapter() | ||
{ | ||
$conn = Doctrine_Manager::getInstance()->getConnection('doctrine'); | ||
$adapter = new ZendX_Doctrine_Auth_Adapter($conn); | ||
$adapter->setTableName('Account') | ||
->setIdentityColumn('username') | ||
->setCredentialColumn('password'); | ||
|
||
return $adapter; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
class RegisterController extends Zend_Controller_Action | ||
{ | ||
|
||
public function init() | ||
{ | ||
$this->_config = Zend_Registry::get('config'); | ||
|
||
} | ||
|
||
public function indexAction() | ||
{ | ||
$form = new Form_Register(); | ||
$request = $this->getRequest(); | ||
if ($request->isPost()) { | ||
if ($form->isValid($request->getPost())) { | ||
$v = $form->getValues(); | ||
$u = new Account(); | ||
$u->username = $v['username']; | ||
$u->password = $v['password']; | ||
$u->email = $v['email']; | ||
$u->save(); | ||
|
||
$mail = new Zend_Mail(); | ||
$mail->setFrom($this->_config['register']['fromEmail']); | ||
$mail->setSubject($this->_config['register']['welcomeSubject']); | ||
$mail->addTo($u->email); | ||
$mail->setBodyText("Welcome!"); | ||
$mail->send(); | ||
|
||
} | ||
} | ||
$this->view->form = $form; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
class Zend_View_Helper_LoggedInAs extends Zend_View_Helper_Abstract | ||
{ | ||
public function loggedInAs () | ||
{ | ||
$auth = Zend_Auth::getInstance(); | ||
if ($auth->hasIdentity()) { | ||
$username = $auth->getIdentity()->username; | ||
$logoutUrl = $this->view->url(array('controller'=>'auth', | ||
'action'=>'logout'), null, true); | ||
return 'Welcome ' . $username . '. <a href="'.$logoutUrl.'">Logout</a>'; | ||
} | ||
|
||
$loginUrl = $this->view->url(array('controller'=>'auth', 'action'=>'index')); | ||
$regUrl = $this->view->url(array('controller'=>'register', 'action'=>'index')); | ||
return '<a href="'.$loginUrl.'">Login</a> | <a href="'.$regUrl.'">Register</a>'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php $this->headTitle('Login'); ?> | ||
<h1>Login</h1> | ||
<?php echo $this->form->setAction($this->url()); ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php $this->headTitle('Register'); ?> | ||
<h1>Register</h1> | ||
<?php echo $this->form->setAction($this->url()); ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
class Form_Login extends Zend_Form | ||
{ | ||
|
||
public function init() | ||
{ | ||
$this->setName("login"); | ||
$this->setMethod('post'); | ||
|
||
$this->addElement('text', 'username', array( | ||
'filters' => array('StringTrim', 'StringToLower'), | ||
'validators' => array( | ||
array('StringLength', false, array(0, 50)), | ||
), | ||
'required' => true, | ||
'label' => 'Username:', | ||
)); | ||
|
||
$this->addElement('password', 'password', array( | ||
'filters' => array('StringTrim'), | ||
'validators' => array( | ||
array('StringLength', false, array(0, 50)), | ||
), | ||
'required' => true, | ||
'label' => 'Password:', | ||
)); | ||
|
||
$this->addElement('submit', 'login', array( | ||
'required' => false, | ||
'ignore' => true, | ||
'label' => 'Login', | ||
)); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
class Form_Register extends Zend_Form | ||
{ | ||
|
||
public function init() | ||
{ | ||
$this->setName("login"); | ||
$this->setMethod('post'); | ||
|
||
$this->addElement('text', 'username', array( | ||
'filters' => array('StringTrim', 'StringToLower'), | ||
'validators' => array( | ||
array('StringLength', false, array(0, 50)), | ||
), | ||
'required' => true, | ||
'label' => 'Username:', | ||
)); | ||
$this->addElement('text', 'email', array( | ||
'filters' => array('StringTrim'), | ||
'validators' => array( | ||
array('StringLength', false, array(0, 50)), | ||
array('EmailAddress') | ||
), | ||
'required' => true, | ||
'label' => 'Email:', | ||
)); | ||
|
||
$this->addElement('password', 'password', array( | ||
'filters' => array('StringTrim'), | ||
'validators' => array( | ||
array('StringLength', false, array(0, 50)), | ||
), | ||
'required' => true, | ||
'label' => 'Password:', | ||
)); | ||
|
||
$this->addElement('submit', 'login', array( | ||
'required' => false, | ||
'ignore' => true, | ||
'label' => 'Login', | ||
)); | ||
} | ||
} | ||
|
Oops, something went wrong.