* @license PHP License * @package WB * @subpackage content */ /** * Load classes */ WBClass::load('WBUser_Storage'); /** * User Storage module Table * * * * @version 0.2.2 * @package WB * @subpackage content */ class WBUser_Storage_Table extends WBUser_Storage { /** * storage * @var WBDatasource_Table */ protected $table; /** * constructor * * @param array $parameter */ public function __construct( $parameter ) { parent::__construct($parameter); $this->table = WBClass::create('WBDatasource_Table', $parameter); } /** * find user * * @see isAuthenticated() * @param array anything to find user * @return string|null either the user id on success, or null */ public function find($data) { // lookup user in DB $clause = array(); //$clause[] = array('field' => 'approved', 'value' => 1); //$clause[] = array('field' => 'enabled', 'value' => 1); // load by id if (isset($data['id'])) { return $this->load($data['id']); } if (isset($data['nickname'])) { $clause[] = array( 'field' => 'nickname', 'value' => $data['nickname'] ); } else if (!isset($data['email'])) { $this->id = null; return null; } else { $email = explode('@', $data['email']); $domain = array_pop($email); $local = implode('@', $email); $clause[] = array('field' => 'emaillocal', 'value' => $local); $clause[] = array('field' => 'emaildomain', 'value' => $domain); } // fetch user data from table $user = $this->table->get('user', null, null, $clause); // user must exist and be unique if (count($user) != 1) { $this->id = null; return null; } // concat email address $user[0]['email'] = $user[0]['emaillocal'] . '@' . $user[0]['emaildomain']; $this->id = $user[0][$this->table->getIdentifier('user')]; $this->data = $user[0]; return $this->id; // fetch user id $log['approved']= $user[0]['approved']; $log['enabled'] = $user[0]['enabled']; $log['id'] = $user[0][$this->table->getIdentifier('user')]; // check password if ($user[0]['password'] != md5($log['id'] . ':' . $data['password'])) { $log['status'] = 'invalid_password'; $this->id = null; $this->log->notice($log); return null; } $this->id = $log['id']; $user[0]['email'] = $user[0]['emaillocal'] . '@' . $user[0]['emaildomain']; $this->data = $user[0]; $this->loadGroup(); $log['status'] = 'success'; $this->log->notice($log); // store last login $save = array( 'lastlogin' => gmdate('Y-m-d H:i:s') ); $this->table->save('user', $this->id, $save); return $this->id; } /** * load user and automatically log in * * This is like the "su" command * * @todo not done yet * @param string $id user's id * @return true on success, false otherwise */ public function load($id) { if ($id == $this->id) { return true; } $user = $this->table->get('user', $id); if (count($user) != 1) { $this->id = null; return false; } $this->id = $user[0][$this->table->getIdentifier('user')]; $user[0]['email'] = $user[0]['emaillocal'] . '@' . $user[0]['emaildomain']; $this->data = $user[0]; return true; } /** * load user groups * * Load group ids and names from database and transform to * associative array * groupid => groupname */ protected function loadGroup() { $this->group = array(); if (!$this->id){ return; } $clause = array(); $clause[] = array( 'field' => $this->table->getIdentifier('user'), 'value' => $this->id ); $gprimary = $this->table->getIdentifier('group'); $list = $this->table->get('usergroup', null, null, $clause); // user is not in any group if (empty($list)) { return; } $gids = array(); foreach ($list as $l) { $gids[] = $l[$gprimary]; } $clause = array(); $clause[] = array( 'field' => $gprimary, 'relation' => 'IN', 'value' => $gids ); $list = $this->table->get('group', null, null, $clause); foreach ($list as $l) { $this->group[$l[$gprimary]] = $l['groupname']; } } /** * update user data * * store changed user data in session and table * * @param array $data * @param bool $new whether to store a new user * @return string $id on success, null otherwise */ public function set($data, $new = false) { $id = $this->id; if ($new) { $id = '__new'; } if (!$id) { return null; } $save = array(); if (isset($data['email'])) { $email = explode('@', $data['email']); $save['emaildomain'] = array_pop($email); $save['emaillocal'] = implode('@', $email); unset($data['email']); } // password needs to be hashed $password = ''; if (isset($data['password'])) { $password = $data['password']; unset($data['password']); } // save everything else foreach ($data as $key => $value) { // ignore values that have not changed if (!$new && $value == $this->data[$key]) { continue; } $save[$key] = $value; } // leave nickname empty if ($new && !isset($data['nickname'])) { $save['nickname'] = ''; } // hash password if ($new) { $save['password'] = 'invalid_password'; } else if (!empty($password)) { $save['password'] = md5($id . ':' . $password); } // nothing to save if (empty($save)) { return true; } // store in DB $newId = $this->table->save('user', $id, $save); if (!$newId) { return null; } if ($new && !empty($password)) { $save = array(); $save['password'] = md5($newId . ':' . $password ); $this->table->save( 'user', $newId, $save ); } foreach( $save as $k => $v ) { $this->data[$k] = $v; } if( $new ) { $this->id = $newId; } return $newId; } /** * save group membership * * @param array $groupIds */ public function setGroups($groupIds) { if (!$this->id) { return; } $uPrimary = $this->table->getIdentifier('user'); $clause = array(); $clause[] = array( 'field' => $uPrimary, 'value' => $this->id ); $this->table->delete('usergroup', null, null, $clause); // not member of any group if (empty($groupIds)) { $this->group = array(); return; } // save new list of groups $gPrimary = $this->table->getIdentifier('group'); $save = array(); foreach ($groupIds as $gid) { $save[] = array( $uPrimary => $this->id, $gPrimary => $gid ); } $this->table->save('usergroup', '__new', $save); $this->loadGroup(); return; } }