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