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