PolkitQt-1

polkitqt1-authority.cpp
1/*
2 This file is part of the Polkit-qt project
3 SPDX-FileCopyrightText: 2009 Daniel Nicoletti <dantti85-pk@yahoo.com.br>
4 SPDX-FileCopyrightText: 2009 Dario Freddi <drf@kde.org>
5 SPDX-FileCopyrightText: 2009 Jaroslav Reznik <jreznik@redhat.com>
6 SPDX-FileCopyrightText: 2009 Radek Novacek <rnovacek@redhat.com>
7
8 SPDX-License-Identifier: LGPL-2.0-or-later
9*/
10
11#include "polkitqt1-authority.h"
12
13#include <QDBusInterface>
14#include <QDBusMessage>
15#include <QDBusReply>
16
17#include <polkit/polkit.h>
18
19namespace PolkitQt1
20{
21
22class AuthorityHelper
23{
24public:
25 AuthorityHelper() : q(nullptr) {}
26 ~AuthorityHelper() {
27 delete q;
28 }
29 Authority *q;
30};
31
32Q_GLOBAL_STATIC(AuthorityHelper, s_globalAuthority)
33
34Authority *Authority::instance(PolkitAuthority *authority)
35{
36 if (!s_globalAuthority()->q) {
37 new Authority(authority);
38 }
39
40 return s_globalAuthority()->q;
41}
42
43Authority::Result polkitResultToResult(PolkitAuthorizationResult *result)
44{
45 if (polkit_authorization_result_get_is_challenge(result)) {
47 } else if (polkit_authorization_result_get_is_authorized(result)) {
48 return Authority::Yes;
49 } else {
50 return Authority::No;
51 }
52}
53
54ActionDescription::List actionsToListAndFree(GList *glist)
55{
56 ActionDescription::List result;
57 for (GList *glist2 = glist; glist2; glist2 = g_list_next(glist2)) {
58 gpointer i = glist2->data;
59 result.append(ActionDescription(static_cast<PolkitActionDescription *>(i)));
60 g_object_unref(i);
61 }
62
63 g_list_free(glist);
64 return result;
65}
66
67class Q_DECL_HIDDEN Authority::Private
68{
69public:
70 // Polkit will return NULL on failures, hence we use it instead of 0
71 Private(Authority *qq) : q(qq)
72 , pkAuthority(nullptr)
73 , m_hasError(false)
74 , m_systemBus(nullptr)
75 {
76 }
77
78 ~Private();
79
80 void init();
81
82 /** Use this method to set the error message to \p message. Set recover to \c true
83 * to try to reinitialize this object with init() method
84 */
85 void setError(Authority::ErrorCode code, const QString &details = QString(), bool recover = false);
86
87 void dbusFilter(const QDBusMessage &message);
88 void dbusSignalAdd(const QString &service, const QString &path, const QString &interface, const QString &name);
89 void seatSignalsConnect(const QString &seat);
90
91 Authority *q;
92 PolkitAuthority *pkAuthority;
93 bool m_hasError;
94 Authority::ErrorCode m_lastError;
95 QString m_errorDetails;
96 // Local system bus. QDBusConnection::systemBus() may only be safely used
97 // inside a QCoreApplication scope as for example destruction of connected
98 // objects need to happen before the bus disappears. Since this class however
99 // is a global static and systemBus() internally is a global static we
100 // cannot assure destruction order. Instead we create a local copy of the
101 // global systemBus instance so we can make life time to our needs.
102 // This prevents crashes when cleaning up the global statics.
103 QDBusConnection *m_systemBus;
104 GCancellable *m_checkAuthorizationCancellable,
105 *m_enumerateActionsCancellable,
106 *m_registerAuthenticationAgentCancellable,
107 *m_unregisterAuthenticationAgentCancellable,
108 *m_authenticationAgentResponseCancellable,
109 *m_enumerateTemporaryAuthorizationsCancellable,
110 *m_revokeTemporaryAuthorizationsCancellable,
111 *m_revokeTemporaryAuthorizationCancellable;
112
113 /**
114 * \brief Convert a Qt DetailsMap to the lower level PolkitDetails type
115 *
116 * The returned pointer needs to be freed via g_object_unref when no
117 * longer needed. Returns nullptr if details is empty.
118 */
119 static PolkitDetails* convertDetailsMap(const DetailsMap &details);
120 static void pk_config_changed();
121 static void checkAuthorizationCallback(GObject *object, GAsyncResult *result, gpointer user_data);
122 static void enumerateActionsCallback(GObject *object, GAsyncResult *result, gpointer user_data);
123 static void registerAuthenticationAgentCallback(GObject *object, GAsyncResult *result, gpointer user_data);
124 static void unregisterAuthenticationAgentCallback(GObject *object, GAsyncResult *result, gpointer user_data);
125 static void authenticationAgentResponseCallback(GObject *object, GAsyncResult *result, gpointer user_data);
126 static void enumerateTemporaryAuthorizationsCallback(GObject *object, GAsyncResult *result, gpointer user_data);
127 static void revokeTemporaryAuthorizationsCallback(GObject *object, GAsyncResult *result, gpointer user_data);
128 static void revokeTemporaryAuthorizationCallback(GObject *object, GAsyncResult *result, gpointer user_data);
129};
130
131Authority::Private::~Private()
132{
133 delete m_systemBus;
134 g_object_unref(m_checkAuthorizationCancellable);
135 g_object_unref(m_enumerateActionsCancellable);
136 g_object_unref(m_registerAuthenticationAgentCancellable);
137 g_object_unref(m_unregisterAuthenticationAgentCancellable);
138 g_object_unref(m_authenticationAgentResponseCancellable);
139 g_object_unref(m_enumerateTemporaryAuthorizationsCancellable);
140 g_object_unref(m_revokeTemporaryAuthorizationsCancellable);
141 g_object_unref(m_revokeTemporaryAuthorizationCancellable);
142}
143
144Authority::Authority(PolkitAuthority *authority, QObject *parent)
145 : QObject(parent)
146 , d(new Private(this))
147{
148 qRegisterMetaType<PolkitQt1::Authority::Result> ();
149 qRegisterMetaType<PolkitQt1::ActionDescription::List>();
150
151 Q_ASSERT(!s_globalAuthority()->q);
152 s_globalAuthority()->q = this;
153
154 if (authority) {
155 d->pkAuthority = authority;
156 }
157
158 d->init();
159}
160
161Authority::~Authority()
162{
163 if (d->pkAuthority != nullptr) {
164 g_object_unref(d->pkAuthority);
165 }
166
167 delete d;
168}
169
170void Authority::Private::init()
171{
173 QDBusError dbus_error;
174
176 QStringLiteral("polkit_qt_system_bus")));
177
178 m_checkAuthorizationCancellable = g_cancellable_new();
179 m_enumerateActionsCancellable = g_cancellable_new();
180 m_registerAuthenticationAgentCancellable = g_cancellable_new();
181 m_unregisterAuthenticationAgentCancellable = g_cancellable_new();
182 m_authenticationAgentResponseCancellable = g_cancellable_new();
183 m_enumerateTemporaryAuthorizationsCancellable = g_cancellable_new();
184 m_revokeTemporaryAuthorizationsCancellable = g_cancellable_new();
185 m_revokeTemporaryAuthorizationCancellable = g_cancellable_new();
186
187#ifndef POLKIT_QT_1_COMPATIBILITY_MODE
188 GError *gerror = nullptr;
189#endif
190 if (pkAuthority == nullptr) {
191#ifndef POLKIT_QT_1_COMPATIBILITY_MODE
192 pkAuthority = polkit_authority_get_sync(nullptr, &gerror);
193 if (gerror != nullptr) {
194 setError(E_GetAuthority, gerror->message);
195 g_error_free(gerror);
196 return;
197 }
198#else
199 pkAuthority = polkit_authority_get();
200#endif
201 }
202
203 if (pkAuthority == nullptr) {
204#ifdef POLKIT_QT_1_COMPATIBILITY_MODE
206#endif
207 return;
208 }
209
210 // connect changed signal
211 g_signal_connect(G_OBJECT(pkAuthority), "changed", G_CALLBACK(pk_config_changed), NULL);
212
213 // need to listen to NameOwnerChanged
214 dbusSignalAdd("org.freedesktop.DBus", "/", "org.freedesktop.DBus", "NameOwnerChanged");
215
216 QString consoleKitService("org.freedesktop.ConsoleKit");
217 QString consoleKitManagerPath("/org/freedesktop/ConsoleKit/Manager");
218 QString consoleKitManagerInterface("org.freedesktop.ConsoleKit.Manager");
219
220 // first, add signals SeadAdded and SeatRemoved from ConsoleKit Manager
221 dbusSignalAdd(consoleKitService, consoleKitManagerPath, consoleKitManagerInterface, "SeatAdded");
222 dbusSignalAdd(consoleKitService, consoleKitManagerPath, consoleKitManagerInterface, "SeatRemoved");
223
224 // then we need to extract all seats from ConsoleKit
225 QDBusMessage msg = QDBusMessage::createMethodCall(consoleKitService, consoleKitManagerPath, consoleKitManagerInterface, "GetSeats");
226 const QDBusMessage reply = m_systemBus->call(msg);
227
228 if (reply.type() != QDBusMessage::ErrorMessage && !reply.arguments().isEmpty()) {
229 // this method returns a list with present seats
230 QStringList seats;
231 QVariant arg = reply.arguments()[0];
232 if (arg.type() == qMetaTypeId<QDBusArgument>()) {
233 arg.value<QDBusArgument>() >> seats;
234 } else {
235 seats = arg.toStringList();
236 }
237 // it can be multiple seats present so connect all their signals
238 Q_FOREACH(const QString &seat, seats) {
239 seatSignalsConnect(seat);
240 }
241 }
242}
243
244void Authority::Private::setError(Authority::ErrorCode code, const QString &details, bool recover)
245{
246 if (recover) {
247 init();
248 }
249 m_lastError = code;
250 m_errorDetails = details;
251 m_hasError = true;
252}
253
254void Authority::Private::seatSignalsConnect(const QString &seat)
255{
256 QString consoleKitService("org.freedesktop.ConsoleKit");
257 QString consoleKitSeatInterface("org.freedesktop.ConsoleKit.Seat");
258 // we want to connect to all slots of the seat
259 dbusSignalAdd(consoleKitService, seat, consoleKitSeatInterface, "DeviceAdded");
260 dbusSignalAdd(consoleKitService, seat, consoleKitSeatInterface, "DeviceRemoved");
261 dbusSignalAdd(consoleKitService, seat, consoleKitSeatInterface, "SessionAdded");
262 dbusSignalAdd(consoleKitService, seat, consoleKitSeatInterface, "SessionRemoved");
263 dbusSignalAdd(consoleKitService, seat, consoleKitSeatInterface, "ActiveSessionChanged");
264}
265
266void Authority::Private::dbusSignalAdd(const QString &service, const QString &path, const QString &interface, const QString &name)
267{
268 // FIXME: This code seems to be nonfunctional - it needs to be fixed somewhere (is it Qt BUG?)
269 m_systemBus->connect(service, path, interface, name, q, SLOT(dbusFilter(QDBusMessage)));
270}
271
272void Authority::Private::dbusFilter(const QDBusMessage &message)
273{
274 if (message.type() == QDBusMessage::SignalMessage) {
275 Q_EMIT q->consoleKitDBChanged();
276
277 // TODO: Test this with the multiseat support
278 if (message.member() == "SeatAdded") {
279 seatSignalsConnect(message.arguments()[0].value<QDBusObjectPath>().path());
280 }
281 }
282}
283
285{
286 return d->m_hasError;
287}
288
290{
291 return d->m_lastError;
292}
293
295{
296 if (d->m_lastError == E_None) {
297 return QString();
298 } else {
299 return d->m_errorDetails;
300 }
301}
302
304{
305 d->m_hasError = false;
306 d->m_lastError = E_None;
307}
308
309PolkitDetails* Authority::Private::convertDetailsMap(const DetailsMap &details)
310{
311 if (details.empty())
312 return nullptr;
313
314 auto ret = polkit_details_new();
315
316 for (const auto &entry: details.toStdMap()) {
317 const auto &key = entry.first;
318 const auto &value = entry.second;
319
320 polkit_details_insert(ret, key.toUtf8().constData(), value.toUtf8().data());
321 }
322
323 return ret;
324}
325
326void Authority::Private::pk_config_changed()
327{
329}
330
331PolkitAuthority *Authority::polkitAuthority() const
332{
333 return d->pkAuthority;
334}
335
337{
338 PolkitAuthorizationResult *pk_result;
339 GError *error = nullptr;
340
341 if (Authority::instance()->hasError()) {
342 return Unknown;
343 }
344
345 if (!subject.isValid()) {
346 d->setError(E_WrongSubject);
347 return Unknown;
348 }
349
350 auto pk_details = Authority::Private::convertDetailsMap(details);
351
352 pk_result = polkit_authority_check_authorization_sync(d->pkAuthority,
353 subject.subject(),
354 actionId.toLatin1().data(),
355 pk_details,
356 (PolkitCheckAuthorizationFlags)(int)flags,
357 nullptr,
358 &error);
359
360 if (pk_details) {
361 g_object_unref(pk_details);
362 }
363
364 if (error != nullptr) {
365 d->setError(E_CheckFailed, error->message);
366 g_error_free(error);
367 return Unknown;
368 }
369
370 if (!pk_result) {
371 d->setError(E_UnknownResult);
372 return Unknown;
373 } else {
374 Authority::Result res = polkitResultToResult(pk_result);
375 g_object_unref(pk_result);
376 return res;
377 }
378}
379
381{
382 return checkAuthorizationSyncWithDetails(actionId, subject, flags, DetailsMap());
383}
384
385void Authority::checkAuthorizationWithDetails(const QString &actionId, const Subject &subject, AuthorizationFlags flags, const DetailsMap &details)
386{
387 if (Authority::instance()->hasError()) {
388 return;
389 }
390
391 if (!subject.isValid()) {
392 d->setError(E_WrongSubject);
393 return;
394 }
395
396 auto pk_details = Authority::Private::convertDetailsMap(details);
397
398 polkit_authority_check_authorization(d->pkAuthority,
399 subject.subject(),
400 actionId.toLatin1().data(),
401 pk_details,
402 (PolkitCheckAuthorizationFlags)(int)flags,
403 d->m_checkAuthorizationCancellable,
404 d->checkAuthorizationCallback, this);
405
406 if (pk_details) {
407 g_object_unref(pk_details);
408 }
409}
410
411void Authority::checkAuthorization(const QString &actionId, const Subject &subject, AuthorizationFlags flags)
412{
413 checkAuthorizationWithDetails(actionId, subject, flags, DetailsMap());
414}
415
416void Authority::Private::checkAuthorizationCallback(GObject *object, GAsyncResult *result, gpointer user_data)
417{
418 Authority *authority = (Authority *) user_data;
419
420 Q_ASSERT(authority != nullptr);
421
422 GError *error = nullptr;
423 PolkitAuthorizationResult *pkResult = polkit_authority_check_authorization_finish((PolkitAuthority *) object, result, &error);
424
425 if (error != nullptr) {
426 // We don't want to set error if this is cancellation of some action
427 if (error->code != 1) {
428 authority->d->setError(E_CheckFailed, error->message);
429 }
430 g_error_free(error);
431 return;
432 }
433 if (pkResult != nullptr) {
434 Q_EMIT authority->checkAuthorizationFinished(polkitResultToResult(pkResult));
435 g_object_unref(pkResult);
436 } else {
437 authority->d->setError(E_UnknownResult);
438 }
439}
440
442{
443 if (!g_cancellable_is_cancelled(d->m_checkAuthorizationCancellable)) {
444 g_cancellable_cancel(d->m_checkAuthorizationCancellable);
445 }
446}
447
449{
450 if (Authority::instance()->hasError()) {
452 }
453
454 GError *error = nullptr;
455
456 GList *glist = polkit_authority_enumerate_actions_sync(d->pkAuthority,
457 nullptr,
458 &error);
459
460 if (error != nullptr) {
461 d->setError(E_EnumFailed, error->message);
462 g_error_free(error);
464 }
465
466 return actionsToListAndFree(glist);
467}
468
470{
471 if (Authority::instance()->hasError()) {
472 return;
473 }
474
475 polkit_authority_enumerate_actions(d->pkAuthority,
476 d->m_enumerateActionsCancellable,
477 d->enumerateActionsCallback,
479}
480
481void Authority::Private::enumerateActionsCallback(GObject *object, GAsyncResult *result, gpointer user_data)
482{
483 Authority *authority = (Authority *) user_data;
484 Q_ASSERT(authority != nullptr);
485 GError *error = nullptr;
486 GList *list = polkit_authority_enumerate_actions_finish((PolkitAuthority *) object, result, &error);
487 if (error != nullptr) {
488 // We don't want to set error if this is cancellation of some action
489 if (error->code != 1) {
490 authority->d->setError(E_EnumFailed, error->message);
491 }
492 g_error_free(error);
493 return;
494 }
495
496 Q_EMIT authority->enumerateActionsFinished(actionsToListAndFree(list));
497}
498
500{
501 if (!g_cancellable_is_cancelled(d->m_enumerateActionsCancellable)) {
502 g_cancellable_cancel(d->m_enumerateActionsCancellable);
503 }
504}
505
506bool Authority::registerAuthenticationAgentSync(const Subject &subject, const QString &locale, const QString &objectPath)
507{
508 if (Authority::instance()->hasError()) {
509 return false;
510 }
511
512 gboolean result;
513 GError *error = nullptr;
514
515 if (!subject.isValid()) {
516 d->setError(E_WrongSubject);
517 return false;
518 }
519
520 result = polkit_authority_register_authentication_agent_sync(d->pkAuthority,
521 subject.subject(), locale.toLatin1().data(),
522 objectPath.toLatin1().data(), nullptr, &error);
523
524 if (error) {
525 d->setError(E_RegisterFailed, error->message);
526 g_error_free(error);
527 return false;
528 }
529
530 return result;
531}
532
533void Authority::registerAuthenticationAgent(const Subject &subject, const QString &locale, const QString &objectPath)
534{
535 if (Authority::instance()->hasError()) {
536 return;
537 }
538
539 if (!subject.isValid()) {
540 d->setError(E_WrongSubject);
541 return;
542 }
543
544 polkit_authority_register_authentication_agent(d->pkAuthority,
545 subject.subject(),
546 locale.toLatin1().data(),
547 objectPath.toLatin1().data(),
548 d->m_registerAuthenticationAgentCancellable,
549 d->registerAuthenticationAgentCallback,
550 this);
551}
552
553void Authority::Private::registerAuthenticationAgentCallback(GObject *object, GAsyncResult *result, gpointer user_data)
554{
555 Authority *authority = (Authority *) user_data;
556 Q_ASSERT(authority != nullptr);
557 GError *error = nullptr;
558 bool res = polkit_authority_register_authentication_agent_finish((PolkitAuthority *) object, result, &error);
559 if (error != nullptr) {
560 // We don't want to set error if this is cancellation of some action
561 if (error->code != 1) {
562 authority->d->setError(E_EnumFailed , error->message);
563 }
564 g_error_free(error);
565 return;
566 }
567
569}
570
572{
573 if (!g_cancellable_is_cancelled(d->m_registerAuthenticationAgentCancellable)) {
574 g_cancellable_cancel(d->m_registerAuthenticationAgentCancellable);
575 }
576}
577
578bool Authority::unregisterAuthenticationAgentSync(const Subject &subject, const QString &objectPath)
579{
580 if (d->pkAuthority) {
581 return false;
582 }
583
584 if (!subject.isValid()) {
585 d->setError(E_WrongSubject);
586 return false;
587 }
588
589 GError *error = nullptr;
590
591 bool result = polkit_authority_unregister_authentication_agent_sync(d->pkAuthority,
592 subject.subject(),
593 objectPath.toUtf8().data(),
594 nullptr,
595 &error);
596
597 if (error != nullptr) {
598 d->setError(E_UnregisterFailed, error->message);
599 g_error_free(error);
600 return false;
601 }
602
603 return result;
604}
605
606void Authority::unregisterAuthenticationAgent(const Subject &subject, const QString &objectPath)
607{
608 if (Authority::instance()->hasError()) {
609 return;
610 }
611
612 if (!subject.isValid()) {
613 d->setError(E_WrongSubject);
614 return;
615 }
616
617 polkit_authority_unregister_authentication_agent(d->pkAuthority,
618 subject.subject(),
619 objectPath.toUtf8().data(),
620 d->m_unregisterAuthenticationAgentCancellable,
621 d->unregisterAuthenticationAgentCallback,
622 this);
623}
624
625void Authority::Private::unregisterAuthenticationAgentCallback(GObject *object, GAsyncResult *result, gpointer user_data)
626{
627 Authority *authority = (Authority *) user_data;
628 Q_ASSERT(authority);
629 GError *error = nullptr;
630 bool res = polkit_authority_unregister_authentication_agent_finish((PolkitAuthority *) object, result, &error);
631 if (error != nullptr) {
632 // We don't want to set error if this is cancellation of some action
633 if (error->code != 1) {
634 authority->d->setError(E_UnregisterFailed, error->message);
635 }
636 g_error_free(error);
637 return;
638 }
639
641}
642
644{
645 if (!g_cancellable_is_cancelled(d->m_unregisterAuthenticationAgentCancellable)) {
646 g_cancellable_cancel(d->m_unregisterAuthenticationAgentCancellable);
647 }
648}
649
651{
652 if (Authority::instance()->hasError()) {
653 return false;
654 }
655
656 if (cookie.isEmpty() || !identity.isValid()) {
657 d->setError(E_CookieOrIdentityEmpty);
658 return false;
659 }
660
661 GError *error = nullptr;
662
663 bool result = polkit_authority_authentication_agent_response_sync(d->pkAuthority,
664 cookie.toUtf8().data(),
665 identity.identity(),
666 nullptr,
667 &error);
668 if (error != nullptr) {
669 d->setError(E_AgentResponseFailed, error->message);
670 g_error_free(error);
671 return false;
672 }
673
674 return result;
675}
676
677void Authority::authenticationAgentResponse(const QString &cookie, const Identity &identity)
678{
679 if (Authority::instance()->hasError()) {
680 return;
681 }
682
683 if (cookie.isEmpty() || !identity.isValid()) {
684 d->setError(E_CookieOrIdentityEmpty);
685 return;
686 }
687
688 polkit_authority_authentication_agent_response(d->pkAuthority,
689 cookie.toUtf8().data(),
690 identity.identity(),
691 d->m_authenticationAgentResponseCancellable,
692 d->authenticationAgentResponseCallback,
693 this);
694}
695
696void Authority::Private::authenticationAgentResponseCallback(GObject *object, GAsyncResult *result, gpointer user_data)
697{
698 Authority *authority = (Authority *) user_data;
699 Q_ASSERT(authority);
700 GError *error = nullptr;
701 bool res = polkit_authority_authentication_agent_response_finish((PolkitAuthority *) object, result, &error);
702 if (error != nullptr) {
703 // We don't want to set error if this is cancellation of some action
704 if (error->code != 1) {
705 authority->d->setError(E_AgentResponseFailed, error->message);
706 }
707 g_error_free(error);
708 return;
709 }
710
712}
713
715{
716 if (!g_cancellable_is_cancelled(d->m_authenticationAgentResponseCancellable)) {
717 g_cancellable_cancel(d->m_authenticationAgentResponseCancellable);
718 }
719}
720
722{
724
725 GError *error = nullptr;
726 GList *glist = polkit_authority_enumerate_temporary_authorizations_sync(d->pkAuthority,
727 subject.subject(),
728 nullptr,
729 &error);
730 if (error != nullptr) {
731 d->setError(E_EnumFailed, error->message);
732 g_error_free(error);
733 return result;
734 }
735
736 GList *glist2;
737 for (glist2 = glist; glist2 != nullptr; glist2 = g_list_next(glist2)) {
738 result.append(TemporaryAuthorization((PolkitTemporaryAuthorization *) glist2->data));
739 g_object_unref(glist2->data);
740 }
741
742 g_list_free(glist);
743
744 return result;
745}
746
747void Authority::Private::enumerateTemporaryAuthorizationsCallback(GObject *object, GAsyncResult *result, gpointer user_data)
748{
749 Authority *authority = (Authority *) user_data;
750 Q_ASSERT(authority);
751 GError *error = nullptr;
752
753 GList *glist = polkit_authority_enumerate_temporary_authorizations_finish((PolkitAuthority *) object, result, &error);
754
755 if (error != nullptr) {
756 // We don't want to set error if this is cancellation of some action
757 if (error->code != 1) {
758 authority->d->setError(E_EnumFailed, error->message);
759 }
760 g_error_free(error);
761 return;
762 }
763 TemporaryAuthorization::List res;
764 GList *glist2;
765 for (glist2 = glist; glist2 != nullptr; glist2 = g_list_next(glist2)) {
766 res.append(TemporaryAuthorization((PolkitTemporaryAuthorization *) glist2->data));
767 g_object_unref(glist2->data);
768 }
769
770 g_list_free(glist);
771
773}
774
776{
777 if (!g_cancellable_is_cancelled(d->m_enumerateTemporaryAuthorizationsCancellable)) {
778 g_cancellable_cancel(d->m_enumerateTemporaryAuthorizationsCancellable);
779 }
780}
781
783{
784 bool result;
785 if (Authority::instance()->hasError()) {
786 return false;
787 }
788
789 GError *error = nullptr;
790 result = polkit_authority_revoke_temporary_authorizations_sync(d->pkAuthority,
791 subject.subject(),
792 nullptr,
793 &error);
794 if (error != nullptr) {
795 d->setError(E_RevokeFailed, error->message);
796 g_error_free(error);
797 return false;
798 }
799 return result;
800}
801
803{
804 if (Authority::instance()->hasError()) {
805 return;
806 }
807
808 polkit_authority_revoke_temporary_authorizations(d->pkAuthority,
809 subject.subject(),
810 d->m_revokeTemporaryAuthorizationsCancellable,
811 d->revokeTemporaryAuthorizationsCallback,
812 this);
813}
814
815void Authority::Private::revokeTemporaryAuthorizationsCallback(GObject *object, GAsyncResult *result, gpointer user_data)
816{
817 Authority *authority = (Authority *) user_data;
818 Q_ASSERT(authority != nullptr);
819 GError *error = nullptr;
820
821 bool res = polkit_authority_revoke_temporary_authorizations_finish((PolkitAuthority *) object, result, &error);
822
823 if (error != nullptr) {
824 // We don't want to set error if this is cancellation of some action
825 if (error->code != 1) {
826 authority->d->setError(E_RevokeFailed, error->message);
827 }
828 g_error_free(error);
829 return;
830 }
831
833}
834
836{
837 if (!g_cancellable_is_cancelled(d->m_revokeTemporaryAuthorizationsCancellable)) {
838 g_cancellable_cancel(d->m_revokeTemporaryAuthorizationsCancellable);
839 }
840}
841
843{
844 bool result;
845 if (Authority::instance()->hasError()) {
846 return false;
847 }
848
849 GError *error = nullptr;
850 result = polkit_authority_revoke_temporary_authorization_by_id_sync(d->pkAuthority,
851 id.toUtf8().data(),
852 nullptr,
853 &error);
854 if (error != nullptr) {
855 d->setError(E_RevokeFailed, error->message);
856 g_error_free(error);
857 return false;
858 }
859 return result;
860}
861
863{
864 if (Authority::instance()->hasError()) {
865 return;
866 }
867
868 polkit_authority_revoke_temporary_authorization_by_id(d->pkAuthority,
869 id.toUtf8().data(),
870 d->m_revokeTemporaryAuthorizationCancellable,
871 d->revokeTemporaryAuthorizationCallback,
872 this);
873}
874
875void Authority::Private::revokeTemporaryAuthorizationCallback(GObject *object, GAsyncResult *result, gpointer user_data)
876{
877 Authority *authority = (Authority *) user_data;
878 Q_ASSERT(authority != nullptr);
879 GError *error = nullptr;
880
881 bool res = polkit_authority_revoke_temporary_authorization_by_id_finish((PolkitAuthority *) object, result, &error);
882
883 if (error != nullptr) {
884 // We don't want to set error if this is cancellation of some action
885 if (error->code != 1) {
886 authority->d->setError(E_RevokeFailed, error->message);
887 }
888 g_error_free(error);
889 return;
890 }
891
893}
894
896{
897 if (!g_cancellable_is_cancelled(d->m_revokeTemporaryAuthorizationCancellable)) {
898 g_cancellable_cancel(d->m_revokeTemporaryAuthorizationCancellable);
899 }
900}
901
902}
903
904#include "moc_polkitqt1-authority.cpp"
Convenience class for Qt/KDE applications.
bool authenticationAgentResponseSync(const QString &cookie, const PolkitQt1::Identity &identity)
Provide response that identity successfully authenticated for the authentication request identified b...
bool unregisterAuthenticationAgentSync(const Subject &subject, const QString &objectPath)
Unregisters an Authentication agent.
@ Yes
The subject is authorized for the specified action
@ Challenge
The subject is authorized if more information is provided.
@ No
The subject is not authorized for the specified action
TemporaryAuthorization::List enumerateTemporaryAuthorizationsSync(const Subject &subject)
Retrieves all temporary action that applies to subject.
void authenticationAgentResponse(const QString &cookie, const Identity &identity)
Provide response that identity successfully authenticated for the authentication request identified b...
void unregisterAuthenticationAgent(const Subject &subject, const QString &objectPath)
Unregisters an Authentication agent.
void unregisterAuthenticationAgentCancel()
This method can be used to cancel the unregistration of the authentication agent.
void registerAuthenticationAgentFinished(bool)
This signal is emitted when asynchronous method registerAuthenticationAgent finishes.
void registerAuthenticationAgentCancel()
This method can be used to cancel the registration of the authentication agent.
Result checkAuthorizationSyncWithDetails(const QString &actionId, const Subject &subject, AuthorizationFlags flags, const DetailsMap &details)
This function does the same as checkAuthorizationSync(const QString&,const Subject&,...
void revokeTemporaryAuthorizationsFinished(bool)
This signal is emitted when asynchronous method revokeTemporaryAuthorizations finishes.
PolkitAuthority * polkitAuthority() const
Returns the current instance of PolkitAuthority.
void clearError()
Use this method to clear the error message.
bool revokeTemporaryAuthorizationSync(const QString &id)
Revokes temporary authorization by id.
void enumerateActionsCancel()
This method can be used to cancel enumeration of actions.
void enumerateActions()
Asynchronously retrieves all registered actions.
void configChanged()
This signal will be emitted when a configuration file gets changed (e.g.
void checkAuthorizationCancel()
This method can be used to cancel last authorization check.
Result checkAuthorizationSync(const QString &actionId, const Subject &subject, AuthorizationFlags flags)
Synchronous version of the checkAuthorization method.
void revokeTemporaryAuthorizationCancel()
This method can be used to cancel the method revokeTemporaryAuthorizationAsync.
void enumerateTemporaryAuthorizationsFinished(PolkitQt1::TemporaryAuthorization::List)
This signal is emitted when asynchronous method enumerateTemporaryAuthorizations finishes.
static Authority * instance(PolkitAuthority *authority=nullptr)
Returns the instance of Authority.
void unregisterAuthenticationAgentFinished(bool)
This signal is emitted when asynchronous method unregisterAuthenticationAgent finishes.
const QString errorDetails() const
Get detail information about error that occurred.
void enumerateActionsFinished(PolkitQt1::ActionDescription::List)
This signal is emitted when asynchronous method enumerateActions finishes.
void revokeTemporaryAuthorizationFinished(bool)
This signal is emitted when asynchronous method revokeTemporaryAuthorization finishes.
void checkAuthorizationWithDetails(const QString &actionId, const Subject &subject, AuthorizationFlags flags, const DetailsMap &details)
This function does the same as checkAuthorization(const QString&, constSubject&, AuthorizationFlags),...
bool revokeTemporaryAuthorizationsSync(const Subject &subject)
Revokes all temporary authorizations that applies to subject.
void revokeTemporaryAuthorizationsCancel()
This method can be used to cancel the method revokeTemporaryAuthorizationsAsync.
void checkAuthorization(const QString &actionId, const Subject &subject, AuthorizationFlags flags)
This function should be used by mechanisms (e.g.: helper applications).
void revokeTemporaryAuthorization(const QString &id)
Revokes temporary authorization by id.
void authenticationAgentResponseFinished(bool)
This signal is emitted when asynchronous method authenticationAgentResponse finishes.
void authenticationAgentResponseCancel()
This method can be used to cancel the authenticationAgentResponseAsync method.
void registerAuthenticationAgent(const Subject &subject, const QString &locale, const QString &objectPath)
Registers an authentication agent.
bool hasError() const
You should always call this method after every action.
void revokeTemporaryAuthorizations(const Subject &subject)
Revokes all temporary authorizations that applies to subject.
void enumerateTemporaryAuthorizationsCancel()
This method can be used to cancel the enumerateTemporaryAuthorizationsAsync method.
ErrorCode
Error codes for the authority class.
@ E_AgentResponseFailed
Response of auth agent failed.
@ E_CheckFailed
Authority check failed.
@ E_UnknownResult
Action returned unknown result.
@ E_CookieOrIdentityEmpty
Cookie or polkitqt1-identity.handled to the action is empty.
@ E_None
No error occurred.
@ E_WrongSubject
Wrong or empty subject was given.
@ E_GetAuthority
Authority cannot be obtained.
@ E_RegisterFailed
Registration of authentication agent failed.
@ E_EnumFailed
Enumerating actions failed.
@ E_RevokeFailed
Revoke temporary authorizations failed.
@ E_UnregisterFailed
Unregistration of authentication agent failed.
void checkAuthorizationFinished(PolkitQt1::Authority::Result)
This signal is emitted when asynchronous method checkAuthorization finishes.
ActionDescription::List enumerateActionsSync()
Synchronously retrieves all registered actions.
bool registerAuthenticationAgentSync(const Subject &subject, const QString &locale, const QString &objectPath)
Registers an authentication agent.
Abstract class representing identities.
PolkitIdentity * identity() const
Gets PolkitIdentity object.
This class represents PolicyKit subjects.
PolkitSubject * subject() const
Gets PolkitSubject object.
This class represents PolicyKit temporary authorization.
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
char * data()
QDBusMessage call(const QDBusMessage &message, QDBus::CallMode mode, int timeout) const const
QDBusConnection connectToBus(BusType type, const QString &name)
QList< QVariant > arguments() const const
QDBusMessage createMethodCall(const QString &service, const QString &path, const QString &interface, const QString &method)
QString member() const const
MessageType type() const const
QString path() const const
void append(QList< T > &&value)
bool empty() const const
std::map< Key, T > toStdMap() &&
Q_EMITQ_EMIT
QObject * parent() const const
bool isEmpty() const const
QByteArray toLatin1() const const
QByteArray toUtf8() const const
Type type() const const
QStringList toStringList() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:47:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.