From c8a6449a45a0c2de9cf5120ae5498deae02b4bec Mon Sep 17 00:00:00 2001 From: WolverinDEV Date: Mon, 9 Sep 2019 14:02:47 +0200 Subject: [PATCH] fixed the default setup --- server/src/InstanceHandlerSetup.cpp | 165 ++++++++++++++-------------- 1 file changed, 83 insertions(+), 82 deletions(-) diff --git a/server/src/InstanceHandlerSetup.cpp b/server/src/InstanceHandlerSetup.cpp index ce443ab..298efd8 100644 --- a/server/src/InstanceHandlerSetup.cpp +++ b/server/src/InstanceHandlerSetup.cpp @@ -23,7 +23,7 @@ struct GroupInfo { * 2 = Channel */ int target; - string propertyName; + std::deque properties; string name; /* permission type, value, granted, skip, negate */ deque> permissions; @@ -31,94 +31,94 @@ struct GroupInfo { /* TODO may use a transaction here? */ bool InstanceHandler::setupDefaultGroups() { - debugMessage(LOG_INSTANCE, "Creating new instance groups"); + debugMessage(LOG_INSTANCE, "Creating new instance groups"); deque> groups; - ifstream in(PERMISSION_TEMPLATE_FILE); - if(!in) { - logCritical(LOG_INSTANCE, "Could not open default permissions file {}", PERMISSION_TEMPLATE_FILE); - return false; - } - string line; - while(getline(in, line)){ - TEST_COMMENT; + ifstream in(PERMISSION_TEMPLATE_FILE); + if(!in) { + logCritical(LOG_INSTANCE, "Could not open default permissions file {}", PERMISSION_TEMPLATE_FILE); + return false; + } + string line; + while(getline(in, line)){ + TEST_COMMENT; - if(line != "--start") { - logCritical(LOG_INSTANCE, R"(Permission template file contains invalid start line ("{}", expected "{}")!)", line, "--start"); - return false; - } - auto group = make_shared(); - while(true){ - getline(in, line); - TEST_COMMENT; + if(line != "--start") { + logCritical(LOG_INSTANCE, R"(Permission template file contains invalid start line ("{}", expected "{}")!)", line, "--start"); + return false; + } + auto group = make_shared(); + while(true){ + getline(in, line); + TEST_COMMENT; - if(line == "--end") break; - if(line.find("name:") == 0) { - group->name = line.substr(5); - continue; - } - if(line.find("target:") == 0) { - group->target = stoi(line.substr(7)); - continue; - } - if(line.find("property:") == 0) { - group->propertyName = line.substr(9); - continue; - } + if(line == "--end") break; + if(line.find("name:") == 0) { + group->name = line.substr(5); + continue; + } + if(line.find("target:") == 0) { + group->target = stoi(line.substr(7)); + continue; + } + if(line.find("property:") == 0) { + group->properties.push_back(line.substr(9)); + continue; + } - if(line.find("permission:") == 0) { - line = line.substr(11); - auto assign_index = line.find('='); + if(line.find("permission:") == 0) { + line = line.substr(11); + auto assign_index = line.find('='); - string permission_name = line.substr(0, assign_index); - string string_value = line.substr(assign_index + 1); - string string_granted, string_skip, string_negate; + string permission_name = line.substr(0, assign_index); + string string_value = line.substr(assign_index + 1); + string string_granted, string_skip, string_negate; - if(string_value.find(',') != -1) { - string_granted = string_value.substr(string_value.find(',') + 1); - string_value = string_value.substr(0, string_value.find(',')); - } - if(string_granted.find(',') != -1) { - string_skip = string_granted.substr(string_granted.find(',') + 1); - string_granted = string_granted.substr(0, string_granted.find(',')); - } - if(string_skip.find(',') != -1) { - string_negate = string_skip.substr(string_skip.find(',') + 1); - string_skip = string_skip.substr(0, string_skip.find(',')); - } + if(string_value.find(',') != -1) { + string_granted = string_value.substr(string_value.find(',') + 1); + string_value = string_value.substr(0, string_value.find(',')); + } + if(string_granted.find(',') != -1) { + string_skip = string_granted.substr(string_granted.find(',') + 1); + string_granted = string_granted.substr(0, string_granted.find(',')); + } + if(string_skip.find(',') != -1) { + string_negate = string_skip.substr(string_skip.find(',') + 1); + string_skip = string_skip.substr(0, string_skip.find(',')); + } - auto permInfo = permission::resolvePermissionData(permission_name); - if(permInfo->type == permission::unknown){ - logError(LOG_INSTANCE, "Default permission file contains unknown permission. Key: {}", permission_name); - continue; - } + auto permInfo = permission::resolvePermissionData(permission_name); + if(permInfo->type == permission::unknown){ + logError(LOG_INSTANCE, "Default permission file contains unknown permission. Key: {}", permission_name); + continue; + } - permission::PermissionValue permission_value; - try { - permission_value = stoi(string_value); - } catch(std::exception& ex) { - logError(LOG_INSTANCE, "Failed to parse value for key {}. Value: {}", permission_name, string_value); - continue; - } + permission::PermissionValue permission_value; + try { + permission_value = stoi(string_value); + } catch(std::exception& ex) { + logError(LOG_INSTANCE, "Failed to parse value for key {}. Value: {}", permission_name, string_value); + continue; + } - permission::PermissionValue permission_granted = permNotGranted; - if(!string_granted.empty()) { - try { - permission_granted = stoi(string_granted); - } catch(std::exception& ex) { - logError(LOG_INSTANCE, "Failed to parse granted value for key {}. Value: {}", permission_name, string_granted); - continue; - } - } + permission::PermissionValue permission_granted = permNotGranted; + if(!string_granted.empty()) { + try { + permission_granted = stoi(string_granted); + } catch(std::exception& ex) { + logError(LOG_INSTANCE, "Failed to parse granted value for key {}. Value: {}", permission_name, string_granted); + continue; + } + } - bool flag_skip = string_skip == "true" || string_skip == "1"; - bool flag_negate = string_negate == "true" || string_negate == "1"; + bool flag_skip = string_skip == "true" || string_skip == "1"; + bool flag_negate = string_negate == "true" || string_negate == "1"; - group->permissions.emplace_back(make_tuple(permInfo->type, permission_value, permission_granted, flag_skip, flag_negate)); - } - } - groups.push_back(group); - } + group->permissions.emplace_back(make_tuple(permInfo->type, permission_value, permission_granted, flag_skip, flag_negate)); + } + } + groups.push_back(group); + } debugMessage(LOG_INSTANCE, "Read " + to_string(groups.size()) + " default groups"); for(const auto& info : groups) { @@ -132,16 +132,17 @@ bool InstanceHandler::setupDefaultGroups() { for(auto perm : info->permissions) { group->permissions()->set_permission(get<0>(perm), {get<1>(perm), get<2>(perm)}, permission::v2::set_value, permission::v2::set_value, get<3>(perm), get<4>(perm)); } - if(!info->propertyName.empty()) { - const auto& prop = property::impl::info(info->propertyName); + + for(const auto& property : info->properties) { + const auto& prop = property::impl::info(property); if(*prop == property::SERVERINSTANCE_UNDEFINED) { - logCritical(LOG_INSTANCE, "Invalid template property name: " + info->propertyName); + logCritical(LOG_INSTANCE, "Invalid template property name: " + property); } else { this->properties()[prop] = group->groupId(); } } } this->save_group_permissions(); - this->getSql()->pool->threads()->wait_for(); //Wait for all permissions to flush - return true; + this->getSql()->pool->threads()->wait_for(); //Wait for all permissions to flush + return true; }