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