diff options
author | Jędrzej Nowacki <jedrzej.nowacki@digia.com> | 2014-04-29 19:05:19 +0000 |
---|---|---|
committer | Jędrzej Nowacki <jedrzej.nowacki@digia.com> | 2014-08-08 11:01:15 +0200 |
commit | e7c189f13d0fc0d72cbcaf1770055da252e0fb3f (patch) | |
tree | cc944bc0a884b66534567994985df7f85d259bd8 | |
parent | 18d4153168e5787b1c1a49bb68207f7f5d6042af (diff) | |
download | qtenginio-e7c189f13d0fc0d72cbcaf1770055da252e0fb3f.tar.gz |
Automatically refresh EnginioModel if identity was changed
The server automatically filter data based on ACL. It means that for
example after a succesfull login, a query result may be different.
[ChangeLog][Enginio] Automatically refresh EnginioModel contents
when identity changes.
Change-Id: Ied814d00b8cfb872fcd2ab2e6e7f51692e894890
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@digia.com>
-rw-r--r-- | src/enginio_client/enginiobasemodel_p.h | 21 | ||||
-rw-r--r-- | tests/auto/enginiomodel/tst_enginiomodel.cpp | 35 |
2 files changed, 56 insertions, 0 deletions
diff --git a/src/enginio_client/enginiobasemodel_p.h b/src/enginio_client/enginiobasemodel_p.h index cf0fd49..c815aad 100644 --- a/src/enginio_client/enginiobasemodel_p.h +++ b/src/enginio_client/enginiobasemodel_p.h @@ -399,6 +399,26 @@ protected: } }; + class RefreshQueryAfterAuthChange // It is needed for compilers that don't support variadic templates + { + EnginioBaseModelPrivate *model; + public: + RefreshQueryAfterAuthChange(EnginioBaseModelPrivate *m) + : model(m) + { + Q_ASSERT(m); + } + + void operator ()(Enginio::AuthenticationState state) const + { + // TODO we do not want to refresh on a failed attempt to login + if (state == Enginio::NotAuthenticated // logout + || state == Enginio::Authenticated // successful login + || state == Enginio::AuthenticationFailure) // token refresh failed + model->execute(); + } + }; + public: EnginioBaseModelPrivate(EnginioBaseModel *q_ptr) : _enginio(0) @@ -958,6 +978,7 @@ struct EnginioModelPrivateT : public EnginioBaseModelPrivate _enginio = EnginioClientConnectionPrivate::get(const_cast<EnginioClientConnection*>(enginio)); _clientConnections.append(QObject::connect(enginio, &QObject::destroyed, EnginioDestroyed(this))); _clientConnections.append(QObject::connect(enginio, &EnginioClientConnection::backendIdChanged, QueryChanged(this))); + _clientConnections.append(QObject::connect(enginio, &EnginioClientConnection::authenticationStateChanged, RefreshQueryAfterAuthChange(this))); } else { _enginio = 0; } diff --git a/tests/auto/enginiomodel/tst_enginiomodel.cpp b/tests/auto/enginiomodel/tst_enginiomodel.cpp index 744ce12..47ad81b 100644 --- a/tests/auto/enginiomodel/tst_enginiomodel.cpp +++ b/tests/auto/enginiomodel/tst_enginiomodel.cpp @@ -50,6 +50,7 @@ #include <Enginio/enginioreply.h> #include <Enginio/enginiomodel.h> #include <Enginio/enginioidentity.h> +#include <Enginio/enginiooauth2authentication.h> #include "../common/common.h" @@ -99,6 +100,7 @@ private slots: void updatingRoles(); void setData(); void reload(); + void identityChange(); private: template<class T> @@ -1687,5 +1689,38 @@ void tst_EnginioModel::reload() QCOMPARE(model.rowCount(), 0); } +void tst_EnginioModel::identityChange() +{ + EnginioClient client; + QObject::connect(&client, SIGNAL(error(EnginioReply *)), this, SLOT(error(EnginioReply *))); + client.setBackendId("5376019e698b3c6ad500095a"); + + QJsonObject query; + query["objectType"] = "objects.EnginioModelIdentityChange"; + + EnginioModel model; + model.setQuery(query); + model.setClient(&client); + + EnginioOAuth2Authentication identity1; + identity1.setUser("test1"); + identity1.setPassword("test1"); + + EnginioOAuth2Authentication identity2; + identity2.setUser("test2"); + identity2.setPassword("test2"); + + QTRY_COMPARE(model.rowCount(), 1); // There is only one publicly visible element + + client.setIdentity(&identity1); + QTRY_COMPARE(model.rowCount(), 2); // Test1 sees one additional element + + client.setIdentity(&identity2); + QTRY_COMPARE(model.rowCount(), 3); // Test2 sees two additional elements + + client.setIdentity(0); + QTRY_COMPARE(model.rowCount(), 1); +} + QTEST_MAIN(tst_EnginioModel) #include "tst_enginiomodel.moc" |