File indexing completed on 2025-01-19 03:53:29

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2010-07-24
0007  * Description : Core database privileges checker
0008  *
0009  * SPDX-FileCopyrightText: 2010 by Holger Foerster <hamsi2k at freenet dot de>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "coredbchecker.h"
0016 
0017 // Qt includes
0018 
0019 #include <QSqlDatabase>
0020 #include <QSqlError>
0021 
0022 // Local includes
0023 
0024 #include "digikam_debug.h"
0025 
0026 namespace Digikam
0027 {
0028 
0029 CoreDbPrivilegesChecker::CoreDbPrivilegesChecker(const DbEngineParameters& parameters)
0030     : m_parameters(parameters)
0031 {
0032 }
0033 
0034 CoreDbPrivilegesChecker::~CoreDbPrivilegesChecker()
0035 {
0036 }
0037 
0038 bool CoreDbPrivilegesChecker::checkPrivileges(QStringList& insufficientRights)
0039 {
0040     bool result = true;
0041     DbEngineLocking fromLocking;
0042     CoreDbBackend fromDBbackend(&fromLocking, QLatin1String("PrivilegesCheckDatabase"));
0043 
0044     if (!fromDBbackend.open(m_parameters))
0045     {
0046         return false;
0047     }
0048 
0049     // After a crash at the start of digiKam, it may happen that the table
0050     // to check the rights remains. Because the next time the start of the
0051     // rights check is constantly failing, we remove the table before
0052     // every start and do not check for errors.
0053     checkPriv(fromDBbackend, QLatin1String("CheckPriv_Cleanup"));
0054 
0055     if (!checkPriv(fromDBbackend, QLatin1String("CheckPriv_CREATE_TABLE")))
0056     {
0057         insufficientRights.append(QLatin1String("CREATE TABLE"));
0058         result = false;
0059     }
0060     else if (!checkPriv(fromDBbackend, QLatin1String("CheckPriv_ALTER_TABLE")))
0061     {
0062         insufficientRights.append(QLatin1String("ALTER TABLE"));
0063         result = false;
0064     }
0065     else if (!checkPriv(fromDBbackend, QLatin1String("CheckPriv_CREATE_TRIGGER")))
0066     {
0067         insufficientRights.append(QLatin1String("CREATE TRIGGER"));
0068         result = false;
0069     }
0070     else if (!checkPriv(fromDBbackend, QLatin1String("CheckPriv_DROP_TRIGGER")))
0071     {
0072         insufficientRights.append(QLatin1String("DROP TRIGGER"));
0073         result = false;
0074     }
0075     else if (!checkPriv(fromDBbackend, QLatin1String("CheckPriv_DROP_TABLE")))
0076     {
0077         insufficientRights.append(QLatin1String("DROP TABLE"));
0078         result = false;
0079     }
0080     else if (!checkPriv(fromDBbackend, QLatin1String("CheckPriv_Cleanup")))
0081     {
0082         insufficientRights.append(QLatin1String("DROP TABLE PrivCheck"));
0083         result = false;
0084     }
0085 
0086     return result;
0087 }
0088 
0089 bool CoreDbPrivilegesChecker::checkPriv(CoreDbBackend& dbBackend, const QString& dbActionName)
0090 {
0091     BdEngineBackend::QueryState queryStateResult = dbBackend.execDBAction(dbActionName);
0092 
0093     if (queryStateResult != BdEngineBackend::NoErrors       &&
0094         dbBackend.lastSQLError().isValid()                  &&
0095         !dbBackend.lastSQLError().nativeErrorCode().isEmpty())
0096     {
0097         qCDebug(DIGIKAM_COREDB_LOG) << "Core database: error while creating a trigger. Details:"
0098                                     << dbBackend.lastSQLError();
0099         return false;
0100     }
0101 
0102     return true;
0103 }
0104 
0105 } // namespace Digikam