55from fastapi import HTTPException
66from sqlalchemy import and_ , text , cast , or_ , func
77from sqlalchemy .dialects .postgresql import JSONB
8- from sqlbot_xpack .permissions .models .ds_permission import DsPermission
8+ from sqlbot_xpack .permissions .api .permission import transRecord2DTO
9+ from sqlbot_xpack .permissions .models .ds_permission import DsPermission , PermissionDTO
910from sqlbot_xpack .permissions .models .ds_rules import DsRules
1011from sqlmodel import select
1112
13+ from apps .datasource .crud .row_permission import transFilterTree
1214from apps .datasource .utils .utils import aes_decrypt
1315from apps .db .constant import DB
1416from apps .db .db import get_engine , get_tables , get_fields , exec_sql
@@ -238,6 +240,9 @@ def updateField(session: SessionDep, field: CoreField):
238240
239241
240242def preview (session : SessionDep , current_user : CurrentUser , id : int , data : TableObj ):
243+ ds = session .query (CoreDatasource ).filter (CoreDatasource .id == id ).first ()
244+ check_status (session , ds , True )
245+
241246 if data .fields is None or len (data .fields ) == 0 :
242247 return {"fields" : [], "data" : [], "sql" : '' }
243248
@@ -261,20 +266,41 @@ def preview(session: SessionDep, current_user: CurrentUser, id: int, data: Table
261266 if fields is None or len (fields ) == 0 :
262267 return {"fields" : [], "data" : [], "sql" : '' }
263268
264- ds = session .query (CoreDatasource ).filter (CoreDatasource .id == id ).first ()
265- check_status (session , ds , True )
269+ # row permission tree
270+ row_permissions = session .query (DsPermission ).filter (
271+ and_ (DsPermission .table_id == data .table .id , DsPermission .type == 'row' )).all ()
272+ res : List [PermissionDTO ] = []
273+ if row_permissions is not None :
274+ for permission in row_permissions :
275+ # check permission and user in same rules
276+ obj = session .query (DsRules ).filter (
277+ and_ (DsRules .permission_list .op ('@>' )(cast ([permission .id ], JSONB )),
278+ or_ (DsRules .user_list .op ('@>' )(cast ([f'{ current_user .id } ' ], JSONB )),
279+ DsRules .user_list .op ('@>' )(cast ([current_user .id ], JSONB ))))
280+ ).first ()
281+ if obj is not None :
282+ res .append (transRecord2DTO (session , permission ))
283+ wheres = transFilterTree (session , res , ds )
284+ where = (' where ' + wheres ) if wheres is not None and wheres != '' else ''
285+
266286 conf = DatasourceConf (** json .loads (aes_decrypt (ds .configuration ))) if ds .type != "excel" else get_engine_config ()
267287 sql : str = ""
268288 if ds .type == "mysql" :
269- sql = f"""SELECT `{ "`, `" .join (fields )} ` FROM `{ data .table .table_name } ` LIMIT 100"""
289+ sql = f"""SELECT `{ "`, `" .join (fields )} ` FROM `{ data .table .table_name } `
290+ { where }
291+ LIMIT 100"""
270292 elif ds .type == "sqlServer" :
271293 sql = f"""SELECT [{ "], [" .join (fields )} ] FROM [{ conf .dbSchema } ].[{ data .table .table_name } ]
294+ { where }
272295 ORDER BY [{ data .fields [0 ].field_name } ]
273296 OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY"""
274297 elif ds .type == "pg" or ds .type == "excel" :
275- sql = f"""SELECT "{ '", "' .join (fields )} " FROM "{ conf .dbSchema } "."{ data .table .table_name } " LIMIT 100"""
298+ sql = f"""SELECT "{ '", "' .join (fields )} " FROM "{ conf .dbSchema } "."{ data .table .table_name } "
299+ { where }
300+ LIMIT 100"""
276301 elif ds .type == "oracle" :
277302 sql = f"""SELECT "{ '", "' .join (fields )} " FROM "{ conf .dbSchema } "."{ data .table .table_name } "
303+ { where }
278304 ORDER BY "{ data .fields [0 ].field_name } "
279305 OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY"""
280306 return exec_sql (ds , sql )
0 commit comments