summaryrefslogtreecommitdiff
path: root/LedgerSMB/User.pm
blob: 41aa0dcd25d0828ac5b97900bc19a44fd461398c (plain)
  1. =head1 NAME
  2. LedgerSMB::User
  3. =head1 SYNOPSIS
  4. This module provides user support and database management functions.
  5. =head1 STATUS
  6. Deprecated
  7. =head1 COPYRIGHT
  8. #====================================================================
  9. # LedgerSMB
  10. # Small Medium Business Accounting software
  11. # http://www.ledgersmb.org/
  12. #
  13. # Copyright (C) 2006
  14. # This work contains copyrighted information from a number of sources
  15. # all used with permission.
  16. #
  17. # This file contains source code included with or based on SQL-Ledger
  18. # which is Copyright Dieter Simader and DWS Systems Inc. 2000-2005
  19. # and licensed under the GNU General Public License version 2 or, at
  20. # your option, any later version. For a full list including contact
  21. # information of contributors, maintainers, and copyright holders,
  22. # see the CONTRIBUTORS file.
  23. #
  24. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  25. # Copyright (C) 2000
  26. #
  27. # Author: DWS Systems Inc.
  28. # Web: http://www.sql-ledger.org
  29. #
  30. # Contributors: Jim Rawlings <jim@your-dba.com>
  31. #
  32. #====================================================================
  33. #
  34. # This file has undergone whitespace cleanup.
  35. #
  36. #====================================================================
  37. #
  38. # user related functions
  39. #
  40. #====================================================================
  41. =head1 METHODS
  42. =over
  43. =cut
  44. # inline documentation
  45. package LedgerSMB::User;
  46. use LedgerSMB::Sysconfig;
  47. use LedgerSMB::Session;
  48. use Data::Dumper;
  49. =item LedgerSMB::User->new($login);
  50. Create a LedgerSMB::User object. If the user $login exists, set the fields
  51. with values retrieved from the database.
  52. =cut
  53. sub new {
  54. my ( $type, $login ) = @_;
  55. my $self = {};
  56. if ( $login ne "" ) {
  57. # use central db
  58. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  59. # for now, this is querying the table directly... ugly
  60. my $fetchUserPrefs = $dbh->prepare(
  61. "SELECT acs, address, businessnumber,
  62. company, countrycode, currency,
  63. dateformat, dbdriver, dbhost, dbname,
  64. dboptions, dbpasswd, dbport, dbuser,
  65. email, fax, menuwidth, name, numberformat,
  66. password, print, printer, role, sid,
  67. signature, stylesheet, tel, templates,
  68. timeout, vclimit, u.username
  69. FROM users_conf as uc, users as u
  70. WHERE u.username = ?
  71. AND u.id = uc.id;"
  72. );
  73. $fetchUserPrefs->execute($login);
  74. my $userHashRef = $fetchUserPrefs->fetchrow_hashref;
  75. while ( my ( $key, $value ) = each( %{$userHashRef} ) ) {
  76. $self->{$key} = $value;
  77. }
  78. chomp( $self->{dbport} );
  79. chomp( $self->{dbname} );
  80. chomp( $self->{dbhost} );
  81. $self->{dbconnect} =
  82. 'dbi:Pg:dbname='
  83. . $self->{dbname}
  84. . ';host='
  85. . $self->{dbhost}
  86. . ';port='
  87. . $self->{dbport};
  88. if ( $self->{username} ) {
  89. $self->{login} = $login;
  90. }
  91. }
  92. bless $self, $type;
  93. }
  94. =item LedgerSMB::User->country_codes();
  95. Returns a hash where the keys are registered locales and the values are the
  96. textual representation of the locale name.
  97. =cut
  98. sub country_codes {
  99. use Locale::Country;
  100. use Locale::Language;
  101. my %cc = ();
  102. # scan the locale directory and read in the LANGUAGE files
  103. opendir DIR, "${LedgerSMB::Sysconfig::localepath}";
  104. my @dir = grep !/^\..*$/, readdir DIR;
  105. foreach my $dir (@dir) {
  106. $dir = substr( $dir, 0, -3 );
  107. $cc{$dir} = code2language( substr( $dir, 0, 2 ) );
  108. $cc{$dir} .= ( "/" . code2country( substr( $dir, 3, 2 ) ) )
  109. if length($dir) > 2;
  110. $cc{$dir} .= ( " " . substr( $dir, 6 ) ) if length($dir) > 5;
  111. }
  112. closedir(DIR);
  113. %cc;
  114. }
  115. =item LedgerSMB::User->fetch_config($login);
  116. Returns a reference to a hash that contains the user config for the user $login.
  117. If that user does not exist, output 'Access denied' if in CGI and die in all
  118. cases.
  119. =cut
  120. sub fetch_config {
  121. #I'm hoping that this function will go and is a temporary bridge
  122. #until we get rid of %myconfig elsewhere in the code
  123. my ( $self, $lsmb ) = @_;
  124. my $login = $lsmb->{login};
  125. my $dbh = $lsmb->{dbh};
  126. if ( !$login ) {
  127. &error( $self, "Access Denied" );
  128. }
  129. # for now, this is querying the table directly... ugly
  130. # my $fetchUserPrefs = $dbh->prepare(
  131. # "SELECT acs, address, businessnumber,
  132. # company, countrycode, currency,
  133. # dateformat, dbdriver, dbhost, dbname,
  134. # dboptions, dbpasswd, dbport, dbuser,
  135. # email, fax, menuwidth, name, numberformat,
  136. # password, print, printer, role, sid,
  137. # signature, stylesheet, tel, templates,
  138. # timeout, vclimit, u.username
  139. # FROM users_conf as uc, users as u
  140. # WHERE u.username = ?
  141. # AND u.id = uc.id;"
  142. # );
  143. my $fetchUserSettings = $dbh->prepare("
  144. SELECT
  145. u.username,
  146. uc.dbname,
  147. uc.port,
  148. uc.host
  149. FROM users u
  150. JOIN user_connection uc ON uc.user_id = u.id
  151. WHERE u.username = ?
  152. ");
  153. $fetchUserSettings->execute($login);
  154. #$fetchUserPrefs->execute($login);
  155. my $userHashRef = $fetchUserSettings->fetchrow_hashref;
  156. if ( !$userHashRef ) {
  157. &error( $self, "Access Denied" );
  158. }
  159. while ( my ( $key, $value ) = each( %{$userHashRef} ) ) {
  160. $myconfig{$key} = $value;
  161. }
  162. chomp( $myconfig{'port'} );
  163. chomp( $myconfig{'dbname'} );
  164. chomp( $myconfig{'host'} );
  165. $myconfig{'login'} = $login;
  166. $myconfig{'dbconnect'} =
  167. 'dbi:Pg:dbname='
  168. . $myconfig{'dbname'}
  169. . ';host='
  170. . $myconfig{'host'}
  171. . ';port='
  172. . $myconfig{'port'};
  173. return \%myconfig;
  174. }
  175. =item LedgerSMB::User->check_recurring($form);
  176. Disused function to return the number of current recurring events.
  177. =cut
  178. sub check_recurring {
  179. my ( $self, $form ) = @_;
  180. my $dbh =
  181. DBI->connect( $self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd} )
  182. or $form->dberror( __FILE__ . ':' . __LINE__ );
  183. $dbh->{pg_encode_utf8} = 1;
  184. my $query = qq|
  185. SELECT count(*) FROM recurring
  186. WHERE enddate >= current_date AND nextdate <= current_date|;
  187. ($_) = $dbh->selectrow_array($query);
  188. $dbh->disconnect;
  189. $_;
  190. }
  191. =item LedgerSMB::User::dbconnect_vars($form, $db);
  192. Converts individual $form values into $form->{dboptions} and $form->{dbconnect}.
  193. =cut
  194. sub dbconnect_vars {
  195. my ( $form, $db ) = @_;
  196. my %dboptions = (
  197. 'Pg' => {
  198. 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
  199. 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
  200. 'mm-dd-yy' => 'set DateStyle to \'POSTGRES, US\'',
  201. 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
  202. 'dd-mm-yy' => 'set DateStyle to \'POSTGRES, EUROPEAN\'',
  203. 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
  204. }
  205. );
  206. $form->{dboptions} = $dboptions{ $form->{dbdriver} }{ $form->{dateformat} };
  207. $form->{dbconnect} = "dbi:$form->{dbdriver}:dbname=$db";
  208. $form->{dbconnect} .= ";host=$form->{dbhost}";
  209. $form->{dbconnect} .= ";port=$form->{dbport}";
  210. }
  211. =item LedgerSMB::User->dbdrivers();
  212. Returns a list of all drivers set up with DBI whose names end in 'Pg'.
  213. =cut
  214. sub dbdrivers {
  215. my @drivers = DBI->available_drivers();
  216. # return (grep { /(Pg|Oracle|DB2)/ } @drivers);
  217. return ( grep { /Pg$/ } @drivers );
  218. }
  219. =item LedgerSMB::User->dbsources($form);
  220. Returns a list of all databases in the same cluster as the database that $form
  221. is set to. If $form->{only_acc_db} is set, only non-template databases that
  222. have a defaults table owned by $form->{dbuser} are returned.
  223. =cut
  224. sub dbsources {
  225. my ( $self, $form ) = @_;
  226. my @dbsources = ();
  227. my ( $sth, $query );
  228. $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
  229. $form->{sid} = $form->{dbdefault};
  230. &dbconnect_vars( $form, $form->{dbdefault} );
  231. my $dbh =
  232. DBI->connect( $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd} )
  233. or $form->dberror( __FILE__ . ':' . __LINE__ );
  234. $dbh->{pg_enable_utf8} = 1;
  235. if ( $form->{dbdriver} eq 'Pg' ) {
  236. $query = qq|SELECT datname FROM pg_database|;
  237. $sth = $dbh->prepare($query);
  238. $sth->execute || $form->dberror( __FILE__ . ':' . __LINE__ . $query );
  239. while ( my ($db) = $sth->fetchrow_array ) {
  240. if ( $form->{only_acc_db} ) {
  241. next if ( $db =~ /^template/ );
  242. &dbconnect_vars( $form, $db );
  243. my $dbh =
  244. DBI->connect( $form->{dbconnect}, $form->{dbuser},
  245. $form->{dbpasswd} )
  246. or $form->dberror( __FILE__ . ':' . __LINE__ );
  247. $dbh->{pg_enable_utf8} = 1;
  248. $query = qq|
  249. SELECT tablename FROM pg_tables
  250. WHERE tablename = 'defaults'
  251. AND tableowner = ?|;
  252. my $sth = $dbh->prepare($query);
  253. $sth->execute( $form->{dbuser} )
  254. || $form->dberror( __FILE__ . ':' . __LINE__ . $query );
  255. if ( $sth->fetchrow_array ) {
  256. push @dbsources, $db;
  257. }
  258. $sth->finish;
  259. $dbh->disconnect;
  260. next;
  261. }
  262. push @dbsources, $db;
  263. }
  264. }
  265. $sth->finish;
  266. $dbh->disconnect;
  267. return @dbsources;
  268. }
  269. =item LedgerSMB::User->dbcreate($form);
  270. Create the database indicated by $form->{db} and load Pg-database.sql, the chart
  271. indicated by $form->{chart} and custom tables and functions
  272. (Pg-custom_tables.sql and Pg-custom_functions).
  273. =cut
  274. sub dbcreate {
  275. my ( $self, $form ) = @_;
  276. my %dbcreate =
  277. ( 'Pg' => qq|CREATE DATABASE "$form->{db}" WITH ENCODING = 'UNICODE'| );
  278. $form->{sid} = $form->{dbdefault};
  279. &dbconnect_vars( $form, $form->{dbdefault} );
  280. # The below line connects to Template1 or another template file in order
  281. # to create the db. One must disconnect and reconnect later.
  282. if ( $form->{dbsuperuser} ) {
  283. my $superdbh =
  284. DBI->connect( $form->{dbconnect}, $form->{dbsuperuser},
  285. $form->{dbsuperpasswd} )
  286. or $form->dberror( __FILE__ . ':' . __LINE__ );
  287. $superdbh->{pg_enable_utf8} = 1;
  288. my $query = qq|$dbcreate{$form->{dbdriver}}|;
  289. $superdbh->do($query)
  290. || $form->dberror( __FILE__ . ':' . __LINE__ . $query );
  291. $superdbh->disconnect;
  292. }
  293. #Reassign for the work below
  294. &dbconnect_vars( $form, $form->{db} );
  295. my $dbh =
  296. DBI->connect( $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd} )
  297. or $form->dberror( __FILE__ . ':' . __LINE__ );
  298. $dbh->{pg_enable_utf8} = 1;
  299. if ( $form->{dbsuperuser} ) {
  300. my $superdbh =
  301. DBI->connect( $form->{dbconnect}, $form->{dbsuperuser},
  302. $form->{dbsuperpasswd} )
  303. or $form->dberror( __FILE__ . ':' . __LINE__ );
  304. $superdbh->{pg_enable_utf8} = 1;
  305. # JD: We need to check for plpgsql,
  306. # if it isn't there create it, if we can't error
  307. # Good chance I will have to do this twice as I get
  308. # used to the way the code is structured
  309. my %langcreate = ( 'Pg' => qq|CREATE LANGUAGE plpgsql| );
  310. my $query = qq|$langcreate{$form->{dbdriver}}|;
  311. $superdbh->do($query);
  312. $superdbh->disconnect;
  313. }
  314. # create the tables
  315. my $dbdriver =
  316. ( $form->{dbdriver} =~ /Pg/ )
  317. ? 'Pg'
  318. : $form->{dbdriver};
  319. my $filename = qq|sql/Pg-database.sql|;
  320. $self->process_query( $form, $dbh, $filename );
  321. # load gifi
  322. ($filename) = split /_/, $form->{chart};
  323. $filename =~ s/_//;
  324. $self->process_query( $form, $dbh, "sql/${filename}-gifi.sql" );
  325. # load chart of accounts
  326. $filename = qq|sql/$form->{chart}-chart.sql|;
  327. $self->process_query( $form, $dbh, $filename );
  328. # create custom tables and functions
  329. my $item;
  330. foreach $item (qw(tables functions)) {
  331. $filename = "sql/${dbdriver}-custom_${item}.sql";
  332. if ( -f "$filename" ) {
  333. $self->process_query( $form, $dbh, $filename );
  334. }
  335. }
  336. $dbh->disconnect;
  337. }
  338. =item LedgerSMB::User->process_query($form, $dbh, $filename);
  339. Load the file $filename into the database indicated through form using psql.
  340. $dbh is ignored.
  341. =cut
  342. sub process_query {
  343. my ( $self, $form, $dbh, $filename ) = @_;
  344. return unless ( -f $filename );
  345. $ENV{PGPASSWORD} = $form->{dbpasswd};
  346. $ENV{PGUSER} = $form->{dbuser};
  347. $ENV{PGDATABASE} = $form->{db};
  348. $ENV{PGHOST} = $form->{dbhost};
  349. $ENV{PGPORT} = $form->{dbport};
  350. $results = `psql -f $filename 2>&1`;
  351. if ($?) {
  352. $form->error($!);
  353. }
  354. elsif ( $results =~ /error/i ) {
  355. $form->error($results);
  356. }
  357. }
  358. =item LedgerSMB::User->dbdelete($form);
  359. Disused function to drop the database $form->{db}.
  360. =cut
  361. sub dbdelete {
  362. my ( $self, $form ) = @_;
  363. $form->{sid} = $form->{dbdefault};
  364. &dbconnect_vars( $form, $form->{dbdefault} );
  365. my $dbh =
  366. DBI->connect( $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd} )
  367. or $form->dberror( __FILE__ . ':' . __LINE__ );
  368. $dbh->{pg_enable_utf8} = 1;
  369. my $query = qq|DROP DATABASE "$form->{db}"|;
  370. $dbh->do($query) || $form->dberror( __FILE__ . ':' . __LINE__ . $query );
  371. $dbh->disconnect;
  372. }
  373. =item LedgerSMB::User->dbsources_unused($form, $memfile);
  374. Disused function to identify all databases in a cluster with a defaults table
  375. that are not mentioned in the memberfile $memfile.
  376. =cut
  377. sub dbsources_unused {
  378. my ( $self, $form, $memfile ) = @_;
  379. my @dbexcl = ();
  380. my @dbsources = ();
  381. $form->error( __FILE__ . ':' . __LINE__ . ": $memfile locked!" )
  382. if ( -f "${memfile}.LCK" );
  383. # open members file
  384. open( FH, '<', "$memfile" )
  385. or $form->error( __FILE__ . ':' . __LINE__ . ": $memfile : $!" );
  386. while (<FH>) {
  387. if (/^dbname=/) {
  388. my ( $null, $item ) = split /=/;
  389. push @dbexcl, $item;
  390. }
  391. }
  392. close FH;
  393. $form->{only_acc_db} = 1;
  394. my @db = &dbsources( "", $form );
  395. push @dbexcl, $form->{dbdefault};
  396. foreach $item (@db) {
  397. unless ( grep /$item$/, @dbexcl ) {
  398. push @dbsources, $item;
  399. }
  400. }
  401. return @dbsources;
  402. }
  403. =item LedgerSMB::User->dbneedsupdate($form);
  404. Disused function to locate all databases owned by $form->{dbuser} that are not
  405. a template* database which have a defaults table with a version entry.
  406. =cut
  407. sub dbneedsupdate {
  408. my ( $self, $form ) = @_;
  409. my %dbsources = ();
  410. my $query;
  411. $form->{sid} = $form->{dbdefault};
  412. &dbconnect_vars( $form, $form->{dbdefault} );
  413. my $dbh =
  414. DBI->connect( $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd} )
  415. or $form->dberror( __FILE__ . ':' . __LINE__ );
  416. $dbh->{pg_enable_utf8} = 1;
  417. if ( $form->{dbdriver} =~ /Pg/ ) {
  418. $query = qq|
  419. SELECT d.datname
  420. FROM pg_database d, pg_user u
  421. WHERE d.datdba = u.usesysid
  422. AND u.usename = ?|;
  423. my $sth = $dbh->prepare($query);
  424. $sth->execute( $form->{dbuser} )
  425. || $form->dberror( __FILE__ . ':' . __LINE__ . $query );
  426. while ( my ($db) = $sth->fetchrow_array ) {
  427. next if ( $db =~ /^template/ );
  428. &dbconnect_vars( $form, $db );
  429. my $dbh =
  430. DBI->connect( $form->{dbconnect}, $form->{dbuser},
  431. $form->{dbpasswd} )
  432. or $form->dberror( __FILE__ . ':' . __LINE__ );
  433. $dbh->{pg_enable_utf8};
  434. $query = qq|
  435. SELECT tablename
  436. FROM pg_tables
  437. WHERE tablename = 'defaults'|;
  438. my $sth = $dbh->prepare($query);
  439. $sth->execute
  440. || $form->dberror( __FILE__ . ':' . __LINE__ . $query );
  441. if ( $sth->fetchrow_array ) {
  442. $query = qq|
  443. SELECT value FROM defaults
  444. WHERE setting_key = 'version'|;
  445. my $sth = $dbh->prepare($query);
  446. $sth->execute;
  447. if ( my ($version) = $sth->fetchrow_array ) {
  448. $dbsources{$db} = $version;
  449. }
  450. $sth->finish;
  451. }
  452. $sth->finish;
  453. $dbh->disconnect;
  454. }
  455. $sth->finish;
  456. }
  457. $dbh->disconnect;
  458. %dbsources;
  459. }
  460. =item LedgerSMB::User->dbupdate($form);
  461. Applies database upgrade scripts to upgrade the database to the current level.
  462. =cut
  463. sub dbupdate {
  464. my ( $self, $form ) = @_;
  465. $form->{sid} = $form->{dbdefault};
  466. my @upgradescripts = ();
  467. my $query;
  468. my $rc = -2;
  469. if ( $form->{dbupdate} ) {
  470. # read update scripts into memory
  471. opendir SQLDIR, "sql/."
  472. or $form->error( __FILE__ . ':' . __LINE__ . ': ' . $! );
  473. @upgradescripts =
  474. sort script_version grep /$form->{dbdriver}-upgrade-.*?\.sql$/,
  475. readdir SQLDIR;
  476. closedir SQLDIR;
  477. }
  478. foreach my $db ( split / /, $form->{dbupdate} ) {
  479. next unless $form->{$db};
  480. # strip db from dataset
  481. $db =~ s/^db//;
  482. &dbconnect_vars( $form, $db );
  483. my $dbh = DBI->connect(
  484. $form->{dbconnect}, $form->{dbuser},
  485. $form->{dbpasswd}, { AutoCommit => 0 }
  486. ) or $form->dberror( __FILE__ . ':' . __LINE__ );
  487. $dbh->{pg_enable_utf8} = 1;
  488. # check version
  489. $query = qq|
  490. SELECT value FROM defaults
  491. WHERE setting_key = 'version'|;
  492. my $sth = $dbh->prepare($query);
  493. # no error check, let it fall through
  494. $sth->execute;
  495. my $version = $sth->fetchrow_array;
  496. $sth->finish;
  497. next unless $version;
  498. $version = calc_version($version);
  499. my $dbversion = calc_version( $form->{dbversion} );
  500. foreach my $upgradescript (@upgradescripts) {
  501. my $a = $upgradescript;
  502. $a =~ s/(^$form->{dbdriver}-upgrade-|\.sql$)//g;
  503. my ( $mindb, $maxdb ) = split /-/, $a;
  504. $mindb = calc_version($mindb);
  505. $maxdb = calc_version($maxdb);
  506. next if ( $version >= $maxdb );
  507. # exit if there is no upgrade script or version == mindb
  508. last if ( $version < $mindb || $version >= $dbversion );
  509. # apply upgrade
  510. $self->process_query( $form, $dbh, "sql/$upgradescript" );
  511. $dbh->commit;
  512. $version = $maxdb;
  513. }
  514. $rc = 0;
  515. $dbh->disconnect;
  516. }
  517. $rc;
  518. }
  519. =item calc_version($version);
  520. Returns a numeric form for the version passed in. The numeric form is derived
  521. by converting each dotted portion of the version to a three-digit number and
  522. appending them.
  523. +----------+------------+
  524. | $version | returned |
  525. +----------+------------+
  526. | 1.0.0 | 1000000 |
  527. | 1.2.33 | 1002033 |
  528. | 189.2.33 | 189002033 |
  529. | 1.2.3.4 | 1002003004 |
  530. +----------+------------+
  531. =cut
  532. sub calc_version {
  533. my @v = split /\./, $_[0];
  534. my $version = 0;
  535. my $i;
  536. for ( $i = 0 ; $i <= $#v ; $i++ ) {
  537. $version *= 1000;
  538. $version += $v[$i];
  539. }
  540. return $version;
  541. }
  542. =item script_version
  543. Sorting function for database upgrade scripts.
  544. =cut
  545. sub script_version {
  546. my ( $my_a, $my_b ) = ( $a, $b );
  547. my ( $a_from, $a_to, $b_from, $b_to );
  548. my ( $res_a, $res_b, $i );
  549. $my_a =~ s/.*-upgrade-//;
  550. $my_a =~ s/.sql$//;
  551. $my_b =~ s/.*-upgrade-//;
  552. $my_b =~ s/.sql$//;
  553. ( $a_from, $a_to ) = split( /-/, $my_a );
  554. ( $b_from, $b_to ) = split( /-/, $my_b );
  555. $res_a = calc_version($a_from);
  556. $res_b = calc_version($b_from);
  557. if ( $res_a == $res_b ) {
  558. $res_a = calc_version($a_to);
  559. $res_b = calc_version($b_to);
  560. }
  561. return $res_a <=> $res_b;
  562. }
  563. =item $user->save_member();
  564. Updates the user config in the database for the user $user. If no config for
  565. the user exists, the user to the database.
  566. =cut
  567. sub save_member {
  568. my ($self) = @_;
  569. # replace \r\n with \n
  570. for (qw(address signature)) { $self->{$_} =~ s/\r?\n/\\n/g }
  571. # use central db
  572. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  573. #check to see if the user exists already
  574. my $userCheck = $dbh->prepare("SELECT id FROM users WHERE username = ?");
  575. $userCheck->execute( $self->{login} );
  576. my ($userID) = $userCheck->fetchrow_array;
  577. if ( !$self->{dbhost} ) {
  578. $self->{dbhost} = 'localhost';
  579. }
  580. if ( !$self->{dbport} ) {
  581. $self->{dbport} = '5432';
  582. }
  583. my $userConfExists = 0;
  584. if ($userID) {
  585. #got an id, check to see if it's in the users_conf table
  586. my $userConfCheck =
  587. $dbh->prepare("SELECT password, 1 FROM users_conf WHERE id = ?");
  588. $userConfCheck->execute($userID);
  589. ( $oldPassword, $userConfExists ) = $userConfCheck->fetchrow_array;
  590. }
  591. else {
  592. my $userConfAdd = $dbh->prepare("SELECT create_user(?);");
  593. $userConfAdd->execute( $self->{login} );
  594. ($userID) = $userConfAdd->fetchrow_array;
  595. }
  596. if ($userConfExists) {
  597. # for now, this is updating the table directly... ugly
  598. my $userConfUpdate = $dbh->prepare(
  599. "UPDATE users_conf
  600. SET acs = ?, address = ?, businessnumber = ?,
  601. company = ?, countrycode = ?, currency = ?,
  602. dateformat = ?, dbdriver = ?,
  603. dbhost = ?, dbname = ?, dboptions = ?,
  604. dbpasswd = ?, dbport = ?, dbuser = ?,
  605. email = ?, fax = ?, menuwidth = ?,
  606. name = ?, numberformat = ?,
  607. print = ?, printer = ?, role = ?,
  608. sid = ?, signature = ?, stylesheet = ?,
  609. tel = ?, templates = ?, timeout = ?,
  610. vclimit = ?
  611. WHERE id = ?;"
  612. );
  613. $userConfUpdate->execute(
  614. $self->{acs}, $self->{address},
  615. $self->{businessnumber}, $self->{company},
  616. $self->{countrycode}, $self->{currency},
  617. $self->{dateformat}, $self->{dbdriver},
  618. $self->{dbhost}, $self->{dbname},
  619. $self->{dboptions}, $self->{dbpasswd},
  620. $self->{dbport}, $self->{dbuser},
  621. $self->{email}, $self->{fax},
  622. $self->{menuwidth}, $self->{name},
  623. $self->{numberformat}, $self->{print},
  624. $self->{printer}, $self->{role},
  625. $self->{sid}, $self->{signature},
  626. $self->{stylesheet}, $self->{tel},
  627. $self->{templates}, $self->{timeout},
  628. $self->{vclimit}, $userID
  629. );
  630. if ( $oldPassword ne $self->{password} ) {
  631. # if they're supplying a 32 char password that matches their old password
  632. # assume they don't want to change passwords
  633. $userConfUpdate = $dbh->prepare(
  634. "UPDATE users_conf
  635. SET password = md5(?)
  636. WHERE id = ?"
  637. );
  638. $userConfUpdate->execute( $self->{password}, $userID );
  639. }
  640. }
  641. else {
  642. my $userConfInsert = $dbh->prepare(
  643. "INSERT INTO users_conf(acs, address, businessnumber,
  644. company, countrycode, currency,
  645. dateformat, dbdriver,
  646. dbhost, dbname, dboptions, dbpasswd,
  647. dbport, dbuser, email, fax, menuwidth,
  648. name, numberformat, print, printer, role,
  649. sid, signature, stylesheet, tel, templates,
  650. timeout, vclimit, id, password)
  651. VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  652. ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  653. ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, md5(?));"
  654. );
  655. $userConfInsert->execute(
  656. $self->{acs}, $self->{address},
  657. $self->{businessnumber}, $self->{company},
  658. $self->{countrycode}, $self->{currency},
  659. $self->{dateformat}, $self->{dbdriver},
  660. $self->{dbhost}, $self->{dbname},
  661. $self->{dboptions}, $self->{dbpasswd},
  662. $self->{dbport}, $self->{dbuser},
  663. $self->{email}, $self->{fax},
  664. $self->{menuwidth}, $self->{name},
  665. $self->{numberformat}, $self->{print},
  666. $self->{printer}, $self->{role},
  667. $self->{sid}, $self->{signature},
  668. $self->{stylesheet}, $self->{tel},
  669. $self->{templates}, $self->{timeout},
  670. $self->{vclimit}, $userID,
  671. $self->{password}
  672. );
  673. }
  674. if ( !$self->{'admin'} ) {
  675. $self->{dbpasswd} =~ s/\\'/'/g;
  676. $self->{dbpasswd} =~ s/\\\\/\\/g;
  677. # format dbconnect and dboptions string
  678. &dbconnect_vars( $self, $self->{dbname} );
  679. # check if login is in database
  680. my $dbh = DBI->connect(
  681. $self->{dbconnect}, $self->{dbuser},
  682. $self->{dbpasswd}, { AutoCommit => 0 }
  683. ) or $self->error($DBI::errstr);
  684. $dbh->{pg_enable_utf8} = 1;
  685. # add login to employees table if it does not exist
  686. my $login = $self->{login};
  687. $login =~ s/@.*//;
  688. my $sth = $dbh->prepare("SELECT entity_id FROM employee WHERE login = ?;");
  689. $sth->execute($login);
  690. my ($id) = $sth->fetchrow_array;
  691. $sth->finish;
  692. my $employeenumber;
  693. my @values;
  694. if ($id) {
  695. $query = qq|UPDATE employee SET
  696. role = ?,
  697. email = ?,
  698. name = ?
  699. WHERE login = ?|;
  700. @values = ( $self->{role}, $self->{email}, $self->{name}, $login );
  701. }
  702. else {
  703. my ($employeenumber) =
  704. Form::update_defaults( "", \%$self, "employeenumber", $dbh );
  705. $query = qq|
  706. INSERT INTO employee
  707. (login, employeenumber, name,
  708. workphone, role, email, sales)
  709. VALUES (?, ?, ?, ?, ?, ?, '1')|;
  710. @values = (
  711. $login, $employeenumber, $self->{name},
  712. $self->{tel}, $self->{role}, $self->{email}
  713. );
  714. }
  715. $sth = $dbh->prepare($query);
  716. $sth->execute(@values);
  717. $dbh->commit;
  718. $dbh->disconnect;
  719. }
  720. }
  721. =item LedgerSMB::User->delete_login($form);
  722. Disused function to delete the user $form->{login}.
  723. =cut
  724. sub delete_login {
  725. my ( $self, $form ) = @_;
  726. my $dbh = DBI->connect(
  727. $form->{dbconnect}, $form->{dbuser},
  728. $form->{dbpasswd}, { AutoCommit => 0 }
  729. ) or $form->dberror( __FILE__ . ':' . __LINE__ );
  730. $dbh->{pg_enable_utf8} = 1;
  731. my $login = $form->{login};
  732. $login =~ s/@.*//;
  733. my $query = qq|SELECT entity_id FROM employee WHERE login = ?|;
  734. my $sth = $dbh->prepare($query);
  735. $sth->execute($login)
  736. || $form->dberror( __FILE__ . ':' . __LINE__ . ': ' . $query );
  737. my ($id) = $sth->fetchrow_array;
  738. $sth->finish;
  739. my $query = qq|
  740. UPDATE employee
  741. SET login = NULL,
  742. enddate = current_date
  743. WHERE login = ?|;
  744. $sth = $dbh->prepare($query);
  745. $sth->execute($login);
  746. $dbh->commit;
  747. $dbh->disconnect;
  748. }
  749. =item LedgerSMB::User->config_vars();
  750. Disused function that returns a list of user config variable names.
  751. =cut
  752. sub config_vars {
  753. my @conf = qw(acs address businessnumber company countrycode
  754. currency dateformat dbconnect dbdriver dbhost dbname dboptions
  755. dbpasswd dbport dbuser email fax menuwidth name numberformat
  756. password printer role sid signature stylesheet tel templates
  757. timeout vclimit);
  758. @conf;
  759. }
  760. =item $self->error($msg);
  761. Privately used error function. Used in places where the more typically used
  762. $form->error cannot be used. Always dies.
  763. =cut
  764. sub error {
  765. my ( $self, $msg ) = @_;
  766. if ( $ENV{GATEWAY_INTERFACE} ) {
  767. print qq|Content-Type: text/html\n\n|
  768. . qq|<body bgcolor=ffffff>\n\n|
  769. . qq|<h2><font color=red>Error!</font></h2>\n|
  770. . qq|<p><b>$msg</b>|;
  771. }
  772. die "Error: $msg\n";
  773. }
  774. 1;
  775. =back