9#include "BackendsManager.h"
10#include "kauthdebug.h"
12#include <QCoreApplication>
19class ExecuteJobPrivate
24 explicit ExecuteJobPrivate(ExecuteJob *parent)
32 Action::ExecutionMode mode;
35 void doExecuteAction();
36 void doAuthorizeAction();
37 void actionPerformedSlot(
const QString &action,
const ActionReply &reply);
38 void progressStepSlot(
const QString &action,
int i);
39 void progressStepSlot(
const QString &action,
const QVariantMap &data);
43ExecuteJob::ExecuteJob(
const Action &action, Action::ExecutionMode mode,
QObject *parent)
45 , d(new ExecuteJobPrivate(this))
50 HelperProxy *helper = BackendsManager::helperProxy();
52 connect(helper, &KAuth::HelperProxy::actionPerformed,
this, [
this](
const QString &action,
const ActionReply &reply) {
53 d->actionPerformedSlot(action, reply);
55 connect(helper, &KAuth::HelperProxy::progressStep,
this, [
this](
const QString &action,
int i) {
56 d->progressStepSlot(action, i);
58 connect(helper, &KAuth::HelperProxy::progressStepData,
this, [
this](
const QString &action,
const QVariantMap &data) {
59 d->progressStepSlot(action, data);
62 connect(BackendsManager::authBackend(), &KAuth::AuthBackend::actionStatusChanged,
this, [
this](
const QString &action, Action::AuthStatus
status) {
63 d->statusChangedSlot(action,
status);
67ExecuteJob::~ExecuteJob() =
default;
74QVariantMap ExecuteJob::data()
const
79void ExecuteJob::start()
81 if (!d->action.isValid()) {
82 qCWarning(KAUTH) <<
"Tried to start an invalid action: " << d->action.name();
85 d->actionPerformedSlot(d->action.name(), reply);
90 case Action::ExecuteMode:
95 case Action::AuthorizeOnlyMode:
97 d->doAuthorizeAction();
101 ActionReply reply(ActionReply::InvalidActionError);
103 d->actionPerformedSlot(d->action.name(), reply);
109bool ExecuteJob::kill(KillVerbosity verbosity)
111 BackendsManager::helperProxy()->stopAction(d->action.name(), d->action.helperId());
116void ExecuteJobPrivate::doExecuteAction()
119 if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::AuthorizeFromClientCapability) {
120 if (BackendsManager::authBackend()->capabilities() & KAuth::AuthBackend::PreAuthActionCapability) {
121 BackendsManager::authBackend()->preAuthAction(action.name(), action.parentWindow());
124 Action::AuthStatus s = BackendsManager::authBackend()->authorizeAction(action.name());
126 if (s == Action::AuthorizedStatus) {
127 if (action.hasHelper()) {
128 BackendsManager::helperProxy()->executeAction(action.name(), action.helperId(), action.detailsV2(), action.arguments(), action.timeout());
131 actionPerformedSlot(action.name(), ActionReply::SuccessReply());
136 case Action::DeniedStatus:
137 actionPerformedSlot(action.name(), ActionReply::AuthorizationDeniedReply());
139 case Action::InvalidStatus:
140 actionPerformedSlot(action.name(), ActionReply::InvalidActionReply());
142 case Action::UserCancelledStatus:
143 actionPerformedSlot(action.name(), ActionReply::UserCancelledReply());
146 ActionReply r(ActionReply::BackendError);
147 r.setErrorDescription(tr(
"Unknown status for the authentication procedure"));
148 actionPerformedSlot(action.name(), r);
153 }
else if (BackendsManager::authBackend()->
capabilities() & KAuth::AuthBackend::AuthorizeFromHelperCapability) {
154 if (BackendsManager::authBackend()->
capabilities() & KAuth::AuthBackend::PreAuthActionCapability) {
155 BackendsManager::authBackend()->preAuthAction(action.name(), action.parentWindow());
157 if (!action.hasHelper()) {
158 ActionReply r(ActionReply::InvalidActionReply());
159 r.setErrorDescription(tr(
"The current backend only allows helper authorization, but this action does not have a helper."));
160 actionPerformedSlot(action.name(), r);
163 BackendsManager::helperProxy()->executeAction(action.name(), action.helperId(), action.detailsV2(), action.arguments(), action.timeout());
166 ActionReply r(ActionReply::BackendError);
167 r.setErrorDescription(tr(
"The backend does not specify how to authorize"));
168 actionPerformedSlot(action.name(), r);
172void ExecuteJobPrivate::doAuthorizeAction()
175 Action::AuthStatus s = action.status();
176 if (s == Action::AuthRequiredStatus) {
178 if (BackendsManager::authBackend()->
capabilities() & KAuth::AuthBackend::AuthorizeFromClientCapability) {
180 if (BackendsManager::authBackend()->
capabilities() & KAuth::AuthBackend::PreAuthActionCapability) {
181 BackendsManager::authBackend()->preAuthAction(action.name(), action.parentWindow());
184 s = BackendsManager::authBackend()->authorizeAction(action.name());
185 }
else if (BackendsManager::authBackend()->
capabilities() & KAuth::AuthBackend::AuthorizeFromHelperCapability) {
187 s = Action::AuthorizedStatus;
190 ActionReply r(ActionReply::BackendError);
191 r.setErrorDescription(tr(
"The backend does not specify how to authorize"));
192 actionPerformedSlot(action.name(), r);
197 if (s == Action::AuthorizedStatus) {
198 actionPerformedSlot(action.name(), ActionReply::SuccessReply());
200 actionPerformedSlot(action.name(), ActionReply::AuthorizationDeniedReply());
204void ExecuteJobPrivate::actionPerformedSlot(
const QString &taction,
const ActionReply &reply)
206 if (taction == action.name()) {
207 if (reply.failed()) {
208 q->setError(reply.errorCode());
209 q->setErrorText(reply.errorDescription());
218void ExecuteJobPrivate::progressStepSlot(
const QString &taction,
int i)
220 if (taction == action.name()) {
225void ExecuteJobPrivate::progressStepSlot(
const QString &taction,
const QVariantMap &data)
227 if (taction == action.name()) {
228 Q_EMIT q->newData(data);
232void ExecuteJobPrivate::statusChangedSlot(
const QString &taction, Action::AuthStatus
status)
234 if (taction == action.name()) {
235 Q_EMIT q->statusChanged(
status);
241#include "moc_executejob.cpp"
Class that encapsulates a reply coming from the helper after executing an action.
void setErrorDescription(const QString &error)
Sets a human-readble description of the error.
Class to access, authorize and execute actions.
AuthStatus
The three values set by authorization methods.
Job for executing an Action.
bool kill(KJob::KillVerbosity verbosity=KJob::Quietly)
Q_SCRIPTABLE CaptureState status()
Capabilities capabilities()
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)