• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 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
29namespace KAuth
30{
31
32class Action::Private
33{
34public:
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
47Action::Action()
48 : d(new Private())
49{
50}
51
52Action::Action(const Action &action)
53 : d(new Private())
54{
55 *this = action;
56}
57
58Action::Action(const QString &name)
59 : d(new Private())
60{
61 setName(name);
62 BackendsManager::authBackend()->setupAction(d->name);
63}
64
65Action::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
73Action::~Action()
74{
75 delete d;
76}
77
78// Operators
79Action &Action::operator=(const Action & action)
80{
81 setName(action.d->name);
82 d->args = action.d->args;
83
84 return *this;
85}
86
87bool Action::operator==(const Action &action) const
88{
89 return d->name == action.d->name;
90}
91
92bool Action::operator!=(const Action &action) const
93{
94 return d->name != action.d->name;
95}
96
97// Accessors
98QString Action::name() const
99{
100 return d->name;
101}
102
103void 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
118QString Action::details() const
119{
120 return d->details;
121}
122
123void Action::setDetails(const QString &details)
124{
125 d->details = details;
126}
127
128bool Action::isValid() const
129{
130 return d->valid;
131}
132
133void Action::setArguments(const QVariantMap &arguments)
134{
135 d->args = arguments;
136}
137
138void Action::addArgument(const QString &key, const QVariant &value)
139{
140 d->args.insert(key, value);
141}
142
143QVariantMap Action::arguments() const
144{
145 return d->args;
146}
147
148ActionWatcher *Action::watcher()
149{
150 return ActionWatcher::watcher(d->name);
151}
152
153QString Action::helperID() const
154{
155 return d->helperId;
156}
157
158// TODO: Check for helper id's syntax
159void Action::setHelperID(const QString &id)
160{
161 d->helperId = id;
162}
163
164void Action::setParentWidget(QWidget* parent)
165{
166 d->parent = parent;
167}
168
169QWidget* Action::parentWidget() const
170{
171 return d->parent;
172}
173
174
175// Authorizaton methods
176Action::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
211Action::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
238Action::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
248bool Action::executeActions(const QList<Action> &actions, QList<Action> *deniedActions, const QString &helperId)
249{
250 return executeActions(actions, deniedActions, helperId, 0);
251}
252
253bool 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
286bool Action::executesAsync() const
287{
288 return d->async;
289}
290
291void Action::setExecutesAsync(bool async)
292{
293 d->async = async;
294}
295
296ActionReply Action::execute() const
297{
298 if (!isValid())
299 return ActionReply::InvalidActionReply;
300
301 return execute(helperID());
302}
303
304ActionReply 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
379void Action::stop()
380{
381 stop(helperID());
382}
383
384void Action::stop(const QString &helperID)
385{
386 BackendsManager::helperProxy()->stopAction(d->name, helperID);
387}
388
389bool Action::hasHelper() const
390{
391 return !d->helperId.isEmpty();
392}
393
394} // namespace Auth
BackendsManager.h
KAuth::ActionReply
Class that encapsulates a reply coming from the helper after executing an action.
Definition: kauthactionreply.h:371
KAuth::ActionReply::SuccessReply
static const ActionReply SuccessReply
An empty successful reply. Same as using the default constructor.
Definition: kauthactionreply.h:385
KAuth::ActionReply::InvalidActionReply
static const ActionReply InvalidActionReply
errorCode() == InvalidAction
Definition: kauthactionreply.h:390
KAuth::ActionReply::AuthorizationDeniedReply
static const ActionReply AuthorizationDeniedReply
errorCode() == AuthorizationDenied
Definition: kauthactionreply.h:391
KAuth::ActionReply::UserCancelledReply
static const ActionReply UserCancelledReply
errorCode() == UserCancelled
Definition: kauthactionreply.h:392
KAuth::ActionWatcher
Class used to receive notifications about the status of an action execution.
Definition: kauthactionwatcher.h:51
KAuth::ActionWatcher::watcher
static ActionWatcher * watcher(const QString &action)
Factory method to get watchers.
Definition: kauthactionwatcher.cpp:67
KAuth::Action
Class to access, authorize and execute actions.
Definition: kauthaction.h:70
KAuth::Action::addArgument
void addArgument(const QString &key, const QVariant &value)
Convenience method to add an argument.
Definition: kauthaction.cpp:138
KAuth::Action::authorize
AuthStatus authorize() const
Acquires authorization for an action without excuting it.
Definition: kauthaction.cpp:176
KAuth::Action::status
AuthStatus status() const
Gets information about the authorization status of an action.
Definition: kauthaction.cpp:238
KAuth::Action::executesAsync
bool executesAsync() const
Definition: kauthaction.cpp:286
KAuth::Action::setDetails
void setDetails(const QString &details)
Sets the action's details.
Definition: kauthaction.cpp:123
KAuth::Action::isValid
bool isValid() const
Returns if the object represents a valid action.
Definition: kauthaction.cpp:128
KAuth::Action::watcher
ActionWatcher * watcher()
Gets the ActionWatcher object for this action.
Definition: kauthaction.cpp:148
KAuth::Action::arguments
QVariantMap arguments() const
Returns map object used to pass arguments to the helper.
Definition: kauthaction.cpp:143
KAuth::Action::parentWidget
QWidget * parentWidget() const
Returns the parent widget for the authentication dialog for this action.
Definition: kauthaction.cpp:169
KAuth::Action::Action
Action()
Default constructor.
Definition: kauthaction.cpp:47
KAuth::Action::setName
void setName(const QString &name)
Sets the action's name.
Definition: kauthaction.cpp:103
KAuth::Action::operator==
bool operator==(const Action &action) const
Comparison operator.
Definition: kauthaction.cpp:87
KAuth::Action::helperID
QString helperID() const
Gets the default helper ID used for actions execution.
Definition: kauthaction.cpp:153
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::Action::earlyAuthorize
AuthStatus earlyAuthorize() const
Tries to resolve authorization status in the best possible way without executing the action.
Definition: kauthaction.cpp:211
KAuth::Action::operator=
Action & operator=(const Action &action)
Assignment operator.
Definition: kauthaction.cpp:79
KAuth::Action::name
QString name() const
Gets the action's name.
Definition: kauthaction.cpp:98
KAuth::Action::stop
void stop()
Ask the helper to stop executing an action.
Definition: kauthaction.cpp:379
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::operator!=
bool operator!=(const Action &action) const
Negated comparison operator.
Definition: kauthaction.cpp:92
KAuth::Action::details
QString details() const
Gets the action's details.
Definition: kauthaction.cpp:118
KAuth::Action::~Action
~Action()
Virtual destructor.
Definition: kauthaction.cpp:73
KAuth::Action::setExecutesAsync
void setExecutesAsync(bool async)
Definition: kauthaction.cpp:291
KAuth::Action::AuthStatus
AuthStatus
The three values returned by authorization methods.
Definition: kauthaction.h:78
KAuth::Action::Invalid
@ Invalid
An invalid action cannot be authorized.
Definition: kauthaction.h:81
KAuth::Action::Denied
@ Denied
The authorization has been denied by the authorization backend.
Definition: kauthaction.h:79
KAuth::Action::Authorized
@ Authorized
The authorization has been granted by the authorization backend.
Definition: kauthaction.h:82
KAuth::Action::UserCancelled
@ UserCancelled
The user pressed Cancel the authentication dialog. Currently used only on the mac.
Definition: kauthaction.h:84
KAuth::Action::AuthRequired
@ AuthRequired
The user could obtain the authorization after authentication.
Definition: kauthaction.h:83
KAuth::Action::hasHelper
bool hasHelper() const
Checks if the action has an helper.
Definition: kauthaction.cpp:389
KAuth::Action::setParentWidget
void setParentWidget(QWidget *parent)
Sets a parent widget for the authentication dialog.
Definition: kauthaction.cpp:164
KAuth::Action::execute
ActionReply execute() const
Synchronously executes the action.
Definition: kauthaction.cpp:296
KAuth::Action::setHelperID
void setHelperID(const QString &id)
Sets the default helper ID used for actions execution.
Definition: kauthaction.cpp:159
KAuth::AuthBackend::actionStatus
virtual Action::AuthStatus actionStatus(const QString &action)=0
KAuth::AuthBackend::authorizeAction
virtual Action::AuthStatus authorizeAction(const QString &action)=0
KAuth::AuthBackend::callerID
virtual QByteArray callerID() const =0
KAuth::AuthBackend::preAuthAction
virtual void preAuthAction(const QString &action, QWidget *parent)
Definition: AuthBackend.cpp:68
KAuth::AuthBackend::actionExists
virtual bool actionExists(const QString &action)
Definition: AuthBackend.cpp:62
KAuth::AuthBackend::setupAction
virtual void setupAction(const QString &action)=0
KAuth::AuthBackend::PreAuthActionCapability
@ PreAuthActionCapability
Definition: AuthBackend.h:42
KAuth::AuthBackend::AuthorizeFromHelperCapability
@ AuthorizeFromHelperCapability
Definition: AuthBackend.h:40
KAuth::AuthBackend::AuthorizeFromClientCapability
@ AuthorizeFromClientCapability
Definition: AuthBackend.h:39
KAuth::AuthBackend::CheckActionExistenceCapability
@ CheckActionExistenceCapability
Definition: AuthBackend.h:41
KAuth::BackendsManager::authBackend
static AuthBackend * authBackend()
Definition: BackendsManager.cpp:120
KAuth::BackendsManager::helperProxy
static HelperProxy * helperProxy()
Definition: BackendsManager.cpp:129
KAuth::HelperProxy::stopAction
virtual void stopAction(const QString &action, const QString &helperID)=0
KAuth::HelperProxy::executeActions
virtual bool executeActions(const QList< QPair< QString, QVariantMap > > &list, const QString &helperID)=0
KAuth::HelperProxy::authorizeAction
virtual Action::AuthStatus authorizeAction(const QString &action, const QString &helperID)=0
KAuth::HelperProxy::executeAction
virtual ActionReply executeAction(const QString &action, const QString &helperID, const QVariantMap &arguments)=0
QList
Definition: kaboutdata.h:33
QPair
QString
QVariant
kauthaction.h
kauthactionwatcher.h
KAuth
Definition: AuthBackend.cpp:24
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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.14.38 API Reference

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