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