Skip to content

Commit

Permalink
Modernize account creation via link
Browse files Browse the repository at this point in the history
  • Loading branch information
teusbenschop committed May 28, 2024
1 parent 217d1ff commit 8b925d5
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 93 deletions.
81 changes: 22 additions & 59 deletions confirm/worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,81 +107,41 @@ void setup (Webserver_Request& webserver_request,
}


} // namespace.






Confirm_Worker::Confirm_Worker (Webserver_Request& webserver_request):
m_webserver_request (webserver_request)
{
}


// Handles a confirmation email received "from" with "subject" and "body".
// Returns true if the mail was handled, else false.
bool Confirm_Worker::handle_email ([[maybe_unused]]std::string from, std::string subject, std::string body)
{
// Find out in the confirmation database whether the subject line contains an active ID.
// If not, bail out.
Database_Confirm database_confirm;
unsigned int id = database_confirm.search_id (subject);
if (id == 0) {
return false;
}
// An active ID was found: Execute the associated database query.
std::string query = database_confirm.get_query (id);
m_webserver_request.database_users()->execute (query);
// Send confirmation mail.
std::string mailto = database_confirm.get_mail_to (id);
subject = database_confirm.get_subject (id);
body = database_confirm.get_body (id);
email_schedule (mailto, subject, body);
// Delete the confirmation record.
database_confirm.erase (id);
// Notify managers.
confirm::worker::inform_managers (mailto, body);
// Job done.
return true;
}


// Handles a confirmation link clicked with a confirmation ID.
// Returns true if link was valid, else false.
bool Confirm_Worker::handle_link (std::string & email)
bool handle_link (Webserver_Request& webserver_request, std::string& email)
{
// Get the confirmation identifier from the link that was clicked.
std::string web_id = m_webserver_request.query["id"];
std::string web_id = webserver_request.query["id"];

// If the identifier was not given, the link was not handled successfully.
if (web_id.empty()) return false;

// Find out in the confirmation database whether the subject line contains an active ID.
if (web_id.empty())
return false;

// Find out from the confirmation database whether the subject line contains an active ID.
// If not, bail out.
Database_Confirm database_confirm;
unsigned int id = database_confirm.search_id (web_id);
Database_Confirm database_confirm {};
const unsigned int id = database_confirm.search_id (web_id);
if (id == 0) {
return false;
}

// An active ID was found: Execute the associated database query.
std::string query = database_confirm.get_query (id);
m_webserver_request.database_users()->execute (query);

const std::string query = database_confirm.get_query (id);
webserver_request.database_users()->execute (query);
// Send confirmation mail.
std::string mailto = database_confirm.get_mail_to (id);
std::string subject = database_confirm.get_subject (id);
std::string body = database_confirm.get_body (id);
const std::string mailto = database_confirm.get_mail_to (id);
const std::string subject = database_confirm.get_subject (id);
const std::string body = database_confirm.get_body (id);
email_schedule (mailto, subject, body);

// Delete the confirmation record.
database_confirm.erase (id);

// Notify managers.
// Notify the manager(s).
confirm::worker::inform_managers (mailto, body);

// Pass the email address to the caller.
email = mailto;

Expand All @@ -190,4 +150,7 @@ bool Confirm_Worker::handle_link (std::string & email)
}


} // namespace.


#endif
12 changes: 2 additions & 10 deletions confirm/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,8 @@ void setup (Webserver_Request& webserver_request,
const std::string& query,
const std::string& subsequent_subject, const std::string& subsequent_body);

}
bool handle_link (Webserver_Request& webserver_request, std::string& email);

class Confirm_Worker
{
public:
Confirm_Worker (Webserver_Request& webserver_request);
bool handle_email (std::string from, std::string subject, std::string body);
bool handle_link (std::string & email);
private:
Webserver_Request& m_webserver_request;
};
}

#endif
13 changes: 1 addition & 12 deletions database/confirm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#ifdef HAVE_CLOUD


const char * Database_Confirm::filename ()
const char * Database_Confirm::filename () // Todo no need for a class here.
{
return "confirm";
}
Expand Down Expand Up @@ -143,17 +143,6 @@ unsigned int Database_Confirm::search_id (std::string subject)
}


std::vector <int> Database_Confirm::get_ids ()
{
SqliteDatabase sql (filename ());
sql.add ("SELECT id FROM confirm;");
std::vector <std::string> s_ids = sql.query () ["id"];
std::vector <int> ids;
for (auto id : s_ids) ids.push_back(filter::strings::convert_to_int(id));
return ids;
}


// Returns the query for $id.
std::string Database_Confirm::get_query (unsigned int id)
{
Expand Down
1 change: 0 additions & 1 deletion database/confirm.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class Database_Confirm
bool id_exists (unsigned int id);
void store (unsigned int id, std::string query, std::string to, std::string subject, std::string body, std::string username);
unsigned int search_id (std::string subject);
std::vector <int> get_ids ();
std::string get_query (unsigned int id);
std::string get_mail_to (unsigned int id);
std::string get_subject (unsigned int id);
Expand Down
6 changes: 1 addition & 5 deletions email/receive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include <filter/string.h>
#include <filter/mail.h>
#include <config/globals.h>
#include <confirm/worker.h>
#include <notes/logic.h>
#include <filter/url.h>

Expand All @@ -50,7 +49,6 @@ void email_receive ()
for (int i = 1; i <= emailcount; i++) {

Webserver_Request webserver_request;
Confirm_Worker confirm_worker = (webserver_request);
Notes_Logic notes_logic (webserver_request);

error.clear ();
Expand All @@ -65,9 +63,7 @@ void email_receive ()

Database_Logs::log ("Processing email from " + from + " with subject " + subject);

if (confirm_worker.handle_email (from, subject, body)) {
}
else if (notes_logic.handleEmailComment (from, subject, body)) {
if (notes_logic.handleEmailComment (from, subject, body)) {
}
else if (notes_logic.handleEmailNew (from, subject, body)) {
}
Expand Down
3 changes: 1 addition & 2 deletions session/confirm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ std::string session_confirm ([[maybe_unused]] Webserver_Request& webserver_reque
{
#ifdef HAVE_CLOUD

Confirm_Worker confirm_worker = (webserver_request);
std::string email;
bool is_valid_confirmation = confirm_worker.handle_link (email);
const bool is_valid_confirmation = confirm::worker::handle_link (webserver_request, email);

// Handle a valid confirmation.
if (is_valid_confirmation) {
Expand Down
4 changes: 0 additions & 4 deletions unittests/confirm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ TEST (database, confirm)
username = database_confirm.get_username(id + 1);
EXPECT_EQ (std::string(), username);

std::vector <int> ids = database_confirm.get_ids();
std::vector <int> standard {static_cast<int>(id)};
EXPECT_EQ (standard, ids);

// Delete this ID.
database_confirm.erase (id);
query = database_confirm.get_query (id);
Expand Down

0 comments on commit 8b925d5

Please sign in to comment.