From ee3e264c9e3d38d7f28af4a4f7ddc18ddb0f33b5 Mon Sep 17 00:00:00 2001 From: Fredric N Date: Sat, 2 Oct 2010 00:09:58 +0200 Subject: [PATCH] Did some work on the User classes --- application/models/Mapper/User.php | 38 ++++++++- application/models/User.php | 79 ++++++++++++++++++- .../default/controllers/IndexController.php | 4 +- .../default/controllers/ProfileController.php | 14 ++-- .../default/views/scripts/index/index.phtml | 13 ++- 5 files changed, 134 insertions(+), 14 deletions(-) diff --git a/application/models/Mapper/User.php b/application/models/Mapper/User.php index 35ea3ab..7ff400e 100644 --- a/application/models/Mapper/User.php +++ b/application/models/Mapper/User.php @@ -8,14 +8,21 @@ class Mapper_User extends Fiktiv_Model_Mapper_DbTableAbstract $user = new User(); + // What happens? + if ($object instanceof stdClass) + $object = (array) $object; + + if ($object instanceof Zend_Db_Table_Row) { + $user->setId($object->id); $user->setEmail($object->email); $user->setFirstName($object->firstName); $user->setLastName($object->lastName); } else if (is_array($object)) { + $user->setId($object['id']); $user->setEmail($object['email']); $user->setFirstName($object['firstName']); $user->setLastName($object['lastName']); @@ -85,13 +92,13 @@ class Mapper_User extends Fiktiv_Model_Mapper_DbTableAbstract // 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'))); - + $storage->write($this->_createUser($authAdapter->getResultRowObject(null, array('password', 'salt')))); + return true; } @@ -102,8 +109,15 @@ class Mapper_User extends Fiktiv_Model_Mapper_DbTableAbstract /** * Change password */ - public function changePassword($userId, $password) + public function setPassword($userId, $password) { + + if ($userId instanceof User) + $userId = $userId->getId(); + + if (!is_numeric($userId)) + return false; + return $this->_dbTable->update( array( 'password' => hash('sha256',$password) @@ -111,4 +125,20 @@ class Mapper_User extends Fiktiv_Model_Mapper_DbTableAbstract $this->_dbTable->getAdapter()->quoteInto('id = ?',$userId) ); } + + + /** + * Save User object + */ + public function save(User $user) + { + $data = $user->toArray(); + unset($data['id']); + + return $this->_dbTable->update( + $data, + $this->_dbTable->getAdapter()->quoteInto('id = ?',$user->getId()) + ); + } + } \ No newline at end of file diff --git a/application/models/User.php b/application/models/User.php index 7ed6bf9..f86f551 100644 --- a/application/models/User.php +++ b/application/models/User.php @@ -12,6 +12,50 @@ class User extends Fiktiv_Model_Abstract protected $_regDate; protected $_isDeleted = false; + + public function __construct($data = array()) + { + $data = (array)$data; + + $this->setAttribs($data); + } + + /** + * Quick way to set user data + * @param array $data + */ + public function setAttribs(array $data) + { + foreach ($data as $key => $value) { + + switch(strtolower($key)) { + case 'email': + $this->setEmail($value); + break; + case 'firstname': + $this->setFirstName($value); + break; + case 'lastname': + $this->setLastName($value); + break; + case 'regdate': + $this->setRegDate(new Zend_Date($value)); + break; + } + } + } + + public function getId() + { + return $this->_id; + } + + public function setId($id) + { + if (is_numeric($id)) + $this->_id = $id; + } + /** * Set user email * @@ -87,6 +131,14 @@ class User extends Fiktiv_Model_Abstract return true; } + public function setPassword($password) + { + if (!is_string($password)) + throw new Fiktiv_Exception('password must be string'); + + Fiktiv_Data_Service::getInstance()->User->setPassword($this, $password); + } + /** * Returns the user registration date * @@ -113,7 +165,7 @@ class User extends Fiktiv_Model_Abstract */ public function __toString() { - return '(' . __CLASS__ . '){' . $this->_email . ' ' . $this->_firstName . ' ' . $this->_lastName . '}'; + return '(' . __CLASS__ . '){' . $this->_email . ', ' . $this->_firstName . ' ' . $this->_lastName . '}'; } /** @@ -130,4 +182,29 @@ class User extends Fiktiv_Model_Abstract 'regDate' => $this->_regDate ); } + + /** + * Load user data from database (restore last saved) + */ + public function reload() + { + $userData = Fiktiv_Data_Service::getInstance()->User->findById($this->getId()); + + if ($userData instanceof User) { + + $this->setAttribs($userData->toArray()); + + return true; + } + + return false; + } + + /** + * Save User + */ + public function save() + { + return Fiktiv_Data_Service::getInstance()->User->save($this); + } } \ No newline at end of file diff --git a/application/modules/default/controllers/IndexController.php b/application/modules/default/controllers/IndexController.php index e5ce8be..12132a7 100644 --- a/application/modules/default/controllers/IndexController.php +++ b/application/modules/default/controllers/IndexController.php @@ -24,8 +24,8 @@ class IndexController extends Fiktiv_Controller_Action //$dbLayer = Fiktiv_Db_Service::getInstance(); //$me = $dbLayer->users->findByEmail('fredric@fiktivkod.org'); - $dbLayer = $this->_helper->dataService(); - $me = $dbLayer->User->findByEmail('fredric@fiktivkod.org'); + $dataLayer = $this->_helper->dataService(); + $me = $dataLayer->User->findByEmail('fredric@fiktivkod.org'); echo $me; } diff --git a/application/modules/default/controllers/ProfileController.php b/application/modules/default/controllers/ProfileController.php index 476cad5..a71eaec 100644 --- a/application/modules/default/controllers/ProfileController.php +++ b/application/modules/default/controllers/ProfileController.php @@ -67,11 +67,14 @@ class ProfileController extends Fiktiv_Controller_Action ); + $user = Zend_Auth::getInstance()->getIdentity(); + + if ($this->_request->isPost()) { $data = $this->_request->getPost(); - if ($data['password'] !== $data['passwordConfirm']) { + if ($data['password'] !== $data['passwordConfirm'] || empty($data['password'])) { if (!empty($data['password'])) $this->view->messages = 'passwords dont match'; @@ -79,7 +82,7 @@ class ProfileController extends Fiktiv_Controller_Action } else { - if ($this->dataService->User->changePassword(Zend_Auth::getInstance()->getIdentity()->id, $data['password'])) + if ($user->setPassword($data['password'])) $this->view->messages = 'Lösenordet är ändrat!'; unset($data['password']); @@ -87,13 +90,12 @@ class ProfileController extends Fiktiv_Controller_Action unset($data['passwordConfirm']); unset($data['save']); - $this->dataService->User->update($data, 'id = '.Zend_Auth::getInstance()->getIdentity()->id); - + + $user->setAttribs($data); + $user->save(); } - - $user = $this->dataService->User->findById(Zend_Auth::getInstance()->getIdentity()->id); $form->populate($user->toArray()); } diff --git a/application/modules/default/views/scripts/index/index.phtml b/application/modules/default/views/scripts/index/index.phtml index fdabb93..84bfe58 100644 --- a/application/modules/default/views/scripts/index/index.phtml +++ b/application/modules/default/views/scripts/index/index.phtml @@ -43,4 +43,15 @@ function get_gravatar( $email, $s = 80, $d = '404', $r = 'g', $img = false, $att abc bbb - \ No newline at end of file + + + +getIdentity(), 'SESSION'); + +?> \ No newline at end of file