* @copyright 2004 by http://wombat.exit0.net * @package wombatSite * @subpackage user */ /** * gsUser * * @version 0.1 * @package wombatSite * @subpackage user */ class wbUser_Admin extends wbUser { /** * constructor * @access public */ function __construct() { parent::__construct(); $this->_ds =& wbFactory::create( 'wbDatasource' ); } /** * php4-constructor * @access public */ function wbUser_Admin() { $this->__construct(); } /** * recieve the key name for data entries in tables * * * @access private * @param string $part * @return string $key */ function getPrimaryKey( $part ) { return $this->_ds->getPrimaryKey( $part ); } /** * recieve user data * * @access private * @param int $id user id * @return array $user primary data of the logged in user, if any */ function getUserData( $id = null ) { if( $id === null ) { return parent::getUserData(); } return $this->_ds->getEntry( 'user', $id ); } /** * recieve group data * * @access public * @param int $id group id * @return boolean $result true on success */ function getGroupData( $id ) { return $this->_ds->getEntry( 'group', $id ); } /** * update user data * * @access public * @param array $data user data to be updated * @return boolean $result true on success */ function setUserData( $data, $id = null ) { if( $id === null ) { $id = $this->_id; } $result = array(); $result['success'] = false; $result['id'] = $id; $new = array(); if( isset( $data['flags' ] ) ) { if( is_array( $data['flags'] ) ) { $data['flags'] = implode( ',', $data['flags'] ); } else { $data['flags'] = ''; } } // store password crypted if( isset( $data['password'] ) ) { $data['password'] = md5( $data['password'] ); } $fields = $this->_ds->getFieldList( 'user' ); foreach( $data as $key => $value ) { if( in_array( $key, $fields ) ) { $new[$key] = $value; } } $primary = $this->_ds->getPrimaryKey( 'user' ); if( isset( $new[$primary] ) ) { unset( $new[$primary] ); } $new['recent'] = date( 'Y-m-d H:i:s' ); $res = $this->_ds->save( 'user', $id, $new ); if( patErrorManager::isError( $res ) ) { switch( $res->getCode() ) { case 'wbDatasource:7': $result['error'] = 'dublicates'; break; // return error if nothing matches default: return $res; break; } return $result; } $msg = array(); foreach( $new as $key => $value ) { array_push( $msg, $key . '=\'' . $value . '\'' ); if( $id === $this->_id ) { $this->_user[$key] = $value; } } if( $id === $this->_id ) { $this->_sess->set( 'wbuser_data', $this->_user ); } wbDebugger::addMsg( 'wbUser', implode( ', ', $msg ), 'Update details' ); $result['success'] = true; return $result; } /** * update user data * * @access public * @param array $data user data to be updated * @param int $id group id * @return boolean $result true on success */ function setGroupData( $data, $id ) { if( $this->_ds === null ) { $this->_ds =& wbFactory::create( 'wbDatasource' ); } $result = array(); $result['success'] = false; $result['id'] = $id; $primary = $this->_ds->getPrimaryKey( 'group' ); $new = array(); $fields = $this->_ds->getFieldList( 'group' ); foreach( $data as $key => $value ) { if( in_array( $key, $fields ) ) { $new[$key] = $value; } } $res = $this->_ds->save( 'group', $id, $new ); if( patErrorManager::isError( $res ) ) { switch( $res->getCode() ) { case 'wbDatasource:7': $result['error'] = 'dublicates'; break; // return error if nothin matches default: return $res; break; } return $result; } $msg = array(); foreach( $new as $key => $value ) { array_push( $msg, $key . '=\'' . $value . '\'' ); } wbDebugger::addMsg( 'wbUser', implode( ', ', $msg ), 'Update group' ); $result['success'] = true; return $result; } /** * mark user as deleted * * @access public * @param array $uid user id * @return boolean $result true on success */ function removeUser( $uid ) { $result = array(); $result['success'] = false; if( $uid == $this->_id ) { $result['error'] = 'suicide'; $result['id'] = $uid; return $result; } if( $this->_ds === null ) { $this->_ds =& wbFactory::create( 'wbDatasource' ); } $id = $this->_ds->delete( 'user', $uid ); if( patErrorManager::isError( $id ) ) { return $id; } $result['success'] = true; return $result; } /** * change the users password * Return values: * - 0 on success * - -1 if user is not logged in! * - 1 if old password did not match * - 2 if the nre passwords are not the same * * @access private * @param string $old old * @param string $new1 * @param string $new2 * @return int $result 0 on success */ function changePasswd( $old, $new1, $new2 ) { if( !$this->_id ) { return -1; } // check old password $crypt = md5( $old ); if( $crypt !== $this->_user['password'] ) { return 1; } if( $new1 !== $new2 ) { return 2; } $pass = md5( $new1 ); $data = array( 'password' => $pass ); if( $this->_ds === null ) { $this->_ds =& wbFactory::create( 'wbDatasource' ); } $this->_ds->save( 'user', $this->_id, $data ); return 0; } /** * insert new user by data * * @access public * @param array $data user data to be added * @param array $groups list if groups * @return array $result result-set */ function addNewUser( &$data, $groups = array() ) { $result = array( 'success' => false, 'id' => 'new' ); if( !is_array( $data ) ) { $result['error'] = 'need user data'; return $result; } $dubs = $this->_ds->getDublicates( 'user', 'new', $data ); if( patErrorManager::isError( $dubs ) ) { return patErrorManager::raiseWarning( 'wbUser:Admin:1', 'Cannot create new user', 'Dublicate check returned error' ); } // cannot add if( !empty( $dubs ) ) { $result['error'] = 'dublicates'; $result['dublicates'] = $dubs; return $result; } if( isset( $data['password' ] ) ) { $result['password_clear'] = $data['password']; if( $data['password'][0] !== '!' ) { $data['password'] = md5( $data['password'] ); } } else { $pass = $this->createPasswd(); $result['password_clear'] = $pass; $data['password'] = md5( $pass ); } $data['created'] = date( 'Y-m-d H:i:s' ); if( !isset( $data['flags'] ) ) { $data['flags'] = 'new'; } else if( is_array( $data['flags'] ) ) { $data['flags'] = implode( ',', $data['flags'] ); } $id = $this->_ds->save( 'user', 'new', $data ); if( patErrorManager::isError( $id ) ) { return $id; } $primary = $this->_ds->getPrimaryKey( 'user' ); $data[$primary] = $id; $result['success'] = true; $result['id'] = $id; $this->addUserToGroups( $id, $groups ); return $result; } /** * activate new user account * * @access public * @param string $user username * @param string $pass users secret password * @return boolean $result true on success */ function activateUser( $user, $pass ) { $clause = array( array( 'field' => 'flags', 'value' => 'new', 'relation' => 'in_set' ), array( 'field' => 'user', 'value' => $user ), array( 'field' => 'password', 'value' => md5( $pass ) ), ); $data = $this->_ds->getEntry( 'user', null, null, $clause ); if( patErrorManager::isError( $data ) ) { return $data; } if( empty( $data ) ) { return false; } $primary = $this->_ds->getPrimaryKey( 'user' ); $id = $data[$primary]; $flags = explode( ',', $data['flags'] ); $flags = array_flip( $flags ); if( isset( $flags['new'] ) ) { unset( $flags['new'] ); } $flags = array_flip( $flags ); array_push( $flags, 'enabled' ); $data['flags'] = implode( ',', $flags ); $result = $this->_ds->save( 'user', $id, $data ); if( patErrorManager::isError( $result ) ) { return $result; } return true; } /** * recieve list if unapproved users * * @access private * @return array $users user list */ function getUnapprovedUsers() { if( !in_array( 'approved', $this->_flagsReqiured ) ) { return array(); } $clause = array( array( 'field' => 'flags', 'value' => 'approved', 'relation' => 'not_in_set' ), array( 'field' => 'flags', 'value' => 'new', 'relation' => 'not_in_set' ), array( 'field' => 'flags', 'value' => 'enabled', 'relation' => 'in_set' ), ); return $this->_ds->getEntries( 'user', null, $clause ); } /** * set approve-flag in user table * * * @access private * @param * @return boolean $result true on success * @see getUnapprovedUsers() */ function approveUser( $uid ) { if( !in_array( 'approved', $this->_flagsReqiured ) ) { return $uid; } $primary = $this->_ds->getPrimaryKey( 'user' ); $data = $this->_ds->getEntry( 'user', $uid ); if( patErrorManager::isError( $data ) ) { return $data; } $flags = explode( ',', $data['flags'] ); $flags = array_flip( $flags ); if( isset( $flags['new'] ) ) { unset( $flags['new'] ); } $flags = array_flip( $flags ); array_push( $flags, 'approved' ); $new = array(); $new['flags'] = implode( ',', $flags ); return $this->_ds->save( 'user', $uid, $new ); } /** * add new group into database * * * @access private * @param string $name group name * @return int $id id of the new group */ function addGroup( $name ) { $result = array(); $result['success'] = false; $data = array( 'groupname' => $name ); $dubs = $this->_ds->getDublicates( 'group', 'new', $data ); if( patErrorManager::isError( $dubs ) ) { return patErrorManager::raiseWarning( 'wbUser:Admin:2', 'Cannot create new group', 'Dublicate check returned error' ); } // cannot add if( !empty( $dubs ) ) { $result['error'] = 'dublicates'; $result['dublicates'] = $dubs; return $result; } $res = $this->_ds->save( 'group', 'new', $data ); if( patErrorManager::isError( $res ) ) { return $res; } $result['success'] = true; $result['id'] = $res; return $result; } /** * remove a group from database * * @access public * @param sint $gid * @return boolean $result true on success * @see addGroup() */ function removeGroup( $gid ) { return $this->_ds->delete( 'group', $gid ); } /** * add user to multiple groups * * * @access private * @param int $uid user id * @param mixed $groups list of groups * @return boolean $result true on success */ function addUserToGroups( $uid, $groups ) { if( !is_array( $groups ) ) { $groups = array( $groups ); } $gPrimary = $this->_ds->getPrimaryKey( 'group' ); $uPrimary = $this->_ds->getPrimaryKey( 'user' ); foreach( $groups as $group ) { $clause = array( array( 'field' => 'groupname', 'value' => $group ) ); $g = $this->_ds->getEntry( 'group', null, null, $clause ); if( patErrorManager::isError( $g) ) { return $g; } if( !is_array( $g ) ) { // group not found! patErrorManager::raiseWarning( 'wbUser:Admin31', 'Add user to group failed', 'Group named "'. $group .'" could not be identified. Usually, this error results from poor configuration' ); continue; } $gid = $g[$gPrimary]; $clause = array( array( 'field' => $uPrimary, 'value' => $uid ), array( 'field' => $gPrimary, 'value' => $gid ), ); $ug = $this->_ds->getEntry( 'usergroup', null, null, $clause ); if( $ug === 0 ) { $data = array( $uPrimary => $uid, $gPrimary => $gid, ); $res = $this->_ds->save( 'usergroup', 'new', $data ); if( patErrorManager::isError( $res ) ) { return $res; } } } return true; } /** * remove user from multiple groups * * * @access private * @param int $uid user id * @param mixed $groups list of groups * @return boolean $result true on success */ function removeUserFromGroups( $uid, $groups ) { if( !is_array( $groups ) ) { $groups = array( $groups ); } $primary = $this->_ds->getPrimaryKey( 'usergroup' ); $uGroups = $this->getGroups( $uid ); foreach( $groups as $group ) { foreach( $uGroups as $uGroup ) { if( $uGroup['groupname'] == $group ) { $res = $this->_ds->delete( 'usergroup', $uGroup[$primary] ); if( patErrorManager::isError( $res ) ) { return $res; } } } } return true; } /** * remove user from multiple groups * * * @access private * @param int $uid user id * @param mixed $groups list of groups * @return boolean $result true on success */ function setUserGroups( $uid, $groups ) { if( !is_array( $groups ) ) { $groups = array( $groups ); } $groupsAll = $this->getGroups( null ); $groupsAdd = array(); $groupsDel = array(); foreach( $groupsAll as $g ) { if( in_array( $g['groupname'], $groups ) ) { array_push( $groupsAdd, $g['groupname'] ); } else { array_push( $groupsDel, $g['groupname'] ); } } $this->removeUserFromGroups( $uid, $groupsDel ); $this->addUserToGroups( $uid, $groupsAdd ); return true; } /** * count number of group members * * @access private * @param int $gid id of the group * @return int $members */ function countMembers( $gid ) { $primary = $this->_ds->getPrimaryKey( 'group' ); $clause = array( array( 'field' => $primary, 'value' => $gid ) ); return $this->_ds->count( 'usergroup', $clause ); } /** * get members of a group * * @access private * @param int $gid id of the group * @return array $users */ function getMembers( $gid ) { $primary = $this->_ds->getPrimaryKey( 'group' ); $clause = array( array( 'field' => $primary, 'value' => $gid ) ); return $this->_ds->getEntries( 'usergroup', null, $clause ); } /** * create random password * * @access private * @return string $password password string */ function createPasswd() { $chars = '_§$%&/()=?+*#,.:;-'; $chars .= implode( '', range( '1', '9' ) ); $chars .= implode( '', range( 'a', 'z' ) ); $chars .= implode( '', range( 'A', 'Z' ) ); $length = 8; // create password $code = ''; for( $i = 0; $i < $length; ++$i ) { $rand = rand( 0, ( strlen( $chars ) - 1 ) ); $code .= $chars[$rand]; } return $code; } /** * get list of users * * @access public * @return array $users */ function getUsers() { return $this->_ds->getEntries( 'user' ); } /** * get list of users that match the data * * @access public * @param array $data * @return array $users */ function findUsers( $data ) { $clause = array(); foreach( $data as $key => $value ) { array_push( $clause, array( 'field' => $key, 'relation' => 'like', 'value' => $value ) ); } return $this->_ds->getEntries( 'user', null, $clause ); } } ?>