PolkitQt-1

polkitqt1-subject.cpp
1/*
2 This file is part of the Polkit-qt project
3 SPDX-FileCopyrightText: 2009 Jaroslav Reznik <jreznik@redhat.com>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "polkitqt1-subject.h"
9#include "polkitqt1-identity.h"
10#include "polkitqt1-config.h"
11
12#include <QDebug>
13#include <polkit/polkit.h>
14
15namespace PolkitQt1
16{
17
18class Q_DECL_HIDDEN Subject::Data : public QSharedData
19{
20public:
21 Data()
22 : QSharedData()
23 , subject(nullptr)
24 {}
25 Data(const Data& other)
26 : QSharedData(other)
27 , subject(other.subject)
28 {
29 g_object_ref(subject);
30 }
31 ~Data()
32 {
33 g_object_unref(subject);
34 }
35
36 PolkitSubject *subject;
37};
38
39Subject::Subject()
40 : d(new Data)
41{
42}
43
44Subject::Subject(PolkitSubject *subject)
45 : d(new Data)
46{
47 d->subject = subject;
48
49 if (d->subject != nullptr) {
50 g_object_ref(d->subject);
51 }
52}
53
54Subject::Subject(const PolkitQt1::Subject& other)
55 : d(other.d)
56{
57
58}
59
60Subject& Subject::operator=(const PolkitQt1::Subject& other)
61{
62 d = other.d;
63 return *this;
64}
65
66Subject::~Subject()
67{
68}
69
70bool Subject::isValid() const
71{
72 return d->subject != nullptr;
73}
74
75PolkitSubject *Subject::subject() const
76{
77 return d->subject;
78}
79
80void Subject::setSubject(PolkitSubject *subject)
81{
82 if (d->subject != nullptr) {
83 g_object_unref(d->subject);
84 }
85 d->subject = subject;
86}
87
88QString Subject::toString() const
89{
90 Q_ASSERT(d->subject);
91 return QString::fromUtf8(polkit_subject_to_string(d->subject));
92}
93
94Subject Subject::fromString(const QString &string)
95{
96 Subject subject;
97 GError *error = nullptr;
98 subject.d->subject = polkit_subject_from_string(string.toUtf8().data(), &error);
99 if (error != nullptr) {
100 qWarning() << QString("Cannot create Subject from string: %1").arg(error->message);
101 return nullptr;
102 }
103 return subject;
104}
105
106UnixProcessSubject::UnixProcessSubject(qint64 pid)
107 : Subject()
108{
109 setSubject(polkit_unix_process_new_for_owner(pid, 0, -1));
110}
111
112UnixProcessSubject::UnixProcessSubject(qint64 pid, quint64 startTime)
113 : Subject()
114{
115 setSubject(polkit_unix_process_new_for_owner(pid, startTime, -1));
116}
117
118UnixProcessSubject::UnixProcessSubject(PolkitUnixProcess *pkUnixProcess)
119 : Subject((PolkitSubject *) pkUnixProcess)
120{
121
122}
123
125{
126 return polkit_unix_process_get_pid((PolkitUnixProcess *) subject());
127}
128
130{
131 return polkit_unix_process_get_start_time((PolkitUnixProcess *) subject());
132}
133
135{
136 return polkit_unix_process_get_uid((PolkitUnixProcess *) subject());
137}
138
140{
141 polkit_unix_process_set_pid((PolkitUnixProcess *) subject(), pid);
142}
143
144// ----- SystemBusName
146 : Subject()
147{
148 setSubject(polkit_system_bus_name_new(name.toUtf8().data()));
149}
150
151SystemBusNameSubject::SystemBusNameSubject(PolkitSystemBusName *pkSystemBusName)
152 : Subject((PolkitSubject *) pkSystemBusName)
153{
154
155}
156
158{
159 return QString::fromUtf8(polkit_system_bus_name_get_name((PolkitSystemBusName *) subject()));
160}
161
163{
164 polkit_system_bus_name_set_name((PolkitSystemBusName *) subject(), name.toUtf8().data());
165}
166
168{
169#if HAVE_POLKIT_SYSTEM_BUS_NAME_GET_USER_SYNC
170 return UnixUserIdentity(polkit_system_bus_name_get_user_sync((PolkitSystemBusName *) subject(), nullptr, nullptr));
171#else
172 qWarning("Polkit is too old, returning invalid user from SystemBusNameSubject::user()!");
173 return UnixUserIdentity();
174#endif
175}
176
177// ----- SystemSession
179 : Subject()
180{
181 setSubject(polkit_unix_session_new(sessionId.toUtf8().data()));
182}
183
185 : Subject()
186{
187 GError *error = nullptr;
188 setSubject(polkit_unix_session_new_for_process_sync(pid, nullptr, &error));
189 if (error != nullptr) {
190 qWarning() << QString("Cannot create unix session: %1").arg(error->message);
191 setSubject(nullptr);
192 }
193}
194
195UnixSessionSubject::UnixSessionSubject(PolkitSystemBusName *pkUnixSession)
196 : Subject((PolkitSubject *) pkUnixSession)
197{
198
199}
200
202{
203 return QString::fromUtf8(polkit_unix_session_get_session_id((PolkitUnixSession *) subject()));
204}
205
207{
208 polkit_unix_session_set_session_id((PolkitUnixSession *) subject(), sessionId.toUtf8().data());
209}
210
211}
This class represents PolicyKit subjects.
PolkitSubject * subject() const
Gets PolkitSubject object.
void setName(const QString &name)
Sets system bus name.
UnixUserIdentity user()
Returns the UnixUserIdentity for this subject.
SystemBusNameSubject(const QString &name)
Subject constructor, takes one parameter - system bus name.
QString name() const
Returns system bus name.
void setPid(qint64 pid)
Sets Unix process PID.
qint64 uid() const
Returns Unix process UID.
UnixProcessSubject(qint64 pid)
Subject constructor, takes one parameter - PID.
qint64 startTime() const
Returns Unix process start time.
qint64 pid() const
Returns Unix process PID.
void setSessionId(const QString &sessionId)
Sets session id.
QString sessionId() const
Returns session id.
UnixSessionSubject(const QString &sessionId)
Subject constructor, takes one parameter - session id.
An object representing a user identity on a UNIX system.
char * data()
QString arg(Args &&... args) const const
QString fromUtf8(QByteArrayView str)
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:54:16 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.