Skip to content

Commit

Permalink
Merge pull request #348 from ucam-department-of-psychiatry/report-fai…
Browse files Browse the repository at this point in the history
…led-database-deletion

Better handling of database deletion when user forgets password
  • Loading branch information
martinburchell committed Apr 27, 2024
2 parents d4ebe3a + e1046d0 commit 254b421
Show file tree
Hide file tree
Showing 20 changed files with 137 additions and 51 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-and-install-packages.yml
Expand Up @@ -21,8 +21,8 @@ jobs:
os: [ubuntu-20.04, ubuntu-22.04]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Change apt mirror
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build-qt.yml
Expand Up @@ -24,15 +24,15 @@ jobs:
os: windows-2019
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Free up disk space
# The runner on Ubuntu will run out of space so we remove some of the things
# we don't need.
if: runner.os == 'Linux'
run: |
set -eux -o pipefail
${GITHUB_WORKSPACE}/.github/scripts/free_up_disk_space.sh
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: 3.8
- name: Common Linux prerequisites
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/cpp-tests.yml
Expand Up @@ -20,7 +20,7 @@ jobs:
cpp-tests:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Qt version check
run: |
set -euxo pipefail
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/database-upgrade-downgrade.yml
Expand Up @@ -14,8 +14,8 @@ jobs:
database-upgrade-downgrade:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.8
- name: Set up MySQL
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docker.yml
Expand Up @@ -15,7 +15,7 @@ jobs:
docker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Build
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docs.yml
Expand Up @@ -22,8 +22,8 @@ jobs:
python-version: "3.10"
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Change apt mirror
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/installer.yml
Expand Up @@ -19,7 +19,7 @@ jobs:
installer:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Run installer
run: |
set -eux -o pipefail
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/launch-cherrypy.yml
Expand Up @@ -19,8 +19,8 @@ jobs:
os: [ubuntu-22.04]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Set up MySQL
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/missing-migration-check.yml
Expand Up @@ -14,8 +14,8 @@ jobs:
migration-check:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.8
- name: Missing migration check
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/precommit.yml
Expand Up @@ -12,8 +12,8 @@ jobs:
precommit-checks:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.8
- name: Pre-commit checks
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/python-package-checks.yml
Expand Up @@ -17,8 +17,8 @@ jobs:
os: [ubuntu-20.04, ubuntu-22.04]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Python package checks
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/python-tests.yml
Expand Up @@ -19,8 +19,8 @@ jobs:
os: [ubuntu-20.04, ubuntu-22.04]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Change apt mirror
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Expand Up @@ -14,7 +14,7 @@ jobs:
contents: write
steps:
- name: Check out repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Work out tag
id: vars
run: echo ::set-output name=tag::${GITHUB_REF#refs/*/}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/report-missing-client-translations.yml
Expand Up @@ -20,7 +20,7 @@ jobs:
report-missing-client-translations:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Qt version check
run: |
set -euxo pipefail
Expand All @@ -34,7 +34,7 @@ jobs:
target: desktop
arch: gcc_64
archives: "qtbase qtdeclarative qttools qttranslations icu"
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Report missing client translations
Expand Down
5 changes: 5 additions & 0 deletions docs/source/changelog.rst
Expand Up @@ -3895,3 +3895,8 @@ Current C++/SQLite client, Python/SQLAlchemy server
the previous registration, if available. This should reduce the amount of data
entry needed following a network or registration failure.
https://github.com/ucam-department-of-psychiatry/camcops/issues/104

- Provide more information if the app cannot delete the SQLite databases when a user
has forgotten their password. Fix a bug where if the initial password dialog was
aborted, the next attempt to set up a password would fail.
https://github.com/ucam-department-of-psychiatry/camcops/issues/346
4 changes: 2 additions & 2 deletions server/setup.py
Expand Up @@ -86,8 +86,8 @@
"Faker==4.1.1", # create fake data; for test and dummy database creation
# FHIR export, our fork until https://github.com/smart-on-fhir/client-py/pull/105 is merged # noqa
"fhirclient @ git+https://github.com/ucam-department-of-psychiatry/client-py@128bbe3c2194a51ba6ff8cf880ef2fdb9bfcc2d6#egg=fhirclient-4.0.0.1", # noqa: E501
"flower==1.2.0", # monitor for Celery
"gunicorn==20.1.0", # web server (Unix only)
"flower==2.0.1", # monitor for Celery
"gunicorn==21.2.0", # web server (Unix only)
"hl7==0.3.5", # For HL7 export
# Celery dependency for Python <= 3.7; workaround import error
# https://github.com/celery/celery/issues/7783; scheduled to be fixed in
Expand Down
68 changes: 60 additions & 8 deletions tablet_qt/core/camcopsapp.cpp
Expand Up @@ -1279,7 +1279,15 @@ bool CamcopsApp::connectDatabaseEncryption(QString& new_user_password,
new_pw_text, new_pw_title,
false /* require_old_password */,
dummy_old_password, new_user_password, nullptr)) {

// The user quit without setting a password.
// If we don't delete the database here, the next attempt to
// set up a password will fail (canReadDatabase() calls below
// will return false) and the user will be forced to set up
// another one.
deleteDatabases();
user_cancelled_please_quit = true;

return false;
}
qInfo() << "Encrypting databases for the first time...";
Expand Down Expand Up @@ -1336,7 +1344,12 @@ bool CamcopsApp::connectDatabaseEncryption(QString& new_user_password,
if (!userConfirmedRetryPassword()) {
if (userConfirmedDeleteDatabases()) {
qInfo() << "... deleting databases.";
deleteDatabases();
const bool ok = deleteDatabases();
if (!ok)
{
user_cancelled_please_quit = true;
return false;
}
qInfo() << "... recreating databases.";
openOrCreateDatabases();
}
Expand Down Expand Up @@ -1390,16 +1403,55 @@ bool CamcopsApp::userConfirmedDeleteDatabases() const
}


void CamcopsApp::deleteDatabases()
bool CamcopsApp::deleteDatabases()
{
const QString data_filename = dbFullPath(dbfunc::DATA_DATABASE_FILENAME);
const QString sys_filename = dbFullPath(dbfunc::SYSTEM_DATABASE_FILENAME);
QString data_error_string;
QString sys_error_string;

const bool data_ok = deleteDatabase(dbfunc::DATA_DATABASE_FILENAME,
data_error_string);
const bool sys_ok = deleteDatabase(dbfunc::SYSTEM_DATABASE_FILENAME,
sys_error_string);

if (data_ok && sys_ok) {
return true;
}

QString error_string;

if (!data_ok) {
error_string = data_error_string;
}

QFile data_file(data_filename);
data_file.remove();
if (!sys_ok) {
error_string += "\n" + sys_error_string;
}
uifunc::alert(
tr("CamCOPS could not delete its databases:\n\n"
"%1\n"
"Please try to delete these files manually and restart CamCOPS\n"
).arg(error_string));

return false;
}


bool CamcopsApp::deleteDatabase(const QString& filename, QString& error_string)
{
const QString fullpath = dbFullPath(filename);
QFile file(fullpath);
const bool ok = file.remove();

if (!ok) {
error_string = tr(
"Failed to delete file:\n"
"%1\n"
"because of this error:\n"
"%2\n"
).arg(fullpath, file.errorString());
}

QFile sys_file(sys_filename);
sys_file.remove();
return ok;
}


Expand Down
7 changes: 5 additions & 2 deletions tablet_qt/core/camcopsapp.h
Expand Up @@ -169,8 +169,11 @@ class CamcopsApp : public QApplication
// Open our pair of databases, or create them if they don't exist.
void openOrCreateDatabases();

// Delete databases
void deleteDatabases();
// Delete databases, returning true if successful
bool deleteDatabases();

// Delete the named database, returning true if successful
bool deleteDatabase(const QString& filename, QString& error_string);

// Close our databases.
void closeDatabases();
Expand Down
Binary file modified tablet_qt/translations/camcops_da_DK.qm
Binary file not shown.

0 comments on commit 254b421

Please sign in to comment.