mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-30 17:14:25 +02:00
Implement validation for modified attributes
This commit is contained in:
parent
16ddc12c06
commit
f405a26052
@ -33,6 +33,7 @@
|
|||||||
#include "base/workqueue.hpp"
|
#include "base/workqueue.hpp"
|
||||||
#include "base/context.hpp"
|
#include "base/context.hpp"
|
||||||
#include "base/application.hpp"
|
#include "base/application.hpp"
|
||||||
|
#include "config/configitem.hpp"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <boost/foreach.hpp>
|
#include <boost/foreach.hpp>
|
||||||
#include <boost/exception/errinfo_api_function.hpp>
|
#include <boost/exception/errinfo_api_function.hpp>
|
||||||
@ -95,6 +96,23 @@ void DynamicObject::ClearExtension(const String& key)
|
|||||||
extensions->Remove(key);
|
extensions->Remove(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ModAttrValidationUtils : public ValidationUtils
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual bool ValidateName(const String& type, const String& name) const override
|
||||||
|
{
|
||||||
|
DynamicType::Ptr dtype = DynamicType::GetByName(type);
|
||||||
|
|
||||||
|
if (!dtype)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (!dtype->GetObject(name))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
void DynamicObject::ModifyAttribute(const String& attr, const Value& value)
|
void DynamicObject::ModifyAttribute(const String& attr, const Value& value)
|
||||||
{
|
{
|
||||||
Dictionary::Ptr original_attributes = GetOriginalAttributes();
|
Dictionary::Ptr original_attributes = GetOriginalAttributes();
|
||||||
@ -118,7 +136,10 @@ void DynamicObject::ModifyAttribute(const String& attr, const Value& value)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: validation, vars.os
|
ModAttrValidationUtils utils;
|
||||||
|
ValidateField(fid, value, utils);
|
||||||
|
|
||||||
|
//TODO: vars.os
|
||||||
SetField(fid, value);
|
SetField(fid, value);
|
||||||
|
|
||||||
if (updated_original_attributes)
|
if (updated_original_attributes)
|
||||||
|
@ -161,6 +161,7 @@ String icinga::DiagnosticInformation(const std::exception& ex, bool verbose, Sta
|
|||||||
Dictionary::Ptr currentHint = vex->GetDebugHint();
|
Dictionary::Ptr currentHint = vex->GetDebugHint();
|
||||||
Array::Ptr messages;
|
Array::Ptr messages;
|
||||||
|
|
||||||
|
if (currentHint) {
|
||||||
BOOST_FOREACH(const String& attr, vex->GetAttributePath()) {
|
BOOST_FOREACH(const String& attr, vex->GetAttributePath()) {
|
||||||
Dictionary::Ptr props = currentHint->Get("properties");
|
Dictionary::Ptr props = currentHint->Get("properties");
|
||||||
|
|
||||||
@ -174,6 +175,7 @@ String icinga::DiagnosticInformation(const std::exception& ex, bool verbose, Sta
|
|||||||
|
|
||||||
messages = currentHint->Get("messages");
|
messages = currentHint->Get("messages");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (messages && messages->GetLength() > 0) {
|
if (messages && messages->GetLength() > 0) {
|
||||||
Array::Ptr message = messages->Get(messages->GetLength() - 1);
|
Array::Ptr message = messages->Get(messages->GetLength() - 1);
|
||||||
|
@ -92,6 +92,11 @@ Value Object::GetField(int id) const
|
|||||||
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
|
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Object::ValidateField(int id, const Value& value, const ValidationUtils& utils)
|
||||||
|
{
|
||||||
|
/* Nothing to do here. */
|
||||||
|
}
|
||||||
|
|
||||||
void Object::NotifyField(int id, const Value& cookie)
|
void Object::NotifyField(int id, const Value& cookie)
|
||||||
{
|
{
|
||||||
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
|
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid field ID."));
|
||||||
|
@ -48,6 +48,7 @@ class Value;
|
|||||||
class Object;
|
class Object;
|
||||||
class Type;
|
class Type;
|
||||||
class String;
|
class String;
|
||||||
|
class ValidationUtils;
|
||||||
|
|
||||||
extern I2_BASE_API Value Empty;
|
extern I2_BASE_API Value Empty;
|
||||||
|
|
||||||
@ -100,6 +101,7 @@ public:
|
|||||||
|
|
||||||
virtual void SetField(int id, const Value& value, bool suppress_events = false, const Value& cookie = Empty);
|
virtual void SetField(int id, const Value& value, bool suppress_events = false, const Value& cookie = Empty);
|
||||||
virtual Value GetField(int id) const;
|
virtual Value GetField(int id) const;
|
||||||
|
virtual void ValidateField(int id, const Value& value, const ValidationUtils& utils);
|
||||||
virtual void NotifyField(int id, const Value& cookie = Empty);
|
virtual void NotifyField(int id, const Value& cookie = Empty);
|
||||||
|
|
||||||
#ifdef I2_DEBUG
|
#ifdef I2_DEBUG
|
||||||
|
@ -596,6 +596,50 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo&)
|
|||||||
|
|
||||||
m_Impl << "}" << std::endl << std::endl;
|
m_Impl << "}" << std::endl << std::endl;
|
||||||
|
|
||||||
|
/* ValidateField */
|
||||||
|
m_Header << "protected:" << std::endl
|
||||||
|
<< "\t" << "virtual void ValidateField(int id, const Value& value, const ValidationUtils& utils);" << std::endl;
|
||||||
|
|
||||||
|
m_Impl << "void ObjectImpl<" << klass.Name << ">::ValidateField(int id, const Value& value, const ValidationUtils& utils)" << std::endl
|
||||||
|
<< "{" << std::endl;
|
||||||
|
|
||||||
|
if (!klass.Parent.empty())
|
||||||
|
m_Impl << "\t" << "int real_id = id - " << klass.Parent << "::TypeInstance->GetFieldCount(); " << std::endl
|
||||||
|
<< "\t" << "if (real_id < 0) { " << klass.Parent << "::ValidateField(id, value, utils); return; }" << std::endl;
|
||||||
|
|
||||||
|
m_Impl << "\t" << "switch (";
|
||||||
|
|
||||||
|
if (!klass.Parent.empty())
|
||||||
|
m_Impl << "real_id";
|
||||||
|
else
|
||||||
|
m_Impl << "id";
|
||||||
|
|
||||||
|
m_Impl << ") {" << std::endl;
|
||||||
|
|
||||||
|
num = 0;
|
||||||
|
for (it = klass.Fields.begin(); it != klass.Fields.end(); it++) {
|
||||||
|
m_Impl << "\t\t" << "case " << num << ":" << std::endl
|
||||||
|
<< "\t\t\t" << "Validate" << it->GetFriendlyName() << "(";
|
||||||
|
|
||||||
|
if (it->Attributes & FAEnum)
|
||||||
|
m_Impl << "static_cast<" << it->Type.GetRealType() << ">(static_cast<int>(";
|
||||||
|
|
||||||
|
m_Impl << "value";
|
||||||
|
|
||||||
|
if (it->Attributes & FAEnum)
|
||||||
|
m_Impl << "))";
|
||||||
|
|
||||||
|
m_Impl << ", utils);" << std::endl
|
||||||
|
<< "\t\t\t" << "break;" << std::endl;
|
||||||
|
num++;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_Impl << "\t\t" << "default:" << std::endl
|
||||||
|
<< "\t\t\t" << "throw std::runtime_error(\"Invalid field ID.\");" << std::endl
|
||||||
|
<< "\t" << "}" << std::endl;
|
||||||
|
|
||||||
|
m_Impl << "}" << std::endl << std::endl;
|
||||||
|
|
||||||
/* NotifyField */
|
/* NotifyField */
|
||||||
m_Header << "protected:" << std::endl
|
m_Header << "protected:" << std::endl
|
||||||
<< "\t" << "virtual void NotifyField(int id, const Value& cookie = Empty);" << std::endl;
|
<< "\t" << "virtual void NotifyField(int id, const Value& cookie = Empty);" << std::endl;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user