QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
qgsauthconfigurationstoragesqlite.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsauthconfigurationstoragesqlite.cpp - QgsAuthConfigurationStorageSqlite
3
4 ---------------------
5 begin : 20.6.2024
6 copyright : (C) 2024 by Alessandro Pasotti
7 email : elpaso at itopen dot it
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
17
18#include "qgsauthcertutils.h"
19#include "qgslogger.h"
20
21#include <QCoreApplication>
22#include <QDir>
23#include <QFileInfo>
24#include <QSqlError>
25#include <QSqlQuery>
26#include <QString>
27#include <QThread>
28
29#include "moc_qgsauthconfigurationstoragesqlite.cpp"
30
31using namespace Qt::StringLiterals;
32
34
35QgsAuthConfigurationStorageSqlite::QgsAuthConfigurationStorageSqlite( const QString &databasePath )
36 : QgsAuthConfigurationStorageDb( {{ u"driver"_s, u"QSQLITE"_s }, { u"database"_s, databasePath }} )
37{
38}
39
40bool QgsAuthConfigurationStorageSqlite::initialize()
41{
42 QMutexLocker locker( &mMutex );
43
44 if ( !QFileInfo::exists( mDatabase ) )
45 {
46 // Check if the parent path exists
47 QFileInfo parentInfo( QFileInfo( mDatabase ).path() );
48 if ( ! parentInfo.exists() )
49 {
50 // Try to create the directory
51 QDir dir;
52 if ( !dir.mkpath( parentInfo.absolutePath() ) )
53 {
54 setError( tr( "Auth db directory path '%1' could not be created" ).arg( mDatabase ) );
55 return false;
56 }
57 }
58
59 // Try to create the database
60 QSqlDatabase db = authDatabaseConnection();
61 if ( !db.open() )
62 {
63 setError( tr( "Auth db file '%1' could not be created" ).arg( mDatabase ) );
64 return false;
65 }
66 }
67
68 // Check if the file is readable
69 const QFileInfo fileInfo( mDatabase );
70 if ( !fileInfo.permission( QFile::ReadOwner ) )
71 {
72 setError( tr( "Auth db file '%1' is not readable" ).arg( mDatabase ) );
73 return false;
74 }
75
76 // Check if the file is writable
77 if ( !fileInfo.permission( QFile::WriteOwner ) )
78 {
79 setError( tr( "Auth db file '%1' is not writable" ).arg( mDatabase ), Qgis::MessageLevel::Warning );
80 }
81
82 const bool ok { createConfigTables() &&createCertTables() };
83 if ( !ok )
84 {
85 setError( tr( "Auth db initialization FAILED" ), Qgis::MessageLevel::Critical );
86 mIsReady = false;
87 return false;
88 }
89
90 mIsReady = true;
91
92 checkCapabilities();
93
94 // Recompute capabilities if needed
95 connect( this, &QgsAuthConfigurationStorageDb::readOnlyChanged, this, [this]( bool )
96 {
97 checkCapabilities();
98 } );
99
100 return true;
101}
102
103QList<QgsAuthConfigurationStorage::SettingParameter> QgsAuthConfigurationStorageSqlite::settingsParameters() const
104{
105 return {{ u"database"_s, tr( "Path to the SQLite database file" ), QVariant::String }};
106}
107
108QString QgsAuthConfigurationStorageSqlite::description() const
109{
110 return tr( "Store credentials in a local SQLite database" );
111}
112
113QString QgsAuthConfigurationStorageSqlite::type() const
114{
115 return u"SQLITE"_s;
116}
117
118bool QgsAuthConfigurationStorageSqlite::tableExists( const QString &table ) const
119{
120 QMutexLocker locker( &mMutex );
121
122 if ( !authDbOpen() )
123 {
124 const_cast< QgsAuthConfigurationStorageSqlite * >( this )->setError( tr( "Auth db could not be opened" ) );
125 return false;
126 }
127
128 QSqlQuery query( authDatabaseConnection() );
129 query.prepare( u"SELECT name FROM sqlite_master WHERE type='table' AND name=:name"_s );
130 query.bindValue( u":name"_s, table );
131
132 if ( !authDbQuery( &query ) )
133 {
134 const_cast< QgsAuthConfigurationStorageSqlite * >( this )->setError( tr( "Failed to check if table '%1' exists" ).arg( table ) );
135 return false;
136 }
137
138 if ( ! query.next() )
139 {
140 return false;
141 }
142
143 return true;
144}
145
146void QgsAuthConfigurationStorageSqlite::checkCapabilities()
147{
148
149 QMutexLocker locker( &mMutex );
150 QFileInfo fileInfo( mDatabase );
151 if ( ! fileInfo.exists() )
152 {
154 return;
155 }
156
157 const bool readOnly { isReadOnly() };
158
159 mIsReadOnly = mIsReadOnly && fileInfo.isWritable();
161
162 if ( ! fileInfo.isReadable() )
163 {
170 }
171
172 // We need to emit the signal without repeating the check
173 if ( mIsReadOnly != readOnly )
174 {
175 mIsReadOnly = readOnly;
176 whileBlocking( this )->setReadOnly( !readOnly );
177 }
178
179}
180
QFlags< AuthConfigurationStorageCapability > AuthConfigurationStorageCapabilities
Authentication configuration storage capabilities.
Definition qgis.h:150
@ Warning
Warning message.
Definition qgis.h:161
@ Critical
Critical/error message.
Definition qgis.h:162
@ ReadSslCertificateCustomConfig
Can read a SSL certificate custom config.
Definition qgis.h:118
@ ReadCertificateTrustPolicy
Can read a certificate trust policy.
Definition qgis.h:128
@ ReadConfiguration
Can read an authentication configuration.
Definition qgis.h:108
@ ReadCertificateAuthority
Can read a certificate authority.
Definition qgis.h:123
@ ReadCertificateIdentity
Can read a certificate identity.
Definition qgis.h:113
@ ReadMasterPassword
Can read the master password.
Definition qgis.h:133
QSqlDatabase based implementation of QgsAuthConfigurationStorage.
virtual void checkCapabilities()
Checks the capabilities of the storage.
void readOnlyChanged(bool readOnly)
Emitted when the storage read-only status was changed.
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition qgis.h:6839