aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-05-16 05:29:39 +0200
committerdec05eba <dec05eba@protonmail.com>2020-08-18 23:25:46 +0200
commitc0574f16ca7960a3301230ba36ba96148818e229 (patch)
tree120eb7a3346adb772c6df015dfd8257e418eb5dd
parentaa07b493ac6259e3ed0c42b60fca1f84ee09799f (diff)
Add method to get user groups
-rw-r--r--include/odhtdb/Database.hpp2
-rw-r--r--include/odhtdb/DatabaseStorage.hpp2
-rw-r--r--src/Database.cpp5
-rw-r--r--src/DatabaseStorage.cpp14
-rw-r--r--tests/main.cpp8
5 files changed, 31 insertions, 0 deletions
diff --git a/include/odhtdb/Database.hpp b/include/odhtdb/Database.hpp
index ca9369f..a68c601 100644
--- a/include/odhtdb/Database.hpp
+++ b/include/odhtdb/Database.hpp
@@ -189,6 +189,8 @@ namespace odhtdb
// Returns nodes, public key and private key of encrypted user.
// Throws DatabaseStorageWrongPassword if password for the stored user is wrong.
std::vector<NodeUserKeyPair> getStoredUserNodeDataDecrypted(const std::string &username, const std::string &password);
+
+ std::vector<OwnedMemory> getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const;
private:
void deserializeCreateRequest(const std::shared_ptr<dht::Value> &value, const Hash &hash, const std::shared_ptr<OwnedMemory> encryptionKey);
void deserializeAddRequest(const std::shared_ptr<dht::Value> &value, const Hash &requestDataHash, const std::shared_ptr<Hash> &nodeHash, const std::shared_ptr<OwnedMemory> encryptionKey);
diff --git a/include/odhtdb/DatabaseStorage.hpp b/include/odhtdb/DatabaseStorage.hpp
index f03bd9e..2b1e552 100644
--- a/include/odhtdb/DatabaseStorage.hpp
+++ b/include/odhtdb/DatabaseStorage.hpp
@@ -140,6 +140,8 @@ namespace odhtdb
const std::vector<dht::NodeExport>& getRemoteNodes() const;
void setRemoteNodes(const std::vector<dht::NodeExport> &remoteNodes);
+ std::vector<OwnedMemory> getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const;
+
const dht::crypto::Identity& getIdentity() const;
// Update storage state (remove quarantine objects if they are too old, etc)
diff --git a/src/Database.cpp b/src/Database.cpp
index 82cb21e..241d9a5 100644
--- a/src/Database.cpp
+++ b/src/Database.cpp
@@ -665,4 +665,9 @@ namespace odhtdb
{
return databaseStorage.getStoredUserNodeDataDecrypted(username, password);
}
+
+ vector<OwnedMemory> Database::getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const
+ {
+ return databaseStorage.getUserGroups(nodeHash, userPublicKey);
+ }
}
diff --git a/src/DatabaseStorage.cpp b/src/DatabaseStorage.cpp
index 3140c96..ed190e8 100644
--- a/src/DatabaseStorage.cpp
+++ b/src/DatabaseStorage.cpp
@@ -1025,6 +1025,20 @@ namespace odhtdb
fileOverwrite(remoteNodesFilePath, DataView(remoteNodePacker.serializer.getBuffer().data(), remoteNodePacker.serializer.getBuffer().size()));
}
+ vector<OwnedMemory> DatabaseStorage::getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const
+ {
+ vector<OwnedMemory> result;
+ SqlQuery query(sqliteDb, "SELECT groupId FROM NodeUserGroupAssoc WHERE node = ? AND userPublicKey = ?", { DataView(nodeHash.getData(), nodeHash.getSize()), DataView((void*)userPublicKey.getData(), userPublicKey.getSize()) });
+ while(query.next())
+ {
+ const DataView groupIdRaw = query.getBlob(0);
+ OwnedMemory groupId(new u8[groupIdRaw.size], groupIdRaw.size);
+ memcpy(groupId.data, groupIdRaw.data, groupIdRaw.size);
+ result.emplace_back(move(groupId));
+ }
+ return result;
+ }
+
bool DatabaseStorage::decryptNodeData(const Hash &nodeHash, const shared_ptr<OwnedMemory> decryptionKey)
{
sqlite3_reset(selectNodeStmt);
diff --git a/tests/main.cpp b/tests/main.cpp
index 5374c2d..dd56164 100644
--- a/tests/main.cpp
+++ b/tests/main.cpp
@@ -241,6 +241,14 @@ int main()
database.storeUserWithoutNodes("anotherUser", password);
if(!database.doesStoredUserExist("anotherUser"))
fail("Added user 'anotherUser' to database without any nodes, but it doesn't seem to be stored");
+
+ auto adminUserGroups = database.getUserGroups(*databaseNode.getRequestHash(), adminUserKey->getPublicKey());
+ if(adminUserGroups.size() != 1 || adminUserGroups[0].getView() != databaseCreateResponse->getNodeAdminGroupId()->getView())
+ fail("Admin group doesn't match group stored in database");
+
+ auto userGroups = database.getUserGroups(*databaseNode.getRequestHash(), localUserKeyPair.getPublicKey());
+ if(userGroups.size() != 1 || userGroups[0].getView() != databaseCreateResponse->getNodeAdminGroupId()->getView())
+ fail("User group doesn't match group stored in database");
}
Log::debug("Callback works when adding data while connected, now testing to reconnect and check if data remains...");
{