KAuth::Action

Search for usage in LXR

KAuth::Action Class Reference

#include <KAuthAction>

Public Types

enum  AuthDetail { DetailOther = 0, AuthDetail::DetailMessage }
 
enum  AuthStatus {
  DeniedStatus, ErrorStatus, InvalidStatus, AuthorizedStatus,
  AuthRequiredStatus, UserCancelledStatus
}
 
typedef QMap< AuthDetail, QVariantDetailsMap
 
enum  ExecutionMode { ExecuteMode, AuthorizeOnlyMode }
 

Public Member Functions

 Action ()
 
 Action (const Action &action)
 
 Action (const QString &name)
 
 Action (const QString &name, const QString &details)
 
 Action (const QString &name, const DetailsMap &details)
 
 ~Action ()
 
void addArgument (const QString &key, const QVariant &value)
 
QVariantMap arguments () const
 
QString details () const
 
DetailsMap detailsV2 () const
 
ExecuteJobexecute (ExecutionMode mode=ExecuteMode)
 
bool hasHelper () const
 
QString helperId () const
 
bool isValid () const
 
QString name () const
 
bool operator!= (const Action &action) const
 
Actionoperator= (const Action &action)
 
bool operator== (const Action &action) const
 
QWidgetparentWidget () const
 
void setArguments (const QVariantMap &arguments)
 
void setDetails (const QString &details)
 
void setDetailsV2 (const DetailsMap &details)
 
void setHelperId (const QString &id)
 
void setName (const QString &name)
 
void setParentWidget (QWidget *parent)
 
void setTimeout (int timeout)
 
AuthStatus status () const
 
int timeout () const
 

Detailed Description

Class to access, authorize and execute actions.

This is the main class of the kauth API. It provides the interface to manipulate actions. Every action is identified by its name. Every instance of the Action class with the same name refers to the same action.

Once you have an action object you can tell the helper to execute it (asking the user to authenticate if needed) with the execute() method. The simplest thing to do is to execute a single action synchronously blocking for the reply by callin exec() on the job object returned by execute().

For asynchronous calls, use KAuth::ExecuteJob::start() instead. It sends the request to the helper and returns immediately. Before doing so you should however connect to at least the KJob::result(KJob *) signal to receive a slot call once the action is done executing.

To use the execute() method you have to set the default helper's ID using the setHelperID() static method. Alternatively, you can specify the helperID using the overloaded version of the methods that takes it as a parameter.

Each action object contains a QVariantMap object that is passed directly to the helper when the action is executed. You can access this map using the arguments() method. You can insert into it any kind of custom data you need to pass to the helper.

void MyApp::runAction()
{
action = KAuth::Action("org.kde.myapp.action");
KAuth::ExecuteJob *job = action.execute();
connect(job, &KAuth::ExecuteJob::result, this, &MyApp::actionResult);
job->start();
}
void MyApp::actionResult(KJob *kjob)
{
auto job = qobject_cast<KAuth::ExecuteJob *>(kjob);
qDebug() << job.error() << job.data();
}
Since
4.4

Definition at line 70 of file kauthaction.h.

Member Typedef Documentation

Map of details.

Definition at line 105 of file kauthaction.h.

Member Enumeration Documentation

The backend specific details.

Enumerator
DetailMessage 

The message to show in authentication dialog.

Definition at line 96 of file kauthaction.h.

The three values set by authorization methods.

Enumerator
DeniedStatus 

The authorization has been denied by the authorization backend.

ErrorStatus 

An error occurred.

InvalidStatus 

An invalid action cannot be authorized.

AuthorizedStatus 

The authorization has been granted by the authorization backend.

AuthRequiredStatus 

The user could obtain the authorization after authentication.

UserCancelledStatus 

The user pressed Cancel the authentication dialog. Currently used only on the mac.

Definition at line 77 of file kauthaction.h.

Constructor & Destructor Documentation

Action::Action ( )

Default constructor.

This constructor sets the name to the empty string. Such an action is invalid and cannot be authorized nor executed, so you need to call setName() before you can use the object.

Definition at line 51 of file kauthaction.cpp.

Action::Action ( const Action action)

Copy constructor.

Definition at line 56 of file kauthaction.cpp.

Action::Action ( const QString name)

This creates a new action object with this name.

Parameters
nameThe name of the new action

Definition at line 61 of file kauthaction.cpp.

Action::Action ( const QString name,
const QString details 
)

This creates a new action object with this name and details.

Parameters
nameThe name of the new action
detailsThe details of the action
See also
setDetails
Deprecated:
since 5.68, use constructor with DetailsMap

Definition at line 69 of file kauthaction.cpp.

Action::Action ( const QString name,
const DetailsMap details 
)

This creates a new action object with this name and details.

Parameters
nameThe name of the new action
detailsThe details of the action
See also
setDetails
Since
5.68

Definition at line 75 of file kauthaction.cpp.

Action::~Action ( )

Virtual destructor.

Definition at line 83 of file kauthaction.cpp.

Member Function Documentation

void Action::addArgument ( const QString key,
const QVariant value 
)

Convenience method to add an argument.

This method adds the pair key/value to the QVariantMap used to send custom data to the helper.

Use this method if you don't want to create a new QVariantMap only to add a new entry.

Parameters
keyThe new entry's key
valueThe value of the new entry

Definition at line 178 of file kauthaction.cpp.

QVariantMap Action::arguments ( ) const

Returns map object used to pass arguments to the helper.

This method returns the variant map that the application can use to pass arbitrary data to the helper when executing the action.

Returns
The arguments map that will be passed to the helper.

Definition at line 183 of file kauthaction.cpp.

QString Action::details ( ) const

Gets the action's details.

The details that will be shown in the authorization dialog, if the backend supports it.

Returns
The action's details
Deprecated:
since 5.68, use detailsV2() with DetailsMap.

Definition at line 132 of file kauthaction.cpp.

Action::DetailsMap Action::detailsV2 ( ) const

Gets the action's details.

The details that will be shown in the authorization dialog, if the backend supports it.

Returns
The action's details
Since
5.68

Definition at line 146 of file kauthaction.cpp.

ExecuteJob * Action::execute ( ExecutionMode  mode = ExecuteMode)

Get the job object used to execute the action.

Returns
The KJob::ExecuteJob object to be used to run the action.

Definition at line 218 of file kauthaction.cpp.

bool Action::hasHelper ( ) const

Checks if the action has an helper.

This function can be used to check if an helper will be called upon the execution of an action. Such an helper can be set through setHelperID. If this function returns false, upon execution the action will be just authorized.

Since
4.5
Returns
Whether the action has an helper or not
See also
setHelperID

Definition at line 223 of file kauthaction.cpp.

QString Action::helperId ( ) const

Gets the default helper ID used for actions execution.

The helper ID is the string that uniquely identifies the helper in the system. It is the string passed to the KAUTH_HELPER() macro in the helper source. Because one could have different helpers, you need to specify an helper ID for each execution, or set a default ID by calling setHelperID(). This method returns the current default value.

Returns
The default helper ID.

Definition at line 188 of file kauthaction.cpp.

bool Action::isValid ( ) const

Returns if the object represents a valid action.

Action names have to respect a simple syntax. They have to be all in lowercase characters, separated by dots. Dots can't appear at the beginning and at the end of the name.

In other words, the action name has to match this perl-like regular expression:

* /^[a-z]+(\.[a-z]+)*$/
* 

This method returns false if the action name doesn't match the valid syntax.

If the backend supports it, this method also checks if the action is valid and recognized by the backend itself.

Note
This may spawn a nested event loop.

Invalid actions cannot be authorized nor executed. The empty string is not a valid action name, so the default constructor returns an invalid action.

Definition at line 156 of file kauthaction.cpp.

QString Action::name ( ) const

Gets the action's name.

This is the unique attribute that identifies an action object. Two action objects with the same name always refer to the same action.

Returns
The action name

Definition at line 110 of file kauthaction.cpp.

bool Action::operator!= ( const Action action) const

Negated comparison operator.

Returns the negation of operator==

Returns
true if the two actions are different and not both invalid

Definition at line 104 of file kauthaction.cpp.

Action & Action::operator= ( const Action action)

Assignment operator.

Definition at line 88 of file kauthaction.cpp.

bool Action::operator== ( const Action action) const

Comparison operator.

This comparison operator compares the names of two actions and returns whether they are the same. It does not care about the arguments stored in the actions. However, if two actions are invalid they'll match as equal, even if the invalid names are different.

Returns
true if the two actions are the same or both invalid

Definition at line 99 of file kauthaction.cpp.

QWidget * Action::parentWidget ( ) const

Returns the parent widget for the authentication dialog for this action.

Since
4.6
Returns
A QWidget which will is being used as the dialog's parent

Definition at line 204 of file kauthaction.cpp.

void Action::setArguments ( const QVariantMap &  arguments)

Sets the map object used to pass arguments to the helper.

This method sets the variant map that the application can use to pass arbitrary data to the helper when executing the action.

Only non-gui variants are supported.

Parameters
argumentsThe new arguments map

Definition at line 173 of file kauthaction.cpp.

void Action::setDetails ( const QString details)

Sets the action's details.

You can use this function to provide the user more details (if the backend supports it) on the action being authorized in the authorization dialog

Deprecated:
since 5.68, use setDetailsV2() with DetailsMap.

Definition at line 139 of file kauthaction.cpp.

void Action::setDetailsV2 ( const DetailsMap details)

Sets the action's details.

You can use this function to provide the user more details (if the backend supports it) on the action being authorized in the authorization dialog

Parameters
detailsthe details describing the action. For e.g, "DetailMessage" key can be used to give a customized authentication message.
Since
5.68

Definition at line 151 of file kauthaction.cpp.

void Action::setHelperId ( const QString id)

Sets the default helper ID used for actions execution.

This method sets the helper ID which contains the body of this action. If the string is non-empty, the corresponding helper will be fired and the action executed inside the helper. Otherwise, the action will be just authorized.

Note
To unset a previously set helper, just pass an empty string
Parameters
idThe default helper ID.
See also
hasHelper
helperId

Definition at line 194 of file kauthaction.cpp.

void Action::setName ( const QString name)

Sets the action's name.

It's not common to change the action name after its creation. Usually you set the name with the constructor (and you have to, because there's no default constructor)

Definition at line 115 of file kauthaction.cpp.

void Action::setParentWidget ( QWidget parent)

Sets a parent widget for the authentication dialog.

This function is used for explicitly setting a parent window for an eventual authentication dialog required when authorization is triggered. Some backends, in fact, (like polkit-1) need to have a parent explicitly set for displaying the dialog correctly.

Note
If you are using KAuth through one of KDE's GUI components (KPushButton, KCModule...) you do not need and should not call this function, as it is already done by the component itself.
Since
4.6
Parameters
parentA QWidget which will be used as the dialog's parent

Definition at line 199 of file kauthaction.cpp.

void Action::setTimeout ( int  timeout)

Sets the action's timeout.

The timeout of the action in milliseconds -1 means the default D-Bus timeout (usually 25 seconds)

Since
5.29

Definition at line 126 of file kauthaction.cpp.

Action::AuthStatus Action::status ( ) const

Gets information about the authorization status of an action.

This methods query the authorization backend to know if the user can try to acquire the authorization for this action. If the result is Action::AuthRequired, the user can try to acquire the authorization by authenticating.

It should not be needed to call this method directly, because the execution methods already take care of all the authorization stuff.

Returns
Action::Denied if the user doesn't have the authorization to execute the action, Action::Authorized if the action can be executed, Action::AuthRequired if the user could acquire the authorization after authentication, Action::UserCancelled if the user cancels the authentication dialog. Not currently supported by the Polkit backend

Definition at line 209 of file kauthaction.cpp.

int Action::timeout ( ) const

Gets the action's timeout.

The timeout of the action in milliseconds -1 means the default D-Bus timeout (usually 25 seconds)

Since
5.29
Returns
The action timeouts

Definition at line 121 of file kauthaction.cpp.


The documentation for this class was generated from the following files:
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Sep 23 2021 22:52:48 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.