Using new permissions system consequently

This commit is contained in:
WolverinDEV
2020-01-26 14:21:34 +01:00
parent bb2e7699dc
commit c7b6c0a3ba
37 changed files with 8982 additions and 657 deletions
+24 -118
View File
@@ -130,126 +130,32 @@ bool DataClient::loadDataForCurrentServer() { //TODO for query
return true;
}
permission::v2::PermissionFlaggedValue DataClient::permissionValueFlagged(permission::PermissionType type, const std::shared_ptr<BasicChannel>& target, std::shared_ptr<CalculateCache> cache, std::shared_ptr<TSServer> server, bool server_defined) {
if(!server_defined && !server)
server = this->server;
std::vector<std::pair<permission::PermissionType, permission::v2::PermissionFlaggedValue>> DataClient::calculate_permissions(
const std::deque<permission::PermissionType> &permissions,
ChannelId channel,
bool granted,
std::shared_ptr<CalculateCache> cache) {
if(permissions.empty()) return {};
if(server) {
auto result = server->calculatePermissions2(this->getClientDatabaseId(), {type}, this->getType(), target ? target->channelId() : 0, false, cache);
if(result.empty() || result[0].first != type)
return permission::v2::empty_permission_flagged_value;
return result[0].second;
} else {
/* if you're not bound to a channel then you cant be bound to a server as well. */
bool permission_set = false;
permission::PermissionValue result = permNotGranted;
for(const auto &gr : serverInstance->getGroupManager()->getServerGroups(this->getClientDatabaseId(), this->getType())){
auto group_permissions = gr->group->permissions();
auto flagged_permissions = group_permissions->permission_value_flagged(type);
if(flagged_permissions.has_value) {
if(flagged_permissions.value > result || flagged_permissions.value == -1) {
result = flagged_permissions.value;
permission_set = true;
}
}
}
return {result, permission_set};
}
if(!cache)
cache = std::make_shared<CalculateCache>();
if(!cache->client_permissions) /* so we don't have to load that shit later */
cache->client_permissions = this->clientPermissions;
if(channel == -1)
channel = this->currentChannel ? this->currentChannel->channelId() : 0;
auto ref_server = this->server;
if(ref_server)
return ref_server->calculate_permissions(permissions, this->getClientDatabaseId(), this->getType(), channel, granted, cache);
else
return serverInstance->calculate_permissions(permissions, this->getClientDatabaseId(), this->getType(), channel, granted, cache);
}
permission::PermissionValue DataClient::permissionValue(permission::PermissionTestType test, permission::PermissionType type, const std::shared_ptr<BasicChannel>& target, std::shared_ptr<CalculateCache> cache, std::shared_ptr<TSServer> server, bool server_defined) {
auto result = this->permissionValueFlagged(type, target, cache, server, server_defined);
if(result.has_value)
return result.value;
return permNotGranted;
}
std::deque<std::pair<permission::PermissionType, permission::PermissionValue>> DataClient::permissionValues(permission::PermissionTestType test, const std::deque<permission::PermissionType>& permissions, const std::shared_ptr<BasicChannel> &channel, std::shared_ptr<CalculateCache> cache, std::shared_ptr<TSServer> server, bool server_defined) {
if(!server_defined && !server) server = this->server;
if(server) return server->calculatePermissions(test, this->getClientDatabaseId(), permissions, this->getType(), channel, cache);
deque<pair<permission::PermissionType, permission::PermissionValue>> result;
/* when you're not bound to any channel cou could only have server group permissions */
/* we're loading here all server groups */
{
auto server_groups = serverInstance->getGroupManager()->getServerGroups(this->getClientDatabaseId(), this->getType());
for(const auto& permission : permissions) {
permission::PermissionValue value = permNotGranted;
for(const auto &gr : serverInstance->getGroupManager()->getServerGroups(this->getClientDatabaseId(), this->getType())){
auto group_permissions = gr->group->permissions();
auto flagged_permissions = group_permissions->permission_value_flagged(permission);
if(flagged_permissions.has_value)
if(flagged_permissions.value > value || flagged_permissions.value == -1) value = flagged_permissions.value;
}
result.emplace_back(permission, value);
}
}
return result;
}
permission::PermissionValue DataClient::getPermissionGrantValue(permission::PermissionTestType test, permission::PermissionType type, const std::shared_ptr<BasicChannel>& target) {
permission::PermissionValue result = permNotGranted;
if(this->server) {
return this->server->calculatePermissionGrant(test, this->getClientDatabaseId(), type, this->getType(), target);
} else {
/* if you're not bound to a channel then you cant be bound to a server as well. */
//TODO: Implement negate flag!
for(const auto &gr : serverInstance->getGroupManager()->getServerGroups(this->getClientDatabaseId(), this->getType())){
auto group_permissions = gr->group->permissions();
auto flagged_permissions = group_permissions->permission_granted_flagged(type);
if(flagged_permissions.has_value)
if(flagged_permissions.value > result || flagged_permissions.value == -1) result = flagged_permissions.value;
}
}
return result;
}
bool DataClient::permissionGranted(PermissionTestType test, permission::PermissionType type, permission::PermissionValue required, const shared_ptr<BasicChannel>& target, bool force_granted, std::shared_ptr<CalculateCache> cache, std::shared_ptr<TSServer> server, bool server_defined) {
auto value = this->permissionValue(test, type, target, cache, server, server_defined);
bool result = this->permission_granted(value, required, force_granted);
#ifdef DEBUG_PERMISSION
{
auto serverId = 0;
auto client = dynamic_cast<ConnectedClient*>(this);
if(client)
serverId = client->getServerId();
debugMessage(serverId, "[Permission] Value test result for test type {}.", test);
debugMessage(serverId,
"[Permission] Permission: {} Required value: {} Gained value: {} Force required: {} Channel: {} Result: {}",
permission::resolvePermissionData(type)->name,
required, value, force_granted, (target ? target->name() : "none"), result
);
};
#endif
return result;
}
bool DataClient::permissionGrantGranted(PermissionTestType test, permission::PermissionType type, permission::PermissionValue required, const shared_ptr<BasicChannel>& target, bool force_granted) {
auto value = this->getPermissionGrantValue(test, type, target);
bool result = this->permission_granted(value, required, force_granted);
#ifdef DEBUG_PERMISSION
{
auto serverId = 0;
auto client = dynamic_cast<ConnectedClient*>(this);
if(client)
serverId = client->getServerId();
debugMessage(serverId, "[Permission] Grant test result for test type {}.", test);
debugMessage(serverId,
"[Permission] Permission: {} Required value: {} Gained value: {} Force required: {} Channel: {} Result: {}",
permission::resolvePermissionData(type)->name,
required, value, force_granted, (target ? target->name() : "none"), result
);
};
#endif
return result;
permission::v2::PermissionFlaggedValue DataClient::calculate_permission(
permission::PermissionType permission, ChannelId channel, bool granted, std::shared_ptr<CalculateCache> cache) {
auto result = this->calculate_permissions({permission}, channel, granted, cache);
if(result.empty()) return {0, false};
return result.back().second;
}
std::vector<std::shared_ptr<GroupAssignment>> DataClient::assignedServerGroups() {
@@ -278,4 +184,4 @@ bool DataClient::channelGroupAssigned(const shared_ptr<Group> &group, const shar
std::string DataClient::getAvatarId() {
return hex::hex(base64::validate(this->getUid()) ? base64::decode(this->getUid()) : this->getUid(), 'a', 'q');
}
}