Exemple #1
0
def rule_create(request, table_name=None):

    errors = list()
    formMode = request.POST.get("conditionMode")
    tableName = request.POST.get("table")
    PreviousPriority = request.POST.get("ppriority")
    editing = request.POST.get("editing")
    ruleid = request.POST.get("uuid")
    ruleCondition = request.POST.get("condition")
    ruleDesc = request.POST.get("description")
    ruleError = request.POST.get("error_message")
    ruleType = request.POST.get("type")
    ruleAction = request.POST.get("action")
    ruleValue = request.POST.get("value")
    rulePriority = request.POST.get("priority")
    ruleEnable = request.POST.get("enable")
    previousTable = request.POST.get("hidden_name")
    expertRule = request.POST.get("expertRule")
    newConditions = request.POST.get("conditionID")
    saved = request.POST.get("saved")

    if rulePriority == 'Last' or rulePriority == '':
        priority = None
    else:
        priority = int(rulePriority)

    if formMode == "easy":
        #Avoid empty fields
        #        	if ruleDesc == "":
        #                	errors.append("Description Field is empty")
        if ruleError == "":
            errors.append("Error Message field is empty")
        if ruleCondition == "":
            errors.append("Condition field is empty")
        try:
            str(ruleDesc)
        except:
            errors.append(
                "Only ascii characters are allowed in Description field")
        try:
            str(ruleError)
        except:
            errors.append(
                "Only ascii characters are allowed in Error Message field")
        try:
            str(ruleCondition)
        except:
            errors.append("Only ascii characters are allowed in Conditions")

    if request.POST.get("enable") == 'enable':
        enable = True
    else:
        enable = False
    if ruleType == "terminal":
        ruleType = ""

    if saved == None:
        saved = False
    #Rule String convertion required
    if formMode == "easy":
        if ruleAction != "None":
            strings = "if " + ruleCondition + " then " + ruleValue + " " + ruleType + " do " + ruleAction + " denyMessage " + ruleError + " #" + ruleDesc
        else:
            strings = "if " + ruleCondition + " then " + ruleValue + " " + ruleType + " denyMessage " + ruleError + " #" + ruleDesc
    else:
        strings = expertRule
        try:
            str(expertRule)
        except:
            errors.append("Only ascii characters are allowed in a Rule")

    try:
        if errors:
            raise Exception("")

        if editing == '1':
            #Editing Rules Case:
            if previousTable == tableName:
                try:
                    RuleTableManager.editRule(strings, enable, priority,
                                              PreviousPriority, tableName)
                except Exception as e:
                    raise e
            #else:
                #Moving a rule to a different RuleTable --> this is not possible yet
                #print 'Changing table...'
                #RuleTableManager.AddRule(strings,enable,priority,tableName=tableName)
                #print 'successful add to ' + tableName
                #RuleTableManager.RemoveRule(None,int(PreviousPriority),'oldTableName')
                #print 'remove from ' +  previousTable + ' successful'
        else:
            RuleTableManager.AddRule(strings,
                                     enable,
                                     priority,
                                     tableName=tableName)

        return HttpResponseRedirect("/policies")

    except Exception as e:

        errors.append(e)
        errors.insert(
            0, "The Rule cannot be generated. Reason(s):"
        )  #Insterting the main message error in the first position of the table
        priority = RuleTableManager.getPriorityList(tableName)
        priority = RuleTableManager.getPriorityList(tableName)

        #if a rule index is the last, insert "LAST" in the rule priority instead the true index.
        try:
            int(rulePriority)
            if int(rulePriority) in priority:
                priority.pop(priority.index(int(rulePriority)))
        except:
            rulePriority = "Last"

        if ruleValue == "accept":
            value2 = ["deny"]
        else:
            value2 = ["accept"]

        if ruleType == "nonterminal":
            type2 = ["terminal"]
        else:
            ruleType = "terminal"
            type2 = ["nonterminal"]

        context = {
            'user': request.user,
            'saved': True,
            'CurrentTable': tableName,
            'priority': PreviousPriority,
            'enabled': ruleEnable,
            'load': 'True',
            'valueS': ruleValue,
            'valueD': value2,
            'terminalS': ruleType,
            'terminalD': type2,
            'errorMsg': ruleError,
            'description': ruleDesc,
            'condition': " " + ruleCondition + " ",
            'ptable': tableName,
            'edit': request.POST.get('edit'),
            'action': ruleAction,
            'PrioritySel': rulePriority,
            'priorityList': priority,
            'allMappings': RuleTableManager.GetResolverMappings(tableName),
            'ConditionMappings': RuleTableManager.getConditionMappings(),
            'ActionMappings': RuleTableManager.getActionMappings(),
            'errors': errors,
            'rule_uuid': ruleid,
        }

        return simple.direct_to_template(
            request,
            template='policyEngine/policy_create.html',
            extra_context=context)