Debian Codegraph @

src/helper/polkit.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2022 Juan Palacios <[email protected]>
3
4 #include "polkit.h"
5
6 #define QT_NO_KEYWORDS
7 #include <polkit/polkit.h>
8
9 #include <spdlog/spdlog.h>
10
11 namespace Polkit {
12
13 class Authority
14 {
15 public:
16 Authority() noexcept;
17 ~Authority();
18
19 PolkitAuthority *get() const;
20
21 private:
22 PolkitAuthority *authority_;
23 };
24
25 Authority::Authority() noexcept
26 : authority_(nullptr)
27 {
28 GError *error = nullptr;
29
30 authority_ = polkit_authority_get_sync(nullptr, &error);
31 if (authority_ == nullptr) {
32 SPDLOG_DEBUG("Could not get Polkit authority: {}", error->message);
33 g_error_free(error);
34 return;
35 }
36 }
37
38 Authority::~Authority()
39 {
40 g_object_unref(authority_);
41 }
42
43 PolkitAuthority *Authority::get() const
44 {
45 return authority_;
46 }
47
48 class BusNameSubjectImpl
49 {
50 public:
51 BusNameSubjectImpl(BusNameSubject const &subject) noexcept;
52 ~BusNameSubjectImpl();
53
54 PolkitSubject *get() const;
55
56 private:
57 PolkitSubject *subject_;
58 };
59
60 BusNameSubjectImpl::BusNameSubjectImpl(BusNameSubject const &subject) noexcept
61 : subject_(nullptr)
62 {
63 subject_ = polkit_system_bus_name_new(subject.bus.c_str());
64 }
65
66 BusNameSubjectImpl::~BusNameSubjectImpl()
67 {
68 g_object_unref(subject_);
69 }
70
71 PolkitSubject *BusNameSubjectImpl::get() const
72 {
73 return subject_;
74 }
75
76 AuthResult checkAuthorizationSync(std::string const &actionId,
77 BusNameSubject const &subject)
78 {
79 auto polkitAuthority = Authority();
80 if (polkitAuthority.get() == nullptr)
81 return AuthResult::Error;
82
83 auto polkitSubject = BusNameSubjectImpl(subject);
84 if (polkitSubject.get() == nullptr)
85 return AuthResult::Error;
86
87 GError *error = nullptr;
88 auto polkitResult = polkit_authority_check_authorization_sync(
89 polkitAuthority.get(), polkitSubject.get(), actionId.c_str(), nullptr,
90 POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, nullptr, &error);
91 if (error) {
92 SPDLOG_DEBUG("Could not check Polkit authorization: {}", error->message);
93 g_error_free(error);
94 return AuthResult::Error;
95 }
96
97 auto result = polkit_authorization_result_get_is_authorized(polkitResult)
98 ? AuthResult::Yes
99 : AuthResult::No;
100
101 g_object_unref(polkitResult);
102
103 return result;
104 }
105
106 } // namespace Polkit