| 1 | /* |
| 2 | * This file is part of OnlineAccountsDaemon |
| 3 | * |
| 4 | * Copyright (C) 2015-2016 Canonical Ltd. |
| 5 | * |
| 6 | * Contact: Alberto Mardegan <[email protected]> |
| 7 | * |
| 8 | * This program is free software: you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU Lesser General Public License version 3, as |
| 10 | * published 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 Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "manager.h" |
| 22 | |
| 23 | #include <Accounts/Account> |
| 24 | #include <Accounts/AccountService> |
| 25 | #include <Accounts/Application> |
| 26 | #include <Accounts/AuthData> |
| 27 | #include <Accounts/Manager> |
| 28 | #include <Accounts/Service> |
| 29 | #include <QCoreApplication> |
| 30 | #include <QDebug> |
| 31 | #include <QHash> |
| 32 | #include <QPair> |
| 33 | #include <QSet> |
| 34 | #include "access_request.h" |
| 35 | #include "authentication_request.h" |
| 36 | #include "authenticator.h" |
| 37 | #include "client_registry.h" |
| 38 | #include "dbus_constants.h" |
| 39 | #include "i18n.h" |
| 40 | #include "manager_adaptor.h" |
| 41 | #include "state_saver.h" |
| 42 | |
| 43 | using namespace OnlineAccountsDaemon; |
| 44 | |
| 45 | namespace OnlineAccountsDaemon { |
| 46 | |
| 47 | struct ActiveAccount { |
| 48 | ActiveAccount(): accountService(0) {} |
| 49 | |
| 50 | bool isValid() const { return accountService != 0; } |
| 51 | Accounts::AccountService *accountService; |
| 52 | QSet<QString> clients; |
| 53 | }; |
| 54 | |
| 55 | typedef QPair<Accounts::AccountId,QString> AccountCoordinates; |
| 56 | typedef QHash<QString,Accounts::Application> ClientMap; |
| 57 | |
| 58 | class ManagerPrivate: public QObject |
| 59 | { |
| 60 | Q_OBJECT |
| 61 | Q_DECLARE_PUBLIC(Manager) |
| 62 | |
| 63 | public: |
| 64 | ManagerPrivate(Manager *q); |
| 65 | ~ManagerPrivate(); |
| 66 | |
| 67 | QString applicationIdFromServiceId(const QString &serviceId); |
| 68 | |
| 69 | void watchAccount(Accounts::Account *account); |
| 70 | void handleNewAccountService(Accounts::Account *account, |
| 71 | const Accounts::Service &service); |
| 72 | void loadActiveAccounts(); |
| 73 | void saveState(); |
| 74 | ActiveAccount &addActiveAccount(Accounts::AccountId accountId, |
| 75 | const QString &serviceName, |
| 76 | const QString &client); |
| 77 | ActiveAccount &addActiveAccount(Accounts::AccountId accountId, |
| 78 | const QString &serviceName, |
| 79 | const QStringList &clients); |
| 80 | |
| 81 | AccountInfo readAccountInfo(const Accounts::AccountService *as); |
| 82 | QList<QVariantMap> buildServiceList(const Accounts::Application &app) const; |
| 83 | QList<AccountInfo> getAccounts(const QVariantMap &filters, |
| 84 | const CallContext &context, |
| 85 | QList<QVariantMap> &services); |
| 86 | void authenticate(uint accountId, const QString &serviceId, |
| 87 | bool interactive, bool invalidate, |
| 88 | const QVariantMap ¶meters, |
| 89 | const CallContext &context); |
| 90 | void requestAccess(const QString &serviceId, |
| 91 | const QVariantMap ¶meters, |
| 92 | const CallContext &context); |
| 93 | bool canAccess(const QString &context, const QString &serviceId); |
| 94 | |
| 95 | static QString applicationIdFromLabel(const QString &label); |
| 96 | |
| 97 | void notifyAccountChange(const ActiveAccount &account, uint change); |
| 98 | |
| 99 | private Q_SLOTS: |
| 100 | void onActiveContextsChanged(); |
| 101 | void onAccountServiceEnabled(bool enabled); |
| 102 | void onAccountServiceChanged(); |
| 103 | void onAccountEnabled(const QString &serviceId, bool enabled); |
| 104 | void onAccountCreated(Accounts::AccountId accountId); |
| 105 | void onLoadRequest(uint accountId, const QString &serviceId); |
| 106 | |
| 107 | private: |
| 108 | ManagerAdaptor *m_adaptor; |
| 109 | Accounts::Manager m_manager; |
| 110 | StateSaver m_stateSaver; |
| 111 | bool m_mustEmitNotifications; |
| 112 | QHash<AccountCoordinates,ActiveAccount> m_activeAccounts; |
| 113 | ClientMap m_clients; |
| 114 | QList<Accounts::Account*> m_watchedAccounts; |
| 115 | bool m_isIdle; |
| 116 | Manager *q_ptr; |
| 117 | }; |
| 118 | |
| 119 | } // namespace |
| 120 | |
| 121 | ManagerPrivate::ManagerPrivate(Manager *q): |
| 122 | QObject(q), |
| 123 | m_adaptor(new ManagerAdaptor(q)), |
| 124 | m_mustEmitNotifications(false), |
| 125 | m_isIdle(true), |
| 126 | q_ptr(q) |
| 127 | { |
| 128 | CallContextCounter *counter = CallContextCounter::instance(); |
| 129 | QObject::connect(counter, SIGNAL(activeContextsChanged()), |
| 130 | this, SLOT(onActiveContextsChanged())); |
| 131 | |
| 132 | loadActiveAccounts(); |
| 133 | |
| 134 | QObject::connect(&m_manager, SIGNAL(accountCreated(Accounts::AccountId)), |
| 135 | this, SLOT(onAccountCreated(Accounts::AccountId))); |
| 136 | } |
| 137 | |
| 138 | ManagerPrivate::~ManagerPrivate() |
| 139 | { |
| 140 | saveState(); |
| 141 | } |
| 142 | |
| 143 | void ManagerPrivate::onActiveContextsChanged() |
| 144 | { |
| 145 | Q_Q(Manager); |
| 146 | CallContextCounter *counter = CallContextCounter::instance(); |
| 147 | if (counter->activeContexts() == 0) { |
| 148 | m_isIdle = true; |
| 149 | Q_EMIT q->isIdleChanged(); |
| 150 | } else if (m_isIdle) { |
| 151 | m_isIdle = false; |
| 152 | Q_EMIT q->isIdleChanged(); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | QString ManagerPrivate::applicationIdFromServiceId(const QString &serviceId) |
| 157 | { |
| 158 | Accounts::Service service = m_manager.service(serviceId); |
| 159 | Accounts::ApplicationList apps = m_manager.applicationList(service); |
| 160 | if (apps.isEmpty()) return QString(); |
| 161 | |
| 162 | /* TODO: to figure out the correct app, take the security context into |
| 163 | * account */ |
| 164 | return apps.first().name(); |
| 165 | } |
| 166 | |
| 167 | void ManagerPrivate::watchAccount(Accounts::Account *account) |
| 168 | { |
| 169 | if (m_watchedAccounts.contains(account)) return; |
| 170 | |
| 171 | QObject::connect(account, SIGNAL(enabledChanged(const QString &, bool)), |
| 172 | this, SLOT(onAccountEnabled(const QString &, bool))); |
| 173 | m_watchedAccounts.append(account); |
| 174 | } |
| 175 | |
| 176 | void ManagerPrivate::handleNewAccountService(Accounts::Account *account, |
| 177 | const Accounts::Service &service) |
| 178 | { |
| 179 | AccountCoordinates coords(account->id(), service.name()); |
| 180 | if (m_activeAccounts.contains(coords)) { |
| 181 | /* This event is also received via the AccountService instance; we'll |
| 182 | * handle it from there. */ |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | QStringList interestedClients; |
| 187 | for (auto i = m_clients.constBegin(); |
| 188 | i != m_clients.constEnd(); i++) { |
| 189 | const Accounts::Application &application = i.value(); |
| 190 | |
| 191 | if (!application.serviceUsage(service).isEmpty()) { |
| 192 | interestedClients.append(i.key()); |
| 193 | } |
| 194 | } |
| 195 | ActiveAccount &activeAccount = |
| 196 | addActiveAccount(account->id(), service.name(), interestedClients); |
| 197 | notifyAccountChange(activeAccount, |
| 198 | ONLINE_ACCOUNTS_INFO_CHANGE_ENABLED); |
| 199 | } |
| 200 | |
| 201 | void ManagerPrivate::loadActiveAccounts() |
| 202 | { |
| 203 | QList<Client> oldClientList = m_stateSaver.clients(); |
| 204 | QSet<Client> oldClients(oldClientList.begin(), oldClientList.end()); |
| 205 | ClientRegistry *clientRegistry = ClientRegistry::instance(); |
| 206 | QStringList oldClientNames; |
| 207 | Q_FOREACH(const Client &client, oldClients) { |
| 208 | oldClientNames.append(client.first); |
| 209 | } |
| 210 | clientRegistry->registerActiveClients(oldClientNames); |
| 211 | |
| 212 | /* Build the table of the active clients */ |
| 213 | QStringList activeClientNames = clientRegistry->clients(); |
| 214 | Q_FOREACH(const Client &client, oldClients) { |
| 215 | if (activeClientNames.contains(client.first)) { |
| 216 | m_clients[client.first] = m_manager.application(client.second); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | QList<AccountInfo> oldAccounts = m_stateSaver.accounts(); |
| 221 | Q_FOREACH(const AccountInfo &accountInfo, oldAccounts) { |
| 222 | // If no clients are interested in this account, ignore it |
| 223 | Accounts::Service service = |
| 224 | m_manager.service(accountInfo.serviceId()); |
| 225 | if (Q_UNLIKELY(!service.isValid())) continue; |
| 226 | |
| 227 | QStringList clients; |
| 228 | for (auto i = m_clients.constBegin(); |
| 229 | i != m_clients.constEnd(); i++) { |
| 230 | const Accounts::Application &application = i.value(); |
| 231 | if (!application.serviceUsage(service).isEmpty()) { |
| 232 | clients.append(i.key()); |
| 233 | } |
| 234 | } |
| 235 | if (clients.isEmpty()) { |
| 236 | // no one is interested in this account |
| 237 | continue; |
| 238 | } |
| 239 | |
| 240 | ActiveAccount &activeAccount = |
| 241 | addActiveAccount(accountInfo.accountId, service.name(), clients); |
| 242 | if (Q_UNLIKELY(!activeAccount.isValid())) continue; |
| 243 | |
| 244 | if (!activeAccount.accountService->isEnabled()) { |
| 245 | // the account got disabled while this daemon was not running |
| 246 | notifyAccountChange(activeAccount, |
| 247 | ONLINE_ACCOUNTS_INFO_CHANGE_DISABLED); |
| 248 | } else { |
| 249 | AccountInfo newAccountInfo = |
| 250 | readAccountInfo(activeAccount.accountService); |
| 251 | if (newAccountInfo.details != accountInfo.details) { |
| 252 | notifyAccountChange(activeAccount, |
| 253 | ONLINE_ACCOUNTS_INFO_CHANGE_UPDATED); |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /* Last, go through all active clients and see if there are new accounts |
| 259 | * for any of them. */ |
| 260 | Q_FOREACH(Accounts::AccountId accountId, m_manager.accountListEnabled()) { |
| 261 | Accounts::Account *account = m_manager.account(accountId); |
| 262 | if (Q_UNLIKELY(!account)) continue; |
| 263 | |
| 264 | watchAccount(account); |
| 265 | Q_FOREACH(Accounts::Service service, account->enabledServices()) { |
| 266 | handleNewAccountService(account, service); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | void ManagerPrivate::saveState() |
| 272 | { |
| 273 | QList<Client> clients; |
| 274 | for (auto i = m_clients.constBegin(); i != m_clients.constEnd(); i++) { |
| 275 | const Accounts::Application &application = i.value(); |
| 276 | clients.append(Client(i.key(), application.name())); |
| 277 | } |
| 278 | m_stateSaver.setClients(clients); |
| 279 | |
| 280 | QList<AccountInfo> accounts; |
| 281 | Q_FOREACH(const ActiveAccount &activeAccount, m_activeAccounts) { |
| 282 | if (!activeAccount.isValid()) continue; |
| 283 | |
| 284 | accounts.append(readAccountInfo(activeAccount.accountService)); |
| 285 | } |
| 286 | m_stateSaver.setAccounts(accounts); |
| 287 | } |
| 288 | |
| 289 | void ManagerPrivate::notifyAccountChange(const ActiveAccount &account, |
| 290 | uint change) |
| 291 | { |
| 292 | AccountInfo info = readAccountInfo(account.accountService); |
| 293 | m_adaptor->notifyAccountChange(info, change); |
| 294 | } |
| 295 | |
| 296 | ActiveAccount &ManagerPrivate::addActiveAccount(Accounts::AccountId accountId, |
| 297 | const QString &serviceName, |
| 298 | const QString &client) |
| 299 | { |
| 300 | return addActiveAccount(accountId, serviceName, QStringList() << client); |
| 301 | } |
| 302 | |
| 303 | ActiveAccount &ManagerPrivate::addActiveAccount(Accounts::AccountId accountId, |
| 304 | const QString &serviceName, |
| 305 | const QStringList &clients) |
| 306 | { |
| 307 | ActiveAccount &activeAccount = |
| 308 | m_activeAccounts[AccountCoordinates(accountId, serviceName)]; |
| 309 | QSet<QString> clientSet(clients.begin(), clients.end()); |
| 310 | activeAccount.clients += clientSet; |
| 311 | if (!activeAccount.accountService) { |
| 312 | Accounts::Account *account = m_manager.account(accountId); |
| 313 | if (Q_UNLIKELY(!account)) return activeAccount; |
| 314 | |
| 315 | Accounts::Service service = m_manager.service(serviceName); |
| 316 | auto as = new Accounts::AccountService(account, service); |
| 317 | activeAccount.accountService = as; |
| 318 | QObject::connect(as, SIGNAL(enabled(bool)), |
| 319 | this, SLOT(onAccountServiceEnabled(bool))); |
| 320 | QObject::connect(as, SIGNAL(changed()), |
| 321 | this, SLOT(onAccountServiceChanged())); |
| 322 | } |
| 323 | |
| 324 | return activeAccount; |
| 325 | } |
| 326 | |
| 327 | AccountInfo ManagerPrivate::readAccountInfo(const Accounts::AccountService *as) |
| 328 | { |
| 329 | QVariantMap info; |
| 330 | |
| 331 | info[ONLINE_ACCOUNTS_INFO_KEY_DISPLAY_NAME] = as->account()->displayName(); |
| 332 | info[ONLINE_ACCOUNTS_INFO_KEY_SERVICE_ID] = as->service().name(); |
| 333 | |
| 334 | info[ONLINE_ACCOUNTS_INFO_KEY_AUTH_METHOD] = Authenticator::authMethod(as->authData()); |
| 335 | QString settingsPrefix(QStringLiteral(ONLINE_ACCOUNTS_INFO_KEY_SETTINGS)); |
| 336 | /* First, read the global settings */ |
| 337 | Accounts::Account *a = as->account(); |
| 338 | a->selectService(); |
| 339 | Q_FOREACH(const QString &key, a->allKeys()) { |
| 340 | if (key == "enabled" || key == "CredentialsId" || key == "name" || |
| 341 | key.startsWith("auth/")) continue; |
| 342 | info[settingsPrefix + key] = a->value(key); |
| 343 | } |
| 344 | |
| 345 | /* Then, add service-specific settings */ |
| 346 | Q_FOREACH(const QString &key, as->allKeys()) { |
| 347 | if (key == "enabled") continue; |
| 348 | info[settingsPrefix + key] = as->value(key); |
| 349 | } |
| 350 | |
| 351 | return AccountInfo(as->account()->id(), info); |
| 352 | } |
| 353 | |
| 354 | QList<QVariantMap> |
| 355 | ManagerPrivate::buildServiceList(const Accounts::Application &app) const |
| 356 | { |
| 357 | QList<QVariantMap> services; |
| 358 | |
| 359 | const auto serviceList = m_manager.serviceList(app); |
| 360 | for (const Accounts::Service &service: serviceList) { |
| 361 | QString displayName = translate(service.displayName(), |
| 362 | service.trCatalog()); |
| 363 | QString icon = service.iconName(); |
| 364 | |
| 365 | /* Applications might declare support for a service while the provider |
| 366 | * (and account plugin) for that account is not installed. We probably |
| 367 | * don't want to include these services in the list, as it would lead |
| 368 | * to empty/invalid UI elements. */ |
| 369 | Accounts::Provider provider = m_manager.provider(service.provider()); |
| 370 | if (!provider.isValid()) continue; |
| 371 | |
| 372 | /* Now check the service data; if either display name or icon are |
| 373 | * empty, fetch this information from the provider file, as a fallback |
| 374 | */ |
| 375 | if (displayName.isEmpty() || |
| 376 | // sometimes we use a '.' as display name placeholder |
| 377 | displayName == ".") { |
| 378 | displayName = translate(provider.displayName(), |
| 379 | provider.trCatalog()); |
| 380 | } |
| 381 | if (icon.isEmpty()) { |
| 382 | icon = provider.iconName(); |
| 383 | } |
| 384 | |
| 385 | QString iconSource; |
| 386 | if (icon.startsWith('/')) { |
| 387 | iconSource = QStringLiteral("file:/") + icon; |
| 388 | } else { |
| 389 | iconSource = QStringLiteral("image://theme/") + icon; |
| 390 | } |
| 391 | |
| 392 | services.append({ |
| 393 | { ONLINE_ACCOUNTS_INFO_KEY_SERVICE_ID, service.name() }, |
| 394 | { ONLINE_ACCOUNTS_INFO_KEY_DISPLAY_NAME, displayName }, |
| 395 | { ONLINE_ACCOUNTS_INFO_KEY_ICON_SOURCE, iconSource }, |
| 396 | }); |
| 397 | } |
| 398 | |
| 399 | return services; |
| 400 | } |
| 401 | |
| 402 | QList<AccountInfo> ManagerPrivate::getAccounts(const QVariantMap &filters, |
| 403 | const CallContext &context, |
| 404 | QList<QVariantMap> &services) |
| 405 | { |
| 406 | QString desiredApplicationId = filters.value("applicationId").toString(); |
| 407 | QString desiredServiceId = filters.value("serviceId").toString(); |
| 408 | Accounts::AccountId desiredAccountId = filters.value("accountId").toUInt(); |
| 409 | |
| 410 | QString applicationId = desiredApplicationId.isEmpty() ? |
| 411 | applicationIdFromLabel(context.securityContext()) : desiredApplicationId; |
| 412 | |
| 413 | Accounts::Application application = m_manager.application(applicationId); |
| 414 | |
| 415 | QList<AccountInfo> accounts; |
| 416 | |
| 417 | if (!application.isValid() || |
| 418 | !canAccess(context.securityContext(), applicationId)) { |
| 419 | if (!desiredApplicationId.isEmpty()) { |
| 420 | context.sendError(ONLINE_ACCOUNTS_ERROR_PERMISSION_DENIED, |
| 421 | QString("App '%1' cannot act as '%2'"). |
| 422 | arg(applicationId).arg(desiredApplicationId)); |
| 423 | return accounts; |
| 424 | } |
| 425 | } else { |
| 426 | m_clients.insert(context.clientName(), application); |
| 427 | } |
| 428 | |
| 429 | services = buildServiceList(application); |
| 430 | |
| 431 | Q_FOREACH(Accounts::AccountId accountId, m_manager.accountListEnabled()) { |
| 432 | if (desiredAccountId != 0 && accountId != desiredAccountId) { |
| 433 | continue; |
| 434 | } |
| 435 | |
| 436 | Accounts::Account *account = m_manager.account(accountId); |
| 437 | if (Q_UNLIKELY(!account)) continue; |
| 438 | |
| 439 | Q_FOREACH(Accounts::Service service, account->enabledServices()) { |
| 440 | if (!desiredServiceId.isEmpty() && |
| 441 | service.name() != desiredServiceId) { |
| 442 | continue; |
| 443 | } |
| 444 | |
| 445 | if (!canAccess(context.securityContext(), service.name())) { |
| 446 | continue; |
| 447 | } |
| 448 | |
| 449 | if (application.isValid() && |
| 450 | application.serviceUsage(service).isEmpty()) { |
| 451 | /* The application does not support this service */ |
| 452 | continue; |
| 453 | } |
| 454 | |
| 455 | ActiveAccount &activeAccount = |
| 456 | addActiveAccount(accountId, service.name(), |
| 457 | context.clientName()); |
| 458 | accounts.append(readAccountInfo(activeAccount.accountService)); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | return accounts; |
| 463 | } |
| 464 | |
| 465 | void ManagerPrivate::authenticate(uint accountId, const QString &serviceId, |
| 466 | bool interactive, bool invalidate, |
| 467 | const QVariantMap ¶meters, |
| 468 | const CallContext &context) |
| 469 | { |
| 470 | if (!canAccess(context.securityContext(), serviceId)) { |
| 471 | context.sendError(ONLINE_ACCOUNTS_ERROR_PERMISSION_DENIED, |
| 472 | QString("Access to service ID %1 forbidden").arg(serviceId)); |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | ActiveAccount &activeAccount = |
| 477 | addActiveAccount(accountId, serviceId, context.clientName()); |
| 478 | auto as = activeAccount.accountService; |
| 479 | if (!as || !as->isEnabled()) { |
| 480 | context.sendError(ONLINE_ACCOUNTS_ERROR_PERMISSION_DENIED, |
| 481 | QString("Account %1 is disabled").arg(accountId)); |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | AuthenticationRequest *authentication = |
| 486 | new AuthenticationRequest(context, this); |
| 487 | authentication->setInteractive(interactive); |
| 488 | if (invalidate) { |
| 489 | authentication->invalidateCache(); |
| 490 | } |
| 491 | authentication->authenticate(as->authData(), parameters); |
| 492 | } |
| 493 | |
| 494 | void ManagerPrivate::requestAccess(const QString &serviceId, |
| 495 | const QVariantMap ¶meters, |
| 496 | const CallContext &context) |
| 497 | { |
| 498 | Q_UNUSED(parameters); |
| 499 | if (!canAccess(context.securityContext(), serviceId)) { |
| 500 | context.sendError(ONLINE_ACCOUNTS_ERROR_PERMISSION_DENIED, |
| 501 | QString("Access to service ID %1 forbidden").arg(serviceId)); |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | AccessRequest *accessRequest = new AccessRequest(context, this); |
| 506 | QObject::connect(accessRequest, SIGNAL(loadRequest(uint, const QString&)), |
| 507 | this, SLOT(onLoadRequest(uint, const QString&))); |
| 508 | QString applicationId = applicationIdFromServiceId(serviceId); |
| 509 | accessRequest->requestAccess(applicationId, serviceId, parameters, |
| 510 | context.clientPid()); |
| 511 | } |
| 512 | |
| 513 | bool ManagerPrivate::canAccess(const QString &context, |
| 514 | const QString &serviceId) |
| 515 | { |
| 516 | // Could not determine peer's AppArmor context, so deny access |
| 517 | if (context.isEmpty()) { |
| 518 | return false; |
| 519 | } |
| 520 | // Unconfined processes can access anything |
| 521 | if (context == "unconfined") { |
| 522 | return true; |
| 523 | } |
| 524 | |
| 525 | // Try to extract the click package name from the AppArmor context. |
| 526 | int pos = context.indexOf('_'); |
| 527 | if (pos < 0) { |
| 528 | qWarning() << "AppArmor context doesn't contain package ID: " << context; |
| 529 | return false; |
| 530 | } |
| 531 | QString pkgname = context.left(pos); |
| 532 | |
| 533 | // Do the same on the service ID: we are only dealing with |
| 534 | // confined apps at this point, so only $pkgname prefixed |
| 535 | // services are accessible. |
| 536 | pos = serviceId.indexOf('_'); |
| 537 | if (pos < 0) { |
| 538 | return false; |
| 539 | } |
| 540 | return serviceId.left(pos) == pkgname; |
| 541 | } |
| 542 | |
| 543 | QString ManagerPrivate::applicationIdFromLabel(const QString &label) |
| 544 | { |
| 545 | QStringList parts = label.split('_'); |
| 546 | if (parts.count() == 3) { |
| 547 | return QStringList(parts.mid(0, 2)).join('_'); |
| 548 | } else { |
| 549 | return QString(); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | void ManagerPrivate::onAccountServiceEnabled(bool enabled) |
| 554 | { |
| 555 | auto as = qobject_cast<Accounts::AccountService*>(sender()); |
| 556 | |
| 557 | ActiveAccount &activeAccount = |
| 558 | m_activeAccounts[AccountCoordinates(as->account()->id(), |
| 559 | as->service().name())]; |
| 560 | if (Q_UNLIKELY(!activeAccount.isValid())) return; |
| 561 | |
| 562 | notifyAccountChange(activeAccount, |
| 563 | enabled ? ONLINE_ACCOUNTS_INFO_CHANGE_ENABLED : |
| 564 | ONLINE_ACCOUNTS_INFO_CHANGE_DISABLED); |
| 565 | } |
| 566 | |
| 567 | void ManagerPrivate::onAccountServiceChanged() |
| 568 | { |
| 569 | auto as = qobject_cast<Accounts::AccountService*>(sender()); |
| 570 | if (!as->isEnabled()) { |
| 571 | // Nobody cares about disabled accounts |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | ActiveAccount &activeAccount = |
| 576 | m_activeAccounts[AccountCoordinates(as->account()->id(), |
| 577 | as->service().name())]; |
| 578 | if (Q_UNLIKELY(!activeAccount.isValid())) return; |
| 579 | |
| 580 | notifyAccountChange(activeAccount, ONLINE_ACCOUNTS_INFO_CHANGE_UPDATED); |
| 581 | } |
| 582 | |
| 583 | void ManagerPrivate::onAccountEnabled(const QString &serviceId, bool enabled) |
| 584 | { |
| 585 | if (!enabled) { |
| 586 | /* We don't care about these. If we have an AccountService active, we |
| 587 | * will be receiving the same event though it. */ |
| 588 | return; |
| 589 | } |
| 590 | auto account = qobject_cast<Accounts::Account*>(sender()); |
| 591 | handleNewAccountService(account, m_manager.service(serviceId)); |
| 592 | } |
| 593 | |
| 594 | void ManagerPrivate::onAccountCreated(Accounts::AccountId accountId) |
| 595 | { |
| 596 | Accounts::Account *account = m_manager.account(accountId); |
| 597 | if (Q_UNLIKELY(!account)) return; |
| 598 | watchAccount(account); |
| 599 | Q_FOREACH(Accounts::Service service, account->enabledServices()) { |
| 600 | handleNewAccountService(account, service); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | void ManagerPrivate::onLoadRequest(uint accountId, const QString &serviceId) |
| 605 | { |
| 606 | AccessRequest *request = qobject_cast<AccessRequest*>(sender()); |
| 607 | |
| 608 | ActiveAccount &activeAccount = |
| 609 | addActiveAccount(accountId, serviceId, |
| 610 | request->context().clientName()); |
| 611 | auto as = activeAccount.accountService; |
| 612 | request->setAccountInfo(readAccountInfo(as), as->authData()); |
| 613 | } |
| 614 | |
| 615 | Manager::Manager(QObject *parent): |
| 616 | QObject(parent), |
| 617 | d_ptr(new ManagerPrivate(this)) |
| 618 | { |
| 619 | } |
| 620 | |
| 621 | Manager::~Manager() |
| 622 | { |
| 623 | delete d_ptr; |
| 624 | } |
| 625 | |
| 626 | bool Manager::isIdle() const |
| 627 | { |
| 628 | Q_D(const Manager); |
| 629 | return d->m_isIdle; |
| 630 | } |
| 631 | |
| 632 | QList<AccountInfo> Manager::getAccounts(const QVariantMap &filters, |
| 633 | const CallContext &context, |
| 634 | QList<QVariantMap> &services) |
| 635 | { |
| 636 | Q_D(Manager); |
| 637 | return d->getAccounts(filters, context, services); |
| 638 | } |
| 639 | |
| 640 | void Manager::authenticate(uint accountId, const QString &serviceId, |
| 641 | bool interactive, bool invalidate, |
| 642 | const QVariantMap ¶meters, |
| 643 | const CallContext &context) |
| 644 | { |
| 645 | Q_D(Manager); |
| 646 | d->authenticate(accountId, serviceId, interactive, invalidate, |
| 647 | parameters, context); |
| 648 | } |
| 649 | |
| 650 | void Manager::requestAccess(const QString &serviceId, |
| 651 | const QVariantMap ¶meters, |
| 652 | const CallContext &context) |
| 653 | { |
| 654 | Q_D(Manager); |
| 655 | d->requestAccess(serviceId, parameters, context); |
| 656 | } |
| 657 | |
| 658 | void Manager::onDisconnected() |
| 659 | { |
| 660 | qDebug() << "Disconnected from D-Bus: quitting"; |
| 661 | QCoreApplication::instance()->quit(); |
| 662 | } |
| 663 | |
| 664 | #include "manager.moc" |