KDECore
PolicyKitBackend.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright (C) 2008 Nicola Gigante <nicola.gigante@gmail.com> 00003 * Copyright (C) 2009 Dario Freddi <drf@kde.org> 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU Lesser General Public License as published by 00007 * the Free Software Foundation; either version 2.1 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public License 00016 * along with this program; if not, write to the 00017 * Free Software Foundation, Inc., 00018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . 00019 */ 00020 00021 #include "PolicyKitBackend.h" 00022 00023 #include <QtCore/qplugin.h> 00024 #include <syslog.h> 00025 #include <polkit-qt/context.h> 00026 #include <polkit-qt/auth.h> 00027 00028 namespace KAuth 00029 { 00030 00031 PolicyKitBackend::PolicyKitBackend() 00032 : AuthBackend() 00033 { 00034 setCapabilities(AuthorizeFromClientCapability); 00035 } 00036 00037 Action::AuthStatus PolicyKitBackend::authorizeAction(const QString &action) 00038 { 00039 switch (PolkitQt::Auth::computeAndObtainAuth(action)) { 00040 case PolkitQt::Auth::Yes: 00041 return Action::Authorized; 00042 default: 00043 return Action::Denied; 00044 } 00045 } 00046 00047 void PolicyKitBackend::setupAction(const QString &action) 00048 { 00049 connect(PolkitQt::Context::instance(), SIGNAL(configChanged()), 00050 this, SLOT(checkForResultChanged())); 00051 connect(PolkitQt::Context::instance(), SIGNAL(consoleKitDBChanged()), 00052 this, SLOT(checkForResultChanged())); 00053 00054 m_cachedResults[action] = actionStatus(action); 00055 } 00056 00057 Action::AuthStatus PolicyKitBackend::actionStatus(const QString &action) 00058 { 00059 PolkitQt::Auth::Result r = PolkitQt::Auth::isCallerAuthorized(action, QCoreApplication::applicationPid(), 00060 false); 00061 switch (r) { 00062 case PolkitQt::Auth::Yes: 00063 return Action::Authorized; 00064 case PolkitQt::Auth::No: 00065 case PolkitQt::Auth::Unknown: 00066 return Action::Denied; 00067 default: 00068 return Action::AuthRequired; 00069 } 00070 } 00071 00072 QByteArray PolicyKitBackend::callerID() const 00073 { 00074 QByteArray a; 00075 QDataStream s(&a, QIODevice::WriteOnly); 00076 s << QCoreApplication::applicationPid(); 00077 00078 return a; 00079 } 00080 00081 bool PolicyKitBackend::isCallerAuthorized(const QString &action, QByteArray callerID) 00082 { 00083 QDataStream s(&callerID, QIODevice::ReadOnly); 00084 qint64 pid; 00085 00086 s >> pid; 00087 00088 return (PolkitQt::Auth::isCallerAuthorized(action, pid, false) == PolkitQt::Auth::Yes); 00089 } 00090 00091 void PolicyKitBackend::checkForResultChanged() 00092 { 00093 QHash<QString, Action::AuthStatus>::iterator i; 00094 for (i = m_cachedResults.begin(); i != m_cachedResults.end(); ++i) { 00095 if (i.value() != actionStatus(i.key())) { 00096 i.value() = actionStatus(i.key()); 00097 emit actionStatusChanged(i.key(), i.value()); 00098 } 00099 } 00100 } 00101 00102 } // namespace Auth 00103 00104 Q_EXPORT_PLUGIN2(kauth_backend, KAuth::PolicyKitBackend)
KDE 4.6 API Reference