Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def enable(self, ruleset, user = None, comment = None):
ruleset.categories.add(self)
ruleset.needs_test()
ruleset.save()
if user:
UserAction.create(
action_type='enable_category',
comment=comment,
user=user,
category=self,
ruleset=ruleset
)
def destroy(self, request, *args, **kwargs):
old_user = self.get_object()
# Do not need to copy 'request.data' and pop 'comment'
# because we are not using serializer there
comment = request.data.get('comment', None)
comment_serializer = CommentSerializer(data={'comment': comment})
comment_serializer.is_valid(raise_exception=True)
UserAction.create(
action_type='delete_user',
user=request.user,
old_user=old_user.user,
comment=comment_serializer.validated_data['comment']
)
return super(AccountViewSet, self).destroy(request, *args, **kwargs)
rule_object.remove_transformations(ruleset, TYPE)
continue
rule_object.set_transformation(ruleset, key=TYPE, value=form_trans)
if form_trans != NONE and form_trans != trans:
UserAction.create(
action_type='transform_rule',
comment=form.cleaned_data['comment'],
user=request.user,
transformation='%s: %s' % (TYPE.value.title(), form_trans.value.title()),
rule=rule_object,
ruleset=ruleset
)
elif form_trans == NONE and trans:
UserAction.create(
action_type='transform_rule',
comment=form.cleaned_data['comment'],
user=request.user,
transformation='%s: %s' % (TYPE.value.title(), trans.value.title()),
rule=rule_object,
ruleset=ruleset
)
return redirect(rule_object)
else:
rulesets_ids = []
current_trans = {
Transformation.ACTION: Transformation.A_CAT_DEFAULT,
Transformation.LATERAL: Transformation.L_CAT_DEFAULT,
Transformation.TARGET: Transformation.T_CAT_DEFAULT
}
def enable(self, ruleset, user = None, comment = None):
enable_rules = [self]
enable_rules.extend(self.get_dependant_rules(ruleset))
ruleset.enable_rules(enable_rules)
if user:
UserAction.create(
action_type='enable_rule',
comment=comment,
user=user,
rule=self,
ruleset=ruleset
)
return
if request.method == 'GET':
if len(tokens) > 0:
token = tokens[0].key
else:
if len(tokens) > 0:
tokens[0].delete()
token = Token.objects.create(user=scirius_user.user).key
data = request.data.copy()
comment = data.pop('comment', None)
comment_serializer = CommentSerializer(data={'comment': comment})
comment_serializer.is_valid(raise_exception=True)
UserAction.create(
action_type='edit_user_token',
comment=comment_serializer.validated_data['comment'],
user=request.user,
other_user=scirius_user.user
)
return Response({'token': token})
raise serializers.ValidationError({'index': ['Invalid index value (too high).']})
instance = super(RuleProcessingFilterSerializer, self).update(instance, validated_data)
user_action = 'edit'
self._reorder(instance, previous_index, new_index)
if filters:
try:
self._set_filters(instance, filters)
except:
if operation == 'create':
instance.delete()
raise
UserAction.create(
action_type='%s_rule_filter' % user_action,
comment=comment,
user=self.context['request'].user,
rule_filter=instance
)
return instance
raise ServiceUnavailableException()
else:
source.update()
except Exception as errors:
if isinstance(errors, (IOError, OSError)):
msg = 'Can not fetch data'
elif isinstance(errors, ValidationError):
msg = 'Source is invalid'
elif isinstance(errors, SuspiciousOperation):
msg = 'Source is not correct'
else:
msg = 'Error updating source'
msg = '%s: %s' % (msg, errors)
raise serializers.ValidationError({'update': [msg]})
UserAction.create(
action_type='update_source',
comment=comment_serializer.validated_data['comment'],
user=request.user,
source=source
)
return Response({'update': msg})
def comment_rule(request, rule_id):
rule_object = get_object_or_404(Rule, sid=rule_id)
if request.method == 'POST': # If the form has been submitted...
if request.user.is_staff:
form = RuleCommentForm(request.POST)
if form.is_valid():
UserAction.create(
action_type='comment_rule',
comment=form.cleaned_data['comment'],
user=request.user,
rule=rule_object
)
return redirect(rule_object)
if form_trans == RULESET_DEFAULT:
cat_object.suppress_transformation(ruleset, key=TYPE)
continue
for _trans in LOOP:
if _trans == form_trans:
continue
if cat_object.is_transformed(ruleset, key=TYPE, value=_trans):
cat_object.toggle_transformation(ruleset, key=TYPE, value=_trans)
# Enable new transformation
if form_trans != trans:
cat_object.toggle_transformation(ruleset, key=TYPE, value=form_trans)
UserAction.create(
action_type='transform_category',
comment=form.cleaned_data['comment'],
user=request.user,
transformation='%s: %s' % (TYPE.value.title(), form_trans.value.title()),
category=cat_object,
ruleset=ruleset
)
elif trans:
UserAction.create(
action_type='transform_category',
comment=form.cleaned_data['comment'],
user=request.user,
transformation='%s: %s' % (TYPE.value.title(), trans.value.title()),
category=cat_object,
ruleset=ruleset
)
def copy(self, request, pk):
data = request.data.copy()
ruleset = self.get_object()
comment = data.pop('comment', None)
copy_serializer = CopyRulesetSerializer(data=data)
copy_serializer.is_valid(raise_exception=True)
ruleset.copy(copy_serializer.validated_data['name'])
UserAction.create(
action_type='copy_ruleset',
comment=comment,
user=request.user,
ruleset=ruleset
)
return Response({'copy': 'ok'})