add use_internal_session_mechanism and use_internal_loggin_mechanism config options

This commit is contained in:
Tomasz Sowa 2022-07-26 21:54:33 +02:00
parent c85a724fec
commit 9e6a5b2d37
5 changed files with 115 additions and 66 deletions

View File

@ -191,6 +191,9 @@ void Config::AssignValues()
templates_request_status = Text(L"templates_request_status", L"request_status.html"); templates_request_status = Text(L"templates_request_status", L"request_status.html");
template_only_root_use_template_fun = Bool(L"template_only_root_use_template_fun", false); template_only_root_use_template_fun = Bool(L"template_only_root_use_template_fun", false);
use_internal_session_mechanism = Bool(L"use_internal_session_mechanism", true);
use_internal_loggin_mechanism = Bool(L"use_internal_loggin_mechanism", true);
http_session_id_name = Text(L"http_session_id_name", L"session_id"); http_session_id_name = Text(L"http_session_id_name", L"session_id");
db_conn_string = Text(L"db_conn_string"); db_conn_string = Text(L"db_conn_string");
db_host = Text(L"db_host"); db_host = Text(L"db_host");

View File

@ -267,6 +267,18 @@ public:
// if a migration fails then stop winix // if a migration fails then stop winix
bool db_stop_if_migration_fails; bool db_stop_if_migration_fails;
// use internal session mechanism
// default: true
// if false then we do not load/save and create internal sessions
// but there always be the temporary session object for a request
// also we do not load 'who' winix function
bool use_internal_session_mechanism;
// use internal loggin mechanism
// default: true
// if false then we do not load 'login' and 'logout' winix functions
// and the system will not allow to login through its api
bool use_internal_loggin_mechanism;
// the name of the cookie which has the session identifier // the name of the cookie which has the session identifier
std::wstring http_session_id_name; std::wstring http_session_id_name;

View File

@ -475,31 +475,34 @@ Session * SessionManager::PrepareSession()
{ {
session = nullptr; session = nullptr;
if( !IsIPBanned() ) if( config->use_internal_session_mechanism )
{ {
CookieTab::iterator i = cur->request->cookie_tab.find(config->http_session_id_name); if( !IsIPBanned() )
if( i != cur->request->cookie_tab.end() )
{ {
if( !SetSessionFromCookie(i->second) ) CookieTab::iterator i = cur->request->cookie_tab.find(config->http_session_id_name);
if( i != cur->request->cookie_tab.end() )
{ {
cur->request->cookie_tab.erase(i); if( !SetSessionFromCookie(i->second) )
{
cur->request->cookie_tab.erase(i);
}
}
else
{
if( cur->request->function && cur->request->function->need_session )
{
NoSessionCookieWasSent();
}
} }
} }
else
{
if( cur->request->function && cur->request->function->need_session )
{
NoSessionCookieWasSent();
}
}
}
if( !session && cur->request->function && cur->request->function->need_session ) if( !session && cur->request->function && cur->request->function->need_session )
{
if( !current_ip_ban || !current_ip_ban->IsIPBanned() )
{ {
CreateSession(); if( !current_ip_ban || !current_ip_ban->IsIPBanned() )
{
CreateSession();
}
} }
} }
@ -516,14 +519,17 @@ Session * SessionManager::PrepareSession()
Session * SessionManager::CheckIfFunctionRequireSession() Session * SessionManager::CheckIfFunctionRequireSession()
{ {
if( cur->request->function && cur->request->function->need_session ) if( config->use_internal_session_mechanism )
{ {
if( session == &temporary_session ) if( cur->request->function && cur->request->function->need_session )
{ {
if( !current_ip_ban || !current_ip_ban->IsIPBanned() ) if( session == &temporary_session )
{ {
CreateSession(); if( !current_ip_ban || !current_ip_ban->IsIPBanned() )
session->ip_ban = current_ip_ban; {
CreateSession();
session->ip_ban = current_ip_ban;
}
} }
} }
} }
@ -652,34 +658,37 @@ void SessionManager::LoadSessions()
SessionParser sp; SessionParser sp;
SessionContainer::Iterator i; SessionContainer::Iterator i;
if( !config->session_file.empty() ) if( config->use_internal_session_mechanism )
{ {
sp.set_dependency(this); if( !config->session_file.empty() )
// sessions will be overwritten (pointers are invalidated)
cur->session = &temporary_session;
sp.SetUsers(&system->users);
sp.Parse(config->session_file, session_tab);
for(i=session_tab.Begin() ; i != session_tab.End() ; ++i)
{ {
i->plugin_data.Resize(plugin->Size()); sp.set_dependency(this);
plugin->Call(main_model_connector, &main_log, &(*i), nullptr, nullptr, WINIX_SESSION_CREATED);
/* // sessions will be overwritten (pointers are invalidated)
!! IMPROVE ME cur->session = &temporary_session;
we do not add it to the last_container (we don't have IP address stored yet)
*/
if( i->puser ) sp.SetUsers(&system->users);
plugin->Call(main_model_connector, &main_log, &(*i), nullptr, nullptr, WINIX_USER_LOGGED); sp.Parse(config->session_file, session_tab);
for(i=session_tab.Begin() ; i != session_tab.End() ; ++i)
{
i->plugin_data.Resize(plugin->Size());
plugin->Call(main_model_connector, &main_log, &(*i), nullptr, nullptr, WINIX_SESSION_CREATED);
/*
!! IMPROVE ME
we do not add it to the last_container (we don't have IP address stored yet)
*/
if( i->puser )
plugin->Call(main_model_connector, &main_log, &(*i), nullptr, nullptr, WINIX_USER_LOGGED);
}
}
else
{
// FIXME this log is not printed, why?
main_log << log1 << "SM: session_file config parameter is empty, not loading sessions" << logend;
} }
}
else
{
// FIXME this log is not printed, why?
main_log << log1 << "SM: session_file config parameter is empty, not loading sessions" << logend;
} }
cur->session = &temporary_session; cur->session = &temporary_session;
@ -693,6 +702,11 @@ void SessionManager::SaveSessions()
{ {
char file_path[WINIX_OS_PATH_SIZE]; char file_path[WINIX_OS_PATH_SIZE];
if( !config->use_internal_session_mechanism )
{
return;
}
if( config->session_file.empty() ) if( config->session_file.empty() )
{ {
main_log << log1 << "SM: session_file config parameter is empty, not saving sessions - sessions lost" << logend; main_log << log1 << "SM: session_file config parameter is empty, not saving sessions - sessions lost" << logend;

View File

@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2008-2021, Tomasz Sowa * Copyright (c) 2008-2022, Tomasz Sowa
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -264,6 +264,11 @@ return puser;
bool Users::LoginUser(long user_id, bool remember_me, bool use_ses_log) bool Users::LoginUser(long user_id, bool remember_me, bool use_ses_log)
{ {
Config * config = get_config();
if( !config || !config->use_internal_loggin_mechanism )
return false;
if( !LoginUserCheckSession(use_ses_log) ) if( !LoginUserCheckSession(use_ses_log) )
return false; return false;
@ -343,26 +348,31 @@ return how_many;
void Users::LogoutCurrentUser() void Users::LogoutCurrentUser()
{ {
Log * log = get_logger(); Config * config = get_config();
Session * session = get_session();
if( !session || !session->puser ) if( config && config->use_internal_loggin_mechanism )
return;
if( log )
{ {
(*log) << log2 << "Users: user " << session->puser->login << ", id: " Log * log = get_logger();
<< session->puser->id << " logged out" << logend; Session * session = get_session();
}
//plugin->Call(WINIX_PREPARE_USER_TO_LOGOUT, cur->session->puser); // FIXME
last.UserLogout(session->puser->id, session->id);
if( how_many_logged > 0 ) // for safety if( !session || !session->puser )
how_many_logged -= 1; return;
session->puser = 0; if( log )
session->remember_me = false; {
(*log) << log2 << "Users: user " << session->puser->login << ", id: "
<< session->puser->id << " logged out" << logend;
}
//plugin->Call(WINIX_PREPARE_USER_TO_LOGOUT, cur->session->puser); // FIXME
last.UserLogout(session->puser->id, session->id);
if( how_many_logged > 0 ) // for safety
how_many_logged -= 1;
session->puser = 0;
session->remember_me = false;
}
} }

View File

@ -244,8 +244,13 @@ void Functions::CreateFunctions()
Add(fun_imgcrop); Add(fun_imgcrop);
Add(fun_last); Add(fun_last);
Add(fun_locale); Add(fun_locale);
Add(fun_login);
Add(fun_logout); if( config->use_internal_loggin_mechanism )
{
Add(fun_login);
Add(fun_logout);
}
Add(fun_ln); Add(fun_ln);
Add(fun_ls); Add(fun_ls);
Add(fun_ipban); Add(fun_ipban);
@ -273,7 +278,12 @@ void Functions::CreateFunctions()
Add(fun_uname); Add(fun_uname);
Add(fun_upload); Add(fun_upload);
Add(fun_uptime); Add(fun_uptime);
Add(fun_who);
if( config->use_internal_session_mechanism )
{
Add(fun_who);
}
Add(fun_vim); Add(fun_vim);
plugin->Call(WINIX_CREATE_FUNCTIONS); plugin->Call(WINIX_CREATE_FUNCTIONS);