QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
39bool QgsAuthConfigurationStorageSqlite::initialize()
40{
41 QMutexLocker locker( &mMutex );
42
43 if ( !QFileInfo::exists( mDatabase ) )
44 {
45 // Check if the parent path exists
46 QFileInfo parentInfo( QFileInfo( mDatabase ).path() );
47 if ( !parentInfo.exists() )
48 {
49 // Try to create the directory
50 QDir dir;
51 if ( !dir.mkpath( parentInfo.absolutePath() ) )
52 {
53 setError( tr( "Auth db directory path '%1' could not be created" ).arg( mDatabase ) );
54 return false;
55 }
56 }
57
58 // Try to create the database
59 QSqlDatabase db = authDatabaseConnection();
60 if ( !db.open() )
61 {
62 setError( tr( "Auth db file '%1' could not be created" ).arg( mDatabase ) );
63 return false;
64 }
65 }
66
67 // Check if the file is readable
68 const QFileInfo fileInfo( mDatabase );
69 if ( !fileInfo.permission( QFile::ReadOwner ) )
70 {
71 setError( tr( "Auth db file '%1' is not readable" ).arg( mDatabase ) );
72 return false;
73 }
74
75 // Check if the file is writable
76 if ( !fileInfo.permission( QFile::WriteOwner ) )
77 {
78 setError( tr( "Auth db file '%1' is not writable" ).arg( mDatabase ), Qgis::MessageLevel::Warning );
79 }
80
81 const bool ok { createConfigTables() && createCertTables() };
82 if ( !ok )
83 {
84 setError( tr( "Auth db initialization FAILED" ), Qgis::MessageLevel::Critical );
85 mIsReady = false;
86 return false;
87 }
88
89 mIsReady = true;
90
91 checkCapabilities();
92
93 // Recompute capabilities if needed
94 connect( this, &QgsAuthConfigurationStorageDb::readOnlyChanged, this, [this]( bool ) { checkCapabilities(); } );
95
96 return true;
97}
98
99QList<QgsAuthConfigurationStorage::SettingParameter> QgsAuthConfigurationStorageSqlite::settingsParameters() const
100{
101 return { { u"database"_s, tr( "Path to the SQLite database file" ), QVariant::String } };
102}
103
104QString QgsAuthConfigurationStorageSqlite::description() const
105{
106 return tr( "Store credentials in a local SQLite database" );
107}
108
109QString QgsAuthConfigurationStorageSqlite::type() const
110{
111 return u"SQLITE"_s;
112}
113
114bool QgsAuthConfigurationStorageSqlite::tableExists( const QString &table ) const
115{
116 QMutexLocker locker( &mMutex );
117
118 if ( !authDbOpen() )
119 {
120 const_cast< QgsAuthConfigurationStorageSqlite * >( this )->setError( tr( "Auth db could not be opened" ) );
121 return false;
122 }
123
124 QSqlQuery query( authDatabaseConnection() );
125 query.prepare( u"SELECT name FROM sqlite_master WHERE type='table' AND name=:name"_s );
126 query.bindValue( u":name"_s, table );
127
128 if ( !authDbQuery( &query ) )
129 {
130 const_cast< QgsAuthConfigurationStorageSqlite * >( this )->setError( tr( "Failed to check if table '%1' exists" ).arg( table ) );
131 return false;
132 }
133
134 if ( !query.next() )
135 {
136 return false;
137 }
138
139 return true;
140}
141
142void QgsAuthConfigurationStorageSqlite::checkCapabilities()
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
QFlags< AuthConfigurationStorageCapability > AuthConfigurationStorageCapabilities
Authentication configuration storage capabilities.
Definition qgis.h:152
@ Warning
Warning message.
Definition qgis.h:162
@ Critical
Critical/error message.
Definition qgis.h:163
@ ReadSslCertificateCustomConfig
Can read a SSL certificate custom config.
Definition qgis.h:120
@ ReadCertificateTrustPolicy
Can read a certificate trust policy.
Definition qgis.h:130
@ ReadConfiguration
Can read an authentication configuration.
Definition qgis.h:110
@ ReadCertificateAuthority
Can read a certificate authority.
Definition qgis.h:125
@ ReadCertificateIdentity
Can read a certificate identity.
Definition qgis.h:115
@ ReadMasterPassword
Can read the master password.
Definition qgis.h:135
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:6880