File indexing completed on 2024-04-14 04:38:32
0001 // This is an example not a library 0002 /* 0003 SPDX-FileCopyrightText: 2008 Daniel Nicoletti <dantti85-pk@yahoo.com.br> 0004 SPDX-FileCopyrightText: 2009 Radek Novacek <rnovacek@redhat.com> 0005 0006 SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include "PkExampleHelper.h" 0010 #include "examplesadaptor.h" 0011 0012 #include <polkitqt1-authority.h> 0013 0014 #include <QDBusConnection> 0015 #include <QTimer> 0016 #include <QDebug> 0017 #include <QDomDocument> 0018 0019 #define MINUTE 60000 0020 0021 using namespace PolkitQt1; 0022 0023 PkExampleHelper::PkExampleHelper(int &argc, char **argv) 0024 : QCoreApplication(argc, argv) 0025 { 0026 qDebug() << "Creating Helper"; 0027 (void) new ExamplesAdaptor(this); 0028 // Register the DBus service 0029 if (!QDBusConnection::systemBus().registerService("org.qt.policykit.examples")) { 0030 qDebug() << QDBusConnection::systemBus().lastError().message();; 0031 QTimer::singleShot(0, this, SLOT(quit())); 0032 return; 0033 } 0034 0035 if (!QDBusConnection::systemBus().registerObject("/", this)) { 0036 qDebug() << "unable to register service interface to dbus"; 0037 QTimer::singleShot(0, this, SLOT(quit())); 0038 return; 0039 } 0040 // Normally you will set a timeout so your application can 0041 // free some resources of the poor client machine ;) 0042 QTimer::singleShot(MINUTE, this, SLOT(quit())); 0043 } 0044 0045 PkExampleHelper::~PkExampleHelper() 0046 { 0047 qDebug() << "Destroying Helper"; 0048 } 0049 0050 bool PkExampleHelper::set(const QString &action) 0051 { 0052 qDebug() << "PkExampleHelper::set" << action; 0053 // message().service() is the service name of the caller 0054 // We can check if the caller is authorized to the following action 0055 Authority::Result result; 0056 SystemBusNameSubject subject(message().service()); 0057 0058 result = Authority::instance()->checkAuthorizationSync("org.qt.policykit.examples.set", 0059 subject , Authority::AllowUserInteraction); 0060 if (result == Authority::Yes) { 0061 qDebug() << message().service() << QString("Implicit authorization set to") << action; 0062 // Caller is authorized so we can perform the action 0063 return setValue(action); 0064 } else { 0065 qDebug() << message().service() << QString("Can't set the implicit authorization"); 0066 // Caller is not authorized so the action can't be performed 0067 return false; 0068 } 0069 } 0070 0071 bool PkExampleHelper::setValue(const QString &action) 0072 { 0073 // This action must be authorized first. It will set the implicit 0074 // authorization for the Shout action by editing the .policy file 0075 QDomDocument doc = QDomDocument("policy"); 0076 QFile file("/usr/share/polkit-1/actions/org.qt.policykit.examples.policy"); 0077 if (!file.open(QIODevice::ReadOnly)) 0078 return false; 0079 doc.setContent(&file); 0080 file.close(); 0081 QDomElement el = doc.firstChildElement("policyconfig"). 0082 firstChildElement("action"); 0083 while (!el.isNull() && el.attribute("id", QString()) != "org.qt.policykit.examples.shout") { 0084 el = el.nextSiblingElement("action"); 0085 } 0086 el = el.firstChildElement("defaults"); 0087 el = el.firstChildElement("allow_active"); 0088 if (el.isNull()) 0089 return false; 0090 el.firstChild().toText().setData(action); 0091 if (!file.open(QIODevice::WriteOnly)) 0092 return false; 0093 QTextStream stream(&file); 0094 doc.save(stream, 2); 0095 file.close(); 0096 return true; 0097 }