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