Hi,
I want to create method in my UserManager that will authenticate user, but i dont know how to send additional data (md5 hash of username and password) with each request. Can you help me?
QxOrm provides the class
qx::QxSimpleCrypt to encrypt/decrypt some datas :
http://www.qxorm.com/doxygen/html/class ... crypt.htmlThis class is used for example by QxService module (
http://www.qxorm.com/doxygen/html/group ... rvice.html) to send encrypted datas over network.
To store an encrypted password in your database, you could imagine to have 2 properties in your class to manage the password :
1- one property containing the password returned by the GUI ;
2- another property (I think
private) containing the encrypted password.
And in your table, you have only one column to manage associated to the second property (encrypted).
Then, using QxOrm triggers (
http://www.qxorm.com/qxorm_en/faq.html#faq_130), you will be able to encrypt the password before saving an instance, or decrypt it after fetching.
For example, here is the file '
UserManager.h' :
- Code: Select all
class QX_DLL2_EXPORT UserManager
{
QX_REGISTER_FRIEND_CLASS(UserManager)
protected:
long id;
QString userName;
QString userPassword;
QString userPasswordEncrypted;
public:
UserManager() : id(0) { ; }
virtual ~UserManager() { ; }
void onBeforeInsert(qx::dao::detail::IxDao_Helper * dao);
void onBeforeUpdate(qx::dao::detail::IxDao_Helper * dao);
void onBeforeDelete(qx::dao::detail::IxDao_Helper * dao);
void onBeforeFetch(qx::dao::detail::IxDao_Helper * dao);
void onAfterInsert(qx::dao::detail::IxDao_Helper * dao);
void onAfterUpdate(qx::dao::detail::IxDao_Helper * dao);
void onAfterDelete(qx::dao::detail::IxDao_Helper * dao);
void onAfterFetch(qx::dao::detail::IxDao_Helper * dao);
};
QX_REGISTER_HPP_QX_DLL2(UserManager, qx::trait::no_base_class_defined, 0)
typedef boost::shared_ptr<UserManager> UserManager_ptr;
namespace qx {
namespace dao {
namespace detail {
template <>
struct QxDao_Trigger<UserManager>
{
static inline void onBeforeInsert(UserManager * t, qx::dao::detail::IxDao_Helper * dao) { if (t) { t->onBeforeInsert(dao); } }
static inline void onBeforeUpdate(UserManager * t, qx::dao::detail::IxDao_Helper * dao) { if (t) { t->onBeforeUpdate(dao); } }
static inline void onBeforeDelete(UserManager * t, qx::dao::detail::IxDao_Helper * dao) { if (t) { t->onBeforeDelete(dao); } }
static inline void onBeforeFetch(UserManager * t, qx::dao::detail::IxDao_Helper * dao) { if (t) { t->onBeforeFetch(dao); } }
static inline void onAfterInsert(UserManager * t, qx::dao::detail::IxDao_Helper * dao) { if (t) { t->onAfterInsert(dao); } }
static inline void onAfterUpdate(UserManager * t, qx::dao::detail::IxDao_Helper * dao) { if (t) { t->onAfterUpdate(dao); } }
static inline void onAfterDelete(UserManager * t, qx::dao::detail::IxDao_Helper * dao) { if (t) { t->onAfterDelete(dao); } }
static inline void onAfterFetch(UserManager * t, qx::dao::detail::IxDao_Helper * dao) { if (t) { t->onAfterFetch(dao); } }
};
} // namespace detail
} // namespace dao
} // namespace qx
And '
UserManager.cpp' file :
- Code: Select all
QX_REGISTER_CPP_QX_DLL2(UserManager)
namespace qx {
template <> void register_class(QxClass<UserManager> & t)
{
qx::IxDataMember * pData = NULL;
pData = t.id(& UserManager::id, "id");
pData = t.data(& UserManager::userName, "userName");
pData = t.data(& UserManager::userPasswordEncrypted, "userPasswordEncrypted");
pData = t.data(& UserManager::userPassword, "userPassword");
pData->setDao(false); // Not a part of the table
}}
void UserManager::onBeforeInsert(qx::dao::detail::IxDao_Helper * dao)
{
if (! dao) { qAssert(false); return; }
if (dao->error().isValid()) { return; }
// Encrypt password before saving
QxSimpleCrypt crypto(QxConnect::getSingleton()->getEncryptKey());
crypto.setCompressionMode(QxSimpleCrypt::CompressionNever);
crypto.setIntegrityProtectionMode(QxSimpleCrypt::ProtectionChecksum);
userPasswordEncrypted = crypto.encryptToString(userPassword);
// Manage the error here !
if ((crypto.lastError() != QxSimpleCrypt::ErrorNoError) || userPasswordEncrypted.isEmpty())
{ qAssert(false); return; }
}
void UserManager::onBeforeUpdate(qx::dao::detail::IxDao_Helper * dao)
{
if (! dao) { qAssert(false); return; }
if (dao->error().isValid()) { return; }
// Encrypt password before saving
QxSimpleCrypt crypto(QxConnect::getSingleton()->getEncryptKey());
crypto.setCompressionMode(QxSimpleCrypt::CompressionNever);
crypto.setIntegrityProtectionMode(QxSimpleCrypt::ProtectionChecksum);
userPasswordEncrypted = crypto.encryptToString(userPassword);
// Manage the error here !
if ((crypto.lastError() != QxSimpleCrypt::ErrorNoError) || userPasswordEncrypted.isEmpty())
{ qAssert(false); return; }
}
void UserManager::onAfterFetch(qx::dao::detail::IxDao_Helper * dao)
{
if (! dao) { qAssert(false); return; }
if (dao->error().isValid()) { return; }
// Decrypt password after fetching
QxSimpleCrypt crypto(QxConnect::getSingleton()->getEncryptKey());
userPassword = crypto.decryptToString(userPasswordEncrypted);
// Manage the error here !
if ((crypto.lastError() != QxSimpleCrypt::ErrorNoError) || userPassword.isEmpty())
{ qAssert(false); return; }
}
void UserManager::onBeforeDelete(qx::dao::detail::IxDao_Helper * dao)
{
if (! dao) { qAssert(false); return; }
if (dao->error().isValid()) { return; }
// Nothing to do here !
}
void UserManager::onBeforeFetch(qx::dao::detail::IxDao_Helper * dao)
{
if (! dao) { qAssert(false); return; }
if (dao->error().isValid()) { return; }
// Nothing to do here !
}
void UserManager::onAfterInsert(qx::dao::detail::IxDao_Helper * dao)
{
if (! dao) { qAssert(false); return; }
if (dao->error().isValid()) { return; }
// Nothing to do here !
}
void UserManager::onAfterUpdate(qx::dao::detail::IxDao_Helper * dao)
{
if (! dao) { qAssert(false); return; }
if (dao->error().isValid()) { return; }
// Nothing to do here !
}
void UserManager::onAfterDelete(qx::dao::detail::IxDao_Helper * dao)
{
if (! dao) { qAssert(false); return; }
if (dao->error().isValid()) { return; }
// Nothing to do here !
}