70 lines
No EOL
1.5 KiB
PHP
70 lines
No EOL
1.5 KiB
PHP
<?php
|
|
/**
|
|
*
|
|
*
|
|
*/
|
|
class Users extends Zend_Db_Table_Abstract
|
|
{
|
|
protected $_schema = 'fiktivkod';
|
|
protected $_name = 'User';
|
|
protected $_primary = 'id';
|
|
protected $_rowClass = 'User';
|
|
|
|
/**
|
|
* 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($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;
|
|
}
|
|
|
|
/**
|
|
* Fetch user based on email
|
|
*
|
|
* @return User
|
|
*/
|
|
public function findByEmail($email)
|
|
{
|
|
// Atleast 6 character long
|
|
if (is_string($email) && isset($email[5])) {
|
|
|
|
return $this->fetchRow($this->getAdapter()->quoteInto('email = ?', $email));
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Get random user
|
|
*
|
|
* @return User
|
|
*/
|
|
public function findRandom()
|
|
{
|
|
return $this->fetchAll(null, 'RAND()', 1)->current();
|
|
}
|
|
} |