1515
1616from application .flow .i_step_node import NodeResult
1717from application .flow .step_node .tool_lib_node .i_tool_lib_node import IToolLibNode
18+ from common .database_model_manage .database_model_manage import DatabaseModelManage
1819from common .exception .app_exception import AppApiException
1920from common .utils .function_code import FunctionExecutor
2021from common .utils .rsa_util import rsa_long_decrypt
2122from maxkb .const import CONFIG
23+ from system_manage .models import AuthTargetType
24+ from system_manage .serializers .user_resource_permission import UserResourcePermissionSerializer
2225from tools .models import Tool
2326
2427function_executor = FunctionExecutor (CONFIG .get ('SANDBOX' ))
@@ -101,13 +104,14 @@ def convert_value(name: str, value, _type, is_required, source, node):
101104 value = value ))
102105
103106
104- def valid_function (function_lib , user_id ):
105- if function_lib is None :
106- raise Exception (_ ('Function does not exist' ))
107- if function_lib .permission_type == 'PRIVATE' and str (function_lib .user_id ) != str (user_id ):
108- raise Exception (_ ('No permission to use this function {name}' ).format (name = function_lib .name ))
109- if not function_lib .is_active :
110- raise Exception (_ ('Function {name} is unavailable' ).format (name = function_lib .name ))
107+ def valid_function (tool_lib , workspace_id ):
108+ if tool_lib is None :
109+ raise Exception (_ ('Tool does not exist' ))
110+ get_authorized_tool = DatabaseModelManage .get_model ("get_authorized_tool" )
111+ if tool_lib and tool_lib .workspace_id != workspace_id and get_authorized_tool is not None :
112+ tool_lib = get_authorized_tool (QuerySet (Tool ).filter (id = tool_lib .id ), workspace_id ).first ()
113+ if tool_lib is None :
114+ raise Exception (_ ("Tool does not exist" ))
111115
112116
113117class BaseToolLibNodeNode (IToolLibNode ):
@@ -116,25 +120,26 @@ def save_context(self, details, workflow_manage):
116120 if self .node_params .get ('is_result' ):
117121 self .answer_text = str (details .get ('result' ))
118122
119- def execute (self , function_lib_id , input_field_list , ** kwargs ) -> NodeResult :
120- function_lib = QuerySet (Tool ).filter (id = function_lib_id ).first ()
121- valid_function (function_lib , self .flow_params_serializer .data .get ('user_id' ))
123+ def execute (self , tool_lib_id , input_field_list , ** kwargs ) -> NodeResult :
124+ workspace_id = self .workflow_manage .get_body ().get ('workspace_id' )
125+ tool_lib = QuerySet (Tool ).filter (id = tool_lib_id ).first ()
126+ valid_function (tool_lib , workspace_id )
122127 params = {field .get ('name' ): convert_value (field .get ('name' ), field .get ('value' ), field .get ('type' ),
123128 field .get ('is_required' ),
124129 field .get ('source' ), self )
125130 for field in
126131 [{'value' : get_field_value (input_field_list , field .get ('name' ), field .get ('is_required' ),
127132 ), ** field }
128133 for field in
129- function_lib .input_field_list ]}
134+ tool_lib .input_field_list ]}
130135
131136 self .context ['params' ] = params
132137 # 合并初始化参数
133- if function_lib .init_params is not None :
134- all_params = json .loads (rsa_long_decrypt (function_lib .init_params )) | params
138+ if tool_lib .init_params is not None :
139+ all_params = json .loads (rsa_long_decrypt (tool_lib .init_params )) | params
135140 else :
136141 all_params = params
137- result = function_executor .exec_code (function_lib .code , all_params )
142+ result = function_executor .exec_code (tool_lib .code , all_params )
138143 return NodeResult ({'result' : result }, {}, _write_context = write_context )
139144
140145 def get_details (self , index : int , ** kwargs ):
0 commit comments