From c47e9bc98ddd61a814405afa77b3116eaeb627f7 Mon Sep 17 00:00:00 2001 From: stephb9959 Date: Mon, 10 Jan 2022 23:40:45 -0800 Subject: [PATCH] Framework update + added modified to userrecord. --- src/AuthService.cpp | 4 ++++ src/RESTAPI/RESTAPI_action_links.cpp | 24 ++++++++++++++++----- src/RESTAPI/RESTAPI_oauth2_handler.cpp | 1 + src/RESTAPI/RESTAPI_suboauth2_handler.cpp | 1 + src/RESTObjects/RESTAPI_SecurityObjects.cpp | 2 ++ src/RESTObjects/RESTAPI_SecurityObjects.h | 1 + src/storage/orm_actionLinks.cpp | 18 ++++++++++++++-- src/storage/orm_actionLinks.h | 6 +++++- 8 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/AuthService.cpp b/src/AuthService.cpp index 2ee5188..813af7d 100644 --- a/src/AuthService.cpp +++ b/src/AuthService.cpp @@ -423,6 +423,7 @@ namespace OpenWifi { } UInfo.userinfo.lastPasswordChange = std::time(nullptr); UInfo.userinfo.changePassword = false; + UInfo.userinfo.modified = std::time(nullptr); StorageService()->UserDB().UpdateUserInfo(AUTHENTICATION_SYSTEM, UInfo.userinfo.id,UInfo.userinfo); } @@ -468,6 +469,7 @@ namespace OpenWifi { } UInfo.userinfo.lastPasswordChange = std::time(nullptr); UInfo.userinfo.changePassword = false; + UInfo.userinfo.modified = std::time(nullptr); StorageService()->SubDB().UpdateUserInfo(AUTHENTICATION_SYSTEM, UInfo.userinfo.id,UInfo.userinfo); } @@ -560,6 +562,7 @@ namespace OpenWifi { A.id = MicroService::CreateUUID(); A.created = std::time(nullptr); A.expires = A.created + 24*60*60; + A.userAction = true; StorageService()->ActionLinksDB().CreateAction(A); UInfo.waitingForEmailCheck = true; return true; @@ -573,6 +576,7 @@ namespace OpenWifi { A.id = MicroService::CreateUUID(); A.created = std::time(nullptr); A.expires = A.created + 24*60*60; + A.userAction = false; StorageService()->ActionLinksDB().CreateAction(A); UInfo.waitingForEmailCheck = true; return true; diff --git a/src/RESTAPI/RESTAPI_action_links.cpp b/src/RESTAPI/RESTAPI_action_links.cpp index 17d7abd..353c053 100644 --- a/src/RESTAPI/RESTAPI_action_links.cpp +++ b/src/RESTAPI/RESTAPI_action_links.cpp @@ -77,7 +77,9 @@ namespace OpenWifi { } SecurityObjects::UserInfo UInfo; - if(!StorageService()->UserDB().GetUserById(Link.userId,UInfo)) { + + bool Found = Link.userAction ? StorageService()->UserDB().GetUserById(Link.userId,UInfo) : StorageService()->SubDB().GetUserById(Link.userId,UInfo); + if(!Found) { Poco::File FormFile{ Daemon()->AssetDir() + "/password_reset_error.html"}; Types::StringPairVec FormVars{ {"UUID", Id}, {"ERROR_TEXT", "This request does not contain a valid user ID. Please contact your system administrator."}}; @@ -91,13 +93,20 @@ namespace OpenWifi { return SendHTMLFileBack(FormFile,FormVars); } - if(!AuthService()->SetPassword(Password1,UInfo)) { + bool GoodPassword = Link.userAction ? AuthService()->SetPassword(Password1,UInfo) : AuthService()->SetSubPassword(Password1,UInfo); + if(!GoodPassword) { Poco::File FormFile{ Daemon()->AssetDir() + "/password_reset_error.html"}; Types::StringPairVec FormVars{ {"UUID", Id}, {"ERROR_TEXT", "You cannot reuse one of your recent passwords."}}; return SendHTMLFileBack(FormFile,FormVars); } - StorageService()->UserDB().UpdateUserInfo(UInfo.email,Link.userId,UInfo); + + UInfo.modified = std::time(nullptr); + if(Link.userAction) + StorageService()->UserDB().UpdateUserInfo(UInfo.email,Link.userId,UInfo); + else + StorageService()->SubDB().UpdateUserInfo(UInfo.email,Link.userId,UInfo); + Poco::File FormFile{ Daemon()->AssetDir() + "/password_reset_success.html"}; Types::StringPairVec FormVars{ {"UUID", Id}, {"USERNAME", UInfo.email}, @@ -118,7 +127,8 @@ namespace OpenWifi { } SecurityObjects::UserInfo UInfo; - if (!StorageService()->UserDB().GetUserById(Link.userId, UInfo)) { + bool Found = Link.userAction ? StorageService()->UserDB().GetUserById(Link.userId,UInfo) : StorageService()->SubDB().GetUserById(Link.userId,UInfo); + if (!Found) { Types::StringPairVec FormVars{{"UUID", Link.id}, {"ERROR_TEXT", "This does not appear to be a valid email verification link.."}}; Poco::File FormFile{Daemon()->AssetDir() + "/email_verification_error.html"}; @@ -130,7 +140,11 @@ namespace OpenWifi { UInfo.validated = true; UInfo.lastEmailCheck = std::time(nullptr); UInfo.validationDate = std::time(nullptr); - StorageService()->UserDB().UpdateUserInfo(UInfo.email, Link.userId, UInfo); + UInfo.modified = std::time(nullptr); + if(Link.userAction) + StorageService()->UserDB().UpdateUserInfo(UInfo.email, Link.userId, UInfo); + else + StorageService()->SubDB().UpdateUserInfo(UInfo.email, Link.userId, UInfo); Types::StringPairVec FormVars{{"UUID", Link.id}, {"USERNAME", UInfo.email}, {"ACTION_LINK",MicroService::instance().GetUIURI()}}; diff --git a/src/RESTAPI/RESTAPI_oauth2_handler.cpp b/src/RESTAPI/RESTAPI_oauth2_handler.cpp index 31d41ba..97c0ae3 100644 --- a/src/RESTAPI/RESTAPI_oauth2_handler.cpp +++ b/src/RESTAPI/RESTAPI_oauth2_handler.cpp @@ -84,6 +84,7 @@ namespace OpenWifi { NewLink.userId = UInfo1.id; NewLink.created = std::time(nullptr); NewLink.expires = NewLink.created + (24*60*60); + NewLink.userAction = true; StorageService()->ActionLinksDB().CreateAction(NewLink); Poco::JSON::Object ReturnObj; diff --git a/src/RESTAPI/RESTAPI_suboauth2_handler.cpp b/src/RESTAPI/RESTAPI_suboauth2_handler.cpp index d72452c..b1f661f 100644 --- a/src/RESTAPI/RESTAPI_suboauth2_handler.cpp +++ b/src/RESTAPI/RESTAPI_suboauth2_handler.cpp @@ -78,6 +78,7 @@ namespace OpenWifi { NewLink.userId = UInfo1.id; NewLink.created = std::time(nullptr); NewLink.expires = NewLink.created + (24*60*60); + NewLink.userAction = false; StorageService()->ActionLinksDB().CreateAction(NewLink); Poco::JSON::Object ReturnObj; diff --git a/src/RESTObjects/RESTAPI_SecurityObjects.cpp b/src/RESTObjects/RESTAPI_SecurityObjects.cpp index 14b2fc1..1b82147 100644 --- a/src/RESTObjects/RESTAPI_SecurityObjects.cpp +++ b/src/RESTObjects/RESTAPI_SecurityObjects.cpp @@ -498,6 +498,7 @@ namespace OpenWifi::SecurityObjects { field_to_json(Obj,"expires",expires); field_to_json(Obj,"completed",completed); field_to_json(Obj,"canceled",canceled); + field_to_json(Obj,"userAction",userAction); } bool ActionLink::from_json(Poco::JSON::Object::Ptr &Obj) { @@ -514,6 +515,7 @@ namespace OpenWifi::SecurityObjects { field_from_json(Obj,"expires",expires); field_from_json(Obj,"completed",completed); field_from_json(Obj,"canceled",canceled); + field_from_json(Obj,"userAction",userAction); return true; } catch(...) { diff --git a/src/RESTObjects/RESTAPI_SecurityObjects.h b/src/RESTObjects/RESTAPI_SecurityObjects.h index cff551e..6c726fc 100644 --- a/src/RESTObjects/RESTAPI_SecurityObjects.h +++ b/src/RESTObjects/RESTAPI_SecurityObjects.h @@ -248,6 +248,7 @@ namespace OpenWifi { uint64_t expires=0; uint64_t completed=0; uint64_t canceled=0; + bool userAction=true; void to_json(Poco::JSON::Object &Obj) const; bool from_json(Poco::JSON::Object::Ptr &Obj); diff --git a/src/storage/orm_actionLinks.cpp b/src/storage/orm_actionLinks.cpp index de7e98f..7dfeb95 100644 --- a/src/storage/orm_actionLinks.cpp +++ b/src/storage/orm_actionLinks.cpp @@ -16,7 +16,8 @@ "created bigint," "expires bigint," "completed bigint," -"canceled bigint" +"canceled bigint, + userAction boolean" */ namespace OpenWifi { @@ -32,7 +33,8 @@ namespace OpenWifi { ORM::Field{"created", ORM::FieldType::FT_BIGINT}, ORM::Field{"expires", ORM::FieldType::FT_BIGINT}, ORM::Field{"completed", ORM::FieldType::FT_BIGINT}, - ORM::Field{"canceled", ORM::FieldType::FT_BIGINT} + ORM::Field{"canceled", ORM::FieldType::FT_BIGINT}, + ORM::Field{"userAction", ORM::FieldType::FT_BOOLEAN} }; ActionLinkDB::ActionLinkDB(const std::string &Name, const std::string &ShortName, OpenWifi::DBType T, @@ -40,6 +42,16 @@ namespace OpenWifi { DB(T, Name.c_str(), ActionLinksDB_Fields,{}, P, L, ShortName.c_str()) { } + bool ActionLinkDB::Upgrade(uint32_t from, uint32_t &to) { + std::vector Statements{ + "alter table " + TableName_ + " add column userAction BOOLEAN default true;" + }; + RunScript(Statements); + to = 1; + return true; + + return true; + } bool ActionLinkDB::CreateAction( SecurityObjects::ActionLink & A) { return CreateRecord(A); } @@ -105,6 +117,7 @@ template<> void ORM::DB(); U.completed = T.get<10>(); U.canceled = T.get<11>(); + U.userAction = T.get<12>(); } template<> void ORM::DB void ORM::DB(U.expires); T.set<10>(U.completed); T.set<11>(U.canceled); + T.set<12>(U.userAction); } diff --git a/src/storage/orm_actionLinks.h b/src/storage/orm_actionLinks.h index 7c5c6d5..f18f1ce 100644 --- a/src/storage/orm_actionLinks.h +++ b/src/storage/orm_actionLinks.h @@ -21,7 +21,8 @@ namespace OpenWifi { uint64_t, // created uint64_t, // expires uint64_t, // completed - uint64_t // canceled + uint64_t, // canceled + bool // userAction > ActionLinkRecordTuple; typedef std::vector ActionLinkRecordTupleList; @@ -38,6 +39,9 @@ namespace OpenWifi { bool GetActions(std::vector &Links, uint64_t Max=200); void CleanOldActionLinks(); + inline uint32_t Version() override { return 1;} + bool Upgrade(uint32_t from, uint32_t &to) override; + private: };