Commit 798f79ce authored by Martin Straka's avatar Martin Straka Committed by Karel Slaný
Browse files

Check if new username is relevant to databox id

parent 7e776261
......@@ -96,17 +96,25 @@ Item {
isds.doIsdsAction("addNewAccount", userNameTextField.text.toString())
}
} else {
// Update account context data.
if (accounts.updateAccount(accountModel, sLoginMethod,
// Change account user name.
if (userNameTextField.text.toString() !== oldUserName) {
if (accounts.createAccount(accountModel, sLoginMethod,
accountNameTextField.text.toString(),
userNameTextField.text.toString(),
passwordTextField.text.toString(),
testAccount.checked, rememberPassword.checked,
useLS.checked, certPathLabelId.text.toString(),
oldUserName)) {
settings.saveAllSettings(accountModel)
if (userNameTextField.text.toString() !== oldUserName) {
isds.doIsdsAction("getAccountInfo", userNameTextField.text.toString())
useLS.checked, certPathLabelId.text.toString())) {
isds.doIsdsAction("changeUserName", userNameTextField.text.toString())
}
} else {
// Update account context data.
if (accounts.updateAccount(accountModel, sLoginMethod,
accountNameTextField.text.toString(),
userNameTextField.text.toString(),
passwordTextField.text.toString(),
testAccount.checked, rememberPassword.checked,
useLS.checked, certPathLabelId.text.toString())) {
settings.saveAllSettings(accountModel)
}
}
pageView.pop(StackView.Immediate)
......@@ -119,24 +127,25 @@ Item {
settings.saveAllSettings(accountModel)
pageView.pop(StackView.Immediate)
}
}
Connections {
// Connection is activated when login to new account fails.
target: isds
onUnsuccessedLoginToIsdsSig: {
if (accounts.removeAccount(accountModel, userName, false)) {
settings.saveAllSettings(accountModel)
}
}
}
Connections {
// Connection is activated when login phase was succeeded and
// try download account info.
target: isds
onRunGetAccountInfoSig: {
settings.saveAllSettings(accountModel)
isds.getAccountInfo(userName)
}
onRunChangeUserNameSig: {
var dbId = isds.getAccountDbId(userName)
if (accounts.changeAccountUserName(accountModel,
accountNameTextField.text.toString(),
userNameTextField.text.toString(),
useLS.checked, oldUserName, dbId)) {
settings.saveAllSettings(accountModel)
isds.getAccountInfo(userName)
}
}
}
}
}
......@@ -178,7 +187,7 @@ Item {
if (sLoginMethod === "cert") {
certificateLabel.visible = true
certPathButtonId.visible = true
certPathLabelId.visible = (certPathLabelId.text != "")
certPathLabelId.visible = (certPathLabelId.text !== "")
} else {
certificateLabel.visible = false
certPathLabelId.visible = false
......
......@@ -153,6 +153,10 @@ bool Accounts::createAccount(const QVariant &acntModelVariant,
{
debugFuncCall();
AcntData acntData;
QString errTxt;
QString newUserName = userName.trimmed();
AccountListModel *accountModel =
AccountListModel::fromVariant(acntModelVariant);
if (accountModel == Q_NULLPTR) {
......@@ -161,17 +165,18 @@ bool Accounts::createAccount(const QVariant &acntModelVariant,
return false;
}
if (userName.isEmpty() || acntName.isEmpty() || pwd.isEmpty()) {
Dialogues::errorMessage(Dialogues::CRITICAL,
tr("Problem while creating account"),
tr("User name, password or account name has not been specified!"),
tr("These fields must be filled in."));
return false;
if (newUserName.isEmpty() || acntName.isEmpty() || pwd.isEmpty()) {
errTxt = tr("User name, password or account name has not been specified. These fields must be filled in");
goto fail;
}
if (!checkCorrectLengthUserName(newUserName)) {
errTxt = tr("User name '%1' has wrong length.").arg(newUserName);
goto fail;
}
AcntData acntData;
acntData.setAccountName(acntName.trimmed());
acntData.setUserName(userName.trimmed());
acntData.setUserName(newUserName);
acntData.setPassword(pwd.trimmed());
acntData.setLoginMethod(loginMetod);
acntData.setTestAccount(isTestAccount);
......@@ -179,21 +184,26 @@ bool Accounts::createAccount(const QVariant &acntModelVariant,
acntData.setStoreToDisk(storeToDisk);
acntData.setCertPath(certPath);
if (accountModel->addAccount(acntData) == -2) {
Dialogues::errorMessage(Dialogues::CRITICAL,
tr("Creating account: %1").arg(userName),
tr("Account '%1' could not be created.").arg(acntName),
tr("Account with user name '%1' already exists.").arg(userName));
return false;
if (accountModel->addAccount(acntData) == AccountListModel::AA_EXISTS) {
errTxt = tr("Account with user name '%1' already exists.").arg(newUserName);
goto fail;
}
return true;
fail:
Dialogues::errorMessage(Dialogues::CRITICAL,
tr("Creating account: %1").arg(newUserName),
tr("Account '%1' could not be created.").arg(acntName),
errTxt);
return false;
}
bool Accounts::updateAccount(const QVariant &acntModelVariant,
const QString &loginMetod, const QString &acntName, const QString &newUserName,
const QString &pwd, bool isTestAccount, bool rememberPwd, bool storeToDisk,
const QString &certPath, const QString &oldUserName)
const QString &certPath)
{
debugFuncCall();
......@@ -223,13 +233,6 @@ bool Accounts::updateAccount(const QVariant &acntModelVariant,
QString userName = newUserName.trimmed();
/* User name is changed */
if (userName != oldUserName) {
return changeAccountUserName(accountModel, loginMetod,
acntName.trimmed(), userName, pwd.trimmed(), isTestAccount,
rememberPwd, storeToDisk, certPath, oldUserName);
}
if (!GlobInstcs::acntMapPtr->contains(userName)) {
return false;
}
......@@ -417,11 +420,9 @@ void Accounts::loadModelCounters(AccountListModel *accountModel)
}
}
bool Accounts::changeAccountUserName(AccountListModel *accountModel,
const QString &loginMetod, const QString &acntName,
const QString &newUserName, const QString &password,
bool isTestAccount, bool rememberPwd, bool storeToDisk,
const QString &certPath, const QString &oldUserName)
bool Accounts::changeAccountUserName(const QVariant &acntModelVariant,
const QString &acntName, const QString &newUserName, bool storeToDisk,
const QString &oldUserName, const QString &newDbId)
{
QString errTxt;
......@@ -435,93 +436,43 @@ bool Accounts::changeAccountUserName(AccountListModel *accountModel,
return false;
}
/* Check if new user name has correct length */
if (newUserName.length() != 6) {
errTxt = tr("User name '%1' has wrong length.").arg(newUserName);
logErrorNL("User name '%s' has wrong length.",
newUserName.toUtf8().constData());
goto fail;
/* Get account model */
AccountListModel *accountModel =
AccountListModel::fromVariant(acntModelVariant);
if (accountModel == Q_NULLPTR) {
Q_ASSERT(0);
logErrorNL("%s", "Cannot access account model.");
return false;
}
/* Check if new user name does not exist in the database */
if (GlobInstcs::acntMapPtr->contains(newUserName)) {
errTxt = tr("User name '%1' already exists.").arg(newUserName);
logErrorNL("User name '%s' already exists.",
newUserName.toUtf8().constData());
/* Check if new user name corresponds with databox id */
if (!isRelevantUserName(oldUserName, newDbId)) {
errTxt = tr("New user name '%1' not corresponds with databox ID.").arg(newUserName);
goto fail;
}
/* First: Change user name in the account database (user_info, account_info) */
if (!GlobInstcs::accountDbPtr->changeUserName(oldUserName, newUserName)) {
errTxt = tr("Cannot change user name '%1' in the account database.").arg(newUserName);
logErrorNL("%s", "Cannot change user name in the account database.");
goto fail;
}
/* Second: Change file database name or rollback in an error */
{
FileDb *fDb = GlobInstcs::fileDbsPtr->accessFileDb(
GlobInstcs::setPtr->dbsLocation, oldUserName, storeToDisk);
if (fDb == Q_NULLPTR) {
errTxt = tr("Cannot access file database for the user name '%1'.").arg(oldUserName);
logErrorNL("Cannot access file database for %s.",
oldUserName.toUtf8().constData());
goto backAccountChanges;
}
QString currentDbFileName = fDb->fileName();
currentDbFileName.replace(oldUserName, newUserName);
if (!fDb->moveDb(currentDbFileName)) {
errTxt = tr("Cannot change file database on the user name '%1'.").arg(newUserName);
logErrorNL("Cannot change file database on the user name %s.",
newUserName.toUtf8().constData());
goto backAccountChanges;
}
if (!changeFileDbName(oldUserName, newUserName, storeToDisk, errTxt)) {
goto rollbackAccountChanges;
}
/* Third: Change message database name or rollback in an error */
{
MessageDb *msgDb = GlobInstcs::messageDbsPtr->accessMessageDb(
GlobInstcs::setPtr->dbsLocation, oldUserName, storeToDisk);
if (msgDb == Q_NULLPTR) {
errTxt = tr("Cannot access message database for user name '%1'.").arg(oldUserName);
logErrorNL("Cannot access message database for %s.",
oldUserName.toUtf8().constData());
goto backFilesChanges;
}
QString currentDbFileName = msgDb->fileName();
currentDbFileName.replace(oldUserName, newUserName);
if (!msgDb->moveDb(currentDbFileName)) {
errTxt = tr("Cannot change message database on the user name '%1'.").arg(newUserName);
logErrorNL("Cannot change message database on the user name %s.",
newUserName.toUtf8().constData());
goto backFilesChanges;
}
if (!changeMessageDbName(oldUserName, newUserName, storeToDisk, errTxt)) {
goto rollbackFilesChanges;
}
/* Final: Create new account with new user name and insert to map */
{
AcntData acntData;
acntData.setAccountName(acntName);
acntData.setUserName(newUserName);
acntData.setPassword(password);
acntData.setLoginMethod(loginMetod);
acntData.setTestAccount(isTestAccount);
acntData.setRememberPwd(rememberPwd);
acntData.setStoreToDisk(storeToDisk);
acntData.setCertPath(certPath);
/* Add account data to model */
if (accountModel->addAccount(acntData) == AccountListModel::AA_SUCCESS) {
/* Delete isds session context of old account */
emit removeIsdsCtx(oldUserName);
/* Remove old account */
accountModel->deleteAccount(oldUserName);
/* Load message counters of new account */
loadModelCounters(accountModel);
}
}
/* Delete isds session context of old account */
//emit removeIsdsCtx(oldUserName);
/* Remove old account */
accountModel->deleteAccount(oldUserName);
/* Load message counters of new account */
loadModelCounters(accountModel);
logInfoNL("Username has been changed on '%s'.",
newUserName.toUtf8().constData());
......@@ -533,26 +484,15 @@ bool Accounts::changeAccountUserName(AccountListModel *accountModel,
return true;
backFilesChanges:
{
/* Change back file database name */
FileDb *fDb = GlobInstcs::fileDbsPtr->accessFileDb(
GlobInstcs::setPtr->dbsLocation, newUserName, storeToDisk);
if (fDb == Q_NULLPTR) {
logErrorNL("Cannot access file database for %s.",
newUserName.toUtf8().constData());
}
rollbackFilesChanges:
QString currentDbFileName = fDb->fileName();
currentDbFileName.replace(newUserName, oldUserName);
if (!fDb->moveDb(currentDbFileName)) {
logErrorNL("Cannot change back file database name %s.",
oldUserName.toUtf8().constData());
}
/* Change back file database name */
if (changeFileDbName(newUserName, oldUserName, storeToDisk, errTxt)) {
logErrorNL("Cannot change back file database name %s.",
oldUserName.toUtf8().constData());
}
backAccountChanges:
rollbackAccountChanges:
/* Change back user name in the account database */
if (!GlobInstcs::accountDbPtr->changeUserName(newUserName, oldUserName)) {
......@@ -568,3 +508,56 @@ fail:
return false;
}
bool Accounts::changeFileDbName(const QString &oldUserName,
const QString &newUserName, bool storeToDisk, QString &errTxt)
{
FileDb *fDb = GlobInstcs::fileDbsPtr->accessFileDb(
GlobInstcs::setPtr->dbsLocation, oldUserName, storeToDisk);
if (fDb == Q_NULLPTR) {
errTxt = tr("Cannot access file database for the user name '%1'.").arg(oldUserName);
return false;
}
QString currentDbFileName = fDb->fileName();
currentDbFileName.replace(oldUserName, newUserName);
if (!fDb->moveDb(currentDbFileName)) {
errTxt = tr("Cannot change file database on the user name '%1'.").arg(newUserName);
return false;
}
return true;
}
bool Accounts::changeMessageDbName(const QString &oldUserName,
const QString &newUserName, bool storeToDisk, QString &errTxt)
{
MessageDb *msgDb = GlobInstcs::messageDbsPtr->accessMessageDb(
GlobInstcs::setPtr->dbsLocation, oldUserName, storeToDisk);
if (msgDb == Q_NULLPTR) {
errTxt = tr("Cannot access message database for user name '%1'.").arg(oldUserName);
return false;
}
QString currentDbFileName = msgDb->fileName();
currentDbFileName.replace(oldUserName, newUserName);
if (!msgDb->moveDb(currentDbFileName)) {
errTxt = tr("Cannot change message database on the user name '%1'.").arg(newUserName);
return false;
}
return true;
}
bool Accounts::checkCorrectLengthUserName(const QString &userName)
{
/* Check if new user name has correct length */
return (userName.length() == 6);
}
bool Accounts::isRelevantUserName(const QString &userName, const QString &dbId)
{
/* Check if new user corresponds with databox id */
return (dbId == GlobInstcs::accountDbPtr->dbId(userName));
}
......@@ -104,7 +104,7 @@ public:
* @param[in] certPath Certificate path (can be null).
* @return True if success.
*/
Q_INVOKABLE static
Q_INVOKABLE
bool createAccount(const QVariant &acntModelVariant,
const QString &loginMetod, const QString &acntName,
const QString &userName, const QString &pwd, bool isTestAccount,
......@@ -122,15 +122,13 @@ public:
* @param[in] rememberPwd True if remember password.
* @param[in] storeToDisk True if database store to local storage.
* @param[in] certPath Certificate path (can be null).
* @param[in] oldUserName Original user name of account.
* @return True if success.
*/
Q_INVOKABLE
bool updateAccount(const QVariant &acntModelVariant,
const QString &loginMetod, const QString &acntName,
const QString &newUserName, const QString &pwd, bool isTestAccount,
bool rememberPwd, bool storeToDisk, const QString &certPath,
const QString &oldUserName);
bool rememberPwd, bool storeToDisk, const QString &certPath);
/*!
* @brief Remove account.
......@@ -143,6 +141,22 @@ public:
bool removeAccount(const QVariant &acntModelVariant,
const QString &userName, bool showDialogue);
/*!
* @brief Change account user name and update account settings.
*
* @param[in,out] acntModelVariant QVariant holding account model.
* @param[in] acntName Account name.
* @param[in] newUserName User name identifying account.
* @param[in] storeToDisk True if database store to local storage.
* @param[in] oldUserName Original user name of account.
* @param[in] newDbId Databox id of new user name.
* @return True if success.
*/
Q_INVOKABLE
bool changeAccountUserName(const QVariant &acntModelVariant,
const QString &acntName, const QString &newUserName,
bool storeToDisk, const QString &oldUserName, const QString &newDbId);
/*!
* @brief Loads model counters from database.
*
......@@ -178,23 +192,44 @@ signals:
private:
/*!
* @brief Change account user name and update account settings.
* @brief Change file database name to new user name.
*
* @param[in,out] accountModel Holding account model.
* @param[in] loginMetod User name identifying account.
* @param[in] acntName Account name.
* @param[in] newUserName User name identifying account.
* @param[in] password Password.
* @param[in] isTestAccount True if account is isds test enveiroment.
* @param[in] rememberPwd True if remember password.
* @param[in] oldUserName Original user name of account.
* @param[in] newUserName New user name identifying account.
* @param[in] storeToDisk True if database store to local storage.
* @param[in] certPath Certificate path (can be null).
* @param[out] errTxt Error description.
* @return True if success.
*/
bool changeFileDbName(const QString &oldUserName,
const QString &newUserName, bool storeToDisk, QString &errTxt);
/*!
* @brief Change message database name to new user name.
*
* @param[in] oldUserName Original user name of account.
* @param[in] newUserName New user name identifying account.
* @param[in] storeToDisk True if database store to local storage.
* @param[out] errTxt Error description.
* @return True if success.
*/
bool changeAccountUserName(AccountListModel *accountModel,
const QString &loginMetod, const QString &acntName,
const QString &newUserName, const QString &password,
bool isTestAccount, bool rememberPwd, bool storeToDisk,
const QString &certPath, const QString &oldUserName);
bool changeMessageDbName(const QString &oldUserName,
const QString &newUserName, bool storeToDisk, QString &errTxt);
/*!
* @brief Check correct length of user name.
*
* @param[in] userName New user name identifying account.
* @return True if length is correct.
*/
bool checkCorrectLengthUserName(const QString &userName);
/*!
* @brief Check if new user name is relevant to databox id.
*
* @param[in] userName Original user name of account.
* @param[in] dbId Databox ID obtained from isds.
* @return True if user name is relevant to databox id.
*/
bool isRelevantUserName(const QString &userName,
const QString &dbId);
};
......@@ -261,6 +261,8 @@ void IsdsWrapper::doIsdsAction(const QString &isdsAction,
emit runDownloadMessageSig(userName);
} else if (isdsAction == "addNewAccount") {
emit runGetAccountInfoSig(userName);
} else if (isdsAction == "changeUserName") {
emit runChangeUserNameSig(userName);
} else if (isdsAction == "getAccountInfo") {
emit runGetAccountInfoSig(userName);
} else if (isdsAction == "syncOneAccount") {
......@@ -321,12 +323,46 @@ void IsdsWrapper::getAccountInfo(const QString &userName)
arg(userName), true, true);
TaskDownloadAccountInfo *task;
/* Last param: true = store account info into database */
task = new (std::nothrow) TaskDownloadAccountInfo(
m_isdsSession.isdsCtxMap[userName], &m_netLayer);
m_isdsSession.isdsCtxMap[userName], &m_netLayer, true);
task->setAutoDelete(true);
GlobInstcs::workPoolPtr->assignHi(task);
}
QString IsdsWrapper::getAccountDbId(const QString &userName)
{
debugFuncCall();
if (Q_UNLIKELY(GlobInstcs::workPoolPtr == Q_NULLPTR)) {
Q_ASSERT(0);
return QString();
}
QString errTxt = tr("Wrong username");
if (userName.isEmpty()) {
Dialogues::errorMessage(Dialogues::CRITICAL,
tr("Error"), errTxt, tr("Internal error"));
return QString();
}
/* User must be logged to isds */
if (!isLoggedToIsds(userName)) {
return QString();
}
TaskDownloadAccountInfo *task;
/* Last param: false = account info won't store into database */
task = new (std::nothrow) TaskDownloadAccountInfo(
m_isdsSession.isdsCtxMap[userName], &m_netLayer, false);
task->setAutoDelete(false);
GlobInstcs::workPoolPtr->runSingle(task);
QString dbId = task->m_dbID;
delete task; task = Q_NULLPTR;
return dbId;
}
int IsdsWrapper::findDataboxFulltext(const QString &userName,
const QVariant &dbModelVariant, const QString &phrase,
const QString &searchType, const QString &searchScope, int page)
......@@ -516,7 +552,7 @@ bool IsdsWrapper::isCorrectPassword(const QString &password)
void IsdsWrapper::inputDialogCancelPostAction(const QString &isdsAction,
const QString &userName)
{
if (isdsAction == "addNewAccount") {
if (isdsAction == "addNewAccount" || isdsAction == "changeUserName") {
emit unsuccessedLoginToIsdsSig(userName);
}
}
......@@ -1124,6 +1160,14 @@ void IsdsWrapper::doLoginAction(const QString &isdsAction,
tr("ISDS returns:") + " " + m_isdsSession.isdsCtxMap[userName].last_isds_msg + "\n\n" +
tr("Check your login data and try again."));
emit unsuccessedLoginToIsdsSig(userName);
} else if (isdsAction == "changeUserName") {
Dialogues::errorMessage(Dialogues::CRITICAL,
tr("User name change: %1").arg(userName),
tr("User name could not be changed because an error occurred "
"while trying to log in with new user name '%1'.").arg(userName),
tr("ISDS returns:") + " " + m_isdsSession.isdsCtxMap[userName].last_isds_msg + "\n\n" +
tr("Check your login data and try again."));
emit unsuccessedLoginToIsdsSig(userName);
} else {
/* Show error login dialogue */
Dialogues::errorMessage(Dialogues::CRITICAL,
......
......@@ -8,7 +8,7 @@
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
......@@ -130,6 +130,15 @@ public:
Q_INVOKABLE
void getAccountInfo(const QString &userName);
/*!
* @brief Get databox ID from ISDS.
*
* @param[in] userName Account username string.
* @return Return databox ID.
*/
Q_INVOKABLE
QString getAccountDbId(const QString &userName);
/*!
* @brief Downloads delivery info.
*
......@@ -347,6 +356,13 @@ signals:
*/
void downloadAccountInfoFinishedSig(QString userName);
/*!
* @brief Signal is emitted when account username can be changed.
*
* @param[in] userName Account user name string.
*/
void runChangeUserNameSig(QString userName);
/*!
* @brief Run download mesasge isds action from QML.
*
......
......@@ -33,10 +33,12 @@
#include "src/xml/xml_get_db_user_info.h"
TaskDownloadAccountInfo::TaskDownloadAccountInfo(IsdsSession::IsdsContext &ctx,
NetLayer *netLayer)
NetLayer *netLayer, bool storeToDb)
: m_result(DL_ERR),
m_dbID(QString()),
m_ctx(ctx),
m_netLayer(netLayer)
m_netLayer(netLayer),
m_storeToDb(storeToDb)
{
}
......@@ -52,13 +54,15 @@ void TaskDownloadAccountInfo::run(void)