Archived
1
0
Fork 0

Merge branch 'master' of git://haze.shylip.com/fiktivkod

This commit is contained in:
H Hautakoski 2010-09-10 23:29:00 +02:00
commit 126ef0ccbd
12 changed files with 151 additions and 38 deletions

View file

@ -0,0 +1,7 @@
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
?>

View file

@ -3,7 +3,7 @@
*
*
*/
class Users extends Zend_Db_Table_Abstract implements Fiktiv_Data_Storage
class Table_User extends Zend_Db_Table_Abstract implements Fiktiv_Data_Storage
{
protected $_schema = 'fiktivkod';
protected $_name = 'User';
@ -25,7 +25,7 @@ class Users extends Zend_Db_Table_Abstract implements Fiktiv_Data_Storage
// Set credentials
$authAdapter->setIdentity($email);
$authAdapter->setCredential($password);
$authAdapter->setCredential(hash('sha256',$password));
// Authenticate
$result = $auth->authenticate($authAdapter);

View file

@ -0,0 +1,101 @@
<?php
class Mapper_User
{
protected $_dbTable = null;
public function __construct($dbtable = null)
{
$this->setDbTable($dbtable);
}
public function setDbTable($dbtable)
{
if (null === $dbtable) {
$this->_dbTable = new Table_User();
} else {
if ($dbtable instanceof Zend_Db_Table) {
$this->_dbTable = $dbtable;
} else if (is_string($dbtable) && class_exists($dbtable)) {
$this->_dbTable = new $dbtable();
} else {
throw new Fiktiv_Exception('Invalid database table supplied to ' . __CLASS__);
}
}
}
/**
* Fetch user based on email
*
* @return User
*/
public function findByEmail($email)
{
// Atleast 6 character long
if (is_string($email) && isset($email[5])) {
return $this->_dbTable->fetchRow($this->getAdapter()->quoteInto('email = ?', $email));
}
return null;
}
/**
* Get random user
*
* @return User
*/
public function findRandom()
{
$row = $this->_dbTable->fetchAll(null, 'RAND()', 1)->current();
if ($row) {
$user = new User();
$user->setFirstName($row->firstName);
$user->setLastName($row->lastName);
$user->setRegDate($row->regDate);
}
return null;
}
/**
* Authenticate user
*
* @param string $email
* @param string $password
*/
public function login($email, $password)
{
$auth = Zend_Auth::getInstance();
// Setup auth adapter
$authAdapter = new Zend_Auth_Adapter_DbTable($this->getAdapter(), $this->_name, 'email', 'password');
// Set credentials
$authAdapter->setIdentity($email);
$authAdapter->setCredential(hash('sha256',$password));
// Authenticate
$result = $auth->authenticate($authAdapter);
// Check result
if ($result->isValid()) {
// Keep all but password and salt in session.
$storage = $auth->getStorage();
$storage->write($authAdapter->getResultRowObject(null, array('password', 'salt')));
return true;
}
return false;
}
}

View file

@ -0,0 +1,8 @@
<?php
class Table_User extends Zend_Db_Table_Abstract
{
protected $_schema = 'fiktivkod';
protected $_name = 'User';
protected $_primary = 'id';
}

View file

@ -5,7 +5,6 @@
*/
class User
{
protected $_email;
protected $_firstName;
protected $_lastName;
@ -13,19 +12,6 @@ class User
protected $_isDeleted = false;
public function __construct(array $data = array()) {
if (isset($data['table']) && $data['table'] instanceof Users) {
$this->setFirstName($data['data']['firstName']);
$this->setLastName($data['data']['lastName']);
}
}
/**
* Set user lastname
*
@ -51,7 +37,7 @@ class User
*/
public function setFirstName($name)
{
if (is_string($name) && preg_match('/^[A-ö\-]{0,10}$/', $name)) {
if (is_string($name) && preg_match('/^[A-ö]{2,8}\-?[A-ö]{2,8}$/', $name)) {
$this->_firstName = $name;
return true;
}
@ -60,6 +46,24 @@ class User
}
public function setRegDate($date)
{
if (is_string($date) && preg_match('/^(?:(?:19[0-9]{2})|(?:20[0-9]{2}))\-' .
'(?:(?:0[1-9])|(?:1[012]))\-(?:(?:0[1-9])|(?:[12][0-9])|(?:3[01]))$/', $date)) {
$this->_regDate = $date;
} else if ($date instanceof Zend_Date) {
$this->_regDate = $date->toString('yyyy-MM-dd');
} else {
return false;
}
return true;
}
/**
* Returns the user registration date
*
@ -82,6 +86,7 @@ class User
return false;
}
/**
* String representation of the object
* @return string

View file

@ -4,5 +4,11 @@ class Blog_IndexController extends Fiktiv_Controller_Action
{
public function indexAction()
{
// TODO: Set latest as default
}
public function latestAction()
{
}
}

View file

@ -15,6 +15,8 @@
<div id="nav">
<?=$this->renderMenu() ?>
<span class="float-right">
<?=$this->authLink() ?>
|
<a href="<?=$this->url(array('lang' => 'sv')) ?>">sv</a>
<a href="<?=$this->url(array('lang' => 'en')) ?>">en</a>
</span>
@ -28,8 +30,6 @@
<?=$this->layout()->content ?>
<div id="footer">
<?=$this->authLink() ?>
|
<a href="http://<?=$this->app['url'] ?>"><?=$this->app['name'] ?></a> v<?=$this->app['version'] ?> &copy; 2010
</div>
</div>

View file

@ -1,6 +0,0 @@
<?php
interface Fiktiv_Data_Interface_Read {
}

View file

@ -1,6 +0,0 @@
<?php
interface Fiktiv_Data_Interface_Write {
}

View file

@ -54,7 +54,7 @@ class Fiktiv_Data_Service
}
} else if (class_exists($name) && array_key_exists('Fiktiv_Data_Storage', class_implements($name))) {
} else if (class_exists($name)) {
$this->_dataStorage[$name] = new $name();
}

View file

@ -1,6 +0,0 @@
<?php
/**
* Fiktiv_Data_Storage interface
*
*/
interface Fiktiv_Data_Storage {}

View file

@ -0,0 +1,4 @@
<?php
class Fiktiv_Exception extends Exception
{}