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