Skip to content

Commit

Permalink
Adding support for contacts and locations.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephb9959 committed Sep 27, 2021
1 parent 7421592 commit 580daec
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 23 deletions.
Binary file added Configuration application.docx
Binary file not shown.
2 changes: 1 addition & 1 deletion build
Original file line number Diff line number Diff line change
@@ -1 +1 @@
209
211
4 changes: 2 additions & 2 deletions openapi/ow_prov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ components:
type: string
format: uuid
example: each uuid is preceded by ent, or ven to say that the elemenet is entity or venue
owner:
entity:
type: string
format: uuid
managementPolicy:
Expand Down Expand Up @@ -421,7 +421,7 @@ components:
items:
type: string
format: uuid
owner:
entity:
type: string
format: uuid
managementPolicy:
Expand Down
8 changes: 4 additions & 4 deletions src/RESTAPI_ProvObjects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ namespace OpenWifi::ProvObjects {
RESTAPI_utils::field_to_json( Obj,"mobiles",mobiles);
RESTAPI_utils::field_to_json( Obj,"geoCode",geoCode);
RESTAPI_utils::field_to_json( Obj,"inUse",inUse);
RESTAPI_utils::field_to_json( Obj,"owner",owner);
RESTAPI_utils::field_to_json( Obj,"entity",entity);
RESTAPI_utils::field_to_json( Obj,"managementPolicy",managementPolicy);
}

Expand All @@ -229,7 +229,7 @@ namespace OpenWifi::ProvObjects {
RESTAPI_utils::field_from_json( Obj,"mobiles",mobiles);
RESTAPI_utils::field_from_json( Obj,"geoCode",geoCode);
RESTAPI_utils::field_from_json( Obj,"inUse",inUse);
RESTAPI_utils::field_from_json( Obj,"owner",owner);
RESTAPI_utils::field_from_json( Obj,"entity",entity);
RESTAPI_utils::field_from_json( Obj,"managementPolicy",managementPolicy);
return true;
} catch (...) {
Expand All @@ -253,7 +253,7 @@ namespace OpenWifi::ProvObjects {
RESTAPI_utils::field_to_json( Obj,"secondaryEmail",secondaryEmail);
RESTAPI_utils::field_to_json( Obj,"accessPIN",accessPIN);
RESTAPI_utils::field_to_json( Obj,"inUse",inUse);
RESTAPI_utils::field_to_json( Obj,"owner",owner);
RESTAPI_utils::field_to_json( Obj,"entity",entity);
RESTAPI_utils::field_to_json( Obj,"managementPolicy",managementPolicy);
}

Expand All @@ -275,7 +275,7 @@ namespace OpenWifi::ProvObjects {
RESTAPI_utils::field_from_json( Obj,"secondaryEmail",secondaryEmail);
RESTAPI_utils::field_from_json( Obj,"accessPIN",accessPIN);
RESTAPI_utils::field_from_json( Obj,"inUse",inUse);
RESTAPI_utils::field_from_json( Obj,"owner",owner);
RESTAPI_utils::field_from_json( Obj,"entity",entity);
RESTAPI_utils::field_from_json( Obj,"managementPolicy",managementPolicy);
return true;
} catch (...) {
Expand Down
4 changes: 2 additions & 2 deletions src/RESTAPI_ProvObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ namespace OpenWifi::ProvObjects {
Types::StringVec mobiles;
std::string geoCode;
Types::StringVec inUse;
Types::UUID_t owner;
Types::UUID_t entity;
Types::UUID_t managementPolicy;

void to_json(Poco::JSON::Object &Obj) const;
Expand Down Expand Up @@ -226,7 +226,7 @@ namespace OpenWifi::ProvObjects {
std::string secondaryEmail;
std::string accessPIN;
Types::StringVec inUse;
Types::UUID_t owner;
Types::UUID_t entity;
Types::UUID_t managementPolicy;

void to_json(Poco::JSON::Object &Obj) const;
Expand Down
47 changes: 43 additions & 4 deletions src/RESTAPI_contact_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ namespace OpenWifi{
return;
}

if(NewObject.entity.empty() || !Storage()->EntityDB().Exists("id",NewObject.entity)) {
BadRequest(RESTAPI::Errors::EntityMustExist);
return;
}

if(!NewObject.managementPolicy.empty() && !Storage()->PolicyDB().Exists("id",NewObject.managementPolicy)) {
BadRequest(RESTAPI::Errors::UnknownManagementPolicyUUID);
return;
}

NewObject.info.id = Daemon()->CreateUUID();
NewObject.info.created = NewObject.info.modified = std::time(nullptr);
NewObject.inUse.clear();
Expand All @@ -112,9 +122,9 @@ namespace OpenWifi{
return;
}

auto Obj = ParseStream();
auto RawObject = ParseStream();
ProvObjects::Contact NewObject;
if (!NewObject.from_json(Obj)) {
if (!NewObject.from_json(RawObject)) {
BadRequest(RESTAPI::Errors::InvalidJSONDocument);
return;
}
Expand All @@ -124,11 +134,40 @@ namespace OpenWifi{
Existing.info.notes.insert(Existing.info.notes.begin(),i);
}

AssignIfPresent(Obj,"name",Existing.info.name);
AssignIfPresent(Obj,"description",Existing.info.description);
std::string MoveEntity, MovePolicy;
bool MovingEntity=false, MovingPolicy=false;
if(AssignIfPresent(RawObject,"managementPolicy",MovePolicy)) {
if(!MovePolicy.empty() && !Storage()->PolicyDB().Exists("id",MovePolicy)) {
BadRequest(RESTAPI::Errors::UnknownManagementPolicyUUID);
return;
}
MovingPolicy = MovePolicy != Existing.managementPolicy;
}
if(AssignIfPresent(RawObject,"entity",MoveEntity)) {
if(MoveEntity.empty() || !Storage()->PolicyDB().Exists("id",MoveEntity)) {
BadRequest(RESTAPI::Errors::EntityMustExist);
return;
}
MovingEntity = MoveEntity != Existing.entity;
}

AssignIfPresent(RawObject,"name",Existing.info.name);
AssignIfPresent(RawObject,"description",Existing.info.description);
Existing.info.modified = std::time(nullptr);

if(DB_.UpdateRecord("id", UUID, Existing)) {
if(MovingPolicy) {
if(!Existing.managementPolicy.empty())
Storage()->PolicyDB().DeleteInUse("id",Existing.managementPolicy,DB_.Prefix(),Existing.info.id);
if(!MovePolicy.empty())
Storage()->PolicyDB().AddInUse("id", MovePolicy, DB_.Prefix(), Existing.info.id);
Existing.managementPolicy = MovePolicy;
}
if(MovingEntity) {
Existing.entity = MoveEntity;
}
DB_.UpdateRecord("id",UUID,Existing);

ProvObjects::Contact NewObjectAdded;
DB_.GetRecord("id", UUID, NewObjectAdded);
Poco::JSON::Object Answer;
Expand Down
1 change: 1 addition & 0 deletions src/RESTAPI_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace OpenWifi::RESTAPI::Errors {
static const std::string LocationMustExist{"Location must exist."};
static const std::string OnlyWSSupported{"This endpoint only supports WebSocket."};
static const std::string DeviceConfigurationUUID{"Unknown DeviceConfiguration UUID."};
static const std::string OwnerMissing{"Missing owning entity UUID."};

}

Expand Down
47 changes: 43 additions & 4 deletions src/RESTAPI_location_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ namespace OpenWifi{
return;
}

if(NewObject.entity.empty() || !Storage()->EntityDB().Exists("id",NewObject.entity)) {
BadRequest(RESTAPI::Errors::EntityMustExist);
return;
}

if(!NewObject.managementPolicy.empty() && !Storage()->PolicyDB().Exists("id",NewObject.managementPolicy)) {
BadRequest(RESTAPI::Errors::UnknownManagementPolicyUUID);
return;
}

NewObject.info.id = Daemon()->CreateUUID();
NewObject.info.created = NewObject.info.modified = std::time(nullptr);
NewObject.inUse.clear();
Expand All @@ -112,23 +122,52 @@ namespace OpenWifi{
return;
}

auto Obj = ParseStream();
auto RawObject = ParseStream();
ProvObjects::Location NewObject;
if (!NewObject.from_json(Obj)) {
if (!NewObject.from_json(RawObject)) {
BadRequest(RESTAPI::Errors::InvalidJSONDocument);
return;
}

std::string MoveEntity, MovePolicy;
bool MovingEntity=false, MovingPolicy=false;
if(AssignIfPresent(RawObject,"managementPolicy",MovePolicy)) {
if(!MovePolicy.empty() && !Storage()->PolicyDB().Exists("id",MovePolicy)) {
BadRequest(RESTAPI::Errors::UnknownManagementPolicyUUID);
return;
}
MovingPolicy = MovePolicy != Existing.managementPolicy;
}
if(AssignIfPresent(RawObject,"entity",MoveEntity)) {
if(MoveEntity.empty() || !Storage()->PolicyDB().Exists("id",MoveEntity)) {
BadRequest(RESTAPI::Errors::EntityMustExist);
return;
}
MovingEntity = MoveEntity != Existing.entity;
}

for(auto &i:NewObject.info.notes) {
i.createdBy = UserInfo_.userinfo.email;
Existing.info.notes.insert(Existing.info.notes.begin(),i);
}

AssignIfPresent(Obj,"name",Existing.info.name);
AssignIfPresent(Obj,"description",Existing.info.description);
AssignIfPresent(RawObject,"name",Existing.info.name);
AssignIfPresent(RawObject,"description",Existing.info.description);
Existing.info.modified = std::time(nullptr);

if(DB_.UpdateRecord("id", UUID, Existing)) {
if(MovingPolicy) {
if(!Existing.managementPolicy.empty())
Storage()->PolicyDB().DeleteInUse("id",Existing.managementPolicy,DB_.Prefix(),Existing.info.id);
if(!MovePolicy.empty())
Storage()->PolicyDB().AddInUse("id", MovePolicy, DB_.Prefix(), Existing.info.id);
Existing.managementPolicy = MovePolicy;
}
if(MovingEntity) {
Existing.entity = MoveEntity;
}

DB_.UpdateRecord("id",UUID,Existing);
ProvObjects::Location NewObjectAdded;
DB_.GetRecord("id", UUID, NewObjectAdded);
Poco::JSON::Object Answer;
Expand Down
6 changes: 3 additions & 3 deletions src/storage_contact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace OpenWifi {
ORM::Field{"inUse",ORM::FieldType::FT_TEXT},
ORM::Field{"tags",ORM::FieldType::FT_TEXT},
ORM::Field{"managementPolicy",ORM::FieldType::FT_TEXT},
ORM::Field{"owner",ORM::FieldType::FT_TEXT}
ORM::Field{"entity",ORM::FieldType::FT_TEXT}
};

static ORM::IndexVec ContactDB_Indexes{
Expand Down Expand Up @@ -75,7 +75,7 @@ template<> void ORM::DB<OpenWifi::ContactDBRecordType, OpenWifi::ProvObjects::Co
OpenWifi::Types::from_string(In.get<18>(), Out.inUse);
Out.info.tags = OpenWifi::RESTAPI_utils::to_taglist(In.get<19>());
Out.managementPolicy = In.get<20>();
Out.owner = In.get<21>();
Out.entity = In.get<21>();
}

template<> void ORM::DB<OpenWifi::ContactDBRecordType, OpenWifi::ProvObjects::Contact>::Convert(OpenWifi::ProvObjects::Contact &In, OpenWifi::ContactDBRecordType &Out) {
Expand All @@ -100,5 +100,5 @@ template<> void ORM::DB<OpenWifi::ContactDBRecordType, OpenWifi::ProvObjects::Co
Out.set<18>(OpenWifi::Types::to_string(In.inUse));
Out.set<19>(OpenWifi::RESTAPI_utils::to_string(In.info.tags));
Out.set<20>(In.managementPolicy);
Out.set<21>(In.owner);
Out.set<21>(In.entity);
}
6 changes: 3 additions & 3 deletions src/storage_location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace OpenWifi {
ORM::Field{"inUse",ORM::FieldType::FT_TEXT},
ORM::Field{"tags",ORM::FieldType::FT_TEXT},
ORM::Field{"managementPolicy",ORM::FieldType::FT_TEXT},
ORM::Field{"owner",ORM::FieldType::FT_TEXT}
ORM::Field{"entity",ORM::FieldType::FT_TEXT}
};

static ORM::IndexVec LocationDB_Indexes{
Expand Down Expand Up @@ -71,7 +71,7 @@ template<> void ORM::DB<OpenWifi::LocationDBRecordType , OpenWifi::ProvObjects::
OpenWifi::Types::from_string(In.get<16>(), Out.inUse);
Out.info.tags = OpenWifi::RESTAPI_utils::to_taglist(In.get<17>());
Out.managementPolicy = In.get<18>();
Out.owner = In.get<19>();
Out.entity = In.get<19>();
}

template<> void ORM::DB<OpenWifi::LocationDBRecordType, OpenWifi::ProvObjects::Location>::Convert(OpenWifi::ProvObjects::Location &In, OpenWifi::LocationDBRecordType &Out) {
Expand All @@ -94,5 +94,5 @@ template<> void ORM::DB<OpenWifi::LocationDBRecordType, OpenWifi::ProvObjects::L
Out.set<16>(OpenWifi::Types::to_string(In.inUse));
Out.set<17>(OpenWifi::RESTAPI_utils::to_string(In.info.tags));
Out.set<18>(In.managementPolicy);
Out.set<19>(In.owner);
Out.set<19>(In.entity);
}

0 comments on commit 580daec

Please sign in to comment.