diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-07-18 19:21:15 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-08-18 23:25:46 +0200 |
commit | 5587f5afd5d21c2294ef7db8c4eebcdb68e5f2ac (patch) | |
tree | 08efbe0d3c5ae2d2971fcc4d3296156765485af1 | |
parent | 25c40e9448dd182913092876ebc341d8022c0997 (diff) |
Add method to get user lowest permission level
-rw-r--r-- | include/odhtdb/Database.hpp | 3 | ||||
-rw-r--r-- | include/odhtdb/DatabaseStorage.hpp | 2 | ||||
-rw-r--r-- | src/Database.cpp | 5 | ||||
-rw-r--r-- | src/DatabaseStorage.cpp | 14 |
4 files changed, 24 insertions, 0 deletions
diff --git a/include/odhtdb/Database.hpp b/include/odhtdb/Database.hpp index 85207a0..372b19c 100644 --- a/include/odhtdb/Database.hpp +++ b/include/odhtdb/Database.hpp @@ -209,6 +209,9 @@ namespace odhtdb MapHash<StoredNodeInfo> getStoredNodeUserInfoDecrypted(const std::string &username, const std::string &password) const; std::vector<OwnedByteArray> getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const; + + // Returns -1 on failure + int getUserLowestPermissionLevel(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const; std::future<size_t> receiveCustomMessage(const dht::InfoHash &requestKey, ReceiveCustomMessageCallbackFunc callbackFunc); diff --git a/include/odhtdb/DatabaseStorage.hpp b/include/odhtdb/DatabaseStorage.hpp index 757fbfd..b84635c 100644 --- a/include/odhtdb/DatabaseStorage.hpp +++ b/include/odhtdb/DatabaseStorage.hpp @@ -114,6 +114,8 @@ namespace odhtdb bool isUserAllowedToAddDataInNode(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const; bool isUserAllowedToAddUserToGroupInNode(const Hash &nodeHash, const Signature::PublicKey &userPublicKey, const DataView &groupToAddUserTo) const; + // Returns -1 on failure + int getUserLowestPermissionLevel(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const; // Throws DatabaseStorageNotFound if user doesn't exist in node u64 getUserActionCounter(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const; diff --git a/src/Database.cpp b/src/Database.cpp index a05aae6..564b3d6 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -714,6 +714,11 @@ namespace odhtdb { return databaseStorage.getUserGroups(nodeHash, userPublicKey); } + + int Database::getUserLowestPermissionLevel(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const + { + return databaseStorage.getUserLowestPermissionLevel(nodeHash, userPublicKey); + } std::future<size_t> Database::receiveCustomMessage(const dht::InfoHash &receiveMessageKey, ReceiveCustomMessageCallbackFunc callbackFunc) { diff --git a/src/DatabaseStorage.cpp b/src/DatabaseStorage.cpp index 407aa5c..9398254 100644 --- a/src/DatabaseStorage.cpp +++ b/src/DatabaseStorage.cpp @@ -825,6 +825,20 @@ namespace odhtdb { DataView(nodeHash.getData(), nodeHash.getSize()), DataView((void*)userPublicKey.getData(), userPublicKey.getSize()), groupToAddPermissionLevel, (i64)PermissionType::ADD_USER_SAME_LEVEL, groupToAddPermissionLevel, (i64)PermissionType::ADD_USER_HIGHER_LEVEL }); return queryCreatorGroupWithRightsToAddUserToGroup.next(); } + + int DatabaseStorage::getUserLowestPermissionLevel(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const + { + SqlQuery query(sqliteDb, "SELECT MIN(nodeGroup.permissionLevel) FROM NodeUserGroupAssoc AS userGroupAssoc" + " INNER JOIN NodeGroup AS nodeGroup ON nodeGroup.groupId = userGroupAssoc.groupId" + " WHERE userGroupAssoc.node = ? AND userGroupAssoc.userPublicKey = ?", + { DataView(nodeHash.getData(), nodeHash.getSize()), DataView((void*)userPublicKey.getData(), userPublicKey.getSize()) }); + if(!query.next()) + { + Log::error("There is no node %s, no user %s in that node or user does not belong to any groups", nodeHash.toString().c_str(), userPublicKey.toString().c_str()); + return -1; + } + return query.getInt(0); + } u64 DatabaseStorage::getUserActionCounter(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const { |