| 1 | /* |
| 2 | * Copyright (C) 2015 Canonical Ltd. |
| 3 | * |
| 4 | * Contact: Alberto Mardegan <[email protected]> |
| 5 | * |
| 6 | * This file is part of lomiri-online-accounts-ui's click hooks |
| 7 | * |
| 8 | * This program is free software: you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 3, as published |
| 10 | * by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranties of |
| 14 | * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "acl-updater.h" |
| 22 | |
| 23 | #include <QCoreApplication> |
| 24 | #include <QDebug> |
| 25 | #include <QEventLoop> |
| 26 | #include <QObject> |
| 27 | #include <SignOn/Identity> |
| 28 | #include <SignOn/IdentityInfo> |
| 29 | |
| 30 | class AclUpdaterPrivate: public QObject |
| 31 | { |
| 32 | Q_OBJECT |
| 33 | |
| 34 | public: |
| 35 | AclUpdaterPrivate(); |
| 36 | |
| 37 | public Q_SLOTS: |
| 38 | void onInfo(const SignOn::IdentityInfo &info); |
| 39 | void onStored(const quint32 id); |
| 40 | void onError(const SignOn::Error &error); |
| 41 | |
| 42 | private: |
| 43 | friend class AclUpdater; |
| 44 | QEventLoop m_loop; |
| 45 | }; |
| 46 | |
| 47 | AclUpdaterPrivate::AclUpdaterPrivate(): |
| 48 | QObject() |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | void AclUpdaterPrivate::onInfo(const SignOn::IdentityInfo &info) |
| 53 | { |
| 54 | SignOn::Identity *identity = qobject_cast<SignOn::Identity*>(sender()); |
| 55 | |
| 56 | QString shortAppId = identity->property("appToBeRemoved").toString(); |
| 57 | QStringList acl; |
| 58 | Q_FOREACH(const QString &token, info.accessControlList()) { |
| 59 | if (!token.startsWith(shortAppId)) { |
| 60 | acl.append(token); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (acl != info.accessControlList()) { |
| 65 | SignOn::IdentityInfo newInfo(info); |
| 66 | newInfo.setAccessControlList(acl); |
| 67 | identity->storeCredentials(newInfo); |
| 68 | } else { |
| 69 | qDebug() << shortAppId << "was not in ACL of" << info.id(); |
| 70 | m_loop.exit(0); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void AclUpdaterPrivate::onStored(const quint32 id) |
| 75 | { |
| 76 | Q_UNUSED(id); |
| 77 | m_loop.exit(0); |
| 78 | } |
| 79 | |
| 80 | void AclUpdaterPrivate::onError(const SignOn::Error &err) |
| 81 | { |
| 82 | qWarning() << "Error occurred updating ACL" << err.message(); |
| 83 | m_loop.exit(1); |
| 84 | } |
| 85 | |
| 86 | AclUpdater::AclUpdater(): |
| 87 | d_ptr(new AclUpdaterPrivate) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | AclUpdater::~AclUpdater() |
| 92 | { |
| 93 | delete d_ptr; |
| 94 | } |
| 95 | |
| 96 | bool AclUpdater::removeApp(const QString &shortAppId, uint credentialsId) |
| 97 | { |
| 98 | Q_D(AclUpdater); |
| 99 | |
| 100 | if (credentialsId == 0 || |
| 101 | !shortAppId.contains('_')) return false; |
| 102 | |
| 103 | SignOn::Identity *identity = |
| 104 | SignOn::Identity::existingIdentity(credentialsId, d); |
| 105 | if (Q_UNLIKELY(!identity)) return false; |
| 106 | |
| 107 | identity->setProperty("appToBeRemoved", shortAppId); |
| 108 | QObject::connect(identity, SIGNAL(info(const SignOn::IdentityInfo&)), |
| 109 | d, SLOT(onInfo(const SignOn::IdentityInfo&))); |
| 110 | QObject::connect(identity, SIGNAL(credentialsStored(const quint32)), |
| 111 | d, SLOT(onStored(const quint32))); |
| 112 | QObject::connect(identity, SIGNAL(error(const SignOn::Error &)), |
| 113 | d, SLOT(onError(const SignOn::Error &))); |
| 114 | identity->queryInfo(); |
| 115 | |
| 116 | bool ok = (d->m_loop.exec() == 0); |
| 117 | delete identity; |
| 118 | return ok; |
| 119 | } |
| 120 | |
| 121 | #include "acl-updater.moc" |