Skip to content

Commit

Permalink
Clearer types in code rather than void pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
teusbenschop committed Jan 5, 2024
1 parent d78b3c3 commit 4218bfe
Show file tree
Hide file tree
Showing 81 changed files with 193 additions and 195 deletions.
42 changes: 18 additions & 24 deletions access/bible.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace access_bible {

// Returns true if the $user has read access to the $bible.
// If no $user is given, it takes the currently logged-in user.
bool read (void * webserver_request, const string & bible, string user)
bool read (Webserver_Request& webserver_request, const string & bible, string user)
{
// Client: User has access to all Bibles.
#ifdef HAVE_CLIENT
Expand All @@ -42,17 +42,16 @@ bool read (void * webserver_request, const string & bible, string user)
#endif

#ifdef HAVE_CLOUD
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);

// Get the level, that is the role, of the given user.
int role_level { 0 };
if (user.empty ()) {
// Current user.
user = request->session_logic ()->currentUser ();
role_level = request->session_logic ()->currentLevel ();
user = webserver_request.session_logic ()->currentUser ();
role_level = webserver_request.session_logic ()->currentLevel ();
} else {
// Take level belonging to user.
role_level = request->database_users ()->get_level (user);
role_level = webserver_request.database_users ()->get_level (user);
}

// Managers and higher have read access.
Expand Down Expand Up @@ -80,7 +79,7 @@ bool read (void * webserver_request, const string & bible, string user)


// Returns true if the user has write access to the $bible.
bool write (void * webserver_request, const string & bible, string user)
bool write (Webserver_Request& webserver_request, const string & bible, string user)
{
#ifdef HAVE_CLIENT
// Client: When not yet connected to the Cloud, the user has access to all Bibles.
Expand All @@ -92,14 +91,13 @@ bool write (void * webserver_request, const string & bible, string user)
#endif

int level {0};
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
if (user.empty ()) {
user = request->session_logic ()->currentUser ();
level = request->session_logic ()->currentLevel ();
user = webserver_request.session_logic ()->currentUser ();
level = webserver_request.session_logic ()->currentLevel ();
}
if (level == 0) {
// Take level belonging to user.
level = request->database_users ()->get_level (user);
level = webserver_request.database_users ()->get_level (user);
}

// Managers and higher always have write access.
Expand Down Expand Up @@ -129,7 +127,7 @@ bool write (void * webserver_request, const string & bible, string user)
// If no user is given, it takes the currently logged-in user.
// If the user has read-only access to even one book of the $bible,
// then the user is considered not to have write access to the entire $bible.
bool book_write (void * webserver_request, string user, const string & bible, int book)
bool book_write (Webserver_Request& webserver_request, string user, const string & bible, int book)
{
#ifdef HAVE_CLIENT
// Client: When not yet connected to the Cloud, the user has access to the book.
Expand All @@ -142,14 +140,13 @@ bool book_write (void * webserver_request, string user, const string & bible, in

// Get the user level (role).
int level {0};
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
if (user.empty ()) {
user = request->session_logic ()->currentUser ();
level = request->session_logic ()->currentLevel ();
user = webserver_request.session_logic ()->currentUser ();
level = webserver_request.session_logic ()->currentLevel ();
}
if (level == 0) {
// Take level belonging to user.
level = request->database_users ()->get_level (user);
level = webserver_request.database_users ()->get_level (user);
}

// Managers and higher always have write access.
Expand Down Expand Up @@ -179,10 +176,9 @@ bool book_write (void * webserver_request, string user, const string & bible, in

// Returns an array of Bibles the user has read access to.
// If no user is given, it takes the currently logged-in user.
vector <string> bibles (void * webserver_request, string user)
vector <string> bibles (Webserver_Request& webserver_request, string user)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
vector <string> allbibles = request->database_bibles()->get_bibles ();
vector <string> allbibles = webserver_request.database_bibles()->get_bibles ();
vector <string> bibles;
for (auto & bible : allbibles) {
if (read (webserver_request, bible, user)) {
Expand All @@ -196,14 +192,13 @@ vector <string> bibles (void * webserver_request, string user)
// This function clamps bible.
// It returns the $bible if the currently logged-in user has access to it.
// Else it returns another accessible bible or nothing.
string clamp (void * webserver_request, string bible)
string clamp (Webserver_Request& webserver_request, string bible)
{
if (!read (webserver_request, bible)) {
bible = string();
vector <string> bibles = access_bible::bibles (webserver_request);
if (!bibles.empty ()) bible = bibles [0];
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
request->database_config_user ()->setBible (bible);
webserver_request.database_config_user ()->setBible (bible);
}
return bible;
}
Expand All @@ -212,12 +207,11 @@ string clamp (void * webserver_request, string bible)
// This function checks whether the user in the $webserver_request
// has $read or $write access to one or more Bibles.
// It returns a tuple <read, write>.
tuple<bool, bool> any (void * webserver_request)
tuple<bool, bool> any (Webserver_Request& webserver_request)
{
bool read {false};
bool write {false};
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);
vector <string> bibles = request->database_bibles()->get_bibles ();
vector <string> bibles = webserver_request.database_bibles()->get_bibles ();
for (auto & bible : bibles) {
if (access_bible::read (webserver_request, bible)) read = true;
if (access_bible::write (webserver_request, bible)) write = true;
Expand Down
14 changes: 8 additions & 6 deletions access/bible.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#include <config/libraries.h>

class Webserver_Request;

namespace access_bible {

bool read (void * webserver_request, const std::string & bible, std::string user = std::string());
bool write (void * webserver_request, const std::string & bible, std::string user = std::string());
bool book_write (void * webserver_request, std::string user, const std::string & bible, int book);
std::vector <std::string> bibles (void * webserver_request, std::string user = std::string());
std::string clamp (void * webserver_request, std::string bible);
std::tuple <bool, bool> any (void * webserver_request);
bool read (Webserver_Request& webserver_request, const std::string & bible, std::string user = std::string());
bool write (Webserver_Request& webserver_request, const std::string & bible, std::string user = std::string());
bool book_write (Webserver_Request& webserver_request, std::string user, const std::string & bible, int book);
std::vector <std::string> bibles (Webserver_Request& webserver_request, std::string user = std::string());
std::string clamp (Webserver_Request& webserver_request, std::string bible);
std::tuple <bool, bool> any (Webserver_Request& webserver_request);

}
4 changes: 2 additions & 2 deletions bb/book.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ std::string bible_book (Webserver_Request& webserver_request)
std::string error_message {};

// The name of the Bible.
const std::string bible = access_bible::clamp (std::addressof(webserver_request), webserver_request.query["bible"]);
const std::string bible = access_bible::clamp (webserver_request, webserver_request.query["bible"]);
view.set_variable ("bible", filter::strings::escape_special_xml_characters (bible));

// The book.
Expand All @@ -78,7 +78,7 @@ std::string bible_book (Webserver_Request& webserver_request)
view.set_variable ("book_name", filter::strings::escape_special_xml_characters (book_name));

// Whether the user has write access to this Bible book.
const bool write_access = access_bible::book_write (std::addressof(webserver_request), std::string(), bible, book);
const bool write_access = access_bible::book_write (webserver_request, std::string(), bible, book);
if (write_access) view.enable_zone ("write_access");

// Delete chapter.
Expand Down
4 changes: 2 additions & 2 deletions bb/chapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ std::string bible_chapter (Webserver_Request& webserver_request)
std::string error_message {};

// The name of the Bible.
const std::string bible = access_bible::clamp (std::addressof(webserver_request), webserver_request.query["bible"]);
const std::string bible = access_bible::clamp (webserver_request, webserver_request.query["bible"]);
view.set_variable ("bible", filter::strings::escape_special_xml_characters (bible));

// The book.
Expand All @@ -73,7 +73,7 @@ std::string bible_chapter (Webserver_Request& webserver_request)
view.set_variable ("chapter", filter::strings::escape_special_xml_characters (chapter));

// Whether the user has write access to this Bible book.
if (bool write_access = access_bible::book_write (std::addressof(webserver_request), std::string(), bible, book); write_access) view.enable_zone ("write_access");
if (bool write_access = access_bible::book_write (webserver_request, std::string(), bible, book); write_access) view.enable_zone ("write_access");

view.set_variable ("success_message", success_message);
view.set_variable ("error_message", error_message);
Expand Down
2 changes: 1 addition & 1 deletion bb/css.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ std::string bible_css (Webserver_Request& webserver_request)
Assets_View view {};

// The name of the Bible.
const std::string bible = access_bible::clamp (std::addressof(webserver_request), webserver_request.query ["bible"]);
const std::string bible = access_bible::clamp (webserver_request, webserver_request.query ["bible"]);
view.set_variable ("bible", filter::strings::escape_special_xml_characters (bible));

// Data submission.
Expand Down
4 changes: 2 additions & 2 deletions bb/import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ std::string bible_import (Webserver_Request& webserver_request)
std::string error_message {};

// The name of the Bible.
const std::string bible = access_bible::clamp (std::addressof(webserver_request), webserver_request.query["bible"]);
const std::string bible = access_bible::clamp (webserver_request, webserver_request.query["bible"]);
view.set_variable ("bible", filter::strings::escape_special_xml_characters (bible));

const int book = Ipc_Focus::getBook (std::addressof(webserver_request));
const int chapter = Ipc_Focus::getChapter (std::addressof(webserver_request));

// Whether the user has write access to this Bible.
if (bool write_access = access_bible::write (std::addressof(webserver_request), bible); write_access) {
if (bool write_access = access_bible::write (webserver_request, bible); write_access) {
view.enable_zone ("write_access");
}

Expand Down
10 changes: 5 additions & 5 deletions bb/manage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ std::string bible_manage (Webserver_Request& webserver_request)
} else {
webserver_request.database_bibles()->create_bible (bible);
// Check / grant access.
if (!access_bible::write (std::addressof(webserver_request), bible)) {
if (!access_bible::write (webserver_request, bible)) {
std::string me = webserver_request.session_logic()->currentUser ();
DatabasePrivileges::set_bible (me, bible, true);
}
Expand Down Expand Up @@ -123,7 +123,7 @@ std::string bible_manage (Webserver_Request& webserver_request)
error_message = translate("Cannot copy the Bible because the destination Bible already exists.");
} else {
// User needs read access to the original.
if (access_bible::read (std::addressof(webserver_request), origin)) {
if (access_bible::read (webserver_request, origin)) {
// Copy the Bible data.
const std::string origin_folder = webserver_request.database_bibles()->bible_folder (origin);
const std::string destination_folder = webserver_request.database_bibles()->bible_folder (destination);
Expand All @@ -133,7 +133,7 @@ std::string bible_manage (Webserver_Request& webserver_request)
// Feedback.
success_message = translate("The Bible was copied.");
// Check / grant access to destination Bible.
if (!access_bible::write (std::addressof(webserver_request), destination)) {
if (!access_bible::write (webserver_request, destination)) {
const std::string me = webserver_request.session_logic ()->currentUser ();
DatabasePrivileges::set_bible (me, destination, true);
}
Expand All @@ -153,7 +153,7 @@ std::string bible_manage (Webserver_Request& webserver_request)
const std::string confirm = webserver_request.query ["confirm"];
if (confirm == "yes") {
// User needs write access for delete operation.
if (access_bible::write (std::addressof(webserver_request), bible)) {
if (access_bible::write (webserver_request, bible)) {
bible_logic::delete_bible (bible);
} else {
page += assets_page::error ("Insufficient privileges to complete action");
Expand All @@ -169,7 +169,7 @@ std::string bible_manage (Webserver_Request& webserver_request)

view.set_variable ("success_message", success_message);
view.set_variable ("error_message", error_message);
const std::vector <std::string> bibles = access_bible::bibles (std::addressof(webserver_request));
const std::vector <std::string> bibles = access_bible::bibles (webserver_request);
pugi::xml_document document{};
for (const auto& bible : bibles) {
pugi::xml_node li_node = document.append_child ("li");
Expand Down
2 changes: 1 addition & 1 deletion bb/order.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ std::string bible_order (Webserver_Request& webserver_request)
Assets_View view {};

// The name of the Bible.
const std::string bible = access_bible::clamp (std::addressof(webserver_request), webserver_request.query ["bible"]);
const std::string bible = access_bible::clamp (webserver_request, webserver_request.query ["bible"]);
view.set_variable ("bible", filter::strings::escape_special_xml_characters (bible));

// The order the user wants for the Bibles.
Expand Down
4 changes: 2 additions & 2 deletions bb/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ std::string bible_settings (Webserver_Request& webserver_request)
// The Bible.
std::string bible = webserver_request.query["bible"];
if (bible.empty()) bible = webserver_request.post ["val1"];
bible = access_bible::clamp (std::addressof(webserver_request), bible);
bible = access_bible::clamp (webserver_request, bible);
view.set_variable ("bible", filter::strings::escape_special_xml_characters (bible));
view.set_variable ("urlbible", filter_url_urlencode(filter::strings::escape_special_xml_characters (bible)));


// Whether the user has write access to this Bible.
bool write_access = access_bible::write (std::addressof(webserver_request), bible);
bool write_access = access_bible::write (webserver_request, bible);
if (write_access) view.enable_zone ("write_access");


Expand Down
2 changes: 1 addition & 1 deletion changes/change.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ string changes_change (Webserver_Request& webserver_request)

// Bibles and passage.
const Passage passage {database_modifications.getNotificationPassage (id)};
const vector <string> bibles {access_bible::bibles (std::addressof(webserver_request))};
const vector <string> bibles {access_bible::bibles (webserver_request)};


// Get notes for the passage.
Expand Down
Loading

0 comments on commit 4218bfe

Please sign in to comment.