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