/doc/todo/git_attribution/

o'/>
summaryrefslogtreecommitdiff
path: root/LedgerSMB/User.pm
blob: 7ef2554724c754461a6b758d013f1b4dc8d886a1 (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(
  45. "SELECT acs, address, businessnumber,
  46. company, countrycode, currency,
  47. dateformat, dbdriver, dbhost, dbname,
  48. dboptions, dbpasswd, dbport, dbuser,
  49. email, fax, menuwidth, name, numberformat,
  50. password, print, printer, role, sid,
  51. signature, stylesheet, tel, templates,
  52. timeout, vclimit, u.username
  53. FROM users_conf as uc, users as u
  54. WHERE u.username = ?
  55. AND u.id = uc.id;"
  56. );
  57. $fetchUserPrefs->execute($login);
  58. my $userHashRef = $fetchUserPrefs->fetchrow_hashref;
  59. while ( my ( $key, $value ) = each( %{$userHashRef} ) ) {
  60. $self->{$key} = $value;
  61. }
  62. chomp( $self->{dbport} );
  63. chomp( $self->{dbname} );
  64. chomp( $self->{dbhost} );
  65. $self->{dbconnect} =
  66. 'dbi:Pg:dbname='
  67. . $self->{dbname}
  68. . ';host='
  69. . $self->{dbhost}
  70. . ';port='
  71. . $self->{dbport};
  72. if ( $self->{username} ) {
  73. $self->{login} = $login;
  74. }
  75. }
  76. bless $self, $type;
  77. }
  78. sub country_codes {
  79. use Locale::Country;
  80. use Locale::Language;
  81. my %cc = ();
  82. # scan the locale directory and read in the LANGUAGE files
  83. opendir DIR, "${LedgerSMB::Sysconfig::localepath}";
  84. my @dir = grep !/^\..*$/, readdir DIR;
  85. foreach my $dir (@dir) {
  86. $dir = substr( $dir, 0, -3 );
  87. $cc{$dir} = code2language( substr( $dir, 0, 2 ) );
  88. $cc{$dir} .= ( "/" . code2country( substr( $dir, 3, 2 ) ) )
  89. if length($dir) > 2;
  90. $cc{$dir} .= ( " " . substr( $dir, 6 ) ) if length($dir) > 5;
  91. }
  92. closedir(DIR);
  93. %cc;
  94. }
  95. sub fetch_config {
  96. #I'm hoping that this function will go and is a temporary bridge
  97. #until we get rid of %myconfig elsewhere in the code
  98. my ( $self, $login ) = @_;
  99. if ( !$login ) {
  100. &error( $self, "Access Denied" );
  101. }
  102. # use central db
  103. my $dbh = ${LedgerSMB::Sysconfig::GLOBALDBH};
  104. # for now, this is querying the table directly... ugly
  105. my $fetchUserPrefs = $dbh->prepare(
  106. "SELECT acs, address, businessnumber,
  107. company, countrycode, currency,
  108. dateformat, dbdriver, dbhost, dbname,
  109. dboptions, dbpasswd, dbport, dbuser,
  110. email, fax, menuwidth, name, numberformat,
  111. password, print, printer, role, sid,
  112. signature, stylesheet, tel, templates,
  113. timeout, vclimit, u.username
  114. FROM users_conf as uc, users as u
  115. WHERE u.username = ?
  116. AND u.id = uc.id;"
  117. );
  118. $fetchUserPrefs->execute($login);
  119. my $userHashRef = $fetchUserPrefs->fetchrow_hashref;
  120. if ( !$userHashRef ) {
  121. &error( $self, "Access Denied" );
  122. }
  123. while ( my ( $key, $value ) = each( %{$userHashRef} ) ) {
  124. $myconfig{$key} = $value;
  125. }
  126. chomp( $myconfig{'dbport'} );
  127. chomp( $myconfig{'dbname'} );
  128. chomp( $myconfig{'dbhost'} );
  129. $myconfig{'login'} = $login;
  130. $myconfig{'dbconnect'} =
  131. 'dbi:Pg:dbname='
  132. . $myconfig{'dbname'}
  133. . ';host='
  134. . $myconfig{'dbhost'}
  135. . ';port='
  136. . $myconfig{'dbport'};
  137. return \%myconfig;
  138. }
  139. sub login {
  140. my ( $self, $form ) = @_;
  141. my $rc = -1;
  142. if ( $self->{login} ne "" ) {
  143. if (
  144. !Session::password_check(
  145. $form, $form->{login}, $form->{password}
  146. )
  147. )
  148. {
  149. return -1;
  150. }
  151. #this is really dumb, but %myconfig will have to stay until 1.3
  152. while ( my ( $key, $value ) = each( %{$self} ) ) {
  153. $myconfig{$key} = $value;
  154. }
  155. # check if database is down
  156. my $dbh =
  157. DBI->connect( $myconfig{dbconnect}, $myconfig{dbuser},
  158. $myconfig{dbpasswd} )
  159. or $self->error( __FILE__ . ':' . __LINE__ . ': ' . $DBI::errstr );
  160. $dbh->{pg_enable_utf8} = 1;
  161. # we got a connection, check the version
  162. my $query = qq|
  163. SELECT value FROM defaults
  164. WHERE setting_key = 'version'|;
  165. my $sth = $dbh->prepare($query);
  166. $sth->execute || $form->dberror( __FILE__ . ':' . __LINE__ . $query );
  167. my ($dbversion) = $sth->fetchrow_array;
  168. $sth->finish;
  169. # add login to employee table if it does not exist
  170. # no error check for employee table, ignore if it does not exist
  171. my $login = $self->{login};
  172. $login =~ s/@.*//;
  173. $query = qq|SELECT id FROM employee WHERE login = ?|;
  174. $sth = $dbh->prepare($query);
  175. $sth->execute($login);
  176. my ($id) = $sth->fetchrow_array;
  177. $sth->finish;
  178. if ( !$id ) {
  179. my ($employeenumber) =
  180. $form->update_defaults( \%myconfig, "employeenumber", $dbh );
  181. $query = qq|
  182. INSERT INTO employee
  183. (login, employeenumber, name,
  184. workphone, role)
  185. VALUES (?, ?, ?, ?, ?)|;
  186. $sth = $dbh->prepare($query);
  187. $sth->execute(
  188. $login, $employeenumber, $myconfig{name},
  189. $myconfig{tel}, $myconfig{role}
  190. );
  191. }
  192. $dbh->disconnect;
  193. $rc = 0;
  194. if ( $form->{dbversion} ne $dbversion ) {
  195. $rc = -3;
  196. $dbupdate =
  197. ( calc_version($dbversion) < calc_version( $form->{dbversion} ) );
  198. }
  199. if ($dbupdate) {
  200. $rc = -4;
  201. # if DB2 bale out
  202. if ( $myconfig{dbdriver} eq 'DB2' ) {
  203. $rc = -2;
  204. }
  205. }
  206. }
  207. $rc;
  208. }
  209. sub check_recurring {
  210. my ( $self, $form ) = @_;
  211. my $dbh =
  212. DBI->connect( $self->{dbconnect}, $self->{dbuser}, $self->{dbpasswd} )
  213. or $form->dberror( __FILE__ . ':' . __LINE__ );
  214. $dbh->{pg_enable_utf8} = 1;
  215. my $query = qq|
  216. SELECT count(*) FROM recurring
  217. WHERE enddate >= current_date AND nextdate <= current_date|;
  218. ($_) = $dbh->selectrow_array($query);
  219. $dbh->disconnect;
  220. $_;
  221. }
  222. sub dbconnect_vars {
  223. my ( $form, $db ) = @_;
  224. my %dboptions = (
  225. 'Pg' => {
  226. 'yy-mm-dd' => 'set DateStyle to \'ISO\'',
  227. 'mm/dd/yy' => 'set DateStyle to \'SQL, US\'',
  228. 'mm-dd-yy' => 'set DateStyle to \'POSTGRES, US\'',
  229. 'dd/mm/yy' => 'set DateStyle to \'SQL, EUROPEAN\'',
  230. 'dd-mm-yy' => 'set DateStyle to \'POSTGRES, EUROPEAN\'',
  231. 'dd.mm.yy' => 'set DateStyle to \'GERMAN\''
  232. }
  233. );
  234. $form->{dboptions} = $dboptions{ $form->{dbdriver} }{ $form->{dateformat} };
  235. $form->{dbconnect} = "dbi:$form->{dbdriver}:dbname=$db";
  236. $form->{dbconnect} .= ";host=$form->{dbhost}";
  237. $form->{dbconnect} .= ";port=$form->{dbport}";
  238. }
  239. sub dbdrivers {
  240. my @drivers = DBI->available_drivers();
  241. # return (grep { /(Pg|Oracle|DB2)/ } @drivers);
  242. return ( grep { /Pg$/ } @drivers );
  243. }
  244. sub dbsources {
  245. my ( $self, $form ) = @_;
  246. my @dbsources = ();
  247. my ( $sth, $query );
  248. $form->{dbdefault} = $form->{dbuser} unless $form->{dbdefault};
  249. $form->{sid} = $form->{dbdefault};
  250. &dbconnect_vars( $form, $form->{dbdefault} );
  251. my $dbh =
  252. DBI->connect( $form->{dbconnect}, $form->{dbuser}, $form->{dbpasswd} )
  253. or $form->dberror( __FILE__ . ':' . __LINE__ );
  254. $dbh->{pg_enable_utf8} = 1;
  255. if ( $form->{dbdriver} eq 'Pg' ) {
  256. $query = qq|SELECT datname FROM pg_database|;
  257. $sth = $dbh->prepare($query);
  258. $sth->execute || $form->dberror( __FILE__ . ':' . __LINE__ . $query );
  259. while ( my ($db) = $sth->fetchrow_array ) {
  260. if ( $form->{only_acc_db} ) {
  261. next if ( $db =~ /^template/ );
  262. &dbconnect_vars( $form, $db );
  263. my $dbh =
  264. DBI->connect( $form->{dbconnect}, $form->{dbuser},
  265. $form->{dbpasswd} )
  266. or $form->dberror( __FILE__ . ':' . __LINE__ );
  267. $dbh->{pg_enable_utf8} = 1;
  268. $query = qq|
  269. SELECT tablename FROM pg_tables
  270. WHERE tablename = 'defaults'
  271. AND tableowner = ?|;
  272. my $sth = $dbh->prepare($query);
  273. $sth->execute( $form->{dbuser} )
  274. || $form->dberror( __FILE__ . ':' . __LINE__ . $query );
  275. if ( $sth->fetchrow_array ) {
  276. push @dbsources, $db;
  277. }
  278. $sth->finish;
  279. $dbh->disconnect;
  280. next;
  281. }
  282. push @dbsources, $db;
  283. }
  284. }
  285. $sth->finish;
  286. $dbh->disconnect;
  287. return @dbsources;
  288. }
  289. sub dbcreate {
  290. my ( $self, $form ) = @_;
  291. my %dbcreate =