summaryrefslogtreecommitdiff
path: root/LedgerSMB/User.pm
blob: 6107cfcbf1ba04315d7bd04a90b2aa11304fcb6d (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. return \%myconfig;
  144. }
  145. =item LedgerSMB::User->check_recurring($form);
  146. Disused function to return the number of current recurring events.
  147. =cut
  148. sub check_recurring {
  149. my ( $self, $form ) = @_;
  150. my $dbh =
  151. DBI->connect( $self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd} )
  152. or $form->dberror( __FILE__ . ':' . __LINE__ );
  153. $dbh->{pg_encode_utf8} = 1;
  154. my $query = qq|
  155. SELECT count(*) FROM recurring
  156. WHERE enddate >= current_date AND nextdate <= current_date|;
  157. ($_) = $dbh->selectrow_array($query);
  158. $dbh->disconnect;
  159. $_;
  160. }
  161. =item LedgerSMB::User::dbconnect_vars($form, $db);
  162. Converts individual $form values into $form->{dboptions} and $form->{dbconnect}.
  163. =cut
  164. sub dbconnect_vars {
  165. my ( $form, $db ) = @_;
  166. my %dboptions = (
  167. 'Pg' => {
  168. 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
  169. 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
  170. 'mm-dd-yy' => 'set DateStyle to \'POSTGRES, US\'',
  171. 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
  172. 'dd-mm-yy' => 'set DateStyle to \'POSTGRES, EUROPEAN\'',
  173. 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
  174. }
  175. );
  176. $form->{dboptions} = $dboptions{ $form->{dbdriver} }{ $form->{dateformat} };
  177. $form->{dbconnect} = "dbi:$form->{dbdriver}:dbname=$db";
  178. $form->{dbconnect} .= ";host=$form->{dbhost}";
  179. $form->{dbconnect} .= ";port=$form->{dbport}";
  180. }
  181. =item LedgerSMB::User->dbdrivers();
  182. Returns a list of all drivers set up with DBI whose names end in 'Pg'.
  183. =cut
  184. sub dbdrivers {
  185. my @drivers = DBI->available_drivers();
  186. # return (grep { /(Pg|Oracle|DB2)/ } @drivers);
  187. return ( grep { /Pg$/ } @drivers );
  188. }
  189. =item LedgerSMB::User->dbsources($form);
  190. Returns a list of all databases in the same cluster as the database that $form
  191. is set to. If $form->{only_acc_db} is set, only non-template databases that
  192. have a defaults table owned by $form->{dbuser} are returned.
  193. =cut
  194. sub dbsources {
  195. my ( $self, $form ) = @_;
  196. my @dbsources = ();
  197. my ( $sth, $query );
  198. $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
  199. $form->{sid} = $form->{dbdefault};
  200. &dbconnect_vars( $form, $form->{dbdefault} );
  201. my $dbh =
  202. DBI->connect( $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd} )
  203. or $form->dberror( __FILE__ . ':' . __LINE__ );
  204. $dbh->{pg_enable_utf8} = 1;
  205. if ( $form->{dbdriver} eq 'Pg' ) {
  206. $query = qq|SELECT datname FROM pg_database|;
  207. $sth = $dbh->prepare($query);
  208. $sth->execute || $form->dberror( __FILE__ . ':' . __LINE__ . $query );
  209. while ( my ($db) = $sth->fetchrow_array ) {
  210. if ( $form->{only_acc_db} ) {
  211. next if ( $db =~ /^template/ );
  212. &dbconnect_vars( $form, $db );
  213. my $dbh =
  214. DBI->connect( $form->{dbconnect}, $form->{dbuser},
  215. $form->{dbpasswd} )
  216. or $form->dberror( __FILE__ . ':' . __LINE__ );
  217. $dbh->{pg_enable_utf8} = 1;
  218. $query = qq|
  219. SELECT tablename FROM pg_tables
  220. WHERE tablename = 'defaults'
  221. AND tableowner = ?|;
  222. my $sth = $dbh->prepare($query);
  223. $sth->execute( $form->{dbuser} )
  224. || $form->dberror( __FILE__ . ':' . __LINE__ . $query );
  225. if ( $sth->fetchrow_array ) {
  226. push @dbsources, $db;
  227. }
  228. $sth->finish;
  229. $dbh->disconnect;
  230. next;
  231. }
  232. push @dbsources, $db;
  233. }
  234. }
  235. $sth->finish;
  236. $dbh->disconnect;
  237. return @dbsources;
  238. }
  239. =item LedgerSMB::User->dbcreate($form);
  240. Create the database indicated by $form->{db} and load Pg-database.sql, the chart
  241. indicated by $form->{chart} and custom tables and functions
  242. (Pg-custom_tables.sql and Pg-custom_functions).
  243. =cut
  244. sub dbcreate {
  245. my ( $self, $form ) = @_;
  246. my %dbcreate =
  247. ( 'Pg' => qq|CREATE DATABASE "$form->{db}" WITH ENCODING = 'UNICODE'| );
  248. $form->{sid} = $form->{dbdefault};
  249. &dbconnect_vars( $form, $form->{dbdefault} );
  250. # The below line connects to Template1 or another template file in order
  251. # to create the db. One must disconnect and reconnect later.
  252. if ( $form->{dbsuperuser} ) {
  253. my $superdbh =
  254. DBI->connect( $form->{dbconnect}, $form->{dbsuperuser},
  255. $form->{dbsuperpasswd} )
  256. or $form->dberror( __FILE__ . ':' . __LINE__ );
  257. $superdbh->{pg_enable_utf8} = 1;
  258. my $query = qq|$dbcreate{$form->{dbdriver}}|;
  259. $superdbh->do($query)
  260. || $form->dberror( __FILE__ . ':' . __LINE__ . $query );
  261. $superdbh->disconnect;
  262. }
  263. #Reassign for the work below
  264. &dbconnect_vars( $form, $form->{db} );
  265. my $dbh =
  266. DBI->connect( $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd} )
  267. or $form->dberror( __FILE__ . ':' . __LINE__ );
  268. $dbh->{pg_enable_utf8} = 1;
  269. if ( $form->{dbsuperuser} ) {
  270. my $superdbh =
  271. DBI->connect( $form->{dbconnect}, $form->{dbsuperuser},
  272. $form->{dbsuperpasswd} )
  273. or $form->dberror( __FILE__ . ':' . __LINE__ );
  274. $superdbh->{pg_enable_utf8} = 1;
  275. # JD: We need to check for plpgsql,
  276. # if it isn't there create it, if we can't error
  277. # Good chance I will have to do this twice as I get
  278. # used to the way the code is structured
  279. my %langcreate = ( 'Pg' => qq|CREATE LANGUAGE plpgsql| );
  280. my $query = qq|$langcreate{$form->{dbdriver}}|;
  281. $superdbh->do($query);
  282. $superdbh->disconnect;
  283. }
  284. # create the tables
  285. my $dbdriver =
  286. ( $form->{dbdriver} =~ /Pg/ )
  287. ? 'Pg'
  288. : $form->{dbdriver};
  289. my $filename = qq|sql/Pg-database.sql|;
  290. $self->process_query( $form, $dbh, $filename );
  291. # load gifi
  292. ($filename) = split /_/, $form->{chart};
  293. $filename =~ s/_//;
  294. $self->process_query( $form, $dbh, "sql/${filename}-gifi.sql" );
  295. # load chart of accounts
  296. $filename = qq|sql/$form->{chart}-chart.sql|;
  297. $self->process_query( $form, $dbh, $filename );
  298. # create custom tables and functions
  299. my $item;
  300. foreach $item (qw(tables functions)) {
  301. $filename = "sql/${dbdriver}-custom_${item}.sql";
  302. if ( -f "$filename" ) {
  303. $self->process_query( $form, $dbh, $filename );
  304. }
  305. }
  306. $dbh->disconnect;
  307. }
  308. =item LedgerSMB::User->process_query($form, $dbh, $filename);
  309. Load the file $filename into the database indicated through form using psql.
  310. $dbh is ignored.
  311. =cut
  312. sub process_query {
  313. my ( $self, $form, $dbh, $filename ) = @_;
  314. return unless ( -f $filename );
  315. $ENV{PGPASSWORD} = $form->{dbpasswd};
  316. $ENV{PGUSER} = $form->{dbuser};
  317. $ENV{PGDATABASE} = $form->{db};
  318. $ENV{PGHOST} = $form->{dbhost};
  319. $ENV{PGPORT} = $form->{dbport};
  320. $results = `psql -f $filename 2>&1`;
  321. if ($?) {
  322. $form->error($!);
  323. }
  324. elsif ( $results =~ /error/i ) {
  325. $form->error($results);
  326. }
  327. }
  328. =item LedgerSMB::User->dbdelete($form);
  329. Disused function to drop the database $form->{db}.
  330. =cut
  331. sub dbdelete {
  332. my ( $self, $form ) = @_;
  333. $form->{sid} = $form->{dbdefault};
  334. &dbconnect_vars( $form, $form->{dbdefault} );
  335. my $dbh =
  336. DBI->connect( $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd} )
  337. or $form->dberror( __FILE__ . ':' . __LINE__ );
  338. $dbh->{pg_enable_utf8} = 1;
  339. my $query = qq|DROP DATABASE "$form->{db}"|;
  340. $dbh->do($query) || $form->dberror( __FILE__ . ':' . __LINE__ . $query );
  341. $dbh->disconnect;
  342. }
  343. =item LedgerSMB::User->dbsources_unused($form, $memfile);
  344. Disused function to identify all databases in a cluster with a defaults table
  345. that are not mentioned in the memberfile $memfile.
  346. =cut
  347. sub dbsources_unused {
  348. my ( $self, $form, $memfile ) = @_;
  349. my @dbexcl = ();
  350. my @dbsources = ();
  351. $form->error( __FILE__ . ':' . __LINE__ . ": $memfile locked!" )
  352. if ( -f "${memfile}.LCK" );
  353. # open members file
  354. open( FH, '<', "$memfile" )
  355. or $form->error( __FILE__ . ':' . __LINE__ . ": $memfile : $!" );
  356. while (<FH>) {
  357. if (/^dbname=/) {
  358. my ( $null, $item ) = split /=/;
  359. push @dbexcl, $item;
  360. }
  361. }
  362. close FH;
  363. $form->{only_acc_db} = 1;
  364. my @db = &dbsources( "", $form );
  365. push @dbexcl, $form->{dbdefault};
  366. foreach $item (@db) {
  367. unless ( grep /$item$/, @dbexcl ) {
  368. push @dbsources, $item;
  369. }
  370. }
  371. return @dbsources;
  372. }
  373. =item LedgerSMB::User->dbneedsupdate($form);
  374. Disused function to locate all databases owned by $form->{dbuser} that are not
  375. a template* database which have a defaults table with a version entry.
  376. =cut
  377. sub dbneedsupdate {
  378. my ( $self, $form ) = @_;
  379. my %dbsources = ();
  380. my $query;
  381. $form->{sid} = $form->{dbdefault};
  382. &dbconnect_vars( $form, $form->{dbdefault} );
  383. my $dbh =
  384. DBI->connect( $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd} )
  385. or $form->dberror( __FILE__ . ':' . __LINE__ );
  386. $dbh->{pg_enable_utf8} = 1;
  387. if ( $form->{dbdriver} =~ /Pg/ ) {
  388. $query = qq|
  389. SELECT d.datname
  390. FROM pg_database d, pg_user u
  391. WHERE d.datdba = u.usesysid
  392. AND u.usename = ?|;
  393. my $sth = $dbh->prepare($query);
  394. $sth->execute( $form->{dbuser} )
  395. || $form->dberror( __FILE__ . ':' . __LINE__ . $query );
  396. while ( my ($db) = $sth->fetchrow_array ) {
  397. next if ( $db =~ /^template/ );
  398. &dbconnect_vars( $form, $db );
  399. my $dbh =
  400. DBI->connect( $form->{dbconnect}, $form->{dbuser},
  401. $form->{dbpasswd} )
  402. or $form->dberror( __FILE__ . ':' . __LINE__ );
  403. $dbh->{pg_enable_utf8};
  404. $query = qq|
  405. SELECT tablename
  406. FROM pg_tables
  407. WHERE tablename = 'defaults'|;
  408. my $sth = $dbh->prepare($query);
  409. $sth->execute
  410. || $form->dberror( __FILE__ . ':' . __LINE__ . $query );
  411. if ( $sth->fetchrow_array ) {
  412. $query = qq|
  413. SELECT value FROM defaults
  414. WHERE setting_key = 'version'|;
  415. my $sth = $dbh->prepare($query);
  416. $sth->execute;
  417. if ( my ($version) = $sth->fetchrow_array ) {
  418. $dbsources{$db} = $version;
  419. }
  420. $sth->finish;
  421. }
  422. $sth->finish;
  423. $dbh->disconnect;
  424. }
  425. $sth->finish;
  426. }
  427. $dbh->disconnect;
  428. %dbsources;
  429. }
  430. =item LedgerSMB::User->dbupdate($form);
  431. Applies database upgrade scripts to upgrade the database to the current level.
  432. =cut
  433. sub dbupdate {
  434. my ( $self, $form ) = @_;
  435. $form->{sid} = $form->{dbdefault};
  436. my @upgradescripts = ();
  437. my $query;
  438. my $rc = -2;
  439. if ( $form->{dbupdate} ) {
  440. # read update scripts into memory
  441. opendir SQLDIR, "sql/."
  442. or $form->error( __FILE__ . ':' . __LINE__ . ': ' . $! );
  443. @upgradescripts =
  444. sort script_version grep /$form->{dbdriver}-upgrade-.*?\.sql$/,
  445. readdir SQLDIR;
  446. closedir SQLDIR;
  447. }
  448. foreach my $db ( split / /, $form->{dbupdate} ) {
  449. next unless $form->{$db};
  450. # strip db from dataset
  451. $db =~ s/^db//;
  452. &dbconnect_vars( $form, $db );
  453. my $dbh = DBI->connect(
  454. $form->{dbconnect}, $form->{dbuser},
  455. $form->{dbpasswd}, { AutoCommit => 0 }
  456. ) or $form->dberror( __FILE__ . ':' . __LINE__ );
  457. $dbh->{pg_enable_utf8} = 1;
  458. # check version
  459. $query = qq|
  460. SELECT value FROM defaults
  461. WHERE setting_key = 'version'|;
  462. my $sth = $dbh->prepare($query);
  463. # no error check, let it fall through
  464. $sth->execute;
  465. my $version = $sth->fetchrow_array;
  466. $sth->finish;
  467. next unless $version;
  468. $version = calc_version($version);
  469. my $dbversion = calc_version( $form->{dbversion} );
  470. foreach my $upgradescript (@upgradescripts) {
  471. my $a = $upgradescript;
  472. $a =~ s/(^$form->{dbdriver}-upgrade-|\.sql$)//g;
  473. my ( $mindb, $maxdb ) = split /-/, $a;
  474. $mindb = calc_version($mindb);
  475. $maxdb = calc_version($maxdb);
  476. next if ( $version >= $maxdb );
  477. # exit if there is no upgrade script or version == mindb
  478. last if ( $version < $mindb || $version >= $dbversion );
  479. # apply upgrade
  480. $self->process_query( $form, $dbh, "sql/$upgradescript" );
  481. $dbh->commit;
  482. $version = $maxdb;
  483. }
  484. $rc = 0;
  485. $dbh->disconnect;
  486. }
  487. $rc;
  488. }
  489. =item calc_version($version);
  490. Returns a numeric form for the version passed in. The numeric form is derived
  491. by converting each dotted portion of the version to a three-digit number and
  492. appending them.
  493. +----------+------------+
  494. | $version | returned |
  495. +----------+------------+
  496. | 1.0.0 | 1000000 |
  497. | 1.2.33 | 1002033 |
  498. | 189.2.33 | 189002033 |
  499. | 1.2.3.4 | 1002003004 |
  500. +----------+------------+
  501. =cut
  502. sub calc_version {
  503. my @v = split /\./, $_[0];
  504. my $version = 0;
  505. my $i;
  506. for ( $i = 0 ; $i <= $#v ; $i++ ) {
  507. $version *= 1000;
  508. $version += $v[$i];
  509. }
  510. return $version;
  511. }
  512. =item script_version
  513. Sorting function for database upgrade scripts.
  514. =cut
  515. sub script_version {
  516. my ( $my_a, $my_b ) = ( $a, $b );
  517. my ( $a_from, $a_to, $b_from, $b_to );
  518. my ( $res_a, $res_b, $i );
  519. $my_a =~ s/.*-upgrade-//;
  520. $my_a =~ s/.sql$//;
  521. $my_b =~ s/.*-upgrade-//;
  522. $my_b =~ s/.sql$//;
  523. ( $a_from, $a_to ) = split( /-/, $my_a );
  524. ( $b_from, $b_to ) = split( /-/, $my_b );
  525. $res_a = calc_version($a_from);
  526. $res_b = calc_version($b_from);
  527. if ( $res_a == $res_b ) {
  528. $res_a = calc_version($a_to);
  529. $res_b = calc_version($b_to);
  530. }
  531. return $res_a <=> $res_b;
  532. }
  533. =item $user->save_member();
  534. Updates the user config in the database for the user $user. If no config for
  535. the user exists, the user to the database.
  536. =cut
  537. sub save_member {
  538. my ($self) = @_;
  539. # replace \r\n with \n
  540. for (qw(address signature)) { $self->{$_} =~ s/\r?\n/\\n/g }
  541. # use central db
  542. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  543. #check to see if the user exists already
  544. my $userCheck = $dbh->prepare("SELECT id FROM users WHERE username = ?");
  545. $userCheck->execute( $self->{login} );
  546. my ($userID) = $userCheck->fetchrow_array;
  547. if ( !$self->{dbhost} ) {
  548. $self->{dbhost} = 'localhost';
  549. }
  550. if ( !$self->{dbport} ) {
  551. $self->{dbport} = '5432';
  552. }
  553. my $userConfExists = 0;
  554. if ($userID) {
  555. #got an id, check to see if it's in the users_conf table
  556. my $userConfCheck =
  557. $dbh->prepare("SELECT password, 1 FROM users_conf WHERE id = ?");
  558. $userConfCheck->execute($userID);
  559. ( $oldPassword, $userConfExists ) = $userConfCheck->fetchrow_array;
  560. }
  561. else {
  562. my $userConfAdd = $dbh->prepare("SELECT create_user(?);");
  563. $userConfAdd->execute( $self->{login} );
  564. ($userID) = $userConfAdd->fetchrow_array;
  565. }
  566. if ($userConfExists) {
  567. # for now, this is updating the table directly... ugly
  568. my $userConfUpdate = $dbh->prepare(
  569. "UPDATE users_conf
  570. SET acs = ?, address = ?, businessnumber = ?,
  571. company = ?, countrycode = ?, currency = ?,
  572. dateformat = ?, dbdriver = ?,
  573. dbhost = ?, dbname = ?, dboptions = ?,
  574. dbpasswd = ?, dbport = ?, dbuser = ?,
  575. email = ?, fax = ?, menuwidth = ?,
  576. name = ?, numberformat = ?,
  577. print = ?, printer = ?, role = ?,
  578. sid = ?, signature = ?, stylesheet = ?,
  579. tel = ?, templates = ?, timeout = ?,
  580. vclimit = ?
  581. WHERE id = ?;"
  582. );
  583. $userConfUpdate->execute(
  584. $self->{acs}, $self->{address},
  585. $self->{businessnumber}, $self->{company},
  586. $self->{countrycode}, $self->{currency},
  587. $self->{dateformat}, $self->{dbdriver},
  588. $self->{dbhost}, $self->{dbname},
  589. $self->{dboptions}, $self->{dbpasswd},
  590. $self->{dbport}, $self->{dbuser},
  591. $self->{email}, $self->{fax},
  592. $self->{menuwidth}, $self->{name},
  593. $self->{numberformat}, $self->{print},
  594. $self->{printer}, $self->{role},
  595. $self->{sid}, $self->{signature},
  596. $self->{stylesheet}, $self->{tel},
  597. $self->{templates}, $self->{timeout},
  598. $self->{vclimit}, $userID
  599. );
  600. if ( $oldPassword ne $self->{password} ) {
  601. # if they're supplying a 32 char password that matches their old password
  602. # assume they don't want to change passwords
  603. $userConfUpdate = $dbh->prepare(
  604. "UPDATE users_conf
  605. SET password = md5(?)
  606. WHERE id = ?"
  607. );
  608. $userConfUpdate->execute( $self->{password}, $userID );
  609. }
  610. }
  611. else {
  612. my $userConfInsert = $dbh->prepare(
  613. "INSERT INTO users_conf(acs, address, businessnumber,
  614. company, countrycode, currency,
  615. dateformat, dbdriver,
  616. dbhost, dbname, dboptions, dbpasswd,
  617. dbport, dbuser, email, fax, menuwidth,
  618. name, numberformat, print, printer, role,
  619. sid, signature, stylesheet, tel, templates,
  620. timeout, vclimit, id, password)
  621. VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  622. ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  623. ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, md5(?));"
  624. );
  625. $userConfInsert->execute(
  626. $self->{acs}, $self->{address},
  627. $self->{businessnumber}, $self->{company},
  628. $self->{countrycode}, $self->{currency},
  629. $self->{dateformat}, $self->{dbdriver},
  630. $self->{dbhost}, $self->{dbname},
  631. $self->{dboptions}, $self->{dbpasswd},
  632. $self->{dbport}, $self->{dbuser},
  633. $self->{email}, $self->{fax},
  634. $self->{menuwidth}, $self->{name},
  635. $self->{numberformat}, $self->{print},
  636. $self->{printer}, $self->{role},
  637. $self->{sid}, $self->{signature},
  638. $self->{stylesheet}, $self->{tel},
  639. $self->{templates}, $self->{timeout},
  640. $self->{vclimit}, $userID,
  641. $self->{password}
  642. );
  643. }
  644. if ( !$self->{'admin'} ) {
  645. $self->{dbpasswd} =~ s/\\'/'/g;
  646. $self->{dbpasswd} =~ s/\\\\/\\/g;
  647. # format dbconnect and dboptions string
  648. &dbconnect_vars( $self, $self->{dbname} );
  649. # check if login is in database
  650. my $dbh = DBI->connect(
  651. $self->{dbconnect}, $self->{dbuser},
  652. $self->{dbpasswd}, { AutoCommit => 0 }
  653. ) or $self->error($DBI::errstr);
  654. $dbh->{pg_enable_utf8} = 1;
  655. # add login to employees table if it does not exist
  656. my $login = $self->{login};
  657. $login =~ s/@.*//;
  658. my $sth = $dbh->prepare("SELECT entity_id FROM employee WHERE login = ?;");
  659. $sth->execute($login);
  660. my ($id) = $sth->fetchrow_array;
  661. $sth->finish;
  662. my $employeenumber;
  663. my @values;
  664. if ($id) {
  665. $query = qq|UPDATE employee SET
  666. role = ?,
  667. email = ?,
  668. name = ?
  669. WHERE login = ?|;
  670. @values = ( $self->{role}, $self->{email}, $self->{name}, $login );
  671. }
  672. else {
  673. my ($employeenumber) =
  674. Form::update_defaults( "", \%$self, "employeenumber", $dbh );
  675. $query = qq|
  676. INSERT INTO employee
  677. (login, employeenumber, name,
  678. workphone, role, email, sales)
  679. VALUES (?, ?, ?, ?, ?, ?, '1')|;
  680. @values = (
  681. $login, $employeenumber, $self->{name},
  682. $self->{tel}, $self->{role}, $self->{email}
  683. );
  684. }
  685. $sth = $dbh->prepare($query);
  686. $sth->execute(@values);
  687. $dbh->commit;
  688. $dbh->disconnect;
  689. }
  690. }
  691. =item LedgerSMB::User->delete_login($form);
  692. Disused function to delete the user $form->{login}.
  693. =cut
  694. sub delete_login {
  695. my ( $self, $form ) = @_;
  696. my $dbh = DBI->connect(
  697. $form->{dbconnect}, $form->{dbuser},
  698. $form->{dbpasswd}, { AutoCommit => 0 }
  699. ) or $form->dberror( __FILE__ . ':' . __LINE__ );
  700. $dbh->{pg_enable_utf8} = 1;
  701. my $login = $form->{login};
  702. $login =~ s/@.*//;
  703. my $query = qq|SELECT entity_id FROM employee WHERE login = ?|;
  704. my $sth = $dbh->prepare($query);
  705. $sth->execute($login)
  706. || $form->dberror( __FILE__ . ':' . __LINE__ . ': ' . $query );
  707. my ($id) = $sth->fetchrow_array;
  708. $sth->finish;
  709. my $query = qq|
  710. UPDATE employee
  711. SET login = NULL,
  712. enddate = current_date
  713. WHERE login = ?|;
  714. $sth = $dbh->prepare($query);
  715. $sth->execute($login);
  716. $dbh->commit;
  717. $dbh->disconnect;
  718. }
  719. =item LedgerSMB::User->config_vars();
  720. Disused function that returns a list of user config variable names.
  721. =cut
  722. sub config_vars {
  723. my @conf = qw(acs address businessnumber company countrycode
  724. currency dateformat dbconnect dbdriver dbhost dbname dboptions
  725. dbpasswd dbport dbuser email fax menuwidth name numberformat
  726. password printer role sid signature stylesheet tel templates
  727. timeout vclimit);
  728. @conf;
  729. }
  730. =item $self->error($msg);
  731. Privately used error function. Used in places where the more typically used
  732. $form->error cannot be used. Always dies.
  733. =cut
  734. sub error {
  735. my ( $self, $msg ) = @_;
  736. if ( $ENV{GATEWAY_INTERFACE} ) {
  737. print qq|Content-Type: text/html\n\n|
  738. . qq|<body bgcolor=ffffff>\n\n|
  739. . qq|<h2><font color=red>Error!</font></h2>\n|
  740. . qq|<p><b>$msg</b>|;
  741. }
  742. die "Error: $msg\n";
  743. }
  744. 1;
  745. =back