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