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