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-10 23:30:22 +02:00

101 lines
No EOL
2.3 KiB
PHP

<?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;
}
}