Skip to content

Commit

Permalink
Adding basic account registration and login/logout - not finished or …
Browse files Browse the repository at this point in the history
…secure yet.
  • Loading branch information
mgkimsal committed Jan 7, 2011
1 parent 5b5b532 commit bc9725e
Show file tree
Hide file tree
Showing 13 changed files with 713 additions and 26 deletions.
4 changes: 4 additions & 0 deletions README
Expand Up @@ -9,6 +9,10 @@ together doctrine, phpunit and Zend Framework in one

Doctrine 1.2 and ZF 1.10.5 are provided in the /library folder,

Some code inspired and/or copied from
http://akrabat.com/zend-auth-tutorial/
http://zendcasts.com


Getting started
===============
Expand Down
5 changes: 5 additions & 0 deletions application/configs/application.ini
Expand Up @@ -13,6 +13,8 @@ resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
autoloaderNamespaces[] = "Doctrine"
autoloaderNamespaces[] = "Test"
autoloaderNamespaces[] = "Zfkit"
autoloaderNamespaces[] = "Zk"
autoloaderNamespaces[] = "Form"

; doctrine
doctrine.dsn = "mysql://user:password@server/prod_dbname"
Expand All @@ -31,6 +33,9 @@ doctrine.generate_models_options.classPrefixFiles = false
doctrine.generate_models_options.generateAccessors = false


register.fromEmail = "Your Name <yoo@domain.com>"
register.welcomeSubject = "Welcome to Sample Site"


[testing : production]
doctrine.dsn = "mysql://user:password@server/testing_dbname"
Expand Down
59 changes: 59 additions & 0 deletions application/controllers/AuthController.php
@@ -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;
}

}
37 changes: 37 additions & 0 deletions application/controllers/RegisterController.php
@@ -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;
}

}
39 changes: 13 additions & 26 deletions application/data/schema.yml
Expand Up @@ -6,37 +6,24 @@ options:
type: InnoDB

#put sample yaml here
Book:
Account:
columns:
id:
type: integer(4)
primary: true
notnull: true
autoincrement: true
isbn:
type: string(20)
title:
type: string(20)
author_id:
type: integer(4)
relations:
author:
class: Author
local: author_id
foreign: id
foreignAlias: books


Author:
columns:
id:
type: integer(4)
primary: true
notnull: true
autoincrement: true
firstName:
type: string(20)
lastName:
type: string(20)
username:
type: string(50)
password:
type: string(50)
email:
type: string(50)
name:
type: string(50)
company:
type: string(50)
created:
type: int(11)


5 changes: 5 additions & 0 deletions application/layouts/scripts/layout.phtml
Expand Up @@ -9,7 +9,12 @@
<body>
<div id="doc3" class="yui-t2">
<div id="hd" role="banner">
<div id="topname">
<span id="name1">ZF</span><span id="name2">Kit</span> - the zend framework starter kit
</div>
<div id="loggedin">
<?php echo $this->loggedInAs(); ?>
</div>
</div>
<div id="bd">
<div id="yui-main">
Expand Down
19 changes: 19 additions & 0 deletions application/views/helpers/LoggedInAs.php
@@ -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>';
}
}
3 changes: 3 additions & 0 deletions application/views/scripts/auth/index.phtml
@@ -0,0 +1,3 @@
<?php $this->headTitle('Login'); ?>
<h1>Login</h1>
<?php echo $this->form->setAction($this->url()); ?>
3 changes: 3 additions & 0 deletions application/views/scripts/register/index.phtml
@@ -0,0 +1,3 @@
<?php $this->headTitle('Register'); ?>
<h1>Register</h1>
<?php echo $this->form->setAction($this->url()); ?>
36 changes: 36 additions & 0 deletions library/Form/Login.php
@@ -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',
));
}
}

45 changes: 45 additions & 0 deletions library/Form/Register.php
@@ -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',
));
}
}

0 comments on commit bc9725e

Please sign in to comment.