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