QGIS API Documentation 3.41.0-Master (af5edcb665c)
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#include "moc_qgsauthconfigurationstoragesqlite.cpp"
18#include "qgslogger.h"
19#include "qgsauthcertutils.h"
20
21#include <QFileInfo>
22#include <QDir>
23#include <QSqlError>
24#include <QSqlQuery>
25#include <QThread>
26#include <QCoreApplication>
27
29
30QgsAuthConfigurationStorageSqlite::QgsAuthConfigurationStorageSqlite( const QString &databasePath )
31 : QgsAuthConfigurationStorageDb( {{ QStringLiteral( "driver" ), QStringLiteral( "QSQLITE" ) }, { QStringLiteral( "database" ), databasePath }} )
32{
33}
34
35bool QgsAuthConfigurationStorageSqlite::initialize()
36{
37 QMutexLocker locker( &mMutex );
38
39 if ( !QFileInfo::exists( mDatabase ) )
40 {
41 // Check if the parent path exists
42 QFileInfo parentInfo( QFileInfo( mDatabase ).path() );
43 if ( ! parentInfo.exists() )
44 {
45 // Try to create the directory
46 QDir dir;
47 if ( !dir.mkpath( parentInfo.absolutePath() ) )
48 {
49 setError( tr( "Auth db directory path '%1' could not be created" ).arg( mDatabase ) );
50 return false;
51 }
52 }
53
54 // Try to create the database
55 QSqlDatabase db = authDatabaseConnection();
56 if ( !db.open() )
57 {
58 setError( tr( "Auth db file '%1' could not be created" ).arg( mDatabase ) );
59 return false;
60 }
61 }
62
63 // Check if the file is readable
64 const QFileInfo fileInfo( mDatabase );
65 if ( !fileInfo.permission( QFile::ReadOwner ) )
66 {
67 setError( tr( "Auth db file '%1' is not readable" ).arg( mDatabase ) );
68 return false;
69 }
70
71 // Check if the file is writable
72 if ( !fileInfo.permission( QFile::WriteOwner ) )
73 {
74 setError( tr( "Auth db file '%1' is not writable" ).arg( mDatabase ), Qgis::MessageLevel::Warning );
75 }
76
77 const bool ok { createConfigTables() &&createCertTables() };
78 if ( !ok )
79 {
80 setError( tr( "Auth db initialization FAILED" ), Qgis::MessageLevel::Critical );
81 mIsReady = false;
82 return false;
83 }
84
85 mIsReady = true;
86
87 checkCapabilities();
88
89 // Recompute capabilities if needed
90 connect( this, &QgsAuthConfigurationStorageDb::readOnlyChanged, this, [this]( bool )
91 {
92 checkCapabilities();
93 } );
94
95 return true;
96}
97
98QList<QgsAuthConfigurationStorage::SettingParameter> QgsAuthConfigurationStorageSqlite::settingsParameters() const
99{
100 return {{ QStringLiteral( "database" ), tr( "Path to the SQLite database file" ), QVariant::String }};
101}
102
103QString QgsAuthConfigurationStorageSqlite::description() const
104{
105 return tr( "Store credentials in a local SQLite database" );
106}
107
108QString QgsAuthConfigurationStorageSqlite::type() const
109{
110 return QStringLiteral( "SQLITE" );
111}
112
113bool QgsAuthConfigurationStorageSqlite::tableExists( const QString &table ) const
114{
115 QMutexLocker locker( &mMutex );
116
117 if ( !authDbOpen() )
118 {
119 const_cast< QgsAuthConfigurationStorageSqlite * >( this )->setError( tr( "Auth db could not be opened" ) );
120 return false;
121 }
122
123 QSqlQuery query( authDatabaseConnection() );
124 query.prepare( QStringLiteral( "SELECT name FROM sqlite_master WHERE type='table' AND name=:name" ) );
125 query.bindValue( QStringLiteral( ":name" ), table );
126
127 if ( !authDbQuery( &query ) )
128 {
129 const_cast< QgsAuthConfigurationStorageSqlite * >( this )->setError( tr( "Failed to check if table '%1' exists" ).arg( table ) );
130 return false;
131 }
132
133 if ( ! query.next() )
134 {
135 return false;
136 }
137
138 return true;
139}
140
141void QgsAuthConfigurationStorageSqlite::checkCapabilities()
142{
143
144 QMutexLocker locker( &mMutex );
145 QFileInfo fileInfo( mDatabase );
146 if ( ! fileInfo.exists() )
147 {
149 return;
150 }
151
152 const bool readOnly { isReadOnly() };
153
154 mIsReadOnly = mIsReadOnly && fileInfo.isWritable();
156
157 if ( ! fileInfo.isReadable() )
158 {
165 }
166
167 // We need to emit the signal without repeating the check
168 if ( mIsReadOnly != readOnly )
169 {
170 mIsReadOnly = readOnly;
171 whileBlocking( this )->setReadOnly( !readOnly );
172 }
173
174}
175
QFlags< AuthConfigurationStorageCapability > AuthConfigurationStorageCapabilities
Authentication configuration storage capabilities.
Definition qgis.h:145
@ Warning
Warning message.
Definition qgis.h:156
@ Critical
Critical/error message.
Definition qgis.h:157
@ ReadSslCertificateCustomConfig
Can read a SSL certificate custom config.
@ ReadCertificateTrustPolicy
Can read a certificate trust policy.
@ ReadConfiguration
Can read an authentication configuration.
@ ReadCertificateAuthority
Can read a certificate authority.
@ ReadCertificateIdentity
Can read a certificate identity.
@ ReadMasterPassword
Can read the master password.
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:5970