Fixed server group copy
This commit is contained in:
		
							parent
							
								
									68d07cf7d3
								
							
						
					
					
						commit
						04fd9ca978
					
				@ -1 +1 @@
 | 
			
		||||
Subproject commit d2b3e9bcadea5130b8ae906d9a1e656351f59936
 | 
			
		||||
Subproject commit 965281a51a1f8fbaebbfc9cfeaa51dd25ce49385
 | 
			
		||||
@ -309,7 +309,7 @@ std::shared_ptr<Group> GroupManager::createGroup(GroupTarget target, GroupType t
 | 
			
		||||
    return group;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool GroupManager::copyGroup(std::shared_ptr<Group> group, GroupType type, std::string name, ServerId targetServerId) {
 | 
			
		||||
GroupId GroupManager::copyGroup(std::shared_ptr<Group> group, GroupType type, std::string name, ServerId targetServerId) {
 | 
			
		||||
	auto group_server = group->handle->getServerId();
 | 
			
		||||
    auto groupId = generateGroupId(this->sql);
 | 
			
		||||
 | 
			
		||||
@ -331,14 +331,14 @@ bool GroupManager::copyGroup(std::shared_ptr<Group> group, GroupType type, std::
 | 
			
		||||
        this->loadGroupFormDatabase(groupId);
 | 
			
		||||
	else if(this->root)
 | 
			
		||||
		this->root->loadGroupFormDatabase(groupId);
 | 
			
		||||
    return true;
 | 
			
		||||
    return groupId;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool GroupManager::copyGroupPermissions(const shared_ptr<Group> &source, const shared_ptr<Group> &target) {
 | 
			
		||||
	auto targetServer = target->handle->getServerId();
 | 
			
		||||
	auto sourceServer = source->handle->getServerId();
 | 
			
		||||
 | 
			
		||||
	auto res = sql::command(this->sql, "DELETE FROM `permissions` WHERE `serverId` = :sid AND `type` = :type AND `id` = :id", variable{":sid", this->getServerId()}, variable{":type", SQL_PERM_GROUP}, variable{":id", target->groupId()}).execute();
 | 
			
		||||
	auto res = sql::command(this->sql, "DELETE FROM `permissions` WHERE `serverId` = :sid AND `type` = :type AND `id` = :id", variable{":sid", targetServer}, variable{":type", SQL_PERM_GROUP}, variable{":id", target->groupId()}).execute();
 | 
			
		||||
	LOG_SQL_CMD(res);
 | 
			
		||||
 | 
			
		||||
    res = sql::command(this->sql, "INSERT INTO `permissions` (`serverId`, `type`, `id`, `channelId`, `permId`, `value`, `grant`) SELECT :tsid AS `serverId`, `type`, :target AS `id`, 0 AS `channelId`, `permId`, `value`,`grant` FROM `permissions` WHERE `serverId` = :ssid AND `type` = :type AND `id` = :source",
 | 
			
		||||
 | 
			
		||||
@ -190,7 +190,7 @@ namespace ts {
 | 
			
		||||
            void setChannelGroup(ClientDbId cldbId, std::shared_ptr<Group>, std::shared_ptr<BasicChannel> , std::chrono::time_point<std::chrono::system_clock> until = std::chrono::time_point<std::chrono::system_clock>());
 | 
			
		||||
 | 
			
		||||
            std::shared_ptr<Group> createGroup(GroupTarget target, GroupType type, std::string name);
 | 
			
		||||
            bool copyGroup(std::shared_ptr<Group> group, GroupType type, std::string name, ServerId targetServerId);
 | 
			
		||||
            GroupId copyGroup(std::shared_ptr<Group> group, GroupType type, std::string name, ServerId targetServerId);
 | 
			
		||||
            bool copyGroupPermissions(const std::shared_ptr<Group>& source, const std::shared_ptr<Group>& target);
 | 
			
		||||
 | 
			
		||||
            bool reloadGroupPermissions(std::shared_ptr<Group>);
 | 
			
		||||
 | 
			
		||||
@ -2519,43 +2519,90 @@ CommandResult ConnectedClient::handleCommandServerGroupCopy(Command &cmd) {
 | 
			
		||||
    CMD_RESET_IDLE;
 | 
			
		||||
    CMD_CHK_AND_INC_FLOOD_POINTS(5);
 | 
			
		||||
 | 
			
		||||
    auto ref_server = this->server;
 | 
			
		||||
	CACHED_PERM_CHECK(permission::b_virtualserver_servergroup_create, 1, true);
 | 
			
		||||
 | 
			
		||||
    auto group_manager = this->server ? this->server->groups : serverInstance->getGroupManager().get();
 | 
			
		||||
 | 
			
		||||
    bool global = false;
 | 
			
		||||
    auto src = group_manager->findGroup(cmd["ssgid"].as<GroupId>());
 | 
			
		||||
    if (!src || src->target() != GROUPTARGET_SERVER) return {findError("parameter_invalid"), "invalid server group id"};
 | 
			
		||||
	if(cmd[0].has("tsgid") && cmd["tsgid"].as<GroupId>() != 0) {
 | 
			
		||||
		auto target = group_manager->findGroup(cmd["tsgid"].as<GroupId>());
 | 
			
		||||
		if (!target || target->target() != GROUPTARGET_SERVER) return {findError("parameter_invalid"), "invalid target server group id"};
 | 
			
		||||
		auto result = group_manager->copyGroupPermissions(src, target);
 | 
			
		||||
		if(!result) return {findError("vs_critical"), "could not copy group!"};
 | 
			
		||||
		global = !this->server || group_manager->isLocalGroup(target);
 | 
			
		||||
	} else {
 | 
			
		||||
	    //GroupType
 | 
			
		||||
        auto type = cmd["type"].as<GroupType>();
 | 
			
		||||
        if(type == GroupType::GROUP_TYPE_NORMAL && !this->server) return {findError("parameter_invalid"), "You cant create normal groups on the template server!"};
 | 
			
		||||
		if(type == GroupType::GROUP_TYPE_QUERY) {
 | 
			
		||||
			if(!this->permission_granted(this->cached_permission_value(permission::b_serverinstance_modify_querygroup), 1, true))
 | 
			
		||||
				return CommandResultPermissionError{permission::b_serverinstance_modify_querygroup};
 | 
			
		||||
		} else if(type == GroupType::GROUP_TYPE_TEMPLATE) {
 | 
			
		||||
			if(!this->permission_granted(this->cached_permission_value(permission::b_serverinstance_modify_templates), 1, true))
 | 
			
		||||
				return CommandResultPermissionError{permission::b_serverinstance_modify_templates};
 | 
			
		||||
    auto source_group_id = cmd["ssgid"].as<GroupId>();
 | 
			
		||||
    auto source_group = group_manager->findGroup(source_group_id);
 | 
			
		||||
 | 
			
		||||
    if(!source_group || source_group->target() != GROUPTARGET_SERVER)
 | 
			
		||||
	    return {findError("group_invalid_id"), "invalid source group"};
 | 
			
		||||
 | 
			
		||||
    const auto group_type_modificable = [&](GroupType type) {
 | 
			
		||||
	    switch(type) {
 | 
			
		||||
		    case GroupType::GROUP_TYPE_TEMPLATE:
 | 
			
		||||
			    if(!this->permission_granted(this->permissionValue(permission::b_serverinstance_modify_templates, 0), 1, true))
 | 
			
		||||
				    return permission::b_serverinstance_modify_templates;
 | 
			
		||||
			    break;
 | 
			
		||||
		    case GroupType::GROUP_TYPE_QUERY:
 | 
			
		||||
			    if(!this->permission_granted(this->permissionValue(permission::b_serverinstance_modify_querygroup, 0), 1, true))
 | 
			
		||||
				    return permission::b_serverinstance_modify_querygroup;
 | 
			
		||||
			    break;
 | 
			
		||||
 | 
			
		||||
		    default:
 | 
			
		||||
			    break;
 | 
			
		||||
	    }
 | 
			
		||||
	    return permission::undefined;
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
	{
 | 
			
		||||
		auto result = group_type_modificable(source_group->type());
 | 
			
		||||
		if(result != permission::undefined)
 | 
			
		||||
			return CommandResultPermissionError{result};
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
		auto result = group_manager->copyGroup(src, type, cmd["name"], type != GroupType::GROUP_TYPE_NORMAL ? 0 : this->getServerId()); //TODO maybe check by name? No duplicated groups?
 | 
			
		||||
		if (!result) return {findError("vs_critical"), "could not copy group!"};
 | 
			
		||||
		global = !this->server || type != GroupType::GROUP_TYPE_NORMAL;
 | 
			
		||||
	auto global_update = false;
 | 
			
		||||
    if(cmd[0].has("tsgid") && cmd["tsgid"].as<GroupId>() != 0) {
 | 
			
		||||
    	//Copy an existing group
 | 
			
		||||
    	auto target_group = group_manager->findGroup(cmd["tsgid"]);
 | 
			
		||||
	    if(!target_group || target_group->target() != GROUPTARGET_SERVER)
 | 
			
		||||
		    return {findError("group_invalid_id"), "invalid target group"};
 | 
			
		||||
 | 
			
		||||
    	{
 | 
			
		||||
		    auto result = group_type_modificable(target_group->type());
 | 
			
		||||
		    if(result != permission::undefined)
 | 
			
		||||
			    return CommandResultPermissionError{result};
 | 
			
		||||
	    }
 | 
			
		||||
 | 
			
		||||
	    if(!target_group->permission_granted(permission::i_server_group_needed_modify_power, this->calculate_permission_value(permission::i_server_group_modify_power, 0), true))
 | 
			
		||||
	    	return CommandResultPermissionError{permission::i_server_group_modify_power};
 | 
			
		||||
 | 
			
		||||
	    if(!group_manager->copyGroupPermissions(source_group, target_group))
 | 
			
		||||
		    return {findError("vs_critical"), "failed to copy group permissions"};
 | 
			
		||||
 | 
			
		||||
	    global_update = !this->server || !group_manager->isLocalGroup(target_group);
 | 
			
		||||
    } else {
 | 
			
		||||
	    //Copy a new group
 | 
			
		||||
	    auto target_type = cmd["type"].as<GroupType>();
 | 
			
		||||
 | 
			
		||||
	    {
 | 
			
		||||
		    auto result = group_type_modificable(target_type);
 | 
			
		||||
		    if(result != permission::undefined)
 | 
			
		||||
			    return CommandResultPermissionError{result};
 | 
			
		||||
	    }
 | 
			
		||||
 | 
			
		||||
	    if(!ref_server && target_type == GroupType::GROUP_TYPE_NORMAL)
 | 
			
		||||
		    return {findError("parameter_invalid"), "You cant create normal groups on the template server!"};
 | 
			
		||||
 | 
			
		||||
	    if(!group_manager->findGroup(GroupTarget::GROUPTARGET_SERVER, cmd["name"].string()).empty())
 | 
			
		||||
		    return {findError("group_name_used"), "You cant create normal groups on the template server!"};
 | 
			
		||||
 | 
			
		||||
	    auto target_group_id = group_manager->copyGroup(source_group, target_type, cmd["name"], target_type != GroupType::GROUP_TYPE_NORMAL ? 0 : this->getServerId());
 | 
			
		||||
	    if(target_group_id == 0)
 | 
			
		||||
	        return {findError("vs_critical"), "failed to copy group"};
 | 
			
		||||
 | 
			
		||||
	    if(this->getType() == ClientType::CLIENT_QUERY) {
 | 
			
		||||
		    Command notify("");
 | 
			
		||||
            notify["sgid"] = group_manager->availableServerGroups(false).back()->groupId();
 | 
			
		||||
		    notify["sgid"] = target_group_id;
 | 
			
		||||
		    this->sendCommand(notify);
 | 
			
		||||
	    }
 | 
			
		||||
 | 
			
		||||
	    global_update = !this->server || !group_manager->isLocalGroup(group_manager->findGroup(target_group_id));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
	for(const auto& server : (global ? serverInstance->getVoiceServerManager()->serverInstances() : deque<shared_ptr<TSServer>>{this->server}))
 | 
			
		||||
	for(const auto& server : (global_update ? serverInstance->getVoiceServerManager()->serverInstances() : deque<shared_ptr<TSServer>>{this->server}))
 | 
			
		||||
		if(server)
 | 
			
		||||
			server->forEachClient([](shared_ptr<ConnectedClient> cl) {
 | 
			
		||||
				cl->notifyServerGroupList();
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										2
									
								
								shared
									
									
									
									
									
								
							
							
								
								
								
								
								
								
									
									
								
							
						
						
									
										2
									
								
								shared
									
									
									
									
									
								
							@ -1 +1 @@
 | 
			
		||||
Subproject commit 3b3574d54df493a8a767e2c691c8769468b758dc
 | 
			
		||||
Subproject commit c532266cb8b1ba577e063c6ed14a810374c8c84f
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user