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