diff --git a/CMakeLists.txt b/CMakeLists.txt index 695f4f9..707f627 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.13) -project(owprov VERSION 2.10.0) +project(owprov VERSION 2.11.0) set(CMAKE_CXX_STANDARD 17) diff --git a/build b/build index 1758ddd..d8263ee 100644 --- a/build +++ b/build @@ -1 +1 @@ -32 \ No newline at end of file +2 \ No newline at end of file diff --git a/src/RESTAPI/RESTAPI_venue_handler.cpp b/src/RESTAPI/RESTAPI_venue_handler.cpp index c7eddc7..af4abea 100644 --- a/src/RESTAPI/RESTAPI_venue_handler.cpp +++ b/src/RESTAPI/RESTAPI_venue_handler.cpp @@ -90,9 +90,9 @@ namespace OpenWifi { } if (!Existing.contacts.empty()) { - for (const auto &i : Existing.contacts) + for (const auto &contact_uuid : Existing.contacts) StorageService()->ContactDB().DeleteInUse( - "id", i, StorageService()->VenueDB().Prefix(), UUID); + "id", contact_uuid, StorageService()->VenueDB().Prefix(), UUID); } if (!Existing.location.empty()) StorageService()->LocationDB().DeleteInUse("id", Existing.location, @@ -101,9 +101,9 @@ namespace OpenWifi { StorageService()->PolicyDB().DeleteInUse("id", Existing.managementPolicy, StorageService()->VenueDB().Prefix(), UUID); if (!Existing.deviceConfiguration.empty()) { - for (auto &i : Existing.deviceConfiguration) + for (auto &configuration_uuid : Existing.deviceConfiguration) StorageService()->ConfigurationDB().DeleteInUse( - "id", i, StorageService()->VenueDB().Prefix(), UUID); + "id", configuration_uuid, StorageService()->VenueDB().Prefix(), UUID); } if (!Existing.parent.empty()) StorageService()->VenueDB().DeleteChild("id", Existing.parent, UUID); @@ -157,6 +157,10 @@ namespace OpenWifi { return BadRequest(RESTAPI::Errors::EntityMustExist); } + if(StorageService()->VenueDB().DoesVenueNameAlreadyExist(NewObject.info.name,NewObject.entity, NewObject.parent)) { + return BadRequest(RESTAPI::Errors::VenuesNameAlreadyExists); + } + if (!NewObject.contacts.empty()) { for (const auto &i : NewObject.contacts) { if (!StorageService()->ContactDB().Exists("id", i)) { @@ -432,7 +436,7 @@ namespace OpenWifi { std::string MoveFromEntity, MoveToEntity; if (AssignIfPresent(RawObject, "entity", MoveToEntity)) { - if (!MoveToEntity.empty() && !StorageService()->EntityDB().Exists("id", MoveToEntity)) { + if (MoveToEntity.empty() || !StorageService()->EntityDB().Exists("id", MoveToEntity)) { return BadRequest(RESTAPI::Errors::EntityMustExist); } MoveFromEntity = Existing.entity; @@ -441,7 +445,7 @@ namespace OpenWifi { std::string MoveToVenue, MoveFromVenue; if (AssignIfPresent(RawObject, "venue", MoveToVenue)) { - if (!MoveToVenue.empty() && !StorageService()->VenueDB().Exists("id", MoveToVenue)) { + if (MoveToVenue.empty() || !StorageService()->VenueDB().Exists("id", MoveToVenue)) { return BadRequest(RESTAPI::Errors::VenueMustExist); } MoveFromVenue = Existing.parent; @@ -450,7 +454,7 @@ namespace OpenWifi { std::string MoveFromLocation, MoveToLocation; if (AssignIfPresent(RawObject, "location", MoveToLocation)) { - if (!MoveToLocation.empty() && + if (MoveToLocation.empty() || !StorageService()->LocationDB().Exists("id", MoveToLocation)) { return BadRequest(RESTAPI::Errors::LocationMustExist); } @@ -460,8 +464,8 @@ namespace OpenWifi { Types::UUIDvec_t MoveFromContacts, MoveToContacts; if (AssignIfPresent(RawObject, "contacts", MoveToContacts)) { - for (const auto &i : NewObject.contacts) { - if (!StorageService()->ContactDB().Exists("id", i)) { + for (const auto &contact : NewObject.contacts) { + if (!StorageService()->ContactDB().Exists("id", contact)) { return BadRequest(RESTAPI::Errors::ContactMustExist); } } @@ -471,7 +475,7 @@ namespace OpenWifi { std::string MoveFromPolicy, MoveToPolicy; if (AssignIfPresent(RawObject, "managementPolicy", MoveToPolicy)) { - if (!MoveToPolicy.empty() && !StorageService()->PolicyDB().Exists("id", MoveToPolicy)) { + if (MoveToPolicy.empty() || !StorageService()->PolicyDB().Exists("id", MoveToPolicy)) { return BadRequest(RESTAPI::Errors::UnknownManagementPolicyUUID); } MoveFromPolicy = Existing.managementPolicy; @@ -481,8 +485,8 @@ namespace OpenWifi { Types::UUIDvec_t MoveToConfigurations, MoveFromConfigurations; if (RawObject->has("deviceConfiguration")) { MoveToConfigurations = NewObject.deviceConfiguration; - for (auto &i : MoveToConfigurations) { - if (!StorageService()->ConfigurationDB().Exists("id", i)) { + for (auto &configuration : MoveToConfigurations) { + if (!StorageService()->ConfigurationDB().Exists("id", configuration)) { return BadRequest(RESTAPI::Errors::ConfigurationMustExist); } } diff --git a/src/framework/ow_constants.h b/src/framework/ow_constants.h index 2b01edd..4e41492 100644 --- a/src/framework/ow_constants.h +++ b/src/framework/ow_constants.h @@ -400,6 +400,10 @@ namespace OpenWifi::RESTAPI::Errors { static const struct msg SimulatedDeviceNotSupported { 1171, "Command not supported on simulated device." }; + static const struct msg VenuesNameAlreadyExists { + 1172, "Venue name already exists." + }; + static const struct msg SimulationDoesNotExist { 7000, "Simulation Instance ID does not exist." diff --git a/src/storage/storage_venue.cpp b/src/storage/storage_venue.cpp index fd5f684..4639211 100644 --- a/src/storage/storage_venue.cpp +++ b/src/storage/storage_venue.cpp @@ -112,6 +112,31 @@ namespace OpenWifi { return true; } + bool VenueDB::DoesVenueNameAlreadyExist(const std::string &name, const std::string &entity_uuid, const std::string &parent_uuid) { + + std::string Statement; + if(!entity_uuid.empty()) { + Statement = fmt::format("select count(*) from venues where entity='{}' and upper(name)='{}'", + entity_uuid, Poco::toUpper(name)); + } else { + Statement = fmt::format("select count(*) from venues where parent='{}' and upper(name)='{}'", + parent_uuid, Poco::toUpper(name)); + } + + std::uint64_t RecordCount = 0; + try { + Poco::Data::Session Session = Pool_.get(); + Poco::Data::Statement Command(Session); + + Command << Statement, + Poco::Data::Keywords::into(RecordCount); + Command.execute(); + } catch (...) { + + } + return RecordCount!=0; + } + } // namespace OpenWifi template <> diff --git a/src/storage/storage_venue.h b/src/storage/storage_venue.h index ec01787..4079034 100644 --- a/src/storage/storage_venue.h +++ b/src/storage/storage_venue.h @@ -26,6 +26,7 @@ namespace OpenWifi { bool GetByIP(const std::string &IP, std::string &uuid); bool Upgrade(uint32_t from, uint32_t &to) override; bool EvaluateDeviceRules(const std::string &id, ProvObjects::DeviceRules &Rules); + bool DoesVenueNameAlreadyExist(const std::string &name, const std::string &entity_uuid, const std::string &parent_uuid); private: };