PolkitQt-1

polkitqt1-authority.cpp
1 /*
2  This file is part of the Polkit-qt project
3  SPDX-FileCopyrightText: 2009 Daniel Nicoletti <[email protected]>
4  SPDX-FileCopyrightText: 2009 Dario Freddi <[email protected]>
5  SPDX-FileCopyrightText: 2009 Jaroslav Reznik <[email protected]>
6  SPDX-FileCopyrightText: 2009 Radek Novacek <[email protected]>
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 
19 namespace PolkitQt1
20 {
21 
22 class AuthorityHelper
23 {
24 public:
25  AuthorityHelper() : q(nullptr) {}
26  ~AuthorityHelper() {
27  delete q;
28  }
29  Authority *q;
30 };
31 
32 Q_GLOBAL_STATIC(AuthorityHelper, s_globalAuthority)
33 
34 Authority *Authority::instance(PolkitAuthority *authority)
35 {
36  if (!s_globalAuthority()->q) {
37  new Authority(authority);
38  }
39 
40  return s_globalAuthority()->q;
41 }
42 
43 Authority::Result polkitResultToResult(PolkitAuthorizationResult *result)
44 {
45  if (polkit_authorization_result_get_is_challenge(result)) {
46  return Authority::Challenge;
47  } else if (polkit_authorization_result_get_is_authorized(result)) {
48  return Authority::Yes;
49  } else {
50  return Authority::No;
51  }
52 }
53 
54 ActionDescription::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 
67 class Q_DECL_HIDDEN Authority::Private
68 {
69 public:
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 
131 Authority::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 
144 Authority::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 
161 Authority::~Authority()
162 {
163  if (d->pkAuthority != nullptr) {
164  g_object_unref(d->pkAuthority);
165  }
166 
167  delete d;
168 }
169 
170 void 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
205  (E_GetAuthority);
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  QString consoleKitSeatInterface("org.freedesktop.ConsoleKit.Seat");
220 
221  // first, add signals SeadAdded and SeatRemoved from ConsoleKit Manager
222  dbusSignalAdd(consoleKitService, consoleKitManagerPath, consoleKitManagerInterface, "SeatAdded");
223  dbusSignalAdd(consoleKitService, consoleKitManagerPath, consoleKitManagerInterface, "SeatRemoved");
224 
225  // then we need to extract all seats from ConsoleKit
226  QDBusMessage msg = QDBusMessage::createMethodCall(consoleKitService, consoleKitManagerPath, consoleKitManagerInterface, "GetSeats");
227  const QDBusMessage reply = m_systemBus->call(msg);
228 
229  if (reply.type() != QDBusMessage::ErrorMessage && !reply.arguments().isEmpty()) {
230  // this method returns a list with present seats
231  QStringList seats;
232  QVariant arg = reply.arguments()[0];
233  if (arg.type() == qMetaTypeId<QDBusArgument>()) {
234  arg.value<QDBusArgument>() >> seats;
235  } else {
236  seats = arg.toStringList();
237  }
238  // it can be multiple seats present so connect all their signals
239  Q_FOREACH(const QString &seat, seats) {
240  seatSignalsConnect(seat);
241  }
242  }
243 }
244 
245 void Authority::Private::setError(Authority::ErrorCode code, const QString &details, bool recover)
246 {
247  if (recover) {
248  init();
249  }
250  m_lastError = code;
251  m_errorDetails = details;
252  m_hasError = true;
253 }
254 
255 void Authority::Private::seatSignalsConnect(const QString &seat)
256 {
257  QString consoleKitService("org.freedesktop.ConsoleKit");
258  QString consoleKitSeatInterface("org.freedesktop.ConsoleKit.Seat");
259  // we want to connect to all slots of the seat
260  dbusSignalAdd(consoleKitService, seat, consoleKitSeatInterface, "DeviceAdded");
261  dbusSignalAdd(consoleKitService, seat, consoleKitSeatInterface, "DeviceRemoved");
262  dbusSignalAdd(consoleKitService, seat, consoleKitSeatInterface, "SessionAdded");
263  dbusSignalAdd(consoleKitService, seat, consoleKitSeatInterface, "SessionRemoved");
264  dbusSignalAdd(consoleKitService, seat, consoleKitSeatInterface, "ActiveSessionChanged");
265 }
266 
267 void Authority::Private::dbusSignalAdd(const QString &service, const QString &path, const QString &interface, const QString &name)
268 {
269  // FIXME: This code seems to be nonfunctional - it needs to be fixed somewhere (is it Qt BUG?)
270  m_systemBus->connect(service, path, interface, name, q, SLOT(dbusFilter(QDBusMessage)));
271 }
272 
273 void Authority::Private::dbusFilter(const QDBusMessage &message)
274 {
275  if (message.type() == QDBusMessage::SignalMessage) {
276  Q_EMIT q->consoleKitDBChanged();
277 
278  // TODO: Test this with the multiseat support
279  if (message.member() == "SeatAdded") {
280  seatSignalsConnect(message.arguments()[0].value<QDBusObjectPath>().path());
281  }
282  }
283 }
284 
286 {
287  return d->m_hasError;
288 }
289 
291 {
292  return d->m_lastError;
293 }
294 
296 {
297  if (d->m_lastError == E_None) {
298  return QString();
299  } else {
300  return d->m_errorDetails;
301  }
302 }
303 
305 {
306  d->m_hasError = false;
307  d->m_lastError = E_None;
308 }
309 
310 PolkitDetails* Authority::Private::convertDetailsMap(const DetailsMap &details)
311 {
312  if (details.empty())
313  return nullptr;
314 
315  auto ret = polkit_details_new();
316 
317  for (const auto &entry: details.toStdMap()) {
318  const auto &key = entry.first;
319  const auto &value = entry.second;
320 
321  polkit_details_insert(ret, key.toUtf8().constData(), value.toUtf8().data());
322  }
323 
324  return ret;
325 }
326 
327 void Authority::Private::pk_config_changed()
328 {
330 }
331 
332 PolkitAuthority *Authority::polkitAuthority() const
333 {
334  return d->pkAuthority;
335 }
336 
338 {
339  PolkitAuthorizationResult *pk_result;
340  GError *error = nullptr;
341 
342  if (Authority::instance()->hasError()) {
343  return Unknown;
344  }
345 
346  if (!subject.isValid()) {
347  d->setError(E_WrongSubject);
348  return Unknown;
349  }
350 
351  auto pk_details = Authority::Private::convertDetailsMap(details);
352 
353  pk_result = polkit_authority_check_authorization_sync(d->pkAuthority,
354  subject.subject(),
355  actionId.toLatin1().data(),
356  pk_details,
357  (PolkitCheckAuthorizationFlags)(int)flags,
358  nullptr,
359  &error);
360 
361  if (pk_details) {
362  g_object_unref(pk_details);
363  }
364 
365  if (error != nullptr) {
366  d->setError(E_CheckFailed, error->message);
367  g_error_free(error);
368  return Unknown;
369  }
370 
371  if (!pk_result) {
372  d->setError(E_UnknownResult);
373  return Unknown;
374  } else {
375  Authority::Result res = polkitResultToResult(pk_result);
376  g_object_unref(pk_result);
377  return res;
378  }
379 }
380 
382 {
383  return checkAuthorizationSyncWithDetails(actionId, subject, flags, DetailsMap());
384 }
385 
386 void Authority::checkAuthorizationWithDetails(const QString &actionId, const Subject &subject, AuthorizationFlags flags, const DetailsMap &details)
387 {
388  if (Authority::instance()->hasError()) {
389  return;
390  }
391 
392  if (!subject.isValid()) {
393  d->setError(E_WrongSubject);
394  return;
395  }
396 
397  auto pk_details = Authority::Private::convertDetailsMap(details);
398 
399  polkit_authority_check_authorization(d->pkAuthority,
400  subject.subject(),
401  actionId.toLatin1().data(),
402  pk_details,
403  (PolkitCheckAuthorizationFlags)(int)flags,
404  d->m_checkAuthorizationCancellable,
405  d->checkAuthorizationCallback, this);
406 
407  if (pk_details) {
408  g_object_unref(pk_details);
409  }
410 }
411 
412 void Authority::checkAuthorization(const QString &actionId, const Subject &subject, AuthorizationFlags flags)
413 {
414  checkAuthorizationWithDetails(actionId, subject, flags, DetailsMap());
415 }
416 
417 void Authority::Private::checkAuthorizationCallback(GObject *object, GAsyncResult *result, gpointer user_data)
418 {
419  Authority *authority = (Authority *) user_data;
420 
421  Q_ASSERT(authority != nullptr);
422 
423  GError *error = nullptr;
424  PolkitAuthorizationResult *pkResult = polkit_authority_check_authorization_finish((PolkitAuthority *) object, result, &error);
425 
426  if (error != nullptr) {
427  // We don't want to set error if this is cancellation of some action
428  if (error->code != 1) {
429  authority->d->setError(E_CheckFailed, error->message);
430  }
431  g_error_free(error);
432  return;
433  }
434  if (pkResult != nullptr) {
435  Q_EMIT authority->checkAuthorizationFinished(polkitResultToResult(pkResult));
436  g_object_unref(pkResult);
437  } else {
438  authority->d->setError(E_UnknownResult);
439  }
440 }
441 
443 {
444  if (!g_cancellable_is_cancelled(d->m_checkAuthorizationCancellable)) {
445  g_cancellable_cancel(d->m_checkAuthorizationCancellable);
446  }
447 }
448 
450 {
451  if (Authority::instance()->hasError()) {
452  return ActionDescription::List();
453  }
454 
455  GError *error = nullptr;
456 
457  GList *glist = polkit_authority_enumerate_actions_sync(d->pkAuthority,
458  nullptr,
459  &error);
460 
461  if (error != nullptr) {
462  d->setError(E_EnumFailed, error->message);
463  g_error_free(error);
464  return ActionDescription::List();
465  }
466 
467  return actionsToListAndFree(glist);
468 }
469 
471 {
472  if (Authority::instance()->hasError()) {
473  return;
474  }
475 
476  polkit_authority_enumerate_actions(d->pkAuthority,
477  d->m_enumerateActionsCancellable,
478  d->enumerateActionsCallback,
480 }
481 
482 void Authority::Private::enumerateActionsCallback(GObject *object, GAsyncResult *result, gpointer user_data)
483 {
484  Authority *authority = (Authority *) user_data;
485  Q_ASSERT(authority != nullptr);
486  GError *error = nullptr;
487  GList *list = polkit_authority_enumerate_actions_finish((PolkitAuthority *) object, result, &error);
488  if (error != nullptr) {
489  // We don't want to set error if this is cancellation of some action
490  if (error->code != 1) {
491  authority->d->setError(E_EnumFailed, error->message);
492  }
493  g_error_free(error);
494  return;
495  }
496 
497  Q_EMIT authority->enumerateActionsFinished(actionsToListAndFree(list));
498 }
499 
501 {
502  if (!g_cancellable_is_cancelled(d->m_enumerateActionsCancellable)) {
503  g_cancellable_cancel(d->m_enumerateActionsCancellable);
504  }
505 }
506 
507 bool Authority::registerAuthenticationAgentSync(const Subject &subject, const QString &locale, const QString &objectPath)
508 {
509  if (Authority::instance()->hasError()) {
510  return false;
511  }
512 
513  gboolean result;
514  GError *error = nullptr;
515 
516  if (!subject.isValid()) {
517  d->setError(E_WrongSubject);
518  return false;
519  }
520 
521  result = polkit_authority_register_authentication_agent_sync(d->pkAuthority,
522  subject.subject(), locale.toLatin1().data(),
523  objectPath.toLatin1().data(), nullptr, &error);
524 
525  if (error) {
526  d->setError(E_RegisterFailed, error->message);
527  g_error_free(error);
528  return false;
529  }
530 
531  return result;
532 }
533 
534 void Authority::registerAuthenticationAgent(const Subject &subject, const QString &locale, const QString &objectPath)
535 {
536  if (Authority::instance()->hasError()) {
537  return;
538  }
539 
540  if (!subject.isValid()) {
541  d->setError(E_WrongSubject);
542  return;
543  }
544 
545  polkit_authority_register_authentication_agent(d->pkAuthority,
546  subject.subject(),
547  locale.toLatin1().data(),
548  objectPath.toLatin1().data(),
549  d->m_registerAuthenticationAgentCancellable,
550  d->registerAuthenticationAgentCallback,
551  this);
552 }
553 
554 void Authority::Private::registerAuthenticationAgentCallback(GObject *object, GAsyncResult *result, gpointer user_data)
555 {
556  Authority *authority = (Authority *) user_data;
557  Q_ASSERT(authority != nullptr);
558  GError *error = nullptr;
559  bool res = polkit_authority_register_authentication_agent_finish((PolkitAuthority *) object, result, &error);
560  if (error != nullptr) {
561  // We don't want to set error if this is cancellation of some action
562  if (error->code != 1) {
563  authority->d->setError(E_EnumFailed , error->message);
564  }
565  g_error_free(error);
566  return;
567  }
568 
569  Q_EMIT authority->registerAuthenticationAgentFinished(res);
570 }
571 
573 {
574  if (!g_cancellable_is_cancelled(d->m_registerAuthenticationAgentCancellable)) {
575  g_cancellable_cancel(d->m_registerAuthenticationAgentCancellable);
576  }
577 }
578 
579 bool Authority::unregisterAuthenticationAgentSync(const Subject &subject, const QString &objectPath)
580 {
581  if (d->pkAuthority) {
582  return false;
583  }
584 
585  if (!subject.isValid()) {
586  d->setError(E_WrongSubject);
587  return false;
588  }
589 
590  GError *error = nullptr;
591 
592  bool result = polkit_authority_unregister_authentication_agent_sync(d->pkAuthority,
593  subject.subject(),
594  objectPath.toUtf8().data(),
595  nullptr,
596  &error);
597 
598  if (error != nullptr) {
599  d->setError(E_UnregisterFailed, error->message);
600  g_error_free(error);
601  return false;
602  }
603 
604  return result;
605 }
606 
607 void Authority::unregisterAuthenticationAgent(const Subject &subject, const QString &objectPath)
608 {
609  if (Authority::instance()->hasError()) {
610  return;
611  }
612 
613  if (!subject.isValid()) {
614  d->setError(E_WrongSubject);
615  return;
616  }
617 
618  polkit_authority_unregister_authentication_agent(d->pkAuthority,
619  subject.subject(),
620  objectPath.toUtf8().data(),
621  d->m_unregisterAuthenticationAgentCancellable,
622  d->unregisterAuthenticationAgentCallback,
623  this);
624 }
625 
626 void Authority::Private::unregisterAuthenticationAgentCallback(GObject *object, GAsyncResult *result, gpointer user_data)
627 {
628  Authority *authority = (Authority *) user_data;
629  Q_ASSERT(authority);
630  GError *error = nullptr;
631  bool res = polkit_authority_unregister_authentication_agent_finish((PolkitAuthority *) object, result, &error);
632  if (error != nullptr) {
633  // We don't want to set error if this is cancellation of some action
634  if (error->code != 1) {
635  authority->d->setError(E_UnregisterFailed, error->message);
636  }
637  g_error_free(error);
638  return;
639  }
640 
641  Q_EMIT authority->unregisterAuthenticationAgentFinished(res);
642 }
643 
645 {
646  if (!g_cancellable_is_cancelled(d->m_unregisterAuthenticationAgentCancellable)) {
647  g_cancellable_cancel(d->m_unregisterAuthenticationAgentCancellable);
648  }
649 }
650 
651 bool Authority::authenticationAgentResponseSync(const QString &cookie, const Identity &identity)
652 {
653  if (Authority::instance()->hasError()) {
654  return false;
655  }
656 
657  if (cookie.isEmpty() || !identity.isValid()) {
658  d->setError(E_CookieOrIdentityEmpty);
659  return false;
660  }
661 
662  GError *error = nullptr;
663 
664  bool result = polkit_authority_authentication_agent_response_sync(d->pkAuthority,
665  cookie.toUtf8().data(),
666  identity.identity(),
667  nullptr,
668  &error);
669  if (error != nullptr) {
670  d->setError(E_AgentResponseFailed, error->message);
671  g_error_free(error);
672  return false;
673  }
674 
675  return result;
676 }
677 
678 void Authority::authenticationAgentResponse(const QString &cookie, const Identity &identity)
679 {
680  if (Authority::instance()->hasError()) {
681  return;
682  }
683 
684  if (cookie.isEmpty() || !identity.isValid()) {
685  d->setError(E_CookieOrIdentityEmpty);
686  return;
687  }
688 
689  polkit_authority_authentication_agent_response(d->pkAuthority,
690  cookie.toUtf8().data(),
691  identity.identity(),
692  d->m_authenticationAgentResponseCancellable,
693  d->authenticationAgentResponseCallback,
694  this);
695 }
696 
697 void Authority::Private::authenticationAgentResponseCallback(GObject *object, GAsyncResult *result, gpointer user_data)
698 {
699  Authority *authority = (Authority *) user_data;
700  Q_ASSERT(authority);
701  GError *error = nullptr;
702  bool res = polkit_authority_authentication_agent_response_finish((PolkitAuthority *) object, result, &error);
703  if (error != nullptr) {
704  // We don't want to set error if this is cancellation of some action
705  if (error->code != 1) {
706  authority->d->setError(E_AgentResponseFailed, error->message);
707  }
708  g_error_free(error);
709  return;
710  }
711 
712  Q_EMIT authority->authenticationAgentResponseFinished(res);
713 }
714 
716 {
717  if (!g_cancellable_is_cancelled(d->m_authenticationAgentResponseCancellable)) {
718  g_cancellable_cancel(d->m_authenticationAgentResponseCancellable);
719  }
720 }
721 
723 {
725 
726  GError *error = nullptr;
727  GList *glist = polkit_authority_enumerate_temporary_authorizations_sync(d->pkAuthority,
728  subject.subject(),
729  nullptr,
730  &error);
731  if (error != nullptr) {
732  d->setError(E_EnumFailed, error->message);
733  g_error_free(error);
734  return result;
735  }
736 
737  GList *glist2;
738  for (glist2 = glist; glist2 != nullptr; glist2 = g_list_next(glist2)) {
739  result.append(TemporaryAuthorization((PolkitTemporaryAuthorization *) glist2->data));
740  g_object_unref(glist2->data);
741  }
742 
743  g_list_free(glist);
744 
745  return result;
746 }
747 
748 void Authority::Private::enumerateTemporaryAuthorizationsCallback(GObject *object, GAsyncResult *result, gpointer user_data)
749 {
750  Authority *authority = (Authority *) user_data;
751  Q_ASSERT(authority);
752  GError *error = nullptr;
753 
754  GList *glist = polkit_authority_enumerate_temporary_authorizations_finish((PolkitAuthority *) object, result, &error);
755 
756  if (error != nullptr) {
757  // We don't want to set error if this is cancellation of some action
758  if (error->code != 1) {
759  authority->d->setError(E_EnumFailed, error->message);
760  }
761  g_error_free(error);
762  return;
763  }
764  TemporaryAuthorization::List res;
765  GList *glist2;
766  for (glist2 = glist; glist2 != nullptr; glist2 = g_list_next(glist2)) {
767  res.append(TemporaryAuthorization((PolkitTemporaryAuthorization *) glist2->data));
768  g_object_unref(glist2->data);
769  }
770 
771  g_list_free(glist);
772 
773  Q_EMIT authority->enumerateTemporaryAuthorizationsFinished(res);
774 }
775 
777 {
778  if (!g_cancellable_is_cancelled(d->m_enumerateTemporaryAuthorizationsCancellable)) {
779  g_cancellable_cancel(d->m_enumerateTemporaryAuthorizationsCancellable);
780  }
781 }
782 
784 {
785  bool result;
786  if (Authority::instance()->hasError()) {
787  return false;
788  }
789 
790  GError *error = nullptr;
791  result = polkit_authority_revoke_temporary_authorizations_sync(d->pkAuthority,
792  subject.subject(),
793  nullptr,
794  &error);
795  if (error != nullptr) {
796  d->setError(E_RevokeFailed, error->message);
797  g_error_free(error);
798  return false;
799  }
800  return result;
801 }
802 
804 {
805  if (Authority::instance()->hasError()) {
806  return;
807  }
808 
809  polkit_authority_revoke_temporary_authorizations(d->pkAuthority,
810  subject.subject(),
811  d->m_revokeTemporaryAuthorizationsCancellable,
812  d->revokeTemporaryAuthorizationsCallback,
813  this);
814 }
815 
816 void Authority::Private::revokeTemporaryAuthorizationsCallback(GObject *object, GAsyncResult *result, gpointer user_data)
817 {
818  Authority *authority = (Authority *) user_data;
819  Q_ASSERT(authority != nullptr);
820  GError *error = nullptr;
821 
822  bool res = polkit_authority_revoke_temporary_authorizations_finish((PolkitAuthority *) object, result, &error);
823 
824  if (error != nullptr) {
825  // We don't want to set error if this is cancellation of some action
826  if (error->code != 1) {
827  authority->d->setError(E_RevokeFailed, error->message);
828  }
829  g_error_free(error);
830  return;
831  }
832 
833  Q_EMIT authority->revokeTemporaryAuthorizationsFinished(res);
834 }
835 
837 {
838  if (!g_cancellable_is_cancelled(d->m_revokeTemporaryAuthorizationsCancellable)) {
839  g_cancellable_cancel(d->m_revokeTemporaryAuthorizationsCancellable);
840  }
841 }
842 
844 {
845  bool result;
846  if (Authority::instance()->hasError()) {
847  return false;
848  }
849 
850  GError *error = nullptr;
851  result = polkit_authority_revoke_temporary_authorization_by_id_sync(d->pkAuthority,
852  id.toUtf8().data(),
853  nullptr,
854  &error);
855  if (error != nullptr) {
856  d->setError(E_RevokeFailed, error->message);
857  g_error_free(error);
858  return false;
859  }
860  return result;
861 }
862 
864 {
865  if (Authority::instance()->hasError()) {
866  return;
867  }
868 
869  polkit_authority_revoke_temporary_authorization_by_id(d->pkAuthority,
870  id.toUtf8().data(),
871  d->m_revokeTemporaryAuthorizationCancellable,
872  d->revokeTemporaryAuthorizationCallback,
873  this);
874 }
875 
876 void Authority::Private::revokeTemporaryAuthorizationCallback(GObject *object, GAsyncResult *result, gpointer user_data)
877 {
878  Authority *authority = (Authority *) user_data;
879  Q_ASSERT(authority != nullptr);
880  GError *error = nullptr;
881 
882  bool res = polkit_authority_revoke_temporary_authorization_by_id_finish((PolkitAuthority *) object, result, &error);
883 
884  if (error != nullptr) {
885  // We don't want to set error if this is cancellation of some action
886  if (error->code != 1) {
887  authority->d->setError(E_RevokeFailed, error->message);
888  }
889  g_error_free(error);
890  return;
891  }
892 
893  Q_EMIT authority->revokeTemporaryAuthorizationFinished(res);
894 }
895 
897 {
898  if (!g_cancellable_is_cancelled(d->m_revokeTemporaryAuthorizationCancellable)) {
899  g_cancellable_cancel(d->m_revokeTemporaryAuthorizationCancellable);
900  }
901 }
902 
903 }
904 
905 #include "moc_polkitqt1-authority.cpp"
@ E_AgentResponseFailed
Response of auth agent failed.
void append(const T &value)
This class represents PolicyKit subjects.
void enumerateActions()
Asynchronously retrieves all registered actions.
std::map< Key, T > toStdMap() const const
bool hasError() const
You should always call this method after every action.
Result checkAuthorizationSyncWithDetails(const QString &actionId, const Subject &subject, AuthorizationFlags flags, const DetailsMap &details)
This function does the same as checkAuthorizationSync(const QString&, const Subject&,...
bool empty() const const
void authenticationAgentResponseFinished(bool)
This signal is emitted when asynchronous method authenticationAgentResponse finishes.
Abstract class representing identities.
T value() const const
@ E_RevokeFailed
Revoke temporary authorizations failed.
@ Challenge
The subject is authorized if more information is provided.
QByteArray toLatin1() const const
Convenience class for Qt/KDE applications.
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
bool revokeTemporaryAuthorizationSync(const QString &id)
Revokes temporary authorization by id.
void unregisterAuthenticationAgentFinished(bool)
This signal is emitted when asynchronous method unregisterAuthenticationAgent finishes.
void revokeTemporaryAuthorizationsCancel()
This method can be used to cancel the method revokeTemporaryAuthorizationsAsync.
void authenticationAgentResponse(const QString &cookie, const Identity &identity)
Provide response that identity successfully authenticated for the authentication request identified b...
Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) class ControlPrivate
void clearError()
Use this method to clear the error message.
ActionDescription::List enumerateActionsSync()
Synchronously retrieves all registered actions.
void revokeTemporaryAuthorizationFinished(bool)
This signal is emitted when asynchronous method revokeTemporaryAuthorization finishes.
QList< QVariant > arguments() const const
QVariant::Type type() const const
@ No
The subject is not authorized for the specified action
void registerAuthenticationAgentFinished(bool)
This signal is emitted when asynchronous method registerAuthenticationAgent finishes.
void checkAuthorizationFinished(PolkitQt1::Authority::Result)
This signal is emitted when asynchronous method checkAuthorization finishes.
bool isEmpty() const const
@ E_None
No error occurred.
@ E_RegisterFailed
Registration of authentication agent failed.
QByteArray toUtf8() const const
PolkitSubject * subject() const
Gets PolkitSubject object.
Result checkAuthorizationSync(const QString &actionId, const Subject &subject, AuthorizationFlags flags)
Synchronous version of the checkAuthorization method.
void checkAuthorization(const QString &actionId, const Subject &subject, AuthorizationFlags flags)
This function should be used by mechanisms (e.g.
void revokeTemporaryAuthorizations(const Subject &subject)
Revokes all temporary authorizations that applies to subject.
bool unregisterAuthenticationAgentSync(const Subject &subject, const QString &objectPath)
Unregisters an Authentication agent.
@ E_UnregisterFailed
Unregistration of authentication agent failed.
bool authenticationAgentResponseSync(const QString &cookie, const PolkitQt1::Identity &identity)
Provide response that identity successfully authenticated for the authentication request identified b...
void init(KXmlGuiWindow *window, KgDifficulty *difficulty=nullptr)
bool revokeTemporaryAuthorizationsSync(const Subject &subject)
Revokes all temporary authorizations that applies to subject.
void registerAuthenticationAgentCancel()
This method can be used to cancel the registration of the authentication agent.
void revokeTemporaryAuthorizationCancel()
This method can be used to cancel the method revokeTemporaryAuthorizationAsync.
void revokeTemporaryAuthorization(const QString &id)
Revokes temporary authorization by id.
void revokeTemporaryAuthorizationsFinished(bool)
This signal is emitted when asynchronous method revokeTemporaryAuthorizations finishes.
This class represents PolicyKit temporary authorization.
QDBusMessage createMethodCall(const QString &service, const QString &path, const QString &interface, const QString &method)
QString path() const const
void checkAuthorizationCancel()
This method can be used to cancel last authorization check.
@ E_CookieOrIdentityEmpty
Cookie or polkitqt1-identity.handled to the action is empty.
@ E_EnumFailed
Enumerating actions failed.
static Authority * instance(PolkitAuthority *authority=nullptr)
Returns the instance of Authority.
const QString errorDetails() const
Get detail information about error that occurred.
TemporaryAuthorization::List enumerateTemporaryAuthorizationsSync(const Subject &subject)
Retrieves all temporary action that applies to subject.
@ E_UnknownResult
Action returned unknown result.
QDBusMessage::MessageType type() const const
@ Yes
The subject is authorized for the specified action
QDBusConnection connectToBus(QDBusConnection::BusType type, const QString &name)
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.
ErrorCode
Error codes for the authority class.
ErrorCode lastError() const
Namespace wrapping Polkit-Qt classes.
QStringList toStringList() const const
void enumerateTemporaryAuthorizationsCancel()
This method can be used to cancel the enumerateTemporaryAuthorizationsAsync method.
void checkAuthorizationWithDetails(const QString &actionId, const Subject &subject, AuthorizationFlags flags, const DetailsMap &details)
This function does the same as checkAuthorization(const QString&, const Subject&, AuthorizationFlags)...
bool registerAuthenticationAgentSync(const Subject &subject, const QString &locale, const QString &objectPath)
Registers an authentication agent.
void configChanged()
This signal will be emitted when a configuration file gets changed (e.g.
@ E_WrongSubject
Wrong or empty subject was given.
void enumerateActionsFinished(PolkitQt1::ActionDescription::List)
This signal is emitted when asynchronous method enumerateActions finishes.
void authenticationAgentResponseCancel()
This method can be used to cancel the authenticationAgentResponseAsync method.
PolkitAuthority * polkitAuthority() const
Returns the current instance of PolkitAuthority.
void enumerateActionsCancel()
This method can be used to cancel enumeration of actions.
@ Unknown
Result unknown.
void enumerateTemporaryAuthorizationsFinished(PolkitQt1::TemporaryAuthorization::List)
This signal is emitted when asynchronous method enumerateTemporaryAuthorizations finishes.
QString message
void registerAuthenticationAgent(const Subject &subject, const QString &locale, const QString &objectPath)
Registers an authentication agent.
char * data()
@ E_CheckFailed
Authority check failed.
PolkitIdentity * identity() const
Gets PolkitIdentity object.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Jun 26 2022 04:06:09 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.