Views: 211
Rating: 3

EZPDO UserManager

Submitted By: root
Licence: PRADO

User Manager that uses EZPDO for storing users.

Files

Info


// RUserManager.php
// written by Robin J. Rogge

// To use this code you need EZPDO, see the EZPDO example posted by wei at http://www.pradosoft.com/forum/index.php/topic,3652.0.html

class RUser extends TUser
{
private
$_userId;

public function
getUserId () // {{{ return user id
{
return
$this->_userId;
}
// }}}

public function loadFromString ($string) // {{{ pick up user data from serialized array
{
if(!empty(
$string))
{
list(
$name, $roles, $isGuest, $userId) = unserialize($string);
$this->setName($name);
$this->setRoles($roles);
$this->setIsGuest($isGuest);
$this->setUserId($userId);
}
return
$this;
}
// }}}

public function saveToString () // {{{ serialize user data
{
return
serialize(array($this->getName(), $this->getRoles(), $this->getIsGuest(), $this->getUserId()));
}
// }}}

public function setUserId ($id) // {{{ set user id
{
$this->_userId = TPropertyValue::ensureInteger($id);
}
// }}}
}

class
RUserManager extends TUserManager
{
private
$_userData = array(); // array containing userdata objects

public function init ($config) // {{{ initialize
{
// this is meant to be empty to disable the init() method of TUserManager
}
// }}}

public function getGuestUser () // {{{ generates a guest user object
{
$guest = new RUser($this);
$guest->setIsGuest(true);
return
$guest;
}
// }}}

public function getPasswordHash ($password) // {{{ generates password hash according to current password mode for given clear text password
{
switch(
$this->getPasswordMode())
{
case
'MD5':
return
md5($password);
case
'SHA1':
return
sha1($password);
default:
return
$password;
}
}
// }}}

public function getUser ($username=null) // {{{ returns user object for given username, overrides TUserManager implementation
{
if(
$username===null)
return
$this->getGuestUser();

$username = strtolower($username);
if(!isset(
$this->_userData[$username]))
return
null;

$account = $this->_userData[$username];

$user = new RUser($this);
$user->setIsGuest(false);
$user->setName($account->getUsername());
$user->setRoles($account->getRoles());
$user->setUserId($account->getId());

return
$user;
}
// }}}

public function validateUser ($username, $password) // {{{ validate user by username and password, overrides TUserManager implementation
{
$pdo = epManager::instance();

$user = $pdo->create('User');
$user->name = strtolower($username);
$user->pass = $this->getPasswordHash($password);

if(!(
$userData = $pdo->find($user)))
return
false;

$this->_userData[$username] = $userData;
return
true;
}
// }}}
}

?>



Please note: This is currently a beta preview and has known bugs. Please see the PRADO forums if you have suggestions or problems using it.