* @license PHP License * @package WB * @subpackage content */ /** * Load classes */ WBClass::load('WBUser_Storage'); /** * User Storage module Table * * Load and save user data in database table * * @version 0.3.0 * @package WB * @subpackage content */ class WBUser_Storage_Table extends WBUser_Storage { /** * storage * @var WBDatasource_Table */ protected $table; const TABLE_USER = 'user'; const TABLE_GROUP = 'group'; const TABLE_USERGROUP = 'usergroup'; /** * 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(self::TABLE_USER, null, null, $clause); // user must exist and be unique if (count($user) != 1) { $this->id = null; return null; } $user = $user[0]; $user['id'] = $user[$this->table->getIdentifier(self::TABLE_USER)]; $this->loadByRaw($user); return $this->id; } /** * Load User Data * * @param string $id user's id * @return true on success, false otherwise */ public function load($id) { $user = $this->table->get(self::TABLE_USER, $id); if (count($user) != 1) { $this->id = null; return false; } $user = $user[0]; $user['id'] = $user[$this->table->getIdentifier(self::TABLE_USER)]; $this->loadByRaw($user); 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(self::TABLE_USER), 'value' => $this->id ); $gprimary = $this->table->getIdentifier(self::TABLE_GROUP); $list = $this->table->get(self::TABLE_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(self::TABLE_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'] = 'overwrite passwort'; $save['password'] = $this->getPasswordHash($password, $save); } // nothing to save if (empty($save)) { return true; } // don't update changed column on every log in if (!$new && !empty($save['lastlogin'])) { $ts = $this->table->getTableInfo(self::TABLE_USER); $ts['changed'] = ''; $ts = array(self::TABLE_USER => $ts); $this->table->setTables($ts); } // store in DB $newId = $this->table->save(self::TABLE_USER, $id, $save); if (!$newId) { return null; } foreach ($save as $k => $v) { $this->data[$k] = $v; } if (isset($save['emaillocal'])) { $this->data['email'] = $save['emaillocal'] . '@' . $save['emaildomain']; } if ($new && !empty($password)) { $user = $this->table->get(self::TABLE_USER, $newId); $user = $user[0]; $user['id'] = $user[$this->table->getIdentifier(self::TABLE_USER)]; $this->loadByRaw($user); $save = array('password' => ''); $save['password'] = $this->getPasswordHash($password, $save); $this->table->save(self::TABLE_USER, $newId, $save); $this->data['password'] = $save['password']; } 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(self::TABLE_USER); $clause = array(); $clause[] = array( 'field' => $uPrimary, 'value' => $this->id ); $this->table->delete(self::TABLE_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(self::TABLE_GROUP); $save = array(); foreach ($groupIds as $gid) { $save[] = array( $uPrimary => $this->id, $gPrimary => $gid ); } $this->table->save(self::TABLE_USERGROUP, '__new', $save); $this->loadGroup(); return; } /** * Get Members of Groups * * Load user ids by group id(s) * * @param string|array $gid * @return array list of user ids */ public function getIdByGroupId($gid) { if (empty($gid)) { return array(); } if (!is_array($gid)) { $gid = array($gid); } $uPrimary = $this->table->getIdentifier(self::TABLE_USER); $gPrimary = $this->table->getIdentifier(self::TABLE_GROUP); $clause = array(); $clause[] = array( 'field' => $gPrimary, 'relation' => 'in', 'value' => $gid ); $options = array( 'groupby' => array('field' => $uPrimary) ); $list = $this->table->get(self::TABLE_USERGROUP, null, null, $clause, $options); $users = array(); foreach ($list as $l) { $users[] = $l[$uPrimary]; } return $users; } }