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

KDECore

  • sources
  • kde-4.14
  • kdelibs
  • kdecore
  • auth
kauthaction.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2009 Nicola Gigante <nicola.gigante@gmail.com>
3 * Copyright (C) 2009-2010 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 "kauthaction.h"
22 
23 #include <QRegExp>
24 #include <QWidget>
25 
26 #include "BackendsManager.h"
27 #include "kauthactionwatcher.h"
28 
29 namespace KAuth
30 {
31 
32 class Action::Private
33 {
34 public:
35  Private() : valid(false), async(false), parent(0) {}
36 
37  QString name;
38  QString details;
39  QString helperId;
40  QVariantMap args;
41  bool valid;
42  bool async;
43  QWidget *parent;
44 };
45 
46 // Constructors
47 Action::Action()
48  : d(new Private())
49 {
50 }
51 
52 Action::Action(const Action &action)
53  : d(new Private())
54 {
55  *this = action;
56 }
57 
58 Action::Action(const QString &name)
59  : d(new Private())
60 {
61  setName(name);
62  BackendsManager::authBackend()->setupAction(d->name);
63 }
64 
65 Action::Action(const QString &name, const QString &details)
66  : d(new Private())
67 {
68  setName(name);
69  setDetails(details);
70  BackendsManager::authBackend()->setupAction(d->name);
71 }
72 
73 Action::~Action()
74 {
75  delete d;
76 }
77 
78 // Operators
79 Action &Action::operator=(const Action & action)
80 {
81  setName(action.d->name);
82  d->args = action.d->args;
83 
84  return *this;
85 }
86 
87 bool Action::operator==(const Action &action) const
88 {
89  return d->name == action.d->name;
90 }
91 
92 bool Action::operator!=(const Action &action) const
93 {
94  return d->name != action.d->name;
95 }
96 
97 // Accessors
98 QString Action::name() const
99 {
100  return d->name;
101 }
102 
103 void Action::setName(const QString &name)
104 {
105  d->name = name;
106 
107  // Does the backend support checking for known actions?
108  if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::CheckActionExistenceCapability) {
109  // In this case, just ask the backend
110  d->valid = BackendsManager::authBackend()->actionExists(name);
111  } else {
112  // Otherwise, check through a regexp
113  QRegExp exp(QLatin1String("[0-z]+(\\.[0-z]+)*"));
114  d->valid = exp.exactMatch(name);
115  }
116 }
117 
118 QString Action::details() const
119 {
120  return d->details;
121 }
122 
123 void Action::setDetails(const QString &details)
124 {
125  d->details = details;
126 }
127 
128 bool Action::isValid() const
129 {
130  return d->valid;
131 }
132 
133 void Action::setArguments(const QVariantMap &arguments)
134 {
135  d->args = arguments;
136 }
137 
138 void Action::addArgument(const QString &key, const QVariant &value)
139 {
140  d->args.insert(key, value);
141 }
142 
143 QVariantMap Action::arguments() const
144 {
145  return d->args;
146 }
147 
148 ActionWatcher *Action::watcher()
149 {
150  return ActionWatcher::watcher(d->name);
151 }
152 
153 QString Action::helperID() const
154 {
155  return d->helperId;
156 }
157 
158 // TODO: Check for helper id's syntax
159 void Action::setHelperID(const QString &id)
160 {
161  d->helperId = id;
162 }
163 
164 void Action::setParentWidget(QWidget* parent)
165 {
166  d->parent = parent;
167 }
168 
169 QWidget* Action::parentWidget() const
170 {
171  return d->parent;
172 }
173 
174 
175 // Authorizaton methods
176 Action::AuthStatus Action::authorize() const
177 {
178  if (!isValid()) {
179  return Action::Invalid;
180  }
181 
182  // If there is any pre auth action, let's perform it
183  if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::PreAuthActionCapability) {
184  BackendsManager::authBackend()->preAuthAction(d->name, d->parent);
185  }
186 
187  // Let's check capabilities
188  if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::AuthorizeFromClientCapability) {
189  // That's easy then
190  return BackendsManager::authBackend()->authorizeAction(d->name);
191  } else if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::AuthorizeFromHelperCapability) {
192  // We need to check if we have an helper in this case
193  if (hasHelper()) {
194  // Ok, we need to use "helper authorization".
195  return BackendsManager::helperProxy()->authorizeAction(d->name, d->helperId);
196  } else {
197  // Ok, in this case we have to fake and just pretend we are an helper
198  if (BackendsManager::authBackend()->isCallerAuthorized(d->name, BackendsManager::authBackend()->callerID())) {
199  return Authorized;
200  } else {
201  return Denied;
202  }
203  }
204  } else {
205  // This should never, never happen
206  return Invalid;
207  }
208 }
209 
210 
211 Action::AuthStatus Action::earlyAuthorize() const
212 {
213  // Check the status first
214  AuthStatus s = status();
215  if (s == AuthRequired) {
216  // Let's check what to do
217  if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::AuthorizeFromClientCapability) {
218  // In this case we can actually try an authorization
219  if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::PreAuthActionCapability) {
220  BackendsManager::authBackend()->preAuthAction(d->name, d->parent);
221  }
222 
223  return BackendsManager::authBackend()->authorizeAction(d->name);
224  } else if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::AuthorizeFromHelperCapability) {
225  // In this case, just throw out Authorized, as the auth will take place later
226  return Authorized;
227  } else {
228  // This should never, never happen
229  return Invalid;
230  }
231  } else {
232  // It's fine, return the status
233  return s;
234  }
235 }
236 
237 
238 Action::AuthStatus Action::status() const
239 {
240  if (!isValid()) {
241  return Action::Invalid;
242  }
243 
244  return BackendsManager::authBackend()->actionStatus(d->name);
245 }
246 
247 // Execution methods
248 bool Action::executeActions(const QList<Action> &actions, QList<Action> *deniedActions, const QString &helperId)
249 {
250  return executeActions(actions, deniedActions, helperId, 0);
251 }
252 
253 bool Action::executeActions(const QList< Action >& actions, QList< Action >* deniedActions, const QString& helperId, QWidget* parent)
254 {
255  QList<QPair<QString, QVariantMap> > list;
256 
257  foreach(const Action &a, actions) {
258  // Save us an additional step
259  if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::AuthorizeFromClientCapability) {
260  if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::PreAuthActionCapability) {
261  BackendsManager::authBackend()->preAuthAction(a.name(), parent);
262  }
263 
264  AuthStatus s = BackendsManager::authBackend()->authorizeAction(a.name());
265 
266  if (s == Authorized) {
267  list.push_back(QPair<QString, QVariantMap>(a.name(), a.arguments()));
268  } else if ((s == Denied || s == Invalid) && deniedActions) {
269  *deniedActions << a;
270  }
271  } else if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::AuthorizeFromHelperCapability) {
272  list.push_back(QPair<QString, QVariantMap>(a.name(), a.arguments()));
273  } else {
274  // There's something totally wrong here
275  return false;
276  }
277  }
278 
279  if (list.isEmpty()) {
280  return false;
281  }
282 
283  return BackendsManager::helperProxy()->executeActions(list, helperId);
284 }
285 
286 bool Action::executesAsync() const
287 {
288  return d->async;
289 }
290 
291 void Action::setExecutesAsync(bool async)
292 {
293  d->async = async;
294 }
295 
296 ActionReply Action::execute() const
297 {
298  if (!isValid())
299  return ActionReply::InvalidActionReply;
300 
301  return execute(helperID());
302 }
303 
304 ActionReply Action::execute(const QString &helperID) const
305 {
306  // Is the action valid?
307  if (!isValid()) {
308  return ActionReply::InvalidActionReply;
309  }
310 
311  // What to do?
312  if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::AuthorizeFromClientCapability) {
313  if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::PreAuthActionCapability) {
314  BackendsManager::authBackend()->preAuthAction(d->name, d->parent);
315  }
316  // Authorize from here
317  AuthStatus s = BackendsManager::authBackend()->authorizeAction(d->name);
318 
319  // Abort if authorization fails
320  switch (s) {
321  case Denied:
322  return ActionReply::AuthorizationDeniedReply;
323  case Invalid:
324  return ActionReply::InvalidActionReply;
325  case UserCancelled:
326  return ActionReply::UserCancelledReply;
327  default:
328  break;
329  }
330  } else if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::AuthorizeFromHelperCapability) {
331  // In this case we care only if the action is not async and does not have an helper
332  if (!d->async && !hasHelper()) {
333  // Authorize!
334  switch (authorize()) {
335  case Denied:
336  return ActionReply::AuthorizationDeniedReply;
337  case Invalid:
338  return ActionReply::InvalidActionReply;
339  case UserCancelled:
340  return ActionReply::UserCancelledReply;
341  default:
342  break;
343  }
344  }
345  } else {
346  // What?
347  return ActionReply::InvalidActionReply;
348  }
349 
350  if (d->async) {
351  if (!hasHelper()) {
352  // It makes no sense
353  return ActionReply::InvalidActionReply;
354  }
355 
356  return executeActions(QList<Action>() << *this, NULL, helperID) ?
357  ActionReply::SuccessReply : ActionReply::AuthorizationDeniedReply;
358  } else {
359 #if defined(Q_OS_MACX) || defined(__APPLE__) || defined(__MACH__)
360  if( BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::AuthorizeFromClientCapability ){
361  // RJVB: authorisation through DBus seems to be flaky (at least when using the OSX keychain ... maybe because DBus
362  // isn't built with Keychain support in MacPorts?)
363  return ActionReply::SuccessReply;
364  }
365 #endif //APPLE
366  if (hasHelper()) {
367  // Perform the pre auth here
368  if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::PreAuthActionCapability) {
369  BackendsManager::authBackend()->preAuthAction(d->name, d->parent);
370  }
371 
372  return BackendsManager::helperProxy()->executeAction(d->name, helperID, d->args);
373  } else {
374  return ActionReply::SuccessReply;
375  }
376  }
377 }
378 
379 void Action::stop()
380 {
381  stop(helperID());
382 }
383 
384 void Action::stop(const QString &helperID)
385 {
386  BackendsManager::helperProxy()->stopAction(d->name, helperID);
387 }
388 
389 bool Action::hasHelper() const
390 {
391  return !d->helperId.isEmpty();
392 }
393 
394 } // namespace Auth
KAuth::Action::~Action
~Action()
Virtual destructor.
Definition: kauthaction.cpp:73
KAuth::HelperProxy::stopAction
virtual void stopAction(const QString &action, const QString &helperID)=0
KAuth::Action::operator=
Action & operator=(const Action &action)
Assignment operator.
Definition: kauthaction.cpp:79
QWidget
KAuth::AuthBackend::setupAction
virtual void setupAction(const QString &action)=0
KAuth::AuthBackend::CheckActionExistenceCapability
Definition: AuthBackend.h:41
KAuth::ActionReply::InvalidActionReply
static const ActionReply InvalidActionReply
errorCode() == InvalidAction
Definition: kauthactionreply.h:390
KAuth::ActionWatcher::watcher
static ActionWatcher * watcher(const QString &action)
Factory method to get watchers.
Definition: kauthactionwatcher.cpp:67
KAuth::Action::AuthRequired
The user could obtain the authorization after authentication.
Definition: kauthaction.h:83
KAuth::Action::status
AuthStatus status() const
Gets information about the authorization status of an action.
Definition: kauthaction.cpp:238
KAuth::Action::setName
void setName(const QString &name)
Sets the action's name.
Definition: kauthaction.cpp:103
QList::push_back
void push_back(const T &value)
KAuth::BackendsManager::authBackend
static AuthBackend * authBackend()
Definition: BackendsManager.cpp:120
KAuth::Action::earlyAuthorize
AuthStatus earlyAuthorize() const
Tries to resolve authorization status in the best possible way without executing the action...
Definition: kauthaction.cpp:211
KAuth::AuthBackend::AuthorizeFromClientCapability
Definition: AuthBackend.h:39
KAuth::AuthBackend::authorizeAction
virtual Action::AuthStatus authorizeAction(const QString &action)=0
KAuth::ActionReply::SuccessReply
static const ActionReply SuccessReply
An empty successful reply. Same as using the default constructor.
Definition: kauthactionreply.h:385
KAuth::AuthBackend::preAuthAction
virtual void preAuthAction(const QString &action, QWidget *parent)
Definition: AuthBackend.cpp:68
kauthactionwatcher.h
KAuth::Action::name
QString name() const
Gets the action's name.
Definition: kauthaction.cpp:98
KAuth::Action::isValid
bool isValid() const
Returns if the object represents a valid action.
Definition: kauthaction.cpp:128
KAuth::Action::UserCancelled
The user pressed Cancel the authentication dialog. Currently used only on the mac.
Definition: kauthaction.h:84
KAuth::Action::Invalid
An invalid action cannot be authorized.
Definition: kauthaction.h:81
KAuth::AuthBackend::AuthorizeFromHelperCapability
Definition: AuthBackend.h:40
KAuth::Action::addArgument
void addArgument(const QString &key, const QVariant &value)
Convenience method to add an argument.
Definition: kauthaction.cpp:138
KAuth::Action::Action
Action()
Default constructor.
Definition: kauthaction.cpp:47
KAuth::ActionReply::AuthorizationDeniedReply
static const ActionReply AuthorizationDeniedReply
errorCode() == AuthorizationDenied
Definition: kauthactionreply.h:391
KAuth::Action::setArguments
void setArguments(const QVariantMap &arguments)
Sets the map object used to pass arguments to the helper.
Definition: kauthaction.cpp:133
KAuth::Action::setExecutesAsync
void setExecutesAsync(bool async)
Definition: kauthaction.cpp:291
KAuth::HelperProxy::executeAction
virtual ActionReply executeAction(const QString &action, const QString &helperID, const QVariantMap &arguments)=0
QRegExp
KAuth::ActionWatcher
Class used to receive notifications about the status of an action execution.
Definition: kauthactionwatcher.h:50
KAuth::BackendsManager::helperProxy
static HelperProxy * helperProxy()
Definition: BackendsManager.cpp:129
KAuth::Action::Denied
The authorization has been denied by the authorization backend.
Definition: kauthaction.h:79
KAuth::Action::execute
ActionReply execute() const
Synchronously executes the action.
Definition: kauthaction.cpp:296
QList::isEmpty
bool isEmpty() const
KAuth::Action::executeActions
static bool executeActions(const QList< Action > &actions, QList< Action > *deniedActions, const QString &helperId)
Asynchronously executes a group of actions with a single request.
Definition: kauthaction.cpp:248
KAuth::HelperProxy::authorizeAction
virtual Action::AuthStatus authorizeAction(const QString &action, const QString &helperID)=0
KAuth::Action::Authorized
The authorization has been granted by the authorization backend.
Definition: kauthaction.h:82
QString
QList
Definition: kaboutdata.h:33
KAuth::ActionReply::UserCancelledReply
static const ActionReply UserCancelledReply
errorCode() == UserCancelled
Definition: kauthactionreply.h:392
kauthaction.h
QPair
KAuth::Action::AuthStatus
AuthStatus
The three values returned by authorization methods.
Definition: kauthaction.h:78
KAuth::Action::hasHelper
bool hasHelper() const
Checks if the action has an helper.
Definition: kauthaction.cpp:389
KAuth::Action::operator!=
bool operator!=(const Action &action) const
Negated comparison operator.
Definition: kauthaction.cpp:92
KAuth::AuthBackend::callerID
virtual QByteArray callerID() const =0
KAuth::Action::setHelperID
void setHelperID(const QString &id)
Sets the default helper ID used for actions execution.
Definition: kauthaction.cpp:159
KAuth::Action::helperID
QString helperID() const
Gets the default helper ID used for actions execution.
Definition: kauthaction.cpp:153
KAuth::AuthBackend::PreAuthActionCapability
Definition: AuthBackend.h:42
KAuth::Action::setDetails
void setDetails(const QString &details)
Sets the action's details.
Definition: kauthaction.cpp:123
QLatin1String
KAuth::AuthBackend::actionExists
virtual bool actionExists(const QString &action)
Definition: AuthBackend.cpp:62
KAuth::HelperProxy::executeActions
virtual bool executeActions(const QList< QPair< QString, QVariantMap > > &list, const QString &helperID)=0
KAuth::Action::stop
void stop()
Ask the helper to stop executing an action.
Definition: kauthaction.cpp:379
KAuth::Action::details
QString details() const
Gets the action's details.
Definition: kauthaction.cpp:118
KAuth::ActionReply
Class that encapsulates a reply coming from the helper after executing an action. ...
Definition: kauthactionreply.h:370
KAuth::Action::arguments
QVariantMap arguments() const
Returns map object used to pass arguments to the helper.
Definition: kauthaction.cpp:143
KAuth::Action::setParentWidget
void setParentWidget(QWidget *parent)
Sets a parent widget for the authentication dialog.
Definition: kauthaction.cpp:164
KAuth::AuthBackend::actionStatus
virtual Action::AuthStatus actionStatus(const QString &action)=0
KAuth::Action::watcher
ActionWatcher * watcher()
Gets the ActionWatcher object for this action.
Definition: kauthaction.cpp:148
KAuth::Action
Class to access, authorize and execute actions.
Definition: kauthaction.h:69
KAuth::Action::executesAsync
bool executesAsync() const
Definition: kauthaction.cpp:286
QRegExp::exactMatch
bool exactMatch(const QString &str) const
KAuth::Action::parentWidget
QWidget * parentWidget() const
Returns the parent widget for the authentication dialog for this action.
Definition: kauthaction.cpp:169
BackendsManager.h
KAuth::Action::authorize
AuthStatus authorize() const
Acquires authorization for an action without excuting it.
Definition: kauthaction.cpp:176
KAuth::AuthBackend::capabilities
Capabilities capabilities() const
Definition: AuthBackend.cpp:47
QVariant
KAuth::Action::operator==
bool operator==(const Action &action) const
Comparison operator.
Definition: kauthaction.cpp:87
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:10 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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