From 30431ab95492e2903b7ac322cd0b66a22a0bbec0 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 10 Jan 2022 22:39:46 -0800 Subject: [PATCH] Framework update + added modified to userrecord. --- openpapi/owsec.yaml | 3 + src/RESTObjects/RESTAPI_ProvObjects.cpp | 2 + src/RESTObjects/RESTAPI_ProvObjects.h | 1 + src/RESTObjects/RESTAPI_SecurityObjects.cpp | 2 + src/RESTObjects/RESTAPI_SecurityObjects.h | 1 + src/framework/RESTAPI_errors.h | 1 + src/framework/orm.h | 86 +++++++++++---------- src/storage/orm_users.cpp | 29 ++----- src/storage/orm_users.h | 3 +- 9 files changed, 64 insertions(+), 64 deletions(-) diff --git a/openpapi/owsec.yaml b/openpapi/owsec.yaml index 339986f..b376b2d 100644 --- a/openpapi/owsec.yaml +++ b/openpapi/owsec.yaml @@ -348,6 +348,9 @@ components: securityPolicyChange: type: integer format: int64 + modified: + type: integer + format: int64 userTypeProprietaryInfo: $ref: '#/components/schemas/UserLoginLoginExtensions' diff --git a/src/RESTObjects/RESTAPI_ProvObjects.cpp b/src/RESTObjects/RESTAPI_ProvObjects.cpp index 3f76c8f..6e6d831 100644 --- a/src/RESTObjects/RESTAPI_ProvObjects.cpp +++ b/src/RESTObjects/RESTAPI_ProvObjects.cpp @@ -309,6 +309,7 @@ namespace OpenWifi::ProvObjects { field_to_json( Obj,"deviceConfiguration",deviceConfiguration); field_to_json( Obj,"rrm",rrm); field_to_json( Obj,"managementPolicy",managementPolicy); + field_to_json( Obj,"state",state); } bool InventoryTag::from_json(const Poco::JSON::Object::Ptr &Obj) { @@ -326,6 +327,7 @@ namespace OpenWifi::ProvObjects { field_from_json( Obj,"deviceConfiguration",deviceConfiguration); field_from_json( Obj,"rrm",rrm); field_from_json( Obj,"managementPolicy",managementPolicy); + field_from_json( Obj,"state",state); return true; } catch(...) { diff --git a/src/RESTObjects/RESTAPI_ProvObjects.h b/src/RESTObjects/RESTAPI_ProvObjects.h index 6ac9b4f..3ae867b 100644 --- a/src/RESTObjects/RESTAPI_ProvObjects.h +++ b/src/RESTObjects/RESTAPI_ProvObjects.h @@ -284,6 +284,7 @@ namespace OpenWifi::ProvObjects { std::string deviceConfiguration; std::string rrm; Types::UUID_t managementPolicy; + std::string state; void to_json(Poco::JSON::Object &Obj) const; bool from_json(const Poco::JSON::Object::Ptr &Obj); diff --git a/src/RESTObjects/RESTAPI_SecurityObjects.cpp b/src/RESTObjects/RESTAPI_SecurityObjects.cpp index b6f0c6d..14b2fc1 100644 --- a/src/RESTObjects/RESTAPI_SecurityObjects.cpp +++ b/src/RESTObjects/RESTAPI_SecurityObjects.cpp @@ -254,6 +254,7 @@ namespace OpenWifi::SecurityObjects { field_to_json(Obj,"lastPasswords",lastPasswords); field_to_json(Obj,"oauthType",oauthType); field_to_json(Obj,"oauthUserInfo",oauthUserInfo); + field_to_json(Obj,"modified",modified); }; bool UserInfo::from_json(const Poco::JSON::Object::Ptr &Obj) { @@ -288,6 +289,7 @@ namespace OpenWifi::SecurityObjects { field_from_json(Obj,"lastPasswords",lastPasswords); field_from_json(Obj,"oauthType",oauthType); field_from_json(Obj,"oauthUserInfo",oauthUserInfo); + field_from_json(Obj,"modified",modified); return true; } catch (const Poco::Exception &E) { diff --git a/src/RESTObjects/RESTAPI_SecurityObjects.h b/src/RESTObjects/RESTAPI_SecurityObjects.h index fd3d5c8..cff551e 100644 --- a/src/RESTObjects/RESTAPI_SecurityObjects.h +++ b/src/RESTObjects/RESTAPI_SecurityObjects.h @@ -136,6 +136,7 @@ namespace OpenWifi { OpenWifi::Types::StringVec lastPasswords; std::string oauthType; std::string oauthUserInfo; + uint64_t modified; void to_json(Poco::JSON::Object &Obj) const; bool from_json(const Poco::JSON::Object::Ptr &Obj); diff --git a/src/framework/RESTAPI_errors.h b/src/framework/RESTAPI_errors.h index b203797..061ba9a 100644 --- a/src/framework/RESTAPI_errors.h +++ b/src/framework/RESTAPI_errors.h @@ -59,5 +59,6 @@ namespace OpenWifi::RESTAPI::Errors { static const std::string MissingAuthenticationInformation{"Missing authentication information."}; static const std::string InsufficientAccessRights{"Insufficient access rights to complete the operation."}; static const std::string ExpiredToken{"Token has expired, user must login."}; + static const std::string SubscriberMustExist{"Subscriber must exist."}; } diff --git a/src/framework/orm.h b/src/framework/orm.h index 51e9b0b..72e0e58 100644 --- a/src/framework/orm.h +++ b/src/framework/orm.h @@ -171,6 +171,9 @@ namespace ORM { template class DB { public: + + typedef const char * field_name_t; + DB( OpenWifi::DBType dbtype, const char *TableName, const FieldVec & Fields, @@ -258,27 +261,27 @@ namespace ORM { [[nodiscard]] const std::string & SelectList() const { return SelectList_; }; [[nodiscard]] const std::string & UpdateFields() const { return UpdateFields_; }; - inline std::string OP(const char *F, SqlComparison O , bool V) { + inline std::string OP(field_name_t F, SqlComparison O , bool V) { assert( FieldNames_.find(F) != FieldNames_.end() ); return std::string{"("} + F + SQLCOMPS[O] + (V ? "true" : "false") + ")" ; } - inline std::string OP(const char *F, SqlComparison O , int V) { + inline std::string OP(field_name_t F, SqlComparison O , int V) { assert( FieldNames_.find(F) != FieldNames_.end() ); return std::string{"("} + F + SQLCOMPS[O] + std::to_string(V) + ")" ; } - inline std::string OP(const char *F, SqlComparison O , uint64_t V) { + inline std::string OP(field_name_t F, SqlComparison O , uint64_t V) { assert( FieldNames_.find(F) != FieldNames_.end() ); return std::string{"("} + F + SQLCOMPS[O] + std::to_string(V) + ")" ; } - std::string OP(const char *F, SqlComparison O , const std::string & V) { + std::string OP(field_name_t F, SqlComparison O , const std::string & V) { assert( FieldNames_.find(F) != FieldNames_.end() ); return std::string{"("} + F + SQLCOMPS[O] + "'" + Escape(V) + "')" ; } - std::string OP(const char *F, SqlComparison O , const char * V) { + std::string OP(field_name_t F, SqlComparison O , const char * V) { assert( FieldNames_.find(F) != FieldNames_.end() ); return std::string{"("} + F + SQLCOMPS[O] + "'" + Escape(V) + "')" ; } @@ -299,9 +302,12 @@ namespace ORM { return std::string{"("} + P1 + BOPS[BOP] + OP(true, P2, More...); } - inline bool Create() { - std::string S; + bool Upgrade() { + uint32_t To; + return Upgrade(0, To); + } + inline bool Create() { switch(Type_) { case OpenWifi::DBType::mysql: { try { @@ -309,12 +315,10 @@ namespace ORM { std::string Statement = IndexCreation_.empty() ? "create table if not exists " + TableName_ +" ( " + CreateFields_ + " )" : "create table if not exists " + TableName_ +" ( " + CreateFields_ + " ), " + IndexCreation_[0] + " )"; Session << Statement , Poco::Data::Keywords::now; - return true; } catch (const Poco::Exception &E) { Logger_.error("Failure to create MySQL DB resources."); Logger_.log(E); } - return false; } break; @@ -326,7 +330,6 @@ namespace ORM { for(const auto &i:IndexCreation_) { Session << i , Poco::Data::Keywords::now; } - return true; } catch (const Poco::Exception &E) { Logger_.error("Failure to create SQLITE DB resources."); Logger_.log(E); @@ -342,7 +345,6 @@ namespace ORM { for(const auto &i:IndexCreation_) { Session << i , Poco::Data::Keywords::now; } - return true; } catch (const Poco::Exception &E) { Logger_.error("Failure to create POSTGRESQL DB resources."); Logger_.log(E); @@ -350,7 +352,7 @@ namespace ORM { } break; } - return false; + return Upgrade(); } [[nodiscard]] std::string ConvertParams(const std::string & S) const { @@ -400,7 +402,7 @@ namespace ORM { return false; } - template bool GetRecord( const char * FieldName, const T & Value, RecordType & R) { + template bool GetRecord(field_name_t FieldName, const T & Value, RecordType & R) { try { assert( FieldNames_.find(FieldName) != FieldNames_.end() ); @@ -438,7 +440,7 @@ namespace ORM { typedef std::vector StringVec; template < typename T, - typename T0, typename T1> bool GR(const char *FieldName, T & R,T0 &V0, T1 &V1) { + typename T0, typename T1> bool GR(field_name_t FieldName, T & R,T0 &V0, T1 &V1) { try { assert( FieldNames_.find(FieldName) != FieldNames_.end() ); @@ -495,7 +497,7 @@ namespace ORM { return false; } - template bool UpdateRecord( const char *FieldName, const T & Value, const RecordType & R) { + template bool UpdateRecord(field_name_t FieldName, const T & Value, const RecordType & R) { try { assert( FieldNames_.find(FieldName) != FieldNames_.end() ); @@ -522,7 +524,7 @@ namespace ORM { return false; } - template bool ReplaceRecord( const char *FieldName, const T & Value, RecordType & R) { + template bool ReplaceRecord(field_name_t FieldName, const T & Value, RecordType & R) { try { if(Exists(FieldName, Value)) { return UpdateRecord(FieldName,Value,R); @@ -534,7 +536,7 @@ namespace ORM { return false; } - template bool GetNameAndDescription(const char *FieldName, const T & Value, std::string & Name, std::string & Description ) { + template bool GetNameAndDescription(field_name_t FieldName, const T & Value, std::string & Name, std::string & Description ) { try { assert( FieldNames_.find(FieldName) != FieldNames_.end() ); Poco::Data::Session Session = Pool_.get(); @@ -561,7 +563,7 @@ namespace ORM { return false; } - template bool DeleteRecord( const char *FieldName, const T & Value) { + template bool DeleteRecord(field_name_t FieldName, const T & Value) { try { assert( FieldNames_.find(FieldName) != FieldNames_.end() ); @@ -599,7 +601,7 @@ namespace ORM { return false; } - bool Exists(const char *FieldName, const std::string & Value) { + bool Exists(field_name_t FieldName, const std::string & Value) { try { assert( FieldNames_.find(FieldName) != FieldNames_.end() ); @@ -616,7 +618,7 @@ namespace ORM { bool Iterate( std::function F) { try { - uint64_t Offset=1; + uint64_t Offset=0; uint64_t Batch=50; bool Done=false; while(!Done) { @@ -691,7 +693,7 @@ namespace ORM { return 0; } - template bool ManipulateVectorMember( X T, const char *FieldName, std::string & ParentUUID, std::string & ChildUUID, bool Add) { + template bool ManipulateVectorMember( X T, field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID, bool Add) { try { assert( FieldNames_.find(FieldName) != FieldNames_.end() ); @@ -751,89 +753,89 @@ namespace ORM { return true; } - inline bool AddChild( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) { + inline bool AddChild(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { return ManipulateVectorMember(&RecordType::children, FieldName, ParentUUID, ChildUUID, true); } - inline bool DeleteChild( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) { + inline bool DeleteChild(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { return ManipulateVectorMember(&RecordType::children, FieldName, ParentUUID, ChildUUID, false); } - inline bool AddLocation( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) { + inline bool AddLocation(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { return ManipulateVectorMember(&RecordType::locations, FieldName, ParentUUID, ChildUUID, true); } - inline bool DeleteLocation( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) { + inline bool DeleteLocation(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { return ManipulateVectorMember(&RecordType::locations, FieldName, ParentUUID, ChildUUID, false); } - inline bool AddContact( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) { + inline bool AddContact(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { return ManipulateVectorMember(&RecordType::contacts, FieldName, ParentUUID, ChildUUID, true); } - inline bool DeleteContact( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) { + inline bool DeleteContact(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { return ManipulateVectorMember(&RecordType::contacts, FieldName, ParentUUID, ChildUUID, false); } - inline bool AddVenue( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) { + inline bool AddVenue(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { return ManipulateVectorMember(&RecordType::venues, FieldName, ParentUUID, ChildUUID, true); } - inline bool DeleteVenue( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) { + inline bool DeleteVenue(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { return ManipulateVectorMember(&RecordType::venues, FieldName, ParentUUID, ChildUUID, false); } - inline bool AddDevice( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) { + inline bool AddDevice(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { return ManipulateVectorMember(&RecordType::devices, FieldName, ParentUUID, ChildUUID, true); } - inline bool DeleteDevice( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) { + inline bool DeleteDevice(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { return ManipulateVectorMember(&RecordType::devices, FieldName, ParentUUID, ChildUUID, false); } - inline bool AddEntity( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) { + inline bool AddEntity(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { return ManipulateVectorMember(&RecordType::entities, FieldName, ParentUUID, ChildUUID, true); } - inline bool DeleteEntity( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) { + inline bool DeleteEntity(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { return ManipulateVectorMember(&RecordType::entities, FieldName, ParentUUID, ChildUUID, false); } - inline bool AddUser( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) { + inline bool AddUser(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { return ManipulateVectorMember(&RecordType::users, FieldName, ParentUUID, ChildUUID, true); } - inline bool DelUser( const char *FieldName, std::string & ParentUUID, std::string & ChildUUID) { + inline bool DelUser(field_name_t FieldName, const std::string & ParentUUID, const std::string & ChildUUID) { return ManipulateVectorMember(&RecordType::users, FieldName, ParentUUID, ChildUUID, false); } - inline bool AddInUse(const char *FieldName, std::string & ParentUUID, const std::string & Prefix, const std::string & ChildUUID) { + inline bool AddInUse(field_name_t FieldName, std::string & ParentUUID, const std::string & Prefix, const std::string & ChildUUID) { std::string FakeUUID{ Prefix + ":" + ChildUUID}; return ManipulateVectorMember(&RecordType::inUse,FieldName, ParentUUID, FakeUUID, true); } - inline bool DeleteInUse(const char *FieldName, std::string & ParentUUID, const std::string & Prefix, const std::string & ChildUUID) { + inline bool DeleteInUse(field_name_t FieldName, std::string & ParentUUID, const std::string & Prefix, const std::string & ChildUUID) { std::string FakeUUID{ Prefix + ":" + ChildUUID}; return ManipulateVectorMember(&RecordType::inUse,FieldName, ParentUUID, FakeUUID, false); } - inline bool DeleteContact(const char *FieldName, std::string & ParentUUID, const std::string & ChildUUID) { + inline bool DeleteContact(field_name_t FieldName, std::string & ParentUUID, const std::string & ChildUUID) { return ManipulateVectorMember(&RecordType::contacts,FieldName, ParentUUID, ChildUUID, false); } - inline bool AddContact(const char *FieldName, std::string & ParentUUID, const std::string & ChildUUID) { + inline bool AddContact(field_name_t FieldName, std::string & ParentUUID, const std::string & ChildUUID) { return ManipulateVectorMember(&RecordType::contacts,FieldName, ParentUUID, ChildUUID, true); } - inline bool DeleteLocation(const char *FieldName, std::string & ParentUUID, const std::string & ChildUUID) { + inline bool DeleteLocation(field_name_t FieldName, std::string & ParentUUID, const std::string & ChildUUID) { return ManipulateVectorMember(&RecordType::locations,FieldName, ParentUUID, ChildUUID, false); } - inline bool AddLocation(const char *FieldName, std::string & ParentUUID, const std::string & ChildUUID) { + inline bool AddLocation(field_name_t FieldName, std::string & ParentUUID, const std::string & ChildUUID) { return ManipulateVectorMember(&RecordType::locations,FieldName, ParentUUID, ChildUUID, true); } - inline bool GetInUse(const char *FieldName, std::string & UUID, std::vector & UUIDs ) { + inline bool GetInUse(field_name_t FieldName, const std::string & UUID, std::vector & UUIDs ) { RecordType R; if(GetRecord(FieldName,UUID,R)) { UUIDs = R.inUse; diff --git a/src/storage/orm_users.cpp b/src/storage/orm_users.cpp index 78997ee..1989b68 100644 --- a/src/storage/orm_users.cpp +++ b/src/storage/orm_users.cpp @@ -72,7 +72,8 @@ namespace OpenWifi { ORM::Field{"currentPassword", ORM::FieldType::FT_TEXT}, ORM::Field{"lastPasswords", ORM::FieldType::FT_TEXT}, ORM::Field{"oauthType", ORM::FieldType::FT_TEXT}, - ORM::Field{"oauthUserInfo", ORM::FieldType::FT_TEXT} + ORM::Field{"oauthUserInfo", ORM::FieldType::FT_TEXT}, + ORM::Field{"modified", ORM::FieldType::FT_TEXT} }; static ORM::IndexVec MakeIndices(const std::string & shortname) { @@ -94,27 +95,11 @@ namespace OpenWifi { } bool BaseUserDB::Upgrade(uint32_t from, uint32_t &to) { - - if(from == CurrentVersion) { - to = CurrentVersion ; - return true; - } - - auto Session = Pool_.get(); - Poco::Data::Statement S(Session); - - if(from==0) { - std::vector Statements{ - "alter table " + TableName_ + " rename column owner to entity;", - "alter table " + TableName_ + " rename column oauth to deviceList;", - "alter table " + TableName_ + " rename column oauthuserinfo to loginRecords;", - "alter table " + TableName_ + " add column modified BIGINT;" - }; - RunScript(Statements); - } - + std::vector Statements{ + "alter table " + TableName_ + " add column modified BIGINT;" + }; + RunScript(Statements); to = CurrentVersion; - return true; } @@ -328,6 +313,7 @@ template<> void ORM::DB()); U.oauthType = T.get<28>(); U.oauthUserInfo = T.get<29>(); + U.modified = T.get<30>(); } template<> void ORM::DB< OpenWifi::UserInfoRecordTuple, @@ -363,4 +349,5 @@ template<> void ORM::DB< OpenWifi::UserInfoRecordTuple, T.set<27>(OpenWifi::RESTAPI_utils::to_string(U.lastPasswords)); T.set<28>(U.oauthType); T.set<29>(U.oauthUserInfo); + T.set<30>(U.modified); } diff --git a/src/storage/orm_users.h b/src/storage/orm_users.h index cde8c96..3a79081 100644 --- a/src/storage/orm_users.h +++ b/src/storage/orm_users.h @@ -39,7 +39,8 @@ namespace OpenWifi { std::string, // currentPassword; std::string, // lastPasswords; std::string, // oauthType; - std::string // oauthUserInfo; + std::string, // oauthUserInfo; + uint64_t // modified > UserInfoRecordTuple; typedef std::vector UserInfoRecordTupleList;