Teaspeak-Server/server/src/DatabaseHelper.h

131 lines
6.4 KiB
C
Raw Normal View History

#pragma once
#include <Definitions.h>
#include <map>
#include <vector>
#include <chrono>
#include <PermissionManager.h>
#include <Properties.h>
#include <cstdint>
namespace ts {
namespace server {
class TSServer;
class DataClient;
struct ClientDatabaseInfo {
ServerId sid;
ClientDbId cldbid;
std::chrono::time_point<std::chrono::system_clock> created;
std::chrono::time_point<std::chrono::system_clock> lastjoin;
std::string uniqueId;
std::string lastName;
uint32_t connections;
};
struct CachedPermissionManager {
ServerId sid;
ClientDbId cldbid;
std::weak_ptr<permission::v2::PermissionManager> manager;
std::shared_ptr<permission::v2::PermissionManager> ownLock;
std::chrono::time_point<std::chrono::system_clock> lastAccess;
};
struct CachedProperties {
ServerId sid;
ClientDbId cldbid;
std::weak_ptr<Properties> properties;
std::shared_ptr<Properties> ownLock;
std::chrono::time_point<std::chrono::system_clock> lastAccess;
};
/*
CREATE_TABLE("properties", "`serverId` INTEGER DEFAULT -1, `type` INTEGER, `id` INTEGER, `key` VARCHAR(" UNKNOWN_KEY_LENGTH "), `value` TEXT", command_append_utf8);
2020-01-23 20:57:58 -05:00
CREATE_TABLE("permissions", "`serverId` INT NOT NULL, `type` INT, `id` INT, `channelId` INT, `permId` VARCHAR(" UNKNOWN_KEY_LENGTH "), `value` INT, `grant` INT", command_append_utf8);
*/
struct StartupPermissionEntry {
permission::PermissionSqlType type = permission::SQL_PERM_CHANNEL;
uint64_t id = 0;
ChannelId channelId = 0;
std::shared_ptr<permission::PermissionTypeEntry> permission = permission::PermissionTypeEntry::unknown;
permission::PermissionValue value = 0;
permission::PermissionValue grant = 0;
bool flag_skip = false;
bool flag_negate = false;
};
struct StartupPropertyEntry {
property::PropertyType type = property::PropertyType::PROP_TYPE_UNKNOWN;
uint64_t id = 0;
std::shared_ptr<property::PropertyDescription> info = property::PropertyDescription::unknown;
std::string value;
};
struct StartupCacheEntry {
ServerId sid;
std::deque<std::unique_ptr<StartupPermissionEntry>> permissions;
std::deque<std::unique_ptr<StartupPropertyEntry>> properties;
};
struct FastPropertyEntry {
2020-01-23 20:57:58 -05:00
std::shared_ptr<property::PropertyDescription> type;
std::string value;
};
class DatabaseHelper {
public:
2019-10-16 15:11:54 -04:00
static std::shared_ptr<Properties> default_properties_client(std::shared_ptr<Properties> /* properties */, ClientType /* type */);
static bool assignDatabaseId(sql::SqlManager *, ServerId id, std::shared_ptr<DataClient>);
explicit DatabaseHelper(sql::SqlManager*);
~DatabaseHelper();
void loadStartupCache();
size_t cacheBinarySize();
void clearStartupCache(ServerId sid = 0);
void deleteClient(const std::shared_ptr<TSServer>&,ClientDbId);
bool validClientDatabaseId(const std::shared_ptr<TSServer>&, ClientDbId);
std::deque<std::shared_ptr<ClientDatabaseInfo>> queryDatabaseInfo(const std::shared_ptr<TSServer>&, const std::deque<ClientDbId>&);
std::deque<std::shared_ptr<ClientDatabaseInfo>> queryDatabaseInfoByUid(const std::shared_ptr<TSServer> &, std::deque<std::string>);
std::shared_ptr<permission::v2::PermissionManager> loadClientPermissionManager(const std::shared_ptr<TSServer>&, ClientDbId); //Just and write
2020-01-23 20:57:58 -05:00
void saveClientPermissions(const std::shared_ptr<TSServer>&, ClientDbId , const std::shared_ptr<permission::v2::PermissionManager>& /* permission manager */);
std::shared_ptr<permission::v2::PermissionManager> loadChannelPermissions(const std::shared_ptr<TSServer>&, ChannelId); //Just read
2020-01-23 20:57:58 -05:00
void saveChannelPermissions(const std::shared_ptr<TSServer>&, ChannelId, const std::shared_ptr<permission::v2::PermissionManager>& /* permission manager */);
2020-01-23 20:57:58 -05:00
std::shared_ptr<permission::v2::PermissionManager> loadGroupPermissions(const std::shared_ptr<TSServer>&, GroupId); //Just read
void saveGroupPermissions(const std::shared_ptr<TSServer>&, GroupId, const std::shared_ptr<permission::v2::PermissionManager>& /* permission manager */);
2020-01-23 20:57:58 -05:00
std::shared_ptr<permission::PermissionManager> loadPlaylistPermissions(const std::shared_ptr<TSServer>&, PlaylistId /* playlist id */); //Read and write
std::shared_ptr<Properties> loadServerProperties(const std::shared_ptr<TSServer>&); //Read and write
2020-01-23 20:57:58 -05:00
std::shared_ptr<Properties> loadPlaylistProperties(const std::shared_ptr<TSServer>&, PlaylistId); //Read and write
std::shared_ptr<Properties> loadChannelProperties(const std::shared_ptr<TSServer>&, ChannelId); //Read and write
std::shared_ptr<Properties> loadClientProperties(const std::shared_ptr<TSServer>&, ClientDbId, ClientType);
bool deleteGroupPermissions(const std::shared_ptr<TSServer>&, GroupId);
2020-01-23 20:57:58 -05:00
bool deleteChannelPermissions(const std::shared_ptr<TSServer>&, ChannelId);
bool deletePlaylist(const std::shared_ptr<TSServer>&, PlaylistId /* playlist id */);
std::deque<std::unique_ptr<FastPropertyEntry>> query_properties(ServerId /* server */, property::PropertyType /* type */, uint64_t /* id */); /* required for server snapshots */
void tick();
private:
void loadStartupPermissionCache();
void loadStartupPropertyCache();
bool use_startup_cache = false;
threads::Mutex startup_lock;
std::deque<std::shared_ptr<StartupCacheEntry>> startup_entries;
sql::SqlManager* sql = nullptr;
threads::Mutex permManagerLock;
std::deque<CachedPermissionManager*> cachedPermissionManagers;
threads::Mutex propsLock;
std::deque<CachedProperties*> cachedProperties;
};
}
}