Changeset View
Changeset View
Standalone View
Standalone View
src/backends/polkit-1/Polkit1Backend.cpp
1 | /* | 1 | /* | ||
---|---|---|---|---|---|
2 | * Copyright (C) 2008 Nicola Gigante <nicola.gigante@gmail.com> | 2 | * Copyright (C) 2008 Nicola Gigante <nicola.gigante@gmail.com> | ||
3 | * Copyright (C) 2009 Radek Novacek <rnovacek@redhat.com> | 3 | * Copyright (C) 2009 Radek Novacek <rnovacek@redhat.com> | ||
4 | * Copyright (C) 2009-2010 Dario Freddi <drf@kde.org> | 4 | * Copyright (C) 2009-2010 Dario Freddi <drf@kde.org> | ||
5 | * Copyright (C) 2020 David Edmundson <davidedmundson@kde.org> | ||||
5 | * | 6 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | 7 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Lesser General Public License as published by | 8 | * it under the terms of the GNU Lesser General Public License as published by | ||
8 | * the Free Software Foundation; either version 2.1 of the License, or | 9 | * the Free Software Foundation; either version 2.1 of the License, or | ||
9 | * (at your option) any later version. | 10 | * (at your option) any later version. | ||
10 | * | 11 | * | ||
11 | * This program is distributed in the hope that it will be useful, | 12 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Line(s) | |||||
56 | 57 | | |||
57 | PolkitQt1::Authority::Result PolkitResultEventLoop::result() const | 58 | PolkitQt1::Authority::Result PolkitResultEventLoop::result() const | ||
58 | { | 59 | { | ||
59 | return m_result; | 60 | return m_result; | ||
60 | } | 61 | } | ||
61 | 62 | | |||
62 | Polkit1Backend::Polkit1Backend() | 63 | Polkit1Backend::Polkit1Backend() | ||
63 | : AuthBackend() | 64 | : AuthBackend() | ||
64 | , m_flyingActions(false) | | |||
65 | { | 65 | { | ||
66 | setCapabilities(AuthorizeFromHelperCapability | CheckActionExistenceCapability | PreAuthActionCapability); | 66 | setCapabilities(AuthorizeFromHelperCapability | CheckActionExistenceCapability | PreAuthActionCapability); | ||
67 | 67 | | |||
68 | // Setup useful signals | 68 | // Setup useful signals | ||
69 | connect(PolkitQt1::Authority::instance(), SIGNAL(configChanged()), | 69 | connect(PolkitQt1::Authority::instance(), SIGNAL(configChanged()), | ||
70 | this, SLOT(checkForResultChanged())); | 70 | this, SLOT(checkForResultChanged())); | ||
71 | connect(PolkitQt1::Authority::instance(), SIGNAL(consoleKitDBChanged()), | 71 | connect(PolkitQt1::Authority::instance(), SIGNAL(consoleKitDBChanged()), | ||
72 | this, SLOT(checkForResultChanged())); | 72 | this, SLOT(checkForResultChanged())); | ||
73 | connect(PolkitQt1::Authority::instance(), SIGNAL(enumerateActionsFinished(PolkitQt1::ActionDescription::List)), | | |||
74 | this, SLOT(updateCachedActions(PolkitQt1::ActionDescription::List))); | | |||
75 | | ||||
76 | // Cache existing action IDs as soon as possible | | |||
77 | m_flyingActions = true; | | |||
78 | PolkitQt1::Authority::instance()->enumerateActions(); | | |||
79 | } | 73 | } | ||
80 | 74 | | |||
81 | Polkit1Backend::~Polkit1Backend() | 75 | Polkit1Backend::~Polkit1Backend() | ||
82 | { | 76 | { | ||
83 | 77 | | |||
84 | } | 78 | } | ||
85 | 79 | | |||
86 | void Polkit1Backend::preAuthAction(const QString &action, QWidget *parent) | 80 | void Polkit1Backend::preAuthAction(const QString &action, QWidget *parent) | ||
Show All 28 Lines | 89 | if (QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String("org.kde.polkit-kde-authentication-agent-1"))) { | |||
115 | if (call.isError()) { | 109 | if (call.isError()) { | ||
116 | qCWarning(KAUTH) << "ERROR while streaming the parent!!" << call.error(); | 110 | qCWarning(KAUTH) << "ERROR while streaming the parent!!" << call.error(); | ||
117 | } | 111 | } | ||
118 | } else { | 112 | } else { | ||
119 | qCDebug(KAUTH) << "KDE polkit agent appears too old or not registered on the bus"; | 113 | qCDebug(KAUTH) << "KDE polkit agent appears too old or not registered on the bus"; | ||
120 | } | 114 | } | ||
121 | } | 115 | } | ||
122 | 116 | | |||
123 | void Polkit1Backend::updateCachedActions(const PolkitQt1::ActionDescription::List &actions) | | |||
124 | { | | |||
125 | m_knownActions.clear(); | | |||
126 | for (const PolkitQt1::ActionDescription &action : actions) { | | |||
127 | m_knownActions << action.actionId(); | | |||
128 | } | | |||
129 | m_flyingActions = false; | | |||
130 | } | | |||
131 | | ||||
132 | Action::AuthStatus Polkit1Backend::authorizeAction(const QString &action) | 117 | Action::AuthStatus Polkit1Backend::authorizeAction(const QString &action) | ||
133 | { | 118 | { | ||
134 | Q_UNUSED(action) | 119 | Q_UNUSED(action) | ||
135 | // Always return Yes here, we'll authorize inside isCallerAuthorized | 120 | // Always return Yes here, we'll authorize inside isCallerAuthorized | ||
136 | return Action::AuthorizedStatus; | 121 | return Action::AuthorizedStatus; | ||
137 | } | 122 | } | ||
138 | 123 | | |||
139 | void Polkit1Backend::setupAction(const QString &action) | 124 | void Polkit1Backend::setupAction(const QString &action) | ||
▲ Show 20 Lines • Show All 63 Lines • ▼ Show 20 Line(s) | |||||
203 | { | 188 | { | ||
204 | for (auto it = m_cachedResults.begin(); it != m_cachedResults.end(); ++it) { | 189 | for (auto it = m_cachedResults.begin(); it != m_cachedResults.end(); ++it) { | ||
205 | const QString action = it.key(); | 190 | const QString action = it.key(); | ||
206 | if (it.value() != actionStatus(action)) { | 191 | if (it.value() != actionStatus(action)) { | ||
207 | *it = actionStatus(action); | 192 | *it = actionStatus(action); | ||
208 | emit actionStatusChanged(action, *it); | 193 | emit actionStatusChanged(action, *it); | ||
209 | } | 194 | } | ||
210 | } | 195 | } | ||
211 | | ||||
212 | // Force updating known actions | | |||
213 | PolkitQt1::Authority::instance()->enumerateActions(); | | |||
214 | m_flyingActions = true; | | |||
215 | } | 196 | } | ||
216 | 197 | | |||
217 | bool Polkit1Backend::actionExists(const QString &action) | 198 | bool Polkit1Backend::actionExists(const QString &action) | ||
218 | { | 199 | { | ||
219 | auto authority = PolkitQt1::Authority::instance(); | 200 | return m_cachedResults.value(action) != Action::InvalidStatus; | ||
220 | if (m_flyingActions && authority->hasError()) { | | |||
221 | // This can happen if enumerateActions call failed | | |||
222 | qWarning() << "Encountered error while enumerating actions, error code:" << authority->lastError() << authority->errorDetails(); | | |||
223 | authority->clearError(); | | |||
224 | m_flyingActions = false; | | |||
225 | } | | |||
226 | | ||||
227 | // Any flying actions? | | |||
228 | if (m_flyingActions) { | | |||
229 | int tries = 0; | | |||
230 | while (m_flyingActions && tries < 10) { | | |||
231 | // Wait max 2 seconds | | |||
232 | QEventLoop e; | | |||
233 | QTimer::singleShot(200, &e, SLOT(quit())); | | |||
234 | e.exec(); | | |||
235 | ++tries; | | |||
236 | } | | |||
237 | } | | |||
238 | | ||||
239 | return m_knownActions.contains(action); | | |||
240 | } | 201 | } | ||
241 | 202 | | |||
242 | } // namespace Auth | 203 | } // namespace Auth | ||
243 | 204 | |