• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • auth
  • backends
  • policykit
PolicyKitBackend.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2008 Nicola Gigante <nicola.gigante@gmail.com>
3 * Copyright (C) 2009 Dario Freddi <drf@kde.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation; either version 2.1 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA .
19 */
20 
21 #include "PolicyKitBackend.h"
22 
23 #include <QtCore/qplugin.h>
24 #include <syslog.h>
25 #include <polkit-qt/context.h>
26 #include <polkit-qt/auth.h>
27 
28 namespace KAuth
29 {
30 
31 PolicyKitBackend::PolicyKitBackend()
32  : AuthBackend()
33 {
34  setCapabilities(AuthorizeFromClientCapability);
35 }
36 
37 Action::AuthStatus PolicyKitBackend::authorizeAction(const QString &action)
38 {
39  switch (PolkitQt::Auth::computeAndObtainAuth(action)) {
40  case PolkitQt::Auth::Yes:
41  return Action::Authorized;
42  default:
43  return Action::Denied;
44  }
45 }
46 
47 void PolicyKitBackend::setupAction(const QString &action)
48 {
49  connect(PolkitQt::Context::instance(), SIGNAL(configChanged()),
50  this, SLOT(checkForResultChanged()));
51  connect(PolkitQt::Context::instance(), SIGNAL(consoleKitDBChanged()),
52  this, SLOT(checkForResultChanged()));
53 
54  m_cachedResults[action] = actionStatus(action);
55 }
56 
57 Action::AuthStatus PolicyKitBackend::actionStatus(const QString &action)
58 {
59  PolkitQt::Auth::Result r = PolkitQt::Auth::isCallerAuthorized(action, QCoreApplication::applicationPid(),
60  false);
61  switch (r) {
62  case PolkitQt::Auth::Yes:
63  return Action::Authorized;
64  case PolkitQt::Auth::No:
65  case PolkitQt::Auth::Unknown:
66  return Action::Denied;
67  default:
68  return Action::AuthRequired;
69  }
70 }
71 
72 QByteArray PolicyKitBackend::callerID() const
73 {
74  QByteArray a;
75  QDataStream s(&a, QIODevice::WriteOnly);
76  s << QCoreApplication::applicationPid();
77 
78  return a;
79 }
80 
81 bool PolicyKitBackend::isCallerAuthorized(const QString &action, QByteArray callerID)
82 {
83  QDataStream s(&callerID, QIODevice::ReadOnly);
84  qint64 pid;
85 
86  s >> pid;
87 
88  return (PolkitQt::Auth::isCallerAuthorized(action, pid, false) == PolkitQt::Auth::Yes);
89 }
90 
91 void PolicyKitBackend::checkForResultChanged()
92 {
93  QHash<QString, Action::AuthStatus>::iterator i;
94  for (i = m_cachedResults.begin(); i != m_cachedResults.end(); ++i) {
95  if (i.value() != actionStatus(i.key())) {
96  i.value() = actionStatus(i.key());
97  emit actionStatusChanged(i.key(), i.value());
98  }
99  }
100 }
101 
102 } // namespace Auth
103 
104 Q_EXPORT_PLUGIN2(kauth_backend, KAuth::PolicyKitBackend)
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Jul 16 2013 11:43:32 by doxygen 1.8.1.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.10.5 API Reference

Skip menu "kdelibs-4.10.5 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal