Changeset View
Changeset View
Standalone View
Standalone View
src/util/externalcommand_polkitbackend.h
- This file was added.
1 | /************************************************************************* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 by Shubham <aryan100jangid@gmail.com> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or * | ||||
5 | * modify it under the terms of the GNU General Public License as * | ||||
6 | * published by the Free Software Foundation; either version 3 of * | ||||
7 | * the License, or (at your option) any later version. * | ||||
8 | * * | ||||
9 | * This program is distributed in the hope that it will be useful, * | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||||
12 | * GNU General Public License for more details. * | ||||
13 | * * | ||||
14 | * You should have received a copy of the GNU General Public License * | ||||
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>.* | ||||
16 | *************************************************************************/ | ||||
17 | | ||||
18 | #ifndef KPMCORE_EXTERNALCOMMAND_POLKITBACKEND_H | ||||
19 | #define KPMCORE_EXTERNALCOMMAND_POLKITBACKEND_H | ||||
20 | | ||||
21 | #include <QEventLoop> | ||||
22 | #include <QHash> | ||||
23 | #include <QObject> | ||||
24 | #include <QString> | ||||
25 | #include <QVariant> | ||||
26 | | ||||
27 | #include <PolkitQt1/Authority> | ||||
28 | | ||||
29 | using namespace PolkitQt1; | ||||
30 | | ||||
31 | namespace Auth | ||||
32 | { | ||||
33 | | ||||
34 | /** A Polkit Qt backend class for authorizing actions. | ||||
35 | | ||||
36 | This class is used to authorize various actions if they | ||||
37 | ask for privileged execution. It starts by verifying the | ||||
38 | action under consideration if it is the one it is | ||||
39 | saying and authorizes it based on the credentials | ||||
40 | provided. | ||||
41 | | ||||
42 | @author Shubham <aryan100jangid@gmail.com> | ||||
43 | **/ | ||||
44 | class PolkitQt1Backend : public QObject | ||||
45 | { | ||||
46 | Q_OBJECT | ||||
47 | Q_DISABLE_COPY(PolkitQt1Backend) | ||||
48 | | ||||
49 | public: | ||||
50 | /** | ||||
51 | * \brief Constructor of PolkitQt1Backend class | ||||
52 | */ | ||||
53 | PolkitQt1Backend(); | ||||
54 | | ||||
55 | /** | ||||
56 | * \brief Destructor of PolkitQt1Backend class | ||||
57 | */ | ||||
58 | ~PolkitQt1Backend(); | ||||
59 | | ||||
60 | /** | ||||
61 | * \brief A function to check for the action's current status. | ||||
62 | * | ||||
63 | * \param action Action name | ||||
64 | * \param helperID Helper service name | ||||
65 | * | ||||
66 | */ | ||||
67 | void startHelper(const QString &action, const QString &helperID, int timeout = 10 * 24 * 3600 * 1000 /*10 days*/); | ||||
68 | | ||||
69 | /** | ||||
70 | * \brief Initializes the KDE Polkit Authentication Agent. | ||||
71 | * | ||||
72 | * \param action Action in question | ||||
73 | * \param parent Parent widget | ||||
74 | * | ||||
75 | */ | ||||
76 | void initPolkitAgent(const QString &action, QWidget *parent = nullptr) const; | ||||
77 | | ||||
78 | /** | ||||
79 | * \brief A function to check for the action's current status. | ||||
80 | * | ||||
81 | * \param action Action in question | ||||
82 | * \param calledID The Application process ID of the action | ||||
83 | * | ||||
84 | * \return the result of action status ie. If action is Authorized or not. | ||||
85 | */ | ||||
86 | Authority::Result actionStatus(const QString &action, const QByteArray &callerID) const; | ||||
87 | | ||||
88 | /** | ||||
89 | * \brief Function to get the current Application process ID | ||||
90 | * | ||||
91 | * \return Application process ID of the action | ||||
92 | */ | ||||
93 | QByteArray callerID() const; | ||||
94 | | ||||
95 | /** | ||||
96 | * \brief Tries to authorize to the \p action in question. | ||||
97 | * | ||||
98 | * \param action Action in question. | ||||
99 | * \param callerID The Application process ID of the action | ||||
100 | * | ||||
101 | * \return \c true if authority authorizes the action successfully, \c false Action is not authorized. | ||||
102 | * | ||||
103 | */ | ||||
104 | bool authorizeAction(const QString &action, const QByteArray &callerID); | ||||
105 | | ||||
106 | /** | ||||
107 | * \brief Stops the running \p action from executing. | ||||
108 | * | ||||
109 | * \param action Action in question. | ||||
110 | * | ||||
111 | */ | ||||
112 | bool revokeAuthorization(const QString &action, const QByteArray &callerID); | ||||
113 | | ||||
114 | public Q_SLOTS: | ||||
115 | void authStatusChanged(); | ||||
116 | | ||||
117 | private: | ||||
118 | QHash<QString, Authority::Result> m_cachedResults; | ||||
119 | bool m_flyingActions; // Already running actions | ||||
120 | }; | ||||
121 | | ||||
122 | /** A Polkit event loop class. | ||||
123 | | ||||
124 | This class is used to implement a polkit event | ||||
125 | loop and has the capability of returning the | ||||
126 | current authorization result of the action in | ||||
127 | que. | ||||
128 | | ||||
129 | @author Shubham <aryan100jangid@gmail.com> | ||||
130 | **/ | ||||
131 | class PolkitEventLoop : public QEventLoop | ||||
132 | { | ||||
133 | Q_OBJECT | ||||
134 | | ||||
135 | public: | ||||
136 | PolkitEventLoop(QObject *parent = nullptr); | ||||
137 | ~PolkitEventLoop(); | ||||
138 | | ||||
139 | Authority::Result result() const; | ||||
140 | | ||||
141 | public Q_SLOTS: | ||||
142 | void requestQuit(const PolkitQt1::Authority::Result &result); | ||||
143 | | ||||
144 | public: | ||||
145 | static Authority::Result m_result; | ||||
146 | }; | ||||
147 | | ||||
148 | } // namespace Auth | ||||
149 | | ||||
150 | #endif // KPMCORE_EXTERNALCOMMAND_POLKITBACKEND_H |