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