From: Will Budicm Date: Wed, 16 Dec 2020 03:01:16 +0000 (+1100) Subject: DBI_MULTI_USER_DB implementation. X-Git-Url: https://lifelog.hopto.org/gitweb/?a=commitdiff_plain;h=ff87d8209363dd37d2f87a8b51f854ab77945ed2;p=LifeLog.git DBI_MULTI_USER_DB implementation. --- diff --git a/Current Development Check List.md b/Current Development Check List.md index 9c42805..d6d095e 100644 --- a/Current Development Check List.md +++ b/Current Development Check List.md @@ -22,9 +22,10 @@ * ✔ Multi db driver type support. Earth stage requires same SQL related code to work on at least one more DBMS type other than SQLight. * PostgreSQL is the candidate as MySQL is not easy to install and bulky for all systems. * Developing and adopting to MySQL or any other system is not prerogative, as it works well as it is with inbuilt simplicity. +* ✔ Single database, multi-user login. * Static pages setting for the pages directory. -* Search on multiple words should rank by encounter of words specified and display first. (That one is difficult) * Provide markdown text functionality to html pages. For quick vanilla plain documentation. +* Search on multiple words should rank by encounter of words specified and display first. (That one is difficult) * Auto collapse/expand on multi line logs by 0-none as default. Setting to 1 or more shows only that number of lines. (That one is difficult) ### v.2.0 SUN RC2 Encountered diff --git a/dbLifeLog/main.cnf b/dbLifeLog/main.cnf index 272368f..fb785a8 100644 --- a/dbLifeLog/main.cnf +++ b/dbLifeLog/main.cnf @@ -10,19 +10,20 @@ Credential format:< , don't enable here using AU # Default database driver prefix, is SQLite. < # ProgresSQL DB driver. Remove -> '!' prefix to tag name, to enable, and override the above SQLite one. -< +< # LOG actual variable size limit, - 1024 default, 128 minimum value. -#< -# Set following to an database name, if multiple aliases connect to a single log database. -# Users have to be set, and alias is then used for their userid to be able to login. -# By default this setting is disabled with 0, making alias, its own log database. -< +< + +# Set the following to an database name, with external managed multiple users, that are to connect to a single database. +# Users have to be externally set, and alias is then used for their userid to be able to login. (Add user roles to AUTH table). +# By default this setting is disabled with 0, making alias name, its own log database, per user. +< #Timezoning, if set to 1, country locale setting for timezone in config page is ignored. < -# Public, enabled list of categories. Remove -> '!' prefix to tag name, to enable. Be careful what category you add/enable here. +# Public, enabled list of categories. Remove -> '!' prefix before tag name, to enable. Be careful what category you add/enable here. # Public records view feature only, it doesn't allow new log entries to be added and have pin point searching of all existing. <#File,System log and Event. # Following is similar to AUTO_LOGIN setting above, and must be set. @@ -38,7 +39,7 @@ America/Texas=America/Chicago < 00|$RELEASE_VER = 2.1`LifeLog Application Version. 01|$REC_LIMIT = 25`Records shown per page. -03|$TIME_ZONE = Australia/Sydney`Time zone of your country. +03|$TIME_ZONE = Australia/Sydney`Time zone of your country and city. 05|$PRC_WIDTH = 80`Default presentation width for pages. 08|$LOG_PATH = ../../dbLifeLog/`Path to folder containing data. 10|$SESSN_EXPR = +30m`Login session expiration time setting, can be minutes or hours. diff --git a/htdocs/cgi-bin/config.cgi b/htdocs/cgi-bin/config.cgi index c11eef5..dc46db0 100755 --- a/htdocs/cgi-bin/config.cgi +++ b/htdocs/cgi-bin/config.cgi @@ -29,7 +29,7 @@ my $sys = `uname -n`; my $db = Settings::fetchDBSettings(); my $cgi = Settings->cgi(); my $sid = Settings::sid(); -my $dbname = Settings::dbname(); +my $dbname = Settings::dbName(); my $alias = Settings::alias(); my $rv; my $dbs; diff --git a/htdocs/cgi-bin/data.cgi b/htdocs/cgi-bin/data.cgi index 0c041b8..c701c3e 100755 --- a/htdocs/cgi-bin/data.cgi +++ b/htdocs/cgi-bin/data.cgi @@ -18,7 +18,7 @@ require Settings; my $db = Settings::fetchDBSettings(); my $cgi = Settings->cgi(); -my $dbname = Settings::dbname(); +my $dbname = Settings::dbName(); my $imgw = 210; my $imgh = 120; my $human = DateTime::Format::Human::Duration->new(); diff --git a/htdocs/cgi-bin/login_ctr.cgi b/htdocs/cgi-bin/login_ctr.cgi index bc40b85..9fce909 100755 --- a/htdocs/cgi-bin/login_ctr.cgi +++ b/htdocs/cgi-bin/login_ctr.cgi @@ -32,6 +32,7 @@ my $SCRIPT_RELEASE = Settings::release(); my $BACKUP_ENABLED = 0; my $AUTO_SET_TIMEZONE = 0; my $TIME_ZONE_MAP = 0; +my $DB_NAME; try{ checkAutologinSet(); @@ -110,7 +111,7 @@ sub processSubmit { $session->param('passw', $passw); $session->param('db_source', Settings::dbSrc()); $session->param('db_file', Settings::dbFile()); - $session->param('database', Settings::dbname()); + $session->param('database', Settings::dbName()); $session->flush(); ### To MAIN PAGE print $cgi->header(-expires=>"0s", -charset=>"UTF-8", -cookie=>$cookie, -location=>"main.cgi"); @@ -141,6 +142,9 @@ sub checkAutologinSet { if($v){$AUTO_SET_TIMEZONE = $v; next} $v = Settings::parseAutonom('DBI_LOG_VAR_SIZE',$line); if($v){Settings::dbVLSZ($v); next} + # From here are config file only autonoms. Don't need or harm being in database configuration. + $v = Settings::parseAutonom('DBI_MULTI_USER_DB',$line); + if($v){$DB_NAME=$v;Settings::dbName($v);next} if($line =~ /<) { @@ -159,7 +163,7 @@ sub checkAutologinSet { return; # Note, we do assign entered password even passw as autologin is set. Not entering one bypasses this. } # If stricter access is required set it to zero in main.cnf, or disable in config. $passw = $cre[1] if (!$passw); - $db = Settings::connectDB($alias, $passw); + $db = Settings::connectDB($DB_NAME, $alias, $passw); #check if autologin enabled. my $st = Settings::selectRecords($db,"SELECT VALUE FROM CONFIG WHERE NAME='AUTO_LOGIN';"); if($st){my @set = $st->fetchrow_array(); @@ -178,7 +182,7 @@ sub checkAutologinSet { sub checkPreparePGDB { my $create =1; - $passw = $cgi->param('passw'); #PG handles password encryption itself. + $passw = $cgi->param('passw'); #We let PG handles password encryption (security) itself. my @data_sources = DBI->data_sources("Pg"); foreach my $ln (@data_sources){ my $i = rindex $ln, '='; @@ -227,9 +231,9 @@ try{ # If brand new database, this sill returns fine an empty array. my %curr_tables = (); - if(Settings::isProgressDB()){ + if(Settings::isProgressDB()){ $changed = checkPreparePGDB(); - $db = Settings::connectDB($alias, $passw); + $db = Settings::connectDB($DB_NAME, $alias, $passw); my @tbls = $db->tables(undef, 'public'); foreach (@tbls){ my $t = uc substr($_,7); @@ -237,7 +241,7 @@ try{ } } else{ - $db = Settings::connectDB($alias, $passw); + $db = Settings::connectDB($DB_NAME, $alias, $passw); $pst = Settings::selectRecords($db,"SELECT name FROM sqlite_master WHERE type='table' or type='view';"); while(my @r = $pst->fetchrow_array()){ $curr_tables{$r[0]} = 1; @@ -353,7 +357,7 @@ try{ $db->do('DROP VIEW VW_LOG;');delete($curr_tables{'VW_LOG'}); delete($curr_tables{'VW_LOG'}); $changed = 1; - } + }elsif($SCRIPT_RELEASE > $DB_VERSION){$changed = 1;} if(!$hasLogTbl) { @@ -579,8 +583,8 @@ sub logout { $alias = $session->param('alias'); $passw = $session->param('passw'); if($alias){ - my $db = Settings::connectDB($alias, $passw); - Settings::toLog($db, "Log properly loged out by $alias."); + my $db = Settings::connectDB($DB_NAME, $alias, $passw); + Settings::toLog($db, "Log has properly been loged out by $alias."); $db->disconnect(); } }catch{ diff --git a/htdocs/cgi-bin/stats.cgi b/htdocs/cgi-bin/stats.cgi index 358769c..7e4ec28 100755 --- a/htdocs/cgi-bin/stats.cgi +++ b/htdocs/cgi-bin/stats.cgi @@ -25,7 +25,7 @@ my $cgi = CGI->new(); my $db = Settings::fetchDBSettings($cgi); my $sss = Settings::session(); my $sid = Settings::sid(); -my $dbname = Settings::dbname(); +my $dbname = Settings::dbName(); my $alias = Settings::alias(); my $passw = Settings::pass(); diff --git a/htdocs/cgi-bin/system/modules/Settings.pm b/htdocs/cgi-bin/system/modules/Settings.pm index cd0c4ee..08e5a41 100644 --- a/htdocs/cgi-bin/system/modules/Settings.pm +++ b/htdocs/cgi-bin/system/modules/Settings.pm @@ -101,7 +101,7 @@ sub dbSrc {my $r = shift; if($r) {$DBI_SOURCE=$r; $IS_PG_DB = 1 if(inde return $DBI_SOURCE} sub dbVLSZ {my $r = shift; if(!$r){$r = $DBI_LVAR_SZ}else{$r=128 if($r<128);$DBI_LVAR_SZ=$r} return $r} sub dbFile {my $r = shift; if($r) {$DBFILE=$r} return $DBFILE} -sub dbName {return $dbname} +sub dbName {my $r = shift; if($r) {$dbname=$r} return $dbname} sub dsn {return $DSN} sub isProgressDB {return $IS_PG_DB} sub sqlPubors {return $SQL_PUB} @@ -109,7 +109,6 @@ sub sqlPubors {return $SQL_PUB} sub cgi {return $cgi} sub session {return $sss} sub sid {return $sid} -sub dbname {return $dbname} sub alias {return $alias} sub pass {return $pass} sub pub {return $pub} @@ -154,11 +153,11 @@ try { # if(!$alias){ # $alias = "admin"; $pass = $alias; dbSrc('dbi:Pg:host=localhost;'); # } - if(!$alias){ + if(!$alias){ print $cgi->redirect("login_ctr.cgi?CGISESSID=$sid"); exit; - } - my $ret = connectDB($alias, $pass); + } + my $ret = connectDB($dbname, $alias, $pass); getConfiguration($ret); getTheme(); $sss->expire($SESSN_EXPR); @@ -611,18 +610,20 @@ sub configProperty { } sub connectDB { - my ($a,$p) = @_; - $a = $alias if(!$a); + my ($d,$u,$p) = @_; + $u = $alias if(!$u); $p = $alias if(!$p); - $dbname = 'data_'.$a.'_log.db'; - $DBFILE = $LOG_PATH.$dbname if(!$DBFILE); - if ($IS_PG_DB) { - $DSN = $DBI_SOURCE .'dbname='.$a; $DBFILE = $a; - }else{ - $DSN = $DBI_SOURCE .'dbname='.$DBFILE; - } + my $db =$u; + if(!$d){$db = 'data_'.$u.'_log.db';} + else{ $db = 'data_'.$d.'_log.db';} + $DBFILE = $LOG_PATH.'/'.$db; + if ($IS_PG_DB) { + $DSN = $DBI_SOURCE .'dbname='.$d; + }else{ + $DSN = $DBI_SOURCE .'dbname='.$DBFILE; + } try{ - return DBI->connect($DSN, $a, $p, {AutoCommit => 1, RaiseError => 1, PrintError => 0, show_trace=>1}); + return DBI->connect($DSN, $u, $p, {AutoCommit => 1, RaiseError => 1, PrintError => 0, show_trace=>1}); }catch{ LifeLogException->throw(error=>"

Error->$@

", show_trace=>1); }