mirror of https://github.com/jumpserver/jumpserver
fix: 【资产登录】属性为标签时,规则不生效
parent
3aeadc2f03
commit
00c5b3c0a2
|
@ -469,7 +469,7 @@ class JSONManyToManyDescriptor:
|
||||||
rule_match = rule.get('match', 'exact')
|
rule_match = rule.get('match', 'exact')
|
||||||
|
|
||||||
custom_filter_q = None
|
custom_filter_q = None
|
||||||
spec_attr_filter = getattr(to_model, "get_filter_{}_attr_q".format(rule['name']), None)
|
spec_attr_filter = getattr(to_model, "get_{}_filter_attr_q".format(rule['name']), None)
|
||||||
if spec_attr_filter:
|
if spec_attr_filter:
|
||||||
custom_filter_q = spec_attr_filter(rule_value, rule_match)
|
custom_filter_q = spec_attr_filter(rule_value, rule_match)
|
||||||
elif custom_attr_filter:
|
elif custom_attr_filter:
|
||||||
|
@ -478,59 +478,61 @@ class JSONManyToManyDescriptor:
|
||||||
custom_q &= custom_filter_q
|
custom_q &= custom_filter_q
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if rule_match == 'in':
|
match rule_match:
|
||||||
res &= value in rule_value or '*' in rule_value
|
case 'in':
|
||||||
elif rule_match == 'exact':
|
res &= value in rule_value or '*' in rule_value
|
||||||
res &= value == rule_value or rule_value == '*'
|
case 'exact':
|
||||||
elif rule_match == 'contains':
|
res &= value == rule_value or rule_value == '*'
|
||||||
res &= (rule_value in value)
|
case 'contains':
|
||||||
elif rule_match == 'startswith':
|
res &= rule_value in value
|
||||||
res &= str(value).startswith(str(rule_value))
|
case 'startswith':
|
||||||
elif rule_match == 'endswith':
|
res &= str(value).startswith(str(rule_value))
|
||||||
res &= str(value).endswith(str(rule_value))
|
case 'endswith':
|
||||||
elif rule_match == 'regex':
|
res &= str(value).endswith(str(rule_value))
|
||||||
try:
|
case 'regex':
|
||||||
matched = bool(re.search(r'{}'.format(rule_value), value))
|
try:
|
||||||
except Exception as e:
|
matched = bool(re.search(r'{}'.format(rule_value), value))
|
||||||
logging.error('Error regex match: %s', e)
|
except Exception as e:
|
||||||
matched = False
|
logging.error('Error regex match: %s', e)
|
||||||
res &= matched
|
matched = False
|
||||||
elif rule_match == 'not':
|
res &= matched
|
||||||
res &= value != rule_value
|
case 'not':
|
||||||
elif rule['match'] == 'gte':
|
res &= value != rule_value
|
||||||
res &= value >= rule_value
|
case 'gte' | 'lte' | 'gt' | 'lt':
|
||||||
elif rule['match'] == 'lte':
|
operations = {
|
||||||
res &= value <= rule_value
|
'gte': lambda x, y: x >= y,
|
||||||
elif rule['match'] == 'gt':
|
'lte': lambda x, y: x <= y,
|
||||||
res &= value > rule_value
|
'gt': lambda x, y: x > y,
|
||||||
elif rule['match'] == 'lt':
|
'lt': lambda x, y: x < y
|
||||||
res &= value < rule_value
|
}
|
||||||
elif rule['match'] == 'ip_in':
|
res &= operations[rule_match](value, rule_value)
|
||||||
if isinstance(rule_value, str):
|
case 'ip_in':
|
||||||
rule_value = [rule_value]
|
if isinstance(rule_value, str):
|
||||||
res &= '*' in rule_value or contains_ip(value, rule_value)
|
rule_value = [rule_value]
|
||||||
elif rule['match'].startswith('m2m'):
|
res &= '*' in rule_value or contains_ip(value, rule_value)
|
||||||
if isinstance(value, Manager):
|
case rule_match if rule_match.startswith('m2m'):
|
||||||
value = value.values_list('id', flat=True)
|
if isinstance(value, Manager):
|
||||||
elif isinstance(value, QuerySet):
|
value = value.values_list('id', flat=True)
|
||||||
value = value.values_list('id', flat=True)
|
elif isinstance(value, QuerySet):
|
||||||
elif isinstance(value, models.Model):
|
value = value.values_list('id', flat=True)
|
||||||
value = [value.id]
|
elif isinstance(value, models.Model):
|
||||||
if isinstance(rule_value, (str, int)):
|
value = [value.id]
|
||||||
rule_value = [rule_value]
|
if isinstance(rule_value, (str, int)):
|
||||||
value = set(map(str, value))
|
rule_value = [rule_value]
|
||||||
rule_value = set(map(str, rule_value))
|
value = set(map(str, value))
|
||||||
|
rule_value = set(map(str, rule_value))
|
||||||
|
|
||||||
if rule['match'] == 'm2m_all':
|
if rule['match'] == 'm2m_all':
|
||||||
res &= rule_value.issubset(value)
|
res &= rule_value.issubset(value)
|
||||||
else:
|
else:
|
||||||
res &= bool(value & rule_value)
|
res &= bool(value & rule_value)
|
||||||
else:
|
case __:
|
||||||
logging.error("unknown match: {}".format(rule['match']))
|
logging.error("unknown match: {}".format(rule['match']))
|
||||||
res &= False
|
res &= False
|
||||||
|
|
||||||
if not res:
|
if not res:
|
||||||
return res
|
return res
|
||||||
|
|
||||||
if custom_q:
|
if custom_q:
|
||||||
res &= to_model.objects.filter(custom_q).filter(id=obj.id).exists()
|
res &= to_model.objects.filter(custom_q).filter(id=obj.id).exists()
|
||||||
return res
|
return res
|
||||||
|
|
Loading…
Reference in New Issue