diff --git a/include/lrdatasourcemanagerintf.h b/include/lrdatasourcemanagerintf.h index 85cf388..57bd0c1 100644 --- a/include/lrdatasourcemanagerintf.h +++ b/include/lrdatasourcemanagerintf.h @@ -37,18 +37,25 @@ class QString; class QAbstractItemModel; namespace LimeReport{ +class IDbCredentialsProvider{ +public: + virtual QString getUserName(const QString& connectionName) = 0; + virtual QString getPassword(const QString& connectionName) = 0; +}; + class IDataSourceManager{ public: - virtual void setReportVariable(const QString& name, const QVariant& value)=0; - virtual void setDefaultDatabasePath(const QString &defaultDatabasePath)=0; - virtual void deleteVariable(const QString& name)=0; - virtual bool containsVariable(const QString& variableName)=0; - virtual QVariant variable(const QString& variableName)=0; - virtual bool addModel(const QString& name, QAbstractItemModel *model, bool owned)=0; - virtual void removeModel(const QString& name)=0; - virtual bool containsDatasource(const QString& dataSourceName)=0; + virtual void setReportVariable(const QString& name, const QVariant& value) = 0; + virtual void setDefaultDatabasePath(const QString &defaultDatabasePath) = 0; + virtual void deleteVariable(const QString& name) = 0; + virtual bool containsVariable(const QString& variableName) = 0; + virtual QVariant variable(const QString& variableName) = 0; + virtual bool addModel(const QString& name, QAbstractItemModel *model, bool owned) = 0; + virtual void removeModel(const QString& name) = 0; + virtual bool containsDatasource(const QString& dataSourceName) = 0; virtual void clearUserVariables()=0; virtual ICallbackDatasource* createCallbackDatasouce(const QString& name) = 0; + virtual void registerDbCredentialsProvider(IDbCredentialsProvider* provider) = 0; //virtual void addCallbackDatasource(ICallbackDatasource* datasource, const QString& name) = 0; }; diff --git a/include/lrreportengine.h b/include/lrreportengine.h index a07d519..9bd87b5 100644 --- a/include/lrreportengine.h +++ b/include/lrreportengine.h @@ -102,6 +102,7 @@ public: void setResultEditable(bool value); bool resultIsEditable(); bool isBusy(); + void setPassPharse(QString& passPharse); signals: void renderStarted(); void renderFinished(); diff --git a/limereport/databrowser/lrconnectiondialog.cpp b/limereport/databrowser/lrconnectiondialog.cpp index 60652cf..b90545e 100644 --- a/limereport/databrowser/lrconnectiondialog.cpp +++ b/limereport/databrowser/lrconnectiondialog.cpp @@ -122,6 +122,7 @@ ConnectionDesc *ConnectionDialog::uiToConnection(LimeReport::ConnectionDesc* con result ->setPassword(ui->lePassword->text()); result ->setDatabaseName(ui->leDataBase->text()); result ->setAutoconnect(ui->cbAutoConnect->isChecked()); + result->setKeepDBCredentials(!ui->cbbKeepCredentials->isChecked()); return result ; } @@ -137,6 +138,7 @@ void ConnectionDialog::connectionToUI() ui->lePassword->setText(m_connection->password()); ui->cbbDrivers->setCurrentIndex(ui->cbbDrivers->findText(m_connection->driver())); ui->cbAutoConnect->setChecked(m_connection->autoconnect()); + ui->cbbKeepCredentials->setChecked(!m_connection->keepDBCredentials()); } void ConnectionDialog::on_toolButton_clicked() diff --git a/limereport/databrowser/lrconnectiondialog.ui b/limereport/databrowser/lrconnectiondialog.ui index aa06393..60bf527 100644 --- a/limereport/databrowser/lrconnectiondialog.ui +++ b/limereport/databrowser/lrconnectiondialog.ui @@ -137,6 +137,13 @@ + + + + Dont keep credentals in lrxml + + + diff --git a/limereport/lrdatadesignintf.cpp b/limereport/lrdatadesignintf.cpp index ccff471..0d2d14b 100644 --- a/limereport/lrdatadesignintf.cpp +++ b/limereport/lrdatadesignintf.cpp @@ -351,13 +351,13 @@ void ModelToDataSource::slotModelDestroed() ConnectionDesc::ConnectionDesc(QSqlDatabase db, QObject *parent) : QObject(parent), m_connectionName(db.connectionName()), m_connectionHost(db.hostName()), m_connectionDriver(db.driverName()), m_databaseName(db.databaseName()), m_user(db.userName()), m_password(db.password()), m_autoconnect(false), - m_internal(false) + m_internal(false), m_keepDBCredentials(true) {} ConnectionDesc::ConnectionDesc(QObject *parent) :QObject(parent),m_connectionName(""),m_connectionHost(""),m_connectionDriver(""), m_databaseName(""), m_user(""), m_password(""), m_autoconnect(false), - m_internal(false) + m_internal(false), m_keepDBCredentials(true) {} ConnectionDesc::Ptr ConnectionDesc::create(QSqlDatabase db, QObject *parent) @@ -391,6 +391,16 @@ QString ConnectionDesc::connectionNameForReport(const QString &connectionName) return connectionName.compare(tr("defaultConnection")) == 0 ? QSqlDatabase::defaultConnection : connectionName; } +bool ConnectionDesc::keepDBCredentials() const +{ + return m_keepDBCredentials; +} + +void ConnectionDesc::setKeepDBCredentials(bool keepDBCredentals) +{ + m_keepDBCredentials = keepDBCredentals; +} + QueryDesc::QueryDesc(QString queryName, QString queryText, QString connection) :m_queryName(queryName), m_queryText(queryText), m_connectionName(connection) {} diff --git a/limereport/lrdatadesignintf.h b/limereport/lrdatadesignintf.h index d2c9685..e2f8134 100644 --- a/limereport/lrdatadesignintf.h +++ b/limereport/lrdatadesignintf.h @@ -110,6 +110,7 @@ class ConnectionDesc : public QObject{ Q_PROPERTY(QString password READ password WRITE setPassword) Q_PROPERTY(QString host READ host WRITE setHost) Q_PROPERTY(bool autoconnect READ autoconnect WRITE setAutoconnect) + Q_PROPERTY(bool keepDBCredentials READ keepDBCredentials WRITE setKeepDBCredentials) public: typedef QSharedPointer Ptr; ConnectionDesc(QSqlDatabase db, QObject* parent=0); @@ -124,17 +125,19 @@ public: void setDatabaseName(const QString &value){m_databaseName=value;} QString databaseName(){return m_databaseName;} void setUserName(const QString &value){m_user=value;} - QString userName(){return m_user;} + QString userName(){ return m_user; } void setPassword(const QString &value){m_password=value;} - QString password(){return m_password;} + QString password(){ return m_password; } void setAutoconnect(bool value){m_autoconnect=value;} bool autoconnect(){return m_autoconnect;} bool isEqual(const QSqlDatabase& db); bool isInternal(){ return m_internal; } void setInternal(bool value) {m_internal = value;} + bool keepDBCredentials() const; + void setKeepDBCredentials(bool keepDBCredentials); public: static QString connectionNameForUser(const QString& connectionName); - static QString connectionNameForReport(const QString& connectionName); + static QString connectionNameForReport(const QString& connectionName); signals: void nameChanged(const QString& oldName,const QString& newName); private: @@ -146,6 +149,7 @@ private: QString m_password; bool m_autoconnect; bool m_internal; + bool m_keepDBCredentials; }; class IConnectionController{ diff --git a/limereport/lrdatasourcemanager.cpp b/limereport/lrdatasourcemanager.cpp index 5607117..74dd69e 100644 --- a/limereport/lrdatasourcemanager.cpp +++ b/limereport/lrdatasourcemanager.cpp @@ -217,7 +217,7 @@ void DataSourceModel::updateModel() } DataSourceManager::DataSourceManager(QObject *parent) : - QObject(parent), m_lastError(""), m_designTime(true), m_needUpdate(false) + QObject(parent), m_lastError(""), m_designTime(true), m_needUpdate(false), m_dbCredentialsProvider(0) { m_groupFunctionFactory.registerFunctionCreator(QLatin1String("COUNT"),new ConstructorGroupFunctionCreator); m_groupFunctionFactory.registerFunctionCreator(QLatin1String("SUM"),new ConstructorGroupFunctionCreator); @@ -324,6 +324,11 @@ ICallbackDatasource *DataSourceManager::createCallbackDatasouce(const QString& n return ds; } +void DataSourceManager::registerDbCredentialsProvider(IDbCredentialsProvider *provider) +{ + m_dbCredentialsProvider = provider; +} + void DataSourceManager::addCallbackDatasource(ICallbackDatasource *datasource, const QString& name) { IDataSource* datasourceIntf = dynamic_cast(datasource); @@ -567,6 +572,11 @@ int DataSourceManager::connectionIndexByName(const QString &connectionName) return -1; } +QList& DataSourceManager::conections() +{ + return m_connections; +} + bool DataSourceManager::dataSourceIsValid(const QString &name) { if (m_datasources.value(name.toLower())) return !m_datasources.value(name.toLower())->isInvalid(); @@ -704,10 +714,19 @@ void DataSourceManager::putProxyDesc(ProxyDesc *proxyDesc) bool DataSourceManager::initAndOpenDB(QSqlDatabase& db, ConnectionDesc& connectionDesc){ bool connected = false; + + db.setHostName(replaceVariables(connectionDesc.host())); db.setUserName(replaceVariables(connectionDesc.userName())); db.setPassword(replaceVariables(connectionDesc.password())); + if (!connectionDesc.keepDBCredentials() && m_dbCredentialsProvider){ + if (!m_dbCredentialsProvider->getUserName(connectionDesc.name()).isEmpty()) + db.setUserName(m_dbCredentialsProvider->getUserName(connectionDesc.name())); + if (!m_dbCredentialsProvider->getPassword(connectionDesc.name()).isEmpty()) + db.setPassword(m_dbCredentialsProvider->getPassword(connectionDesc.name())); + } + QString dbName = replaceVariables(connectionDesc.databaseName()); if (connectionDesc.driver().compare("QSQLITE")==0){ if (!defaultDatabasePath().isEmpty()){ @@ -726,7 +745,6 @@ bool DataSourceManager::initAndOpenDB(QSqlDatabase& db, ConnectionDesc& connecti } connected=db.open(); - connectionDesc.setInternal(true); if (!connected) setLastError(db.lastError().text()); return connected; } @@ -758,10 +776,17 @@ bool DataSourceManager::connectConnection(ConnectionDesc *connectionDesc) } if (!QSqlDatabase::contains(connectionDesc->name())){ - QSqlDatabase db = QSqlDatabase::addDatabase(connectionDesc->driver(),connectionDesc->name()); - connected=initAndOpenDB(db, *connectionDesc); + QString dbError; + { + QSqlDatabase db = QSqlDatabase::addDatabase(connectionDesc->driver(),connectionDesc->name()); + connectionDesc->setInternal(true); + connected=initAndOpenDB(db, *connectionDesc); + dbError = db.lastError().text(); + } if (!connected){ - setLastError(db.lastError().text()); + if (!dbError.trimmed().isEmpty()) + setLastError(dbError); + QSqlDatabase::removeDatabase(connectionDesc->name()); return false; } } else { @@ -868,8 +893,9 @@ bool DataSourceManager::isConnection(const QString &connectionName) bool DataSourceManager::isConnectionConnected(const QString &connectionName) { - if (isConnection(connectionName)){ - return QSqlDatabase::database(connectionName).isOpen(); + if (isConnection(connectionName) && QSqlDatabase::contains(connectionName)){ + QSqlDatabase db = QSqlDatabase::database(connectionName); + return db.isValid() && QSqlDatabase::database(connectionName).isOpen(); } return false; } @@ -893,8 +919,10 @@ void DataSourceManager::disconnectConnection(const QString& connectionName) ConnectionDesc* connectionDesc = connectionByName(connectionName); if (connectionDesc->isInternal()){ - QSqlDatabase db = QSqlDatabase::database(connectionName); - if (db.isOpen()) db.close(); + { + QSqlDatabase db = QSqlDatabase::database(connectionName); + if (db.isOpen()) db.close(); + } if (QSqlDatabase::contains(connectionName)) QSqlDatabase::removeDatabase(connectionName); } diff --git a/limereport/lrdatasourcemanager.h b/limereport/lrdatasourcemanager.h index c60bfa5..77967c5 100644 --- a/limereport/lrdatasourcemanager.h +++ b/limereport/lrdatasourcemanager.h @@ -115,6 +115,7 @@ public: bool addModel(const QString& name, QAbstractItemModel *model, bool owned); void removeModel(const QString& name); ICallbackDatasource* createCallbackDatasouce(const QString &name); + void registerDbCredentialsProvider(IDbCredentialsProvider *provider); void addCallbackDatasource(ICallbackDatasource *datasource, const QString &name); void setReportVariable(const QString& name, const QVariant& value); void deleteVariable(const QString& name); @@ -150,6 +151,7 @@ public: int proxyIndexByName(const QString& dataSourceName); int connectionIndexByName(const QString& connectionName); + QList &conections(); bool dataSourceIsValid(const QString& name); IDataSource* dataSource(const QString& name); IDataSourceHolder* dataSourceHolder(const QString& name); @@ -250,8 +252,7 @@ private: ReportSettings* m_reportSettings; QHash m_groupFunctionsExpressionsMap; QVector m_groupFunctionsExpressions; - - + IDbCredentialsProvider* m_dbCredentialsProvider; }; } diff --git a/limereport/lrdatasourcemanagerintf.h b/limereport/lrdatasourcemanagerintf.h index 85cf388..57bd0c1 100644 --- a/limereport/lrdatasourcemanagerintf.h +++ b/limereport/lrdatasourcemanagerintf.h @@ -37,18 +37,25 @@ class QString; class QAbstractItemModel; namespace LimeReport{ +class IDbCredentialsProvider{ +public: + virtual QString getUserName(const QString& connectionName) = 0; + virtual QString getPassword(const QString& connectionName) = 0; +}; + class IDataSourceManager{ public: - virtual void setReportVariable(const QString& name, const QVariant& value)=0; - virtual void setDefaultDatabasePath(const QString &defaultDatabasePath)=0; - virtual void deleteVariable(const QString& name)=0; - virtual bool containsVariable(const QString& variableName)=0; - virtual QVariant variable(const QString& variableName)=0; - virtual bool addModel(const QString& name, QAbstractItemModel *model, bool owned)=0; - virtual void removeModel(const QString& name)=0; - virtual bool containsDatasource(const QString& dataSourceName)=0; + virtual void setReportVariable(const QString& name, const QVariant& value) = 0; + virtual void setDefaultDatabasePath(const QString &defaultDatabasePath) = 0; + virtual void deleteVariable(const QString& name) = 0; + virtual bool containsVariable(const QString& variableName) = 0; + virtual QVariant variable(const QString& variableName) = 0; + virtual bool addModel(const QString& name, QAbstractItemModel *model, bool owned) = 0; + virtual void removeModel(const QString& name) = 0; + virtual bool containsDatasource(const QString& dataSourceName) = 0; virtual void clearUserVariables()=0; virtual ICallbackDatasource* createCallbackDatasouce(const QString& name) = 0; + virtual void registerDbCredentialsProvider(IDbCredentialsProvider* provider) = 0; //virtual void addCallbackDatasource(ICallbackDatasource* datasource, const QString& name) = 0; }; diff --git a/limereport/lrreportengine.cpp b/limereport/lrreportengine.cpp index 113dbfa..d4b4fa2 100644 --- a/limereport/lrreportengine.cpp +++ b/limereport/lrreportengine.cpp @@ -59,7 +59,7 @@ ReportEnginePrivate::ReportEnginePrivate(QObject *parent) : m_printer(new QPrinter(QPrinter::HighResolution)), m_printerSelected(false), m_showProgressDialog(true), m_reportName(""), m_activePreview(0), m_previewWindowIcon(":/report/images/logo32"), m_previewWindowTitle(tr("Preview")), - m_reportRendering(false), m_resultIsEditable(true) + m_reportRendering(false), m_resultIsEditable(true), m_passPhrase("HjccbzHjlbyfCkjy") { m_datasources = new DataSourceManager(this); m_datasources->setReportSettings(&m_reportSettings); @@ -513,14 +513,29 @@ bool ReportEnginePrivate::loadFromFile(const QString &fileName) { if (!QFile::exists(fileName)) return false; - clearReport(); + clearReport(); ItemsReaderIntf::Ptr reader = FileXMLReader::create(fileName); + reader->setPassPhrase(m_passPhrase); if (reader->first()){ if (reader->readItem(this)){ m_fileName=fileName; QFileInfo fi(fileName); m_reportName = fi.fileName(); + + QString dbSettingFileName = fi.absolutePath()+"/"+fi.baseName()+".db"; + if (QFile::exists(dbSettingFileName)){ + QSettings dbcredentals(dbSettingFileName, QSettings::IniFormat); + foreach (ConnectionDesc* connection, dataManager()->conections()) { + if (!connection->keepDBCredentials()){ + dbcredentals.beginGroup(connection->name()); + connection->setUserName(dbcredentals.value("user").toString()); + connection->setPassword(dbcredentals.value("password").toString()); + dbcredentals.endGroup(); + } + } + } + dataManager()->connectAutoConnections(); return true; }; @@ -533,6 +548,7 @@ bool ReportEnginePrivate::loadFromByteArray(QByteArray* data, const QString &nam clearReport(); ItemsReaderIntf::Ptr reader = ByteArrayXMLReader::create(data); + reader->setPassPhrase(m_passPhrase); if (reader->first()){ if (reader->readItem(this)){ m_fileName = ""; @@ -548,6 +564,7 @@ bool ReportEnginePrivate::loadFromString(const QString &report, const QString &n clearReport(); ItemsReaderIntf::Ptr reader = StringXMLreader::create(report); + reader->setPassPhrase(m_passPhrase); if (reader->first()){ if (reader->readItem(this)){ m_fileName = ""; @@ -566,10 +583,35 @@ bool ReportEnginePrivate::saveToFile(const QString &fileName) if (fi.suffix().isEmpty()) fn+=".lrxml"; + QString dbSettingFileName = fi.absolutePath()+"/"+fi.baseName()+".db"; + QSettings dbcredentals(dbSettingFileName, QSettings::IniFormat); + + foreach (ConnectionDesc* connection, dataManager()->conections()) { + if (!connection->keepDBCredentials()){ + dbcredentals.beginGroup(connection->name()); + dbcredentals.setValue("user",connection->userName()); + dbcredentals.setValue("password",connection->password()); + dbcredentals.endGroup(); + connection->setPassword(""); + connection->setUserName(""); + } + } + QScopedPointer< ItemsWriterIntf > writer(new XMLWriter()); + writer->setPassPhrase(m_passPhrase); writer->putItem(this); - m_fileName=fn; + m_fileName=fn; bool saved = writer->saveToFile(fn); + + foreach (ConnectionDesc* connection, dataManager()->conections()) { + if (!connection->keepDBCredentials()){ + dbcredentals.beginGroup(connection->name()); + connection->setUserName(dbcredentals.value("user").toString()); + connection->setPassword(dbcredentals.value("password").toString()); + dbcredentals.endGroup(); + } + } + if (saved){ foreach(PageDesignIntf* page, m_pages){ page->setToSaved(); @@ -581,6 +623,7 @@ bool ReportEnginePrivate::saveToFile(const QString &fileName) QByteArray ReportEnginePrivate::saveToByteArray() { QScopedPointer< ItemsWriterIntf > writer(new XMLWriter()); + writer->setPassPhrase(m_passPhrase); writer->putItem(this); QByteArray result = writer->saveToByteArray(); if (!result.isEmpty()){ @@ -593,6 +636,7 @@ QByteArray ReportEnginePrivate::saveToByteArray() QString ReportEnginePrivate::saveToString(){ QScopedPointer< ItemsWriterIntf > writer(new XMLWriter()); + writer->setPassPhrase(m_passPhrase); writer->putItem(this); QString result = writer->saveToString(); if (!result.isEmpty()){ @@ -629,6 +673,11 @@ QString ReportEnginePrivate::renderToString() }else return QString(); } +void ReportEnginePrivate::setPassPhrase(const QString &passPhrase) +{ + m_passPhrase = passPhrase; +} + bool ReportEnginePrivate::resultIsEditable() const { return m_resultIsEditable; @@ -807,6 +856,12 @@ bool ReportEngine::isBusy() return d->isBusy(); } +void ReportEngine::setPassPharse(QString &passPharse) +{ + Q_D(ReportEngine); + d->setPassPhrase(passPharse); +} + void ReportEngine::setShowProgressDialog(bool value) { Q_D(ReportEngine); diff --git a/limereport/lrreportengine.h b/limereport/lrreportengine.h index a07d519..9bd87b5 100644 --- a/limereport/lrreportengine.h +++ b/limereport/lrreportengine.h @@ -102,6 +102,7 @@ public: void setResultEditable(bool value); bool resultIsEditable(); bool isBusy(); + void setPassPharse(QString& passPharse); signals: void renderStarted(); void renderFinished(); diff --git a/limereport/lrreportengine_p.h b/limereport/lrreportengine_p.h index 85aff24..4ec3508 100644 --- a/limereport/lrreportengine_p.h +++ b/limereport/lrreportengine_p.h @@ -125,6 +125,8 @@ public: bool resultIsEditable() const; void setResultEditable(bool value); + void setPassPhrase(const QString &passPhrase); + signals: void pagesLoadFinished(); void datasourceCollectionLoadFinished(const QString& collectionName); @@ -175,6 +177,7 @@ private: ReportSettings m_reportSettings; bool m_reportRendering; bool m_resultIsEditable; + QString m_passPhrase; }; } diff --git a/limereport/lrsimplecrypt.cpp b/limereport/lrsimplecrypt.cpp index 0b9aa06..d79273e 100644 --- a/limereport/lrsimplecrypt.cpp +++ b/limereport/lrsimplecrypt.cpp @@ -41,6 +41,8 @@ const int b = 16; /* number of bytes in key */ const int c = 4; /* number words in key = ceil(8*b/w)*/ const int t = 26; /* size of table S = 2*(r+1) words */ +const char* passPhrase = "HjccbzHjlbyfCkjy"; + WORD P = 0xb7e15163, Q = 0x9e3779b9; #define ROTL(x,y) (((x)<<(y&(w-1))) | ((x)>>(w-(y&(w-1))))) @@ -64,11 +66,15 @@ namespace LimeReport { class ChipperPrivate{ friend class Chipper; +public: + ChipperPrivate():m_prepared(false){} + bool isPrepared(){ return m_prepared;} private: void RC5_SETUP(const char *K); void RC5_ENCRYPT(WORD *pt, WORD *ct); void RC5_DECRYPT(WORD *ct, WORD *pt); WORD S[26]; + bool m_prepared; }; void ChipperPrivate::RC5_SETUP(const char *K) @@ -82,6 +88,7 @@ void ChipperPrivate::RC5_SETUP(const char *K) A = S[i] = ROTL(S[i]+(A+B),3); B = L[j] = ROTL(L[j]+(A+B),(A+B)); } + m_prepared = true; } void ChipperPrivate::RC5_ENCRYPT(WORD *pt, WORD *ct) @@ -111,7 +118,8 @@ QByteArray Chipper::cryptString(QString value) buff += value; WTB pt, ct, prior; - d->RC5_SETUP("HjccbzHjlbyfCkjy"); + if (!d->isPrepared()) + d->RC5_SETUP(passPhrase); prior.word[0]=0; prior.word[1]=0; @@ -144,7 +152,8 @@ QString Chipper::decryptByteArray(QByteArray value) QByteArray result; WTB pt, ct, prior; - d->RC5_SETUP("HjccbzHjlbyfCkjy"); + if (!d->isPrepared()) + d->RC5_SETUP(passPhrase); prior.word[0] = 0; prior.word[1] = 0; @@ -174,6 +183,14 @@ QString Chipper::decryptByteArray(QByteArray value) Chipper::Chipper(): d(new ChipperPrivate()){} +Chipper::Chipper(QString passphrase): d(new ChipperPrivate()) +{ + if (!passphrase.trimmed().isEmpty()){ + passphrase = passphrase.leftJustified(16,'#'); + d->RC5_SETUP(passphrase.toLatin1().data()); + } +} + Chipper::~Chipper() { delete d; diff --git a/limereport/lrsimplecrypt.h b/limereport/lrsimplecrypt.h index 68978b5..4088726 100644 --- a/limereport/lrsimplecrypt.h +++ b/limereport/lrsimplecrypt.h @@ -42,6 +42,7 @@ public: QByteArray cryptString(QString value); QString decryptByteArray(QByteArray value); Chipper(); + Chipper(QString passphrase); ~Chipper(); private: ChipperPrivate* d; diff --git a/limereport/serializators/lrserializatorintf.h b/limereport/serializators/lrserializatorintf.h index ce6a44b..b16263a 100644 --- a/limereport/serializators/lrserializatorintf.h +++ b/limereport/serializators/lrserializatorintf.h @@ -35,10 +35,16 @@ namespace LimeReport { class SerializatorIntf { public: - virtual QVariant loadValue()=0; - virtual void save(const QVariant& value,QString name)=0; + virtual QVariant loadValue() = 0; + virtual void save(const QVariant& value,QString name) = 0; virtual ~SerializatorIntf(){} }; +class CryptedSerializator : public virtual SerializatorIntf +{ +public: + virtual void setPassPhrase(const QString& passPhrase) = 0; +}; + } #endif // LRSERIALIZATORINTF_H diff --git a/limereport/serializators/lrstorageintf.h b/limereport/serializators/lrstorageintf.h index 85fb7ad..74b83ef 100644 --- a/limereport/serializators/lrstorageintf.h +++ b/limereport/serializators/lrstorageintf.h @@ -39,18 +39,19 @@ namespace LimeReport{ class ObjectLoadingStateIntf{ public: - virtual bool isLoading()=0; - virtual void objectLoadStarted()=0; - virtual void objectLoadFinished()=0; + virtual bool isLoading() = 0; + virtual void objectLoadStarted() = 0; + virtual void objectLoadFinished() = 0; }; class ItemsWriterIntf { public: - virtual void putItem(QObject* item)=0; + virtual void putItem(QObject* item) = 0; virtual bool saveToFile(QString fileName) = 0; virtual QString saveToString() = 0; virtual QByteArray saveToByteArray() = 0; + virtual void setPassPhrase(const QString& passPhrase) = 0; virtual ~ItemsWriterIntf(){} }; @@ -58,14 +59,15 @@ class ItemsReaderIntf { public: typedef QSharedPointer Ptr; - virtual bool first()=0; - virtual bool next()=0; - virtual bool prior()=0; - virtual QString itemType()=0; - virtual QString itemClassName()=0; - virtual bool readItem(QObject *item)=0; - virtual int firstLevelItemsCount()=0; - virtual QString lastError()=0; + virtual bool first() = 0; + virtual bool next() = 0; + virtual bool prior() = 0; + virtual QString itemType() = 0; + virtual QString itemClassName() = 0; + virtual bool readItem(QObject *item) = 0; + virtual int firstLevelItemsCount() = 0; + virtual QString lastError() = 0; + virtual void setPassPhrase(const QString& passPhrase) = 0; virtual ~ItemsReaderIntf(){} }; diff --git a/limereport/serializators/lrxmlbasetypesserializators.cpp b/limereport/serializators/lrxmlbasetypesserializators.cpp index ebc4260..c0c2ce7 100644 --- a/limereport/serializators/lrxmlbasetypesserializators.cpp +++ b/limereport/serializators/lrxmlbasetypesserializators.cpp @@ -109,7 +109,7 @@ void XmlQStringSerializator::save(const QVariant &value, QString name) QDomElement _node = doc()->createElement(name); _node.setAttribute("Type","QString"); if (name.compare("password")==0){ - Chipper chipper; + Chipper chipper(passPhrase()); QByteArray ba = chipper.cryptString(value.toString()); //ba.append(); _node.setAttribute("Value",QString(ba.toBase64())); @@ -123,7 +123,7 @@ QVariant XmlQStringSerializator::loadValue() { if (node()->tagName().compare("password")==0){ QByteArray ba; - Chipper chipper; + Chipper chipper(passPhrase()); ba.append(node()->attribute("Value").toLatin1()); return chipper.decryptByteArray(QByteArray::fromBase64(ba)); } else diff --git a/limereport/serializators/lrxmlbasetypesserializators.h b/limereport/serializators/lrxmlbasetypesserializators.h index 70bd40d..56f62c1 100644 --- a/limereport/serializators/lrxmlbasetypesserializators.h +++ b/limereport/serializators/lrxmlbasetypesserializators.h @@ -36,7 +36,7 @@ namespace LimeReport{ -class XmlBaseSerializator : public SerializatorIntf{ +class XmlBaseSerializator : public virtual SerializatorIntf{ public: XmlBaseSerializator(QDomDocument *doc, QDomElement *node):m_doc(doc),m_node(node){} protected: @@ -48,10 +48,21 @@ private: QDomElement *m_node; }; -class XmlQStringSerializator : public XmlBaseSerializator +class XmlCryptedSerializator : public XmlBaseSerializator, public virtual CryptedSerializator{ +public: + XmlCryptedSerializator(QDomDocument *doc, QDomElement *node) + :XmlBaseSerializator(doc, node){} + virtual void setPassPhrase(const QString& passPhrase) {m_passPhrase = passPhrase;} +protected: + QString passPhrase(){ return m_passPhrase;} +private: + QString m_passPhrase; +}; + +class XmlQStringSerializator : public XmlCryptedSerializator { public: - XmlQStringSerializator(QDomDocument *doc, QDomElement *node):XmlBaseSerializator(doc,node){} + XmlQStringSerializator(QDomDocument *doc, QDomElement *node):XmlCryptedSerializator(doc,node){} private: virtual void save(const QVariant &value,QString name); virtual QVariant loadValue(); diff --git a/limereport/serializators/lrxmlreader.cpp b/limereport/serializators/lrxmlreader.cpp index 545c4ca..bee2ff8 100644 --- a/limereport/serializators/lrxmlreader.cpp +++ b/limereport/serializators/lrxmlreader.cpp @@ -135,6 +135,11 @@ QString XMLReader::lastError() return m_error; } +void XMLReader::setPassPhrase(const QString &passPhrase) +{ + m_passPhrase = passPhrase; +} + bool XMLReader::extractFirstNode() { if (m_firstNode.isNull()){ @@ -166,6 +171,10 @@ QVariant XMLReader::getValue(QDomElement *node) if (creator) { QScopedPointerserializator(creator(m_doc.data(),node)); + CryptedSerializator* cs = dynamic_cast(serializator.data()); + if (cs){ + cs->setPassPhrase(m_passPhrase); + } return serializator->loadValue(); } return QVariant(); diff --git a/limereport/serializators/lrxmlreader.h b/limereport/serializators/lrxmlreader.h index f677c68..0bc17ee 100644 --- a/limereport/serializators/lrxmlreader.h +++ b/limereport/serializators/lrxmlreader.h @@ -44,14 +44,17 @@ public: XMLReader(); XMLReader(QSharedPointer doc); protected: +//ItemsReaderIntf interface + bool first(); + bool next(); + bool prior(); + QString itemType(); + QString itemClassName(); + bool readItem(QObject *item); + int firstLevelItemsCount(); + QString lastError(); + void setPassPhrase(const QString &passPhrase); - virtual bool first(); - virtual bool next(); - virtual bool prior(); - virtual QString itemType(); - virtual QString itemClassName(); - virtual bool readItem(QObject *item); - virtual int firstLevelItemsCount(); virtual bool prepareReader(QDomDocument *doc); void readItemFromNode(QObject *item, QDomElement *node); @@ -60,7 +63,7 @@ protected: void readCollection(QObject *item, QDomElement *node); QVariant getValue(QDomElement *node); - virtual QString lastError(); + protected: bool extractFirstNode(); QString m_error; @@ -68,6 +71,11 @@ private: QSharedPointer m_doc; QDomElement m_curNode; QDomElement m_firstNode; + QString m_passPhrase; + + + + }; class FileXMLReader : public XMLReader{ diff --git a/limereport/serializators/lrxmlwriter.cpp b/limereport/serializators/lrxmlwriter.cpp index 1a6e68a..3038373 100644 --- a/limereport/serializators/lrxmlwriter.cpp +++ b/limereport/serializators/lrxmlwriter.cpp @@ -113,6 +113,11 @@ QByteArray XMLWriter::saveToByteArray() return res; } +void XMLWriter::setPassPhrase(const QString &passPhrase) +{ + m_passPhrase = passPhrase; +} + QDomElement XMLWriter::putQObjectItem(QString name, QObject *item) { Q_UNUSED(name) @@ -158,6 +163,10 @@ void XMLWriter::saveProperty(QString name, QObject* item, QDomElement *node) if (creator) { QScopedPointer serializator(creator(m_doc.data(),node)); + CryptedSerializator* cs = dynamic_cast(serializator.data()); + if (cs){ + cs->setPassPhrase(m_passPhrase); + } serializator->save( item->property(name.toLatin1()), name diff --git a/limereport/serializators/lrxmlwriter.h b/limereport/serializators/lrxmlwriter.h index 399e2e2..0259a75 100644 --- a/limereport/serializators/lrxmlwriter.h +++ b/limereport/serializators/lrxmlwriter.h @@ -44,10 +44,13 @@ public: XMLWriter(QSharedPointer doc); ~XMLWriter() {} private: + // ItemsWriterIntf interface void putItem(QObject* item); bool saveToFile(QString fileName); QString saveToString(); QByteArray saveToByteArray(); + void setPassPhrase(const QString &passPhrase); + void init(); QDomElement putQObjectItem(QString name, QObject* item); void putChildQObjectItem(QString name, QObject* item, QDomElement* parentNode); @@ -66,6 +69,7 @@ private: QSharedPointer m_doc; QString m_fileName; QDomElement m_rootElement; + QString m_passPhrase; }; }