def test_query_add_func(): ab1 = Ability({}, based_on=ab) def func1(ability: Ability, user, query: 'SQLQueryInfo', view: "AbstractSQLView"): query.add_condition('nickname', '=', 'aa') ab1.add_query_condition('user', func=func1) sqi = SQLQueryInfo() sqi.select = sqi.parse_select('username, nickname, password') sqi.parse_then_add_condition('username', '=', 'b') sqi.check_query_permission_full(None, 'user', ab1, None) assert sqi.conditions == [['username', SQL_OP.EQ, 'b'], ['nickname', SQL_OP.EQ, 'aa'],] ab2 = Ability({}, based_on=ab) def func2(ability: Ability, user, query: 'SQLQueryInfo'): query.add_condition('nickname', '=', 'aa') ab2.add_query_condition('user', func=func2) sqi = SQLQueryInfo() sqi.select = sqi.parse_select('username, nickname, password') sqi.parse_then_add_condition('username', '=', 'b') sqi.check_query_permission_full(None, 'user', ab2, None) assert sqi.conditions == [['username', SQL_OP.EQ, 'b'], ['nickname', SQL_OP.EQ, 'aa'],]
def test_query_condition_add2(): """ 测试添加多个条件 """ ab2 = Ability({}, based_on=ab) ab2.add_query_condition('user', [ ['username', 'like', '1%'], ['nickname', 'like', '1%'], ]) sqi = SQLQueryInfo() sqi.select = sqi.parse_select('username, nickname, password') sqi.parse_then_add_condition('username', '=', 'b') sqi.check_query_permission_full(None, 'user', ab2, None) assert sqi.conditions == [['username', SQL_OP.EQ, 'b'], ['username', SQL_OP.LIKE, '1%'], ['nickname', SQL_OP.LIKE, '1%']]
def test_query_condition_add1(): """ 测试添加单个条件 :return: """ ab1 = Ability({}, based_on=ab) ab1.add_query_condition('user', ['phone', '>=', '123456']) sqi = SQLQueryInfo() sqi.select = sqi.parse_select('username, nickname, password') sqi.parse_then_add_condition('username', '=', 'b') assert sqi.conditions[-1] == ['username', SQL_OP.EQ, 'b'] sqi.check_query_permission_full(None, 'user', ab1, None) assert sqi.conditions[-1] == ['phone', SQL_OP.GE, '123456']
permission.add(inactive_user) permission.add(normal_user) permission.add(super_user) permission.add(admin) # user def func(ability, user, query: 'SQLQueryInfo'): # 如果查询的是自己,附带更多信息 for i in query.conditions.find('id'): if i[1] == SQL_OP.EQ and i[2] == user.id.hex(): query.select.add('email') inactive_user.add_query_condition('user', func=func) normal_user.add_query_condition('user', func=func) def check_is_user(ability, user, action, record: DataRecord, available_columns: list): if user: if record.get('id') != user.id: available_columns.clear() return True normal_user.add_record_check((A.WRITE,), 'user', func=check_is_user) # upload def func(ability, user, query: 'SQLQueryInfo'):