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