Skip to content

Commit

Permalink
Add fields compare in policy checker
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime Gervais <[email protected]>
  • Loading branch information
g-maxime committed May 19, 2024
1 parent 4d44b94 commit 3587575
Show file tree
Hide file tree
Showing 24 changed files with 1,087 additions and 144 deletions.
274 changes: 228 additions & 46 deletions Source/Checker/Checker.cpp

Large diffs are not rendered by default.

20 changes: 16 additions & 4 deletions Source/Checker/Checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
//---------------------------------------------------------------------------
#ifndef TFSXML_NAMESPACE
#define TFSXML_NAMESPACE 1
#endif // TFSXML_NAMESPACEs
#endif // TFSXML_NAMESPACE
#include "ThirdParty/tfsxml/tfsxml.h"
#include "Path.h"

#include <string>
#include <vector>
#include <map>

//---------------------------------------------------------------------------

Expand Down Expand Up @@ -73,11 +74,21 @@ class PolicyChecker

class RuleElement : public Element {
public:
struct Source
{
std::vector<PathElement> path;
std::map<const char*, std::string> values;
std::string scope;
std::string field;
std::string tracktype;
std::string occurrence;
};

RuleElement();
~RuleElement();

void reset();
bool compare(const std::string& value);
bool compare(const std::string& v1, const std::string& v2);

virtual void resolve();
virtual Result result();
Expand All @@ -93,8 +104,9 @@ class PolicyChecker
std::string operand;
std::string xpath;
std::string requested;
std::vector<std::string> values;
std::map<const char*, std::string> values;
std::vector<std::string> failing_values;
Source* source;
size_t tracks;

private:
Expand Down Expand Up @@ -129,7 +141,7 @@ class PolicyChecker

RuleElement* parse_rule(tfsxml::tfsxml_string& tfsxml_priv);
PolicyElement* parse_policy(tfsxml::tfsxml_string& tfsxml_priv);
void parse_node(tfsxml::tfsxml_string& tfsxml_priv, std::vector<RuleElement*> rules, size_t level);
void parse_node(tfsxml::tfsxml_string& tfsxml_priv, std::vector<RuleElement*> rules, std::vector<RuleElement*> sources, size_t level);

std::vector<PolicyElement*> policies;
std::vector<RuleElement*> rules;
Expand Down
5 changes: 2 additions & 3 deletions Source/Checker/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ std::vector<PathElement> parse_path(const std::string& xpath)
//---------------------------------------------------------------------------
bool path_is_matching(tfsxml_string& tfsxml_priv, tfsxml_string& node, PathElement path, size_t& occurrence)
{
tfsxml_string tfsxml_priv_local = tfsxml_priv;
tfsxml_string value;

// compare names
Expand All @@ -125,10 +126,8 @@ bool path_is_matching(tfsxml_string& tfsxml_priv, tfsxml_string& node, PathEleme

tfsxml_string attribute_name;
tfsxml_string attribute_value;
tfsxml_string tfsxml_priv_save=tfsxml_priv;
while (!tfsxml_attr(&tfsxml_priv, &attribute_name, &attribute_value))
while (!tfsxml_attr(&tfsxml_priv_local, &attribute_name, &attribute_value))
attributes[std::string(attribute_name.buf, attribute_name.len)]=std::string(attribute_value.buf, attribute_value.len);
tfsxml_priv=tfsxml_priv_save;

std::map<std::string, std::string>::iterator it=path.attributes.begin();
while (it!=path.attributes.end())
Expand Down
17 changes: 17 additions & 0 deletions Source/Common/DaemonClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,14 @@ XsltPolicyRule *DaemonClient::xslt_policy_rule_get(int user, int policy_id, int
rule->occurrence = res->rule.occurrence;
rule->ope = rule->ope;
rule->value = res->rule.value;
if (res->rule.source)
{
rule->source = new XsltPolicyRule::Source;
rule->source->track_type = res->rule.source->tracktype;
rule->source->field = res->rule.source->field;
rule->source->scope = res->rule.source->scope;
rule->source->occurrence = res->rule.source->occurrence;
}
}

delete res;
Expand All @@ -1327,8 +1335,17 @@ int DaemonClient::xslt_policy_rule_edit(int user, int policy_id, int rule_id, co
req.rule.occurrence = rule->occurrence;
req.rule.ope = rule->ope;
req.rule.value = rule->value;
if (rule->source)
{
req.rule.source = new MediaConchLib::XSLT_Policy_Rule::Source;
req.rule.source->tracktype = rule->source->track_type;
req.rule.source->field = rule->source->field;
req.rule.source->scope = rule->source->scope;
req.rule.source->occurrence = rule->source->occurrence;
}
req.user = user;


COMMON_HTTP_REQ_RES(xslt_policy_rule_edit, -1)

int ret = -1;
Expand Down
14 changes: 12 additions & 2 deletions Source/Common/JS_Tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,8 +700,18 @@ int JsTree::rule_to_js_tree(MediaConchLib::XSLT_Policy_Rule* rule, std::string&
ss << "\",\"level\":\"" << unified_json_value(rule->level);
ss << "\",\"occurrence\":\"" << unified_json_value(rule->occurrence);
ss << "\",\"ope\":\"" << unified_json_value(rule->ope);
ss << "\",\"value\":\"" << unified_json_value(rule->value);
ss <<"\"}}";
if (rule->source)
{
ss << "\",\"source\":{";
ss << "\"tracktype\":\"" << unified_json_value(rule->source->tracktype);
ss << "\",\"field\":\"" << unified_json_value(rule->source->field);
ss << "\",\"scope\":\"" << unified_json_value(rule->source->scope);
ss << "\",\"occurrence\":\"" << unified_json_value(rule->source->occurrence);
ss <<"\"}";
}
else
ss << "\",\"value\":\"" << unified_json_value(rule->value) << "\"";
ss <<"}}";

json = ss.str();
return 0;
Expand Down
15 changes: 14 additions & 1 deletion Source/Common/MediaConchLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,20 @@ std::string MediaConchLib::XSLT_Policy_Rule::to_str() const
out << ",occurrence:\"" << occurrence << "\"";
if (ope.size())
out << ",\"ope\":\"" << ope << "\"";
if (value.size())
if (source)
{
out << ",source:{";
if (source->tracktype.size())
out << ",\"tracktype\":\"" << source->tracktype << "\"";
if (source->field.size())
out << ",\"field\":\"" << source->field << "\"";
if (source->scope.size())
out << ",\"scope\":\"" << source->scope << "\"";
if (source->occurrence.size())
out << ",occurrence:\"" << source->occurrence << "\"";
out << "}";
}
else if (value.size())
out << ",\"value\":\"" << value << "\"";
out << "}";
return out.str();
Expand Down
14 changes: 13 additions & 1 deletion Source/Common/MediaConchLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,18 @@ class MediaConchLib

struct XSLT_Policy_Rule
{
XSLT_Policy_Rule() : id(-1) {}
XSLT_Policy_Rule() : id(-1), source(NULL) {}
~XSLT_Policy_Rule() {
if (source)
delete source;
}

struct Source {
std::string tracktype;
std::string field;
std::string scope;
std::string occurrence;
};

int id;
std::string name;
Expand All @@ -186,6 +197,7 @@ class MediaConchLib
std::string occurrence;
std::string ope;
std::string value;
Source* source;
std::string to_str() const;
};

Expand Down
8 changes: 8 additions & 0 deletions Source/Common/Policies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,14 @@ MediaConchLib::XSLT_Policy_Rule* Policies::xslt_policy_rule_to_mcl_policy(XsltPo
r->occurrence = rule->occurrence;
r->ope = rule->ope;
r->value = rule->value;
if (rule->source)
{
r->source = new MediaConchLib::XSLT_Policy_Rule::Source;
r->source->tracktype = rule->source->track_type;
r->source->field = rule->source->field;
r->source->scope = rule->source->scope;
r->source->occurrence = rule->source->occurrence;
}

return r;
}
Expand Down
119 changes: 119 additions & 0 deletions Source/Common/REST_API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2597,6 +2597,42 @@ int RESTAPI::serialize_xslt_policy_rule(MediaConchLib::XSLT_Policy_Rule& rule, C
val.obj["value"] = value;
}

if (rule.source)
{
Container::Value source, src_tracktype, src_field, src_scope, src_occurrence;
source.type = Container::Value::CONTAINER_TYPE_OBJECT;

if (rule.source->tracktype.size())
{
src_tracktype.type = Container::Value::CONTAINER_TYPE_STRING;
src_tracktype.s = rule.source->tracktype;
source.obj["tracktype"] = src_tracktype;
}

if (rule.source->field.size())
{
src_field.type = Container::Value::CONTAINER_TYPE_STRING;
src_field.s = rule.source->field;
source.obj["field"] = src_field;
}

if (rule.source->scope.size())
{
src_scope.type = Container::Value::CONTAINER_TYPE_STRING;
src_scope.s = rule.source->scope;
source.obj["scope"] = src_scope;
}

if (rule.source->occurrence.size())
{
src_occurrence.type = Container::Value::CONTAINER_TYPE_STRING;
src_occurrence.s = rule.source->occurrence;
source.obj["occurrence"] = src_occurrence;
}

val.obj["source"] = source;
}

return 0;
}

Expand Down Expand Up @@ -5364,6 +5400,31 @@ int RESTAPI::parse_xslt_policy_rule(Container::Value *val, MediaConchLib::XSLT_P
if (value && value->type == Container::Value::CONTAINER_TYPE_STRING)
rule->value = value->s;

Container::Value *source = model->get_value_by_key(*val, "source");
if (source && source->type == Container::Value::CONTAINER_TYPE_OBJECT)
{
if (rule->source)
delete rule->source;

rule->source = new MediaConchLib::XSLT_Policy_Rule::Source;

Container::Value *src_tracktype = model->get_value_by_key(*source, "tracktype");
if (src_tracktype && src_tracktype->type == Container::Value::CONTAINER_TYPE_STRING)
rule->source->tracktype = src_tracktype->s;

Container::Value *src_field = model->get_value_by_key(*source, "field");
if (src_field && src_field->type == Container::Value::CONTAINER_TYPE_STRING)
rule->source->field = src_field->s;

Container::Value *src_scope = model->get_value_by_key(*source, "scope");
if (src_scope && src_scope->type == Container::Value::CONTAINER_TYPE_STRING)
rule->source->scope = src_scope->s;

Container::Value *src_occurrence = model->get_value_by_key(*source, "occurrence");
if (src_occurrence && src_occurrence->type == Container::Value::CONTAINER_TYPE_STRING)
rule->source->occurrence = src_occurrence->s;
}

return 0;
}

Expand Down Expand Up @@ -9421,6 +9482,42 @@ void RESTAPI::serialize_a_xslt_policy_rule(MediaConchLib::XSLT_Policy_Rule* rule
value.s = rule->value;
ok_v.obj["value"] = value;
}

if (rule->source)
{
Container::Value source, src_tracktype, src_field, src_scope, src_occurrence;
source.type = Container::Value::CONTAINER_TYPE_OBJECT;

if (rule->source->tracktype.size())
{
src_tracktype.type = Container::Value::CONTAINER_TYPE_STRING;
src_tracktype.s = rule->source->tracktype;
source.obj["tracktype"] = src_tracktype;
}

if (rule->source->field.size())
{
src_field.type = Container::Value::CONTAINER_TYPE_STRING;
src_field.s = rule->source->field;
source.obj["field"] = src_field;
}

if (rule->source->scope.size())
{
src_scope.type = Container::Value::CONTAINER_TYPE_STRING;
src_scope.s = rule->source->scope;
source.obj["scope"] = src_scope;
}

if (rule->source->occurrence.size())
{
src_occurrence.type = Container::Value::CONTAINER_TYPE_STRING;
src_occurrence.s = rule->source->occurrence;
source.obj["occurrence"] = src_occurrence;
}

ok_v.obj["source"] = source;
}
}

//---------------------------------------------------------------------------
Expand Down Expand Up @@ -10188,6 +10285,28 @@ MediaConchLib::XSLT_Policy_Rule* RESTAPI::parse_a_xslt_policy_rule(Container::Va
if (value && value->type == Container::Value::CONTAINER_TYPE_STRING)
ok->value = value->s;

Container::Value *source = model->get_value_by_key(*rule, "source");
if (source && source->type == Container::Value::CONTAINER_TYPE_OBJECT)
{
ok->source = new MediaConchLib::XSLT_Policy_Rule::Source;

Container::Value *src_tracktype = model->get_value_by_key(*source, "tracktype");
if (src_tracktype && src_tracktype->type == Container::Value::CONTAINER_TYPE_STRING)
ok->source->tracktype = src_tracktype->s;

Container::Value *src_field = model->get_value_by_key(*source, "field");
if (src_field && src_field->type == Container::Value::CONTAINER_TYPE_STRING)
ok->source->field = src_field->s;

Container::Value *src_scope = model->get_value_by_key(*source, "scope");
if (src_scope && src_scope->type == Container::Value::CONTAINER_TYPE_STRING)
ok->source->scope = src_scope->s;

Container::Value *src_occurrence = model->get_value_by_key(*source, "occurrence");
if (src_occurrence && src_occurrence->type == Container::Value::CONTAINER_TYPE_STRING)
ok->source->occurrence = src_occurrence->s;
}

return ok;
}

Expand Down
Loading

0 comments on commit 3587575

Please sign in to comment.