Commit 91edda35 authored by Martin Straka's avatar Martin Straka Committed by Karel Slaný

Allow to change user name for account

parent 7233124f
......@@ -39,6 +39,7 @@ Item {
property bool isNewAccount: (userName == "")
property string sLoginMethod: "pwd"
property string originUserName
Component.onCompleted: {
if (!isNewAccount) {
......@@ -101,7 +102,8 @@ Item {
userNameTextField.text.toString(),
passwordTextField.text.toString(),
testAccount.checked, rememberPassword.checked,
useLS.checked, certPathLabelId.text.toString())) {
useLS.checked, certPathLabelId.text.toString(),
originUserName)) {
settings.saveAllSettings(accountModel)
}
pageView.pop(StackView.Immediate)
......@@ -318,7 +320,7 @@ Item {
useLS.checked = storeToDisk
certPathLabelId.text = certPath
loginMethodComboBox.selectCurrentKey(loginMethod)
userNameTextField.enabled = false
originUserName = userName
testAccount.enabled = false
headerBar.title = qsTr("Account settings")
}
......
......@@ -32,6 +32,7 @@
#include "src/net/isds_session.h"
#include "src/settings.h"
#include "src/sqlite/account_db.h"
#include "src/sqlite/file_db_container.h"
#include "src/sqlite/message_db_container.h"
Accounts::Accounts(QObject *parent)
......@@ -190,9 +191,9 @@ bool Accounts::createAccount(const QVariant &acntModelVariant,
}
bool Accounts::updateAccount(const QVariant &acntModelVariant,
const QString &loginMetod, const QString &acntName, const QString &userName,
const QString &loginMetod, const QString &acntName, const QString &newUserName,
const QString &pwd, bool isTestAccount, bool rememberPwd, bool storeToDisk,
const QString &certPath)
const QString &certPath, const QString &originUserName)
{
debugFuncCall();
......@@ -220,8 +221,19 @@ bool Accounts::updateAccount(const QVariant &acntModelVariant,
return false;
}
if (!GlobInstcs::acntMapPtr->contains(userName)) {
return false;
QString userName = newUserName.trimmed();
if (userName != originUserName) {
bool success = changeAccountUserName(originUserName,
userName.trimmed(), acntName);
if (!success) {
return false;
}
} else {
if (!GlobInstcs::acntMapPtr->contains(userName)) {
return false;
}
}
AcntData &acntData((*GlobInstcs::acntMapPtr)[userName]);
......@@ -406,3 +418,112 @@ void Accounts::loadModelCounters(AccountListModel *accountModel)
msgDb->getMessageCountFromDb(MessageDb::TYPE_SENT));
}
}
bool Accounts::changeAccountUserName(const QString &originUserName,
const QString &newUserName, const QString &acntName)
{
int msgResponse = Dialogues::message(Dialogues::QUESTION,
tr("Change user name: %1").arg(originUserName),
tr("Do you want to change user name from '%1' to '%2' of account '%3'?").arg(originUserName).arg(newUserName).arg(acntName),
tr("Note: It will also change all related local database names and account information."),
Dialogues::NO | Dialogues::YES, Dialogues::NO);
if (msgResponse == Dialogues::NO) {
return false;
}
/* Check if new user name has correct length */
if (newUserName.length() != 6) {
logErrorNL("Username '%s' has wrong length!",
newUserName.toUtf8().constData());
return false;
}
/* Check if new user name is not exists in the database */
if (GlobInstcs::acntMapPtr->contains(newUserName)) {
logErrorNL("Username '%s' already exists!",
newUserName.toUtf8().constData());
return false;
}
/* Change user name in the account database */
if (!GlobInstcs::accountDbPtr->changeUserName(originUserName, newUserName)) {
logErrorNL("%s", "Could not change user name in the account database.");
return false;
}
/* Update user name in the account map and settings */
AcntData &acntData((*GlobInstcs::acntMapPtr)[originUserName]);
acntData.setUserName(newUserName);
{
/* Change message database name */
MessageDb *msgDb = GlobInstcs::messageDbsPtr->accessMessageDb(
GlobInstcs::setPtr->dbsLocation, originUserName,
(*GlobInstcs::acntMapPtr)[originUserName].storeToDisk());
if (msgDb == Q_NULLPTR) {
logErrorNL("Cannot access message database for %s.",
originUserName.toUtf8().constData());
goto backAccount;
}
QString currentDbFileName = msgDb->fileName();
currentDbFileName.replace(originUserName, newUserName);
if (!msgDb->moveDb(currentDbFileName)) {
goto backAccount;
}
}
{
/* Change file database name */
FileDb *fDb = GlobInstcs::fileDbsPtr->accessFileDb(
GlobInstcs::setPtr->dbsLocation, originUserName,
(*GlobInstcs::acntMapPtr)[originUserName].storeToDisk());
if (fDb == Q_NULLPTR) {
logErrorNL("Cannot access file database for %s.",
originUserName.toUtf8().constData());
goto backMessage;
}
QString currentDbFileName = fDb->fileName();
currentDbFileName.replace(originUserName, newUserName);
if (!fDb->moveDb(currentDbFileName)) {
goto backMessage;
}
}
logInfoNL("Username has been changed on '%s'.",
newUserName.toUtf8().constData());
return true;
backMessage:
{
/* Change back message database name */
MessageDb *msgDb = GlobInstcs::messageDbsPtr->accessMessageDb(
GlobInstcs::setPtr->dbsLocation, newUserName,
(*GlobInstcs::acntMapPtr)[newUserName].storeToDisk());
if (msgDb == Q_NULLPTR) {
logErrorNL("Cannot access message database for %s.",
newUserName.toUtf8().constData());
}
QString currentDbFileName = msgDb->fileName();
currentDbFileName.replace(newUserName, originUserName);
msgDb->moveDb(currentDbFileName);
}
backAccount:
/* Change back user name in the account database */
if (!GlobInstcs::accountDbPtr->changeUserName(newUserName, originUserName)) {
logErrorNL("%s", "Could not change user name in the account database.");
}
Dialogues::errorMessage(Dialogues::CRITICAL,
tr("User name problem: %1").arg(originUserName),
tr("The new user name '%1' for account '%2' has not been set!").arg(newUserName).arg(acntName),
tr("Account will use the origin settings."));
return false;
}
......@@ -116,19 +116,21 @@ public:
* @param[in,out] acntModelVariant QVariant holding account model.
* @param[in] loginMetod User name identifying account.
* @param[in] acntName Account name.
* @param[in] userName User name identifying account.
* @param[in] newUserName User name identifying account.
* @param[in] pwd Password.
* @param[in] isTestAccount True if account is isds test enveiroment.
* @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] originUserName Origin user name of account.
* @return True if success.
*/
Q_INVOKABLE
bool updateAccount(const QVariant &acntModelVariant,
const QString &loginMetod, const QString &acntName,
const QString &userName, const QString &pwd, bool isTestAccount,
bool rememberPwd, bool storeToDisk, const QString &certPath);
const QString &newUserName, const QString &pwd, bool isTestAccount,
bool rememberPwd, bool storeToDisk, const QString &certPath,
const QString &originUserName);
/*!
* @brief Remove account.
......@@ -172,4 +174,17 @@ signals:
* @param[in] acntName Account name.
*/
void removeIsdsCtx(QString userName);
private:
/*!
* @brief Change account user name.
*
* @param[in] originUserName Origin user name of account.
* @param[in] newUserName New user name of account.
* @param[in] acntName Account name.
* @return True if success.
*/
bool changeAccountUserName(const QString &originUserName,
const QString &newUserName, const QString &acntName);
};
......@@ -608,6 +608,52 @@ fail:
return Isds::DbOwnerInfo();
}
bool AccountDb::changeUserName(const QString &oldUserName,
const QString &newUserName)
{
QSqlQuery query(m_db);
QString queryStr;
if (!m_db.isOpen()) {
logErrorNL("%s", "Account database seems not to be open.");
goto fail;
}
queryStr = "UPDATE account_info "
"SET key = :newUserName WHERE key = :oldUserName";
if (!query.prepare(queryStr)) {
logErrorNL("Cannot prepare SQL query: %s",
query.lastError().text().toUtf8().constData());
goto fail;
}
query.bindValue(":oldUserName", oldUserName);
query.bindValue(":newUserName", newUserName);
if (!query.exec()) {
logErrorNL("Cannot execute SQL query: %s",
query.lastError().text().toUtf8().constData());
goto fail;
}
queryStr = "UPDATE user_info "
"SET key = :newUserName WHERE key = :oldUserName";
if (!query.prepare(queryStr)) {
logErrorNL("Cannot prepare SQL query: %s",
query.lastError().text().toUtf8().constData());
goto fail;
}
query.bindValue(":oldUserName", oldUserName);
query.bindValue(":newUserName", newUserName);
if (query.exec()) {
return true;
} else {
logErrorNL("Cannot execute SQL query: %s",
query.lastError().text().toUtf8().constData());
goto fail;
}
fail:
return false;
}
QList<class SQLiteTbl *> AccountDb::listOfTables(void) const
{
QList<class SQLiteTbl *> tables;
......
......@@ -117,6 +117,16 @@ public:
*/
const Isds::DbOwnerInfo getOwnerInfo(const QString &key) const;
/*!
* @brief Change user name in account db.
*
* @param[in] oldUserName Current account user name.
* @param[in] newUserName New account user name.
* @return True on success.
*/
bool changeUserName(const QString &oldUserName,
const QString &newUserName);
protected:
/*!
* @brief Returns list of tables.
......
......@@ -96,6 +96,11 @@ bool FileDb::reopenDb(const QString &newFileName)
return SQLiteDb::reopenDb(newFileName, SQLiteDb::CREATE_MISSING);
}
bool FileDb::moveDb(const QString &newFileName)
{
return SQLiteDb::moveDb(newFileName, SQLiteDb::CREATE_MISSING);
}
bool FileDb::deleteFilesFromDb(qint64 dmId)
{
QSqlQuery query(m_db);
......
......@@ -67,6 +67,14 @@ public:
*/
bool reopenDb(const QString &newFileName);
/*!
* @brief Move db.
*
* @param[in] newFileName New file path.
* @return True on success.
*/
bool moveDb(const QString &newFileName);
/*!
* @brief Delete all files related to message with given id.
*
......
......@@ -68,6 +68,11 @@ bool MessageDb::reopenDb(const QString &newFileName)
return SQLiteDb::reopenDb(newFileName, SQLiteDb::CREATE_MISSING);
}
bool MessageDb::moveDb(const QString &newFileName)
{
return SQLiteDb::moveDb(newFileName, SQLiteDb::CREATE_MISSING);
}
bool MessageDb::deleteMessageFromDb(qint64 msgId)
{
QSqlQuery query(m_db);
......
......@@ -70,6 +70,14 @@ public:
*/
bool reopenDb(const QString &newFileName);
/*!
* @brief Move db.
*
* @param[in] newFileName New file path.
* @return True on success.
*/
bool moveDb(const QString &newFileName);
/*!
* @brief Delete message from db.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment