* @license PHP License * @package WB */ /** * Load classes */ WBClass::load('WBUser_Storage'); /** * User Storage module Table * * Load and save user data in database table * * @version 0.6.0 * @package WB */ class WBUser_Storage_Table extends WBUser_Storage { /** * storage * @var WBDatasource_Table */ protected $table; /** * @daprecated in favour if WBDatasource::TABLE_USER */ const TABLE_USER = 'user'; /** * @daprecated in favour if WBDatasource::TABLE_GROUP */ const TABLE_GROUP = 'group'; /** * @daprecated in favour if WBDatasource::TABLE_USERGROUP */ const TABLE_USERGROUP = 'usergroup'; /** * Constructor * * @param array $parameter */ public function __construct($parameter = array()) { if (empty($parameter['table'])) { $parameter['table'] = WBClass::create('WBDatasource_Table', $parameter); } parent::__construct($parameter); $this->table = $parameter['table']; } /** * find user * * @see isAuthenticated() * @param array anything to find user * @param bool autoLoad * @return string|null either the user id on success, or null */ public function find($data, $autoLoad = true) { // 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); } if (!$autoLoad) { $user = $this->table->getIds(WBDatasource::TABLE_USER, null, $clause); return $user; } // fetch user data from table $user = $this->table->get(WBDatasource::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(WBDatasource::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(WBDatasource::TABLE_USER, $id); if (count($user) != 1) { $this->id = null; return false; } $user = $user[0]; $user['id'] = $user[$this->table->getIdentifier(WBDatasource::TABLE_USER)]; $this->loadByRaw($user); return true; } /** * Load List of Mandators of User * * @return array */ protected function getMandatorList() { if (!$this->id){ return array(); } $mPrimary = $this->mandator->getIdentifier(); $uPrimary = $this->table->getIdentifier(WBDatasource::TABLE_USER); $clause = array(); $clause[] = array( 'field' => $uPrimary, 'value' => $this->id ); $option = array( 'column' => $mPrimary ); $list = $this->table->getColumn(WBDatasource::TABLE_MANDATORUSER, null, $clause, $option); return $list; } /** * load user groups * * Load group ids and names from database and transform to * associative array * groupid => groupname * * @todo use join instead of two simple selects */ protected function loadGroup() { $this->group = array(); if (!$this->id){ return; } $uPrimary = $this->table->getIdentifier(WBDatasource::TABLE_USER); $gPrimary = $this->table->getIdentifier(WBDatasource::TABLE_GROUP); $clause = array(); $clause[] = array( 'field' => $uPrimary, 'value' => $this->id ); if ($this->mandator->isEnabled()) { $mPrimary = $this->mandator->getIdentifier(); $clause[] = array( 'field' => $mPrimary, 'value' => $this->mandatorId ); } $list = $this->table->get(WBDatasource::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 ); $option = array( 'limit' => 0 ); $list = $this->table->get(WBDatasource::TABLE_GROUP, null, null, $clause, $option); 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 password'; $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(WBDatasource::TABLE_USER); $ts['changed'] = ''; $ts = array(WBDatasource::TABLE_USER => $ts); $this->table->setTables($ts); } // store in DB $newId = $this->table->save(WBDatasource::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(WBDatasource::TABLE_USER, $newId); $user = $user[0]; $user['id'] = $user[$this->table->getIdentifier(WBDatasource::TABLE_USER)]; $this->loadByRaw($user); $save = array('password' => ''); $save['password'] = $this->getPasswordHash($password, $save); $this->table->save(WBDatasource::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(WBDatasource::TABLE_USER); $clause = array(); $clause[] = array( 'field' => $uPrimary, 'value' => $this->id ); if ($this->mandator->isEnabled()) { $mPrimary = $this->table->getIdentifier(WBDatasource::TABLE_MANDATOR); $clause[] = array( 'field' => $mPrimary, 'value' => $this->mandatorId ); } $this->table->delete(WBDatasource::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(WBDatasource::TABLE_GROUP); $save = array(); $skel = array( $uPrimary => $this->id, $gPrimary => 'x' ); if ($this->mandator->isEnabled()) { $skel[$mPrimary] = $this->mandatorId; } foreach ($groupIds as $gid) { $skel[$gPrimary] = $gid; $save[] = $skel; } $this->table->save(WBDatasource::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(WBDatasource::TABLE_USER); $gPrimary = $this->table->getIdentifier(WBDatasource::TABLE_GROUP); $clause = array(); if ($this->mandator->isEnabled()) { $mPrimary = $this->table->getIdentifier(WBDatasource::TABLE_MANDATOR); $clause[] = array( 'field' => $mPrimary, 'value' => $this->mandatorId ); } $clause[] = array( 'field' => $gPrimary, 'relation' => 'in', 'value' => $gid ); $options = array( 'groupby' => array('field' => $uPrimary) ); $list = $this->table->get(WBDatasource::TABLE_USERGROUP, null, null, $clause, $options); $users = array(); foreach ($list as $l) { $users[] = $l[$uPrimary]; } return $users; } /** * Get List Of Known user Groups * * @return array list of user ids */ public function getAllGroups() { $clause = array(); $options = array( 'limit' => -1 ); $group = array(); $list = $this->table->get(WBDatasource::TABLE_GROUP, null, null, $clause, $options); foreach ($list as $l) { $group[$l['id']] = $l['groupname']; } return $group; } }