Archived
1
0
Fork 0
This repository has been archived on 2026-05-10. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
fiktivkod/application/models/Mapper/User.php
2010-09-22 22:22:50 +02:00

114 lines
No EOL
2.6 KiB
PHP

<?php
class Mapper_User extends Fiktiv_Model_Mapper_DbTableAbstract
{
protected function _createUser($object)
{
$user = new User();
if ($object instanceof Zend_Db_Table_Row) {
$user->setEmail($object->email);
$user->setFirstName($object->firstName);
$user->setLastName($object->lastName);
} else if (is_array($object)) {
$user->setEmail($object['email']);
$user->setFirstName($object['firstName']);
$user->setLastName($object['lastName']);
} else {
$user = null;
}
return $user;
}
public function findById($id)
{
if (is_numeric($id)) {
return $this->_createUser($this->_dbTable->find($id)->current());
}
}
/**
* Fetch user based on email
*
* @return User
*/
public function findByEmail($email)
{
// Atleast 6 character long
if (is_string($email) && isset($email[5])) {
$user = $this->_createUser($this->_dbTable->fetchRow($this->_dbTable->getAdapter()->quoteInto('email = ?', $email)));
return $user;
}
return null;
}
/**
* Get random user
*
* @return User
*/
public function findRandom()
{
return $this->_createUser($this->_dbTable->fetchAll(null, 'RAND()', 1)->current());
}
/**
* 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->_dbTable->getAdapter(), $this->_dbTable->_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;
}
/**
* Change password
*/
public function changePassword($userId, $password)
{
return $this->_dbTable->update(
array(
'password' => hash('sha256',$password)
),
$this->_dbTable->getAdapter()->quoteInto('id = ?',$userId)
);
}
}