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