mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-13 12:58:17 +03:00
Feature #407: User object modified for new XML DB schema
This commit is contained in:
parent
0bcf29fb4b
commit
de03c2859f
@ -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:
|
||||
|
||||
|
@ -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_*/
|
||||
|
@ -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
|
||||
|
154
src/um/User.cc
154
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<Callbackable::Callback>(&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 <<
|
||||
"<USER>" <<
|
||||
"<ID>" << values[OID] <<"</ID>" <<
|
||||
"<NAME>" << values[USERNAME]<<"</NAME>" <<
|
||||
"<PASSWORD>"<< values[PASSWORD]<<"</PASSWORD>"<<
|
||||
"<ENABLED>" << values[ENABLED] <<"</ENABLED>" <<
|
||||
"</USER>";
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
|
@ -343,7 +343,7 @@ int UserPool::dump(ostringstream& oss, const string& where)
|
||||
set_callback(static_cast<Callbackable::Callback>(&UserPool::dump_cb),
|
||||
static_cast<void *>(&oss));
|
||||
|
||||
cmd << "SELECT " << User::db_names << " FROM " << User::table;
|
||||
cmd << "SELECT body FROM " << User::table;
|
||||
|
||||
if ( !where.empty() )
|
||||
{
|
||||
|
@ -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 );
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user