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

KDECore

  • 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 (hasHelper()) {
360  // Perform the pre auth here
361  if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::PreAuthActionCapability) {
362  BackendsManager::authBackend()->preAuthAction(d->name, d->parent);
363  }
364 
365  return BackendsManager::helperProxy()->executeAction(d->name, helperID, d->args);
366  } else {
367  return ActionReply::SuccessReply;
368  }
369  }
370 }
371 
372 void Action::stop()
373 {
374  stop(helperID());
375 }
376 
377 void Action::stop(const QString &helperID)
378 {
379  BackendsManager::helperProxy()->stopAction(d->name, helperID);
380 }
381 
382 bool Action::hasHelper() const
383 {
384  return !d->helperId.isEmpty();
385 }
386 
387 } // namespace Auth
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Apr 16 2013 20:55:35 by doxygen 1.8.3.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.2 API Reference

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