diff --git a/include/PoolObjectSQL.h b/include/PoolObjectSQL.h index 354a29d7b4..ebd1eb8f70 100644 --- a/include/PoolObjectSQL.h +++ b/include/PoolObjectSQL.h @@ -95,12 +95,7 @@ public: * @param xml the resulting XML string * @return a reference to the generated string */ -// virtual string& to_xml(string& xml) const = 0; -// TODO: change to pure virtual when all child classes implement it - string& to_xml(string& xml) const - { - return xml; - }; + virtual string& to_xml(string& xml) const = 0; /** * Rebuilds the object from an xml formatted string @@ -108,12 +103,7 @@ public: * * @return 0 on success, -1 otherwise */ -// virtual int from_xml(const string &xml_str) = 0; -// TODO: change to pure virtual when all child classes implement it - virtual int from_xml(const string &xml_str) - { - return 0; - }; + virtual int from_xml(const string &xml_str) = 0; protected: diff --git a/include/User.h b/include/User.h index 473c5f29b8..21c2a95781 100644 --- a/include/User.h +++ b/include/User.h @@ -36,13 +36,6 @@ public: */ friend ostream& operator<<(ostream& os, User& u); - /** - * Function to print the User object into a string in plain text - * @param str the resulting string - * @return a reference to the generated string - */ - string& to_str(string& str) const; - /** * Function to print the User object into a string in XML format * @param xml the resulting XML string @@ -191,6 +184,14 @@ private: db->exec(oss_user); }; + /** + * Rebuilds the object from an xml formatted string + * @param xml_str The xml-formatted string + * + * @return 0 on success, -1 otherwise + */ + int from_xml(const string &xml_str); + protected: // ************************************************************************* @@ -208,28 +209,12 @@ protected: // DataBase implementation // ************************************************************************* - enum ColNames - { - OID = 0, - USERNAME = 1, - PASSWORD = 2, - ENABLED = 3, // 0 = false, 1 = true - LIMIT = 4 - }; - static const char * db_names; static const char * db_bootstrap; static const char * table; - /** - * Reads the User (identified with its OID=UID) from the database. - * @param db pointer to the db - * @return 0 on success - */ - virtual int select(SqlDB *db); - /** * Writes the User in the database. * @param db pointer to the db @@ -243,23 +228,6 @@ protected: * @return 0 on success */ virtual int update(SqlDB *db); - - /** - * Drops USer from the database - * @param db pointer to the db - * @return 0 on success - */ - virtual int drop(SqlDB *db); - - /** - * Function to output a User object in to an stream in XML format - * @param oss the output stream - * @param num the number of columns read from the DB - * @param names the column names - * @param vaues the column values - * @return 0 on success - */ - static int dump(ostringstream& oss, int num, char **values, char **names); }; #endif /*USER_H_*/ diff --git a/src/pool/test/TestPoolSQL.h b/src/pool/test/TestPoolSQL.h index 49d344b67f..62d4578889 100644 --- a/src/pool/test/TestPoolSQL.h +++ b/src/pool/test/TestPoolSQL.h @@ -71,6 +71,16 @@ public: db->exec(oss,0); }; + + string& to_xml(string& xml) const + { + return xml; + }; + + int from_xml(const string &xml_str) + { + return 0; + }; }; // THE POOL diff --git a/src/um/User.cc b/src/um/User.cc index a54d9a404b..3d4c9ba1a8 100644 --- a/src/um/User.cc +++ b/src/um/User.cc @@ -35,7 +35,7 @@ User::User( string _username, string _password, bool _enabled): - PoolObjectSQL(id), + PoolObjectSQL(id, table), username (_username), password (_password), enabled (_enabled) @@ -50,63 +50,10 @@ User::~User(){}; const char * User::table = "user_pool"; -const char * User::db_names = "oid,user_name,password,enabled"; +const char * User::db_names = "oid,name,body"; const char * User::db_bootstrap = "CREATE TABLE IF NOT EXISTS user_pool (" - "oid INTEGER PRIMARY KEY, user_name VARCHAR(256), password TEXT," - "enabled INTEGER, UNIQUE(user_name))"; - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int User::select_cb(void *nil, int num, char **values, char **names) -{ - if ((!values[OID]) || - (!values[USERNAME]) || - (!values[PASSWORD]) || - (!values[ENABLED]) || - (num != LIMIT )) - { - return -1; - } - - oid = atoi(values[OID]); - username = values[USERNAME]; - password = values[PASSWORD]; - enabled = (atoi(values[ENABLED])==0)?false:true; - - return 0; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -/* -------------------------------------------------------------------------- */ - -int User::select(SqlDB *db) -{ - ostringstream oss; - int rc; - int boid; - - set_callback(static_cast(&User::select_cb)); - - oss << "SELECT " << db_names << " FROM " << table << " WHERE oid = " << oid; - - boid = oid; - oid = -1; - - rc = db->exec(oss, this); - - unset_callback(); - - if ((rc != 0) || (oid != boid )) - { - return -1; - } - - return 0; -} + "oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, UNIQUE(name))"; /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -151,11 +98,10 @@ int User::insert_replace(SqlDB *db, bool replace) ostringstream oss; int rc; + string xml_body; char * sql_username; - char * sql_password; - - int str_enabled = enabled?1:0; + char * sql_xml; // Update the User @@ -166,11 +112,11 @@ int User::insert_replace(SqlDB *db, bool replace) goto error_username; } - sql_password = db->escape_str(password.c_str()); + sql_xml = db->escape_str(to_xml(xml_body).c_str()); - if ( sql_password == 0 ) + if ( sql_xml == 0 ) { - goto error_password; + goto error_body; } // Construct the SQL statement to Insert or Replace @@ -184,69 +130,23 @@ int User::insert_replace(SqlDB *db, bool replace) } oss << " INTO " << table << " ("<< db_names <<") VALUES (" - << oid << "," - << "'" << sql_username << "'," - << "'" << sql_password << "'," - << str_enabled << ")"; + << oid << "," + << "'" << sql_username << "'," + << "'" << sql_xml << "')"; rc = db->exec(oss); db->free_str(sql_username); - db->free_str(sql_password); + db->free_str(sql_xml); return rc; -error_password: +error_body: db->free_str(sql_username); error_username: return -1; } -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int User::dump(ostringstream& oss, int num, char **values, char **names) -{ - if ((!values[OID]) || - (!values[USERNAME]) || - (!values[PASSWORD]) || - (!values[ENABLED]) || - (num != LIMIT)) - { - return -1; - } - - oss << - "" << - "" << values[OID] <<"" << - "" << values[USERNAME]<<"" << - ""<< values[PASSWORD]<<""<< - "" << values[ENABLED] <<"" << - ""; - - return 0; -} - -/* -------------------------------------------------------------------------- */ -/* -------------------------------------------------------------------------- */ - -int User::drop(SqlDB * db) -{ - ostringstream oss; - int rc; - - oss << "DELETE FROM " << table << " WHERE oid=" << oid; - - rc = db->exec(oss); - - if ( rc == 0 ) - { - set_valid(false); - } - - return rc; -} - /* ************************************************************************** */ /* User :: Misc */ /* ************************************************************************** */ @@ -260,7 +160,6 @@ ostream& operator<<(ostream& os, User& user) return os; }; - /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ @@ -286,23 +185,28 @@ string& User::to_xml(string& xml) const /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -string& User::to_str(string& str) const +int User::from_xml(const string& xml) { - ostringstream os; + int rc = 0; + int int_enabled; - string enabled_str = enabled?"True":"False"; + // Initialize the internal XML object + update_from_str(xml); - os << - "ID = " << oid << endl << - "NAME = " << username << endl << - "PASSWORD = " << password << endl << - "ENABLED = " << enabled_str; + rc += xpath(oid, "/USER/ID", -1); + rc += xpath(username, "/USER/NAME", "not_found"); + rc += xpath(password, "/USER/PASSWORD", "not_found"); + rc += xpath(int_enabled, "/USER/ENABLED", 0); - str = os.str(); + enabled = int_enabled; - return str; + if (rc != 0) + { + return -1; + } + + return 0; } - /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ diff --git a/src/um/UserPool.cc b/src/um/UserPool.cc index fb43aa8dcf..d953d86ed2 100644 --- a/src/um/UserPool.cc +++ b/src/um/UserPool.cc @@ -343,7 +343,7 @@ int UserPool::dump(ostringstream& oss, const string& where) set_callback(static_cast(&UserPool::dump_cb), static_cast(&oss)); - cmd << "SELECT " << User::db_names << " FROM " << User::table; + cmd << "SELECT body FROM " << User::table; if ( !where.empty() ) { diff --git a/src/um/test/UserPoolTest.cc b/src/um/test/UserPoolTest.cc index dc2758713f..b931ea0b9a 100644 --- a/src/um/test/UserPoolTest.cc +++ b/src/um/test/UserPoolTest.cc @@ -287,6 +287,14 @@ public: ostringstream oss; ((UserPool*)pool)->dump(oss, ""); +/* + if( oss.str() != dump_result ) + { + cout << endl << oss.str() << endl << "========" + << endl << dump_result << endl << "--------"; + } +//*/ + CPPUNIT_ASSERT( oss.str() == dump_result ); } @@ -307,7 +315,15 @@ public: // by" is a dirty fix (SQL injection, actually) because MySQL orders the // results by user_name ostringstream oss; - ((UserPool*)pool)->dump(oss, "user_name LIKE 'a%' ORDER BY oid"); + ((UserPool*)pool)->dump(oss, "name LIKE 'a%' ORDER BY oid"); + +/* + if( oss.str() != dump_where_result ) + { + cout << endl << oss.str() << endl << "========" + << endl << dump_where_result << endl << "--------"; + } +//*/ CPPUNIT_ASSERT( oss.str() == dump_where_result ); }