diff --git a/src/app/main.cpp b/src/app/main.cpp index 58feae15..1b95da17 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -14,11 +14,26 @@ #include #include #include +#include #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #include #endif +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) +#if defined (Q_OS_LINUX) +#include +#include +#endif +#else +#if defined (Q_OS_LINUX) +#include +#include +#endif +#endif + +const QString s_PolkitActionCreate = "com.deepin.bootmaker.create"; + DCORE_USE_NAMESPACE DWIDGET_USE_NAMESPACE @@ -53,6 +68,27 @@ static bool switchToRoot(QApplication &app) #endif +// 在前端中预先进行身份验证, 便于流程控制 +static bool checkAuthorization() +{ +#if defined (Q_OS_LINUX) + QString busName = QDBusConnection::systemBus().baseService(); + auto authority = PolkitQt1::Authority::instance(); + if (!authority) { + qWarning() << "Failed to get Polkit authority instance"; + return false; + } + PolkitQt1::Authority::Result ret = authority->checkAuthorizationSync( + s_PolkitActionCreate, + PolkitQt1::SystemBusNameSubject(busName), + PolkitQt1::Authority::AllowUserInteraction); + + return PolkitQt1::Authority::Yes == ret; +#else + return true; +#endif +} + int main(int argc, char **argv) { qInfo() << "Starting Boot Maker application"; @@ -78,6 +114,11 @@ int main(int argc, char **argv) // app.setApplicationVersion(DApplication::buildVersion(VERSION)); // app.setTheme("light"); + if (!checkAuthorization()) { + qInfo() << "Authorization failed, exiting"; + exit(0); + } + #ifdef Q_OS_MAC qDebug() << "Checking root privileges on macOS"; if (switchToRoot(app)) { diff --git a/src/service/bootmakerservice.cpp b/src/service/bootmakerservice.cpp index 0b81f2ce..8052d2cc 100644 --- a/src/service/bootmakerservice.cpp +++ b/src/service/bootmakerservice.cpp @@ -128,7 +128,13 @@ void BootMakerService::Reboot() void BootMakerService::Start() { - // 启动服务, 不会修改系统, 不需要鉴权 + Q_D(BootMakerService); + qInfo() << "Start requested"; + if (!d->checkAuthorization(s_PolkitActionCreate)) { + qWarning() << "Start request denied - Authorization failed"; + return; + } + qDebug() << "Starting Boot Maker"; emit s_StartBootMarker(); }