From 7d15daaf6ebbde5c2e6e06541c4150b3805fb501 Mon Sep 17 00:00:00 2001 From: Fredric N Date: Fri, 10 Sep 2010 23:30:22 +0200 Subject: [PATCH] Rewrite of modelstructure --- application/datastorage/Posts.php | 7 ++ application/datastorage/Users.php | 4 +- application/models/Mapper/User.php | 101 ++++++++++++++++++ application/models/Table/User.php | 8 ++ application/models/User.php | 35 +++--- .../blog/controllers/IndexController.php | 6 ++ .../default/views/layout/default.phtml | 4 +- library/Fiktiv/Data/Interface/Read.php | 6 -- library/Fiktiv/Data/Interface/Write.php | 6 -- library/Fiktiv/Data/Service.php | 2 +- library/Fiktiv/Data/Storage.php | 6 -- library/Fiktiv/Exception.php | 4 + 12 files changed, 151 insertions(+), 38 deletions(-) create mode 100644 application/datastorage/Posts.php create mode 100644 application/models/Mapper/User.php create mode 100644 application/models/Table/User.php delete mode 100644 library/Fiktiv/Data/Interface/Read.php delete mode 100644 library/Fiktiv/Data/Interface/Write.php delete mode 100644 library/Fiktiv/Data/Storage.php create mode 100644 library/Fiktiv/Exception.php diff --git a/application/datastorage/Posts.php b/application/datastorage/Posts.php new file mode 100644 index 0000000..a79177e --- /dev/null +++ b/application/datastorage/Posts.php @@ -0,0 +1,7 @@ + diff --git a/application/datastorage/Users.php b/application/datastorage/Users.php index dc72568..3c6683f 100644 --- a/application/datastorage/Users.php +++ b/application/datastorage/Users.php @@ -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); diff --git a/application/models/Mapper/User.php b/application/models/Mapper/User.php new file mode 100644 index 0000000..8ea87ca --- /dev/null +++ b/application/models/Mapper/User.php @@ -0,0 +1,101 @@ +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; + } +} \ No newline at end of file diff --git a/application/models/Table/User.php b/application/models/Table/User.php new file mode 100644 index 0000000..505953b --- /dev/null +++ b/application/models/Table/User.php @@ -0,0 +1,8 @@ +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 diff --git a/application/modules/blog/controllers/IndexController.php b/application/modules/blog/controllers/IndexController.php index db1a9e5..23bc73b 100644 --- a/application/modules/blog/controllers/IndexController.php +++ b/application/modules/blog/controllers/IndexController.php @@ -4,5 +4,11 @@ class Blog_IndexController extends Fiktiv_Controller_Action { public function indexAction() { + // TODO: Set latest as default + } + + public function latestAction() + { + } } diff --git a/application/modules/default/views/layout/default.phtml b/application/modules/default/views/layout/default.phtml index ee3f587..7af7330 100644 --- a/application/modules/default/views/layout/default.phtml +++ b/application/modules/default/views/layout/default.phtml @@ -15,6 +15,8 @@ diff --git a/library/Fiktiv/Data/Interface/Read.php b/library/Fiktiv/Data/Interface/Read.php deleted file mode 100644 index f822a70..0000000 --- a/library/Fiktiv/Data/Interface/Read.php +++ /dev/null @@ -1,6 +0,0 @@ -_dataStorage[$name] = new $name(); } diff --git a/library/Fiktiv/Data/Storage.php b/library/Fiktiv/Data/Storage.php deleted file mode 100644 index 4554837..0000000 --- a/library/Fiktiv/Data/Storage.php +++ /dev/null @@ -1,6 +0,0 @@ -