Some minor fixes

This commit is contained in:
WolverinDEV 2020-08-19 14:50:42 +02:00
parent 28b13093f6
commit a80c90f025
5 changed files with 57 additions and 23 deletions

View File

@ -452,6 +452,9 @@ bool GroupManager::deleteGroup(std::shared_ptr<Group> group) {
flag_sql |= !res; flag_sql |= !res;
serverInstance->databaseHelper()->deleteGroupArtifacts(this->getServerId(), group->groupId()); serverInstance->databaseHelper()->deleteGroupArtifacts(this->getServerId(), group->groupId());
if(auto server{this->server.lock()}; server)
server->getTokenManager()->handleGroupDelete(group->groupId());
if(flag_sql) if(flag_sql)
logError(this->getServerId(), "Could not delete group {} ({}) from database. May leader to invalid data", group->name(), group->groupId()); logError(this->getServerId(), "Could not delete group {} ({}) from database. May leader to invalid data", group->name(), group->groupId());

View File

@ -185,6 +185,10 @@ namespace ts {
inline ServerChannelTree* getChannelTree(){ return this->channelTree; } inline ServerChannelTree* getChannelTree(){ return this->channelTree; }
inline GroupManager* getGroupManager() { return this->groups; } inline GroupManager* getGroupManager() { return this->groups; }
[[nodiscard]] inline auto getTokenManager() -> token::TokenManager* {
return this->tokenManager;
}
bool notifyServerEdited(std::shared_ptr<ConnectedClient>, std::deque<std::string> keys); bool notifyServerEdited(std::shared_ptr<ConnectedClient>, std::deque<std::string> keys);
bool notifyClientPropertyUpdates(std::shared_ptr<ConnectedClient>, const std::deque<const property::PropertyDescription*>& keys, bool selfNotify = true); /* execute only with at least channel tree read lock! */ bool notifyClientPropertyUpdates(std::shared_ptr<ConnectedClient>, const std::deque<const property::PropertyDescription*>& keys, bool selfNotify = true); /* execute only with at least channel tree read lock! */
inline bool notifyClientPropertyUpdates(const std::shared_ptr<ConnectedClient>& client, const std::deque<property::ClientProperties>& keys, bool selfNotify = true) { inline bool notifyClientPropertyUpdates(const std::shared_ptr<ConnectedClient>& client, const std::deque<property::ClientProperties>& keys, bool selfNotify = true) {

View File

@ -541,11 +541,11 @@ bool SqlDataManager::update_database(std::string &error) {
} }
case 14: { case 14: {
constexpr static std::array<std::string_view, 5> kUpdateCommands{ constexpr static std::array<std::string_view, 5> kUpdateCommands{
"CREATE TABLE general_dg_tmp(id INTEGER NOT NULL PRIMARY KEY [AUTO_INCREMENT], key VARCHAR(256), value TEXT);", "CREATE TABLE general_dg_tmp(`id` INTEGER NOT NULL PRIMARY KEY [AUTO_INCREMENT], `key` VARCHAR(256), `value` TEXT);",
"INSERT INTO general_dg_tmp(key, value) SELECT key, value FROM general;", "INSERT INTO general_dg_tmp(`key`, `value`) SELECT `key`, `value` FROM `general`;",
"DROP TABLE general;", "DROP TABLE `general`;",
"ALTER TABLE general_dg_tmp rename TO general;", "ALTER TABLE `general_dg_tmp` RENAME TO `general`;",
"CREATE UNIQUE INDEX general_id_uindex ON general (id);" "CREATE UNIQUE INDEX `general_id_uindex` ON `general` (`id`);"
}; };
if(!execute_commands(this->sql(), error, kUpdateCommands)) if(!execute_commands(this->sql(), error, kUpdateCommands))

View File

@ -6,6 +6,7 @@
#include <sqlite3.h> #include <sqlite3.h>
#include <iostream> #include <iostream>
#include <random> #include <random>
#include <utility>
#include <misc/rnd.h> #include <misc/rnd.h>
#include <log/LogUtils.h> #include <log/LogUtils.h>
#include "TokeManager.h" #include "TokeManager.h"
@ -21,11 +22,10 @@ TokenManager::TokenManager(server::VirtualServer* handle) : handle(handle) {
} }
TokenManager::~TokenManager() {} TokenManager::~TokenManager() = default;
bool TokenManager::loadTokens() { bool TokenManager::loadTokens() {
auto fn = LOG_SQL_CMD; LOG_SQL_CMD(sql::command(handle->getSql(), "SELECT * FROM `tokens` WHERE `serverId` = :sid", variable{":sid", handle->getServerId()}).query(&TokenManager::loadTokenFromDb, this));
fn(sql::command(handle->getSql(), "SELECT * FROM `tokens` WHERE `serverId` = :sid", variable{":sid", handle->getServerId()}).query(&TokenManager::loadTokenFromDb, this));
return true; return true;
} }
@ -59,15 +59,22 @@ int TokenManager::loadTokenFromDb(int length, char **values, char **columns) {
assert(group != 0); assert(group != 0);
assert(created.time_since_epoch().count() != 0); assert(created.time_since_epoch().count() != 0);
this->tokens.push_back(make_shared<TokenEntry>(token, type, group, chId, system_clock::now(), description)); auto token_entry = std::make_shared<TokenEntry>(token, type, group, chId, system_clock::now(), description);
{
std::lock_guard tlock{this->token_lock};
this->tokens.push_back(token_entry);
}
return 0; return 0;
} }
std::shared_ptr<TokenEntry> TokenManager::createToken(TokenType type, GroupId group, std::string description, ChannelId chid, std::string token) { std::shared_ptr<TokenEntry> TokenManager::createToken(TokenType type, GroupId group, const std::string& description, ChannelId chid, std::string token) {
token = token.empty() ? rnd_string(20) : token; token = token.empty() ? rnd_string(20) : token;
shared_ptr<TokenEntry> entry = make_shared<TokenEntry>(token, type, group, chid, system_clock::now(), description); shared_ptr<TokenEntry> entry = make_shared<TokenEntry>(token, type, group, chid, system_clock::now(), description);
{
std::lock_guard tlock{this->token_lock};
this->tokens.push_back(entry); this->tokens.push_back(entry);
}
auto res = sql::command(handle->getSql(), "INSERT INTO `tokens` (`serverId`, `type`, `token`, `targetGroup`, `targetChannel`, `description`, `created`) VALUES (:sid, :type, :token, :targetGroup, :targetChannel, :desc, :created);", auto res = sql::command(handle->getSql(), "INSERT INTO `tokens` (`serverId`, `type`, `token`, `targetGroup`, `targetChannel`, `description`, `created`) VALUES (:sid, :type, :token, :targetGroup, :targetChannel, :desc, :created);",
variable{":sid", this->handle->getServerId()}, variable{":sid", this->handle->getServerId()},
@ -84,9 +91,16 @@ std::shared_ptr<TokenEntry> TokenManager::createToken(TokenType type, GroupId gr
return entry; return entry;
} }
bool TokenManager::deleteToke(const std::string& str) { bool TokenManager::deleteToke(const std::string& str) {
auto token = this->findToken(str); std::shared_ptr<TokenEntry> token;
if(!token) return false; {
this->tokens.erase(std::find(this->tokens.begin(), this->tokens.end(), token)); std::lock_guard tlock{this->token_lock};
auto it = std::find_if(this->tokens.begin(), this->tokens.end(), [&](const auto& token) { return token->token == str; });
if(it == this->tokens.end())
return false;
token = std::move(*it);
this->tokens.erase(it);
}
auto res = sql::command(handle->getSql(), "DELETE FROM `tokens` WHERE `token` = :token", variable{":token", token->token}).execute(); auto res = sql::command(handle->getSql(), "DELETE FROM `tokens` WHERE `token` = :token", variable{":token", token->token}).execute();
auto pf = LOG_SQL_CMD; auto pf = LOG_SQL_CMD;
@ -95,11 +109,20 @@ bool TokenManager::deleteToke(const std::string& str) {
return true; return true;
} }
std::shared_ptr<TokenEntry> TokenManager::findToken(std::string str) { void TokenManager::handleGroupDelete(GroupId group_id) {
for(auto& elm : this->tokens) std::lock_guard tlock{this->token_lock};
if(elm->token == str) return elm; this->tokens.erase(std::remove_if(this->tokens.begin(), this->tokens.end(), [&](const std::shared_ptr<TokenEntry>& token) {
return token->groupId == group_id;
}), this->tokens.end());
}
std::shared_ptr<TokenEntry> TokenManager::findToken(const std::string& str) {
std::lock_guard tlock{this->token_lock};
for(const auto& elm : this->tokens)
if(elm->token == str)
return elm;
return nullptr; return nullptr;
} }
TokenEntry::TokenEntry(const string &token, TokenType type, GroupId groupId, ChannelId channelId, const time_point<system_clock> &created, const string &description) : token(token), type(type), groupId(groupId), channelId(channelId), TokenEntry::TokenEntry(string token, TokenType type, GroupId groupId, ChannelId channelId, const time_point<system_clock> &created, string description) : token(std::move(token)), type(type), groupId(groupId), channelId(channelId),
created(created), description(description) {} created(created), description(std::move(description)) {}

View File

@ -17,7 +17,7 @@ namespace ts {
TOKEN_CHANNEL TOKEN_CHANNEL
}; };
struct TokenEntry { struct TokenEntry {
TokenEntry(const std::string &token, TokenType type, GroupId groupId, ChannelId channelId, const std::chrono::time_point<std::chrono::system_clock> &created, const std::string &description); TokenEntry(std::string token, TokenType type, GroupId groupId, ChannelId channelId, const std::chrono::time_point<std::chrono::system_clock> &created, std::string description);
std::string token; std::string token;
TokenType type; //token_type TokenType type; //token_type
@ -29,17 +29,21 @@ namespace ts {
class TokenManager { class TokenManager {
public: public:
TokenManager(server::VirtualServer*); explicit TokenManager(server::VirtualServer*);
~TokenManager(); ~TokenManager();
bool loadTokens(); bool loadTokens();
std::vector<std::shared_ptr<TokenEntry>> avariableTokes(){ return tokens; } std::vector<std::shared_ptr<TokenEntry>> avariableTokes(){ return tokens; }
std::shared_ptr<TokenEntry> findToken(std::string); std::shared_ptr<TokenEntry> findToken(const std::string&);
std::shared_ptr<TokenEntry> createToken(TokenType, GroupId, std::string, ChannelId = 0, std::string token = ""); std::shared_ptr<TokenEntry> createToken(TokenType, GroupId, const std::string&, ChannelId = 0, std::string token = "");
bool deleteToke(const std::string&); bool deleteToke(const std::string&);
void handleGroupDelete(GroupId);
private: private:
int loadTokenFromDb(int length, char** values, char** columns); int loadTokenFromDb(int length, char** values, char** columns);
server::VirtualServer* handle; server::VirtualServer* handle;
std::mutex token_lock{};
std::vector<std::shared_ptr<TokenEntry>> tokens; std::vector<std::shared_ptr<TokenEntry>> tokens;
}; };
} }