File indexing completed on 2024-04-21 04:43:22

0001 /*
0002     This file is part of the Polkit-qt project
0003     SPDX-FileCopyrightText: 2009 Lukas Tinkl <ltinkl@redhat.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "polkitqt1-identity.h"
0009 
0010 #include <polkit/polkit.h>
0011 
0012 #include <QDebug>
0013 
0014 namespace PolkitQt1
0015 {
0016 
0017 class Q_DECL_HIDDEN Identity::Data : public QSharedData
0018 {
0019 public:
0020     Data() : identity(nullptr) {}
0021     Data(const Data& other)
0022         : QSharedData(other)
0023         , identity(other.identity)
0024     {
0025         if (identity) {
0026             g_object_ref(identity);
0027         }
0028     }
0029     ~Data()
0030     {
0031         if (identity) {
0032             g_object_unref(identity);
0033         }
0034     }
0035 
0036     PolkitIdentity *identity;
0037 };
0038 
0039 Identity::Identity()
0040         : d(new Data)
0041 {
0042 }
0043 
0044 Identity::Identity(PolkitIdentity *polkitIdentity)
0045         : d(new Data)
0046 {
0047     d->identity = polkitIdentity;
0048 
0049     if (d->identity) {
0050         g_object_ref(d->identity);
0051     }
0052 }
0053 
0054 Identity::Identity(const PolkitQt1::Identity& other)
0055         : d(other.d)
0056 {
0057 
0058 }
0059 
0060 Identity::~Identity()
0061 {
0062 }
0063 
0064 Identity& Identity::operator=(const PolkitQt1::Identity& other)
0065 {
0066     d = other.d;
0067     return *this;
0068 }
0069 
0070 bool Identity::isValid() const
0071 {
0072     return d->identity != nullptr;
0073 }
0074 
0075 PolkitIdentity *Identity::identity() const
0076 {
0077     return d->identity;
0078 }
0079 
0080 void Identity::setIdentity(PolkitIdentity *identity)
0081 {
0082     if (d->identity == identity) {
0083         return;
0084     }
0085 
0086     if (d->identity) {
0087         g_object_unref(d->identity);
0088     }
0089 
0090     d->identity = identity;
0091 
0092     if (d->identity) {
0093         g_object_ref(d->identity);
0094     }
0095 }
0096 
0097 QString Identity::toString() const
0098 {
0099     Q_ASSERT(d->identity);
0100     return QString::fromUtf8(polkit_identity_to_string(d->identity));
0101 }
0102 
0103 Identity Identity::fromString(const QString &string)
0104 {
0105     GError *error = nullptr;
0106     PolkitIdentity *pkIdentity = polkit_identity_from_string(string.toUtf8().data(), &error);
0107     if (error != nullptr) {
0108         qWarning() << QString("Cannot create Identity from string: %1").arg(error->message);
0109         return Identity();
0110     }
0111     return Identity(pkIdentity);
0112 }
0113 
0114 UnixGroupIdentity Identity::toUnixGroupIdentity()
0115 {
0116     UnixGroupIdentity *ugid = static_cast< UnixGroupIdentity* >(this);
0117     if (!ugid) {
0118         return UnixGroupIdentity();
0119     }
0120 
0121     return *ugid;
0122 }
0123 
0124 UnixUserIdentity Identity::toUnixUserIdentity()
0125 {
0126     UnixUserIdentity *uuid = static_cast< UnixUserIdentity* >(this);
0127     if (!uuid) {
0128         return UnixUserIdentity();
0129     }
0130 
0131     return *uuid;
0132 }
0133 
0134 UnixUserIdentity::UnixUserIdentity(const QString &name)
0135         : Identity()
0136 {
0137     GError *error = nullptr;
0138     setIdentity(polkit_unix_user_new_for_name(name.toUtf8().data(), &error));
0139     if (error != nullptr) {
0140         qWarning() << QString("Cannot create UnixUserIdentity: %1").arg(error->message);
0141         setIdentity(nullptr);
0142     }
0143 }
0144 
0145 UnixUserIdentity::UnixUserIdentity(uid_t uid)
0146         : Identity()
0147 {
0148     setIdentity(polkit_unix_user_new(uid));
0149 }
0150 
0151 UnixUserIdentity::UnixUserIdentity(PolkitUnixUser *pkUnixUser)
0152         : Identity((PolkitIdentity *)pkUnixUser)
0153 {
0154 
0155 }
0156 
0157 UnixUserIdentity::UnixUserIdentity()
0158         : Identity()
0159 {
0160 
0161 }
0162 
0163 uid_t UnixUserIdentity::uid() const
0164 {
0165     return polkit_unix_user_get_uid((PolkitUnixUser *) identity());
0166 }
0167 
0168 void UnixUserIdentity::setUid(uid_t uid)
0169 {
0170     polkit_unix_user_set_uid((PolkitUnixUser *) identity(), uid);
0171 }
0172 
0173 UnixGroupIdentity::UnixGroupIdentity(const QString &name)
0174         : Identity()
0175 {
0176     GError *error = nullptr;
0177     setIdentity(polkit_unix_group_new_for_name(name.toUtf8().data(), &error));
0178     if (error != nullptr) {
0179         qWarning() << QString("Cannot create UnixGroupIdentity: %1").arg(error->message);
0180         setIdentity(nullptr);
0181     }
0182 }
0183 
0184 UnixGroupIdentity::UnixGroupIdentity(gid_t gid)
0185         : Identity()
0186 {
0187     setIdentity(polkit_unix_group_new(gid));
0188 }
0189 
0190 UnixGroupIdentity::UnixGroupIdentity(PolkitUnixGroup *pkUnixGroup)
0191         : Identity((PolkitIdentity *) pkUnixGroup)
0192 {
0193 
0194 }
0195 
0196 UnixGroupIdentity::UnixGroupIdentity()
0197         : Identity()
0198 {
0199 
0200 }
0201 
0202 gid_t UnixGroupIdentity::gid() const
0203 {
0204     return polkit_unix_group_get_gid((PolkitUnixGroup *) identity());
0205 }
0206 
0207 void UnixGroupIdentity::setGid(gid_t gid)
0208 {
0209     polkit_unix_group_set_gid((PolkitUnixGroup *) identity(), gid);
0210 }
0211 
0212 }