summaryrefslogtreecommitdiff
path: root/LedgerSMB/User.pm
blob: 4bdb05198dbf90f1c5e830bcfa73272d5beee518 (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. while (<FH>){
  342. print PSQL $_;
  343. }
  344. close FH;
  345. }
  346. sub dbdelete {
  347. my ($self, $form) = @_;
  348. $form->{sid} = $form->{dbdefault};
  349. &dbconnect_vars($form, $form->{dbdefault});
  350. my $dbh = DBI->connect(
  351. $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
  352. or $form->dberror(__FILE__.':'.__LINE__);
  353. my $query = qq|DROP DATABASE "$form->{db}"|;
  354. $dbh->do($query) || $form->dberror(__FILE__.':'.__LINE__.$query);
  355. $dbh->disconnect;
  356. }
  357. sub dbsources_unused {
  358. my ($self, $form, $memfile) = @_;
  359. my @dbexcl = ();
  360. my @dbsources = ();
  361. $form->error(__FILE__.':'.__LINE__.": $memfile locked!") if (-f "${memfile}.LCK");
  362. # open members file
  363. open(FH, "$memfile") or $form->error(__FILE__.':'.__LINE__.": $memfile : $!");
  364. while (<FH>) {
  365. if (/^dbname=/) {
  366. my ($null,$item) = split /=/;
  367. push @dbexcl, $item;
  368. }
  369. }
  370. close FH;
  371. $form->{only_acc_db} = 1;
  372. my @db = &dbsources("", $form);
  373. push @dbexcl, $form->{dbdefault};
  374. foreach $item (@db) {
  375. unless (grep /$item$/, @dbexcl) {
  376. push @dbsources, $item;
  377. }
  378. }
  379. return @dbsources;
  380. }
  381. sub dbneedsupdate {
  382. my ($self, $form) = @_;
  383. my %dbsources = ();
  384. my $query;
  385. $form->{sid} = $form->{dbdefault};
  386. &dbconnect_vars($form, $form->{dbdefault});
  387. my $dbh = DBI->connect(
  388. $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd})
  389. or $form->dberror(__FILE__.':'.__LINE__);
  390. if ($form->{dbdriver} =~ /Pg/) {
  391. $query = qq|
  392. SELECT d.datname
  393. FROM pg_database d, pg_user u
  394. WHERE d.datdba = u.usesysid
  395. AND u.usename = ?|;
  396. my $sth = $dbh->prepare($query);
  397. $sth->execute($form->{dbuser}) || $form->dberror(__FILE__.':'.__LINE__.$query);
  398. while (my ($db) = $sth->fetchrow_array) {
  399. next if ($db =~ /^template/);
  400. &dbconnect_vars($form, $db);
  401. my $dbh = DBI->connect(
  402. $form->{dbconnect}, $form->{dbuser},
  403. $form->{dbpasswd})
  404. or $form->dberror(__FILE__.':'.__LINE__);
  405. $query = qq|
  406. SELECT tablename
  407. FROM pg_tables
  408. WHERE tablename = 'defaults'|;
  409. my $sth = $dbh->prepare($query);
  410. $sth->execute || $form->dberror(__FILE__.':'.__LINE__.$query);
  411. if ($sth->fetchrow_array) {
  412. $query = qq|
  413. SELECT value FROM defaults
  414. WHERE setting_key = 'version'|;
  415. my $sth = $dbh->prepare($query);
  416. $sth->execute;
  417. if (my ($version) = $sth->fetchrow_array) {
  418. $dbsources{$db} = $version;
  419. }
  420. $sth->finish;
  421. }
  422. $sth->finish;
  423. $dbh->disconnect;
  424. }
  425. $sth->finish;
  426. }
  427. $dbh->disconnect;
  428. %dbsources;
  429. }
  430. sub dbupdate {
  431. my ($self, $form) = @_;
  432. $form->{sid} = $form->{dbdefault};
  433. my @upgradescripts = ();
  434. my $query;
  435. my $rc = -2;
  436. if ($form->{dbupdate}) {
  437. # read update scripts into memory
  438. opendir SQLDIR, "sql/." or $form->error(__FILE__.':'.__LINE__.': '.$!);
  439. @upgradescripts =
  440. sort script_version
  441. grep /$form->{dbdriver}-upgrade-.*?\.sql$/,
  442. readdir SQLDIR;
  443. closedir SQLDIR;
  444. }
  445. foreach my $db (split / /, $form->{dbupdate}) {
  446. next unless $form->{$db};
  447. # strip db from dataset
  448. $db =~ s/^db//;
  449. &dbconnect_vars($form, $db);
  450. my $dbh = DBI->connect(
  451. $form->{dbconnect}, $form->{dbuser},
  452. $form->{dbpasswd}, {AutoCommit => 0})
  453. or $form->dberror(__FILE__.':'.__LINE__);
  454. # check version
  455. $query = qq|
  456. SELECT value FROM defaults
  457. WHERE setting_key = 'version'|;
  458. my $sth = $dbh->prepare($query);
  459. # no error check, let it fall through
  460. $sth->execute;
  461. my $version = $sth->fetchrow_array;
  462. $sth->finish;
  463. next unless $version;
  464. $version = calc_version($version);
  465. my $dbversion = calc_version($form->{dbversion});
  466. foreach my $upgradescript (@upgradescripts) {
  467. my $a = $upgradescript;
  468. $a =~ s/(^$form->{dbdriver}-upgrade-|\.sql$)//g;
  469. my ($mindb, $maxdb) = split /-/, $a;
  470. $mindb = calc_version($mindb);
  471. $maxdb = calc_version($maxdb);
  472. next if ($version >= $maxdb);
  473. # exit if there is no upgrade script or version == mindb
  474. last if ($version < $mindb || $version >= $dbversion);
  475. # apply upgrade
  476. $self->process_query($form, $dbh, "sql/$upgradescript");
  477. $dbh->commit;
  478. $version = $maxdb;
  479. }
  480. $rc = 0;
  481. $dbh->disconnect;
  482. }
  483. $rc;
  484. }
  485. sub calc_version {
  486. my @v = split /\./, $_[0];
  487. my $version = 0;
  488. my $i;
  489. for ($i = 0; $i <= $#v; $i++) {
  490. $version *= 1000;
  491. $version += $v[$i];
  492. }
  493. return $version;
  494. }
  495. sub script_version {
  496. my ($my_a, $my_b) = ($a, $b);
  497. my ($a_from, $a_to, $b_from, $b_to);
  498. my ($res_a, $res_b, $i);
  499. $my_a =~ s/.*-upgrade-//;
  500. $my_a =~ s/.sql$//;
  501. $my_b =~ s/.*-upgrade-//;
  502. $my_b =~ s/.sql$//;
  503. ($a_from, $a_to) = split(/-/, $my_a);
  504. ($b_from, $b_to) = split(/-/, $my_b);
  505. $res_a = calc_version($a_from);
  506. $res_b = calc_version($b_from);
  507. if ($res_a == $res_b) {
  508. $res_a = calc_version($a_to);
  509. $res_b = calc_version($b_to);
  510. }
  511. return $res_a <=> $res_b;
  512. }
  513. sub save_member {
  514. my ($self) = @_;
  515. # replace \r\n with \n
  516. for (qw(address signature)) { $self->{$_} =~ s/\r?\n/\\n/g }
  517. # use central db
  518. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  519. #check to see if the user exists already
  520. my $userCheck = $dbh->prepare("SELECT id FROM users WHERE username = ?");
  521. $userCheck->execute($self->{login});
  522. my ($userID) = $userCheck->fetchrow_array;
  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{HTTP_USER_AGENT}) {
  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;