summaryrefslogtreecommitdiff
path: root/scripts/login.pl
blob: 1ce79587bdd3b0859735716fb6c5759cb43ee11e (plain)
  1. package LedgerSMB::Scripts::login;
  2. our $VERSION = 1.0;
  3. use LedgerSMB::Locale;
  4. use LedgerSMB::Form; # Required for now to integrate with menu module.
  5. use LedgerSMB::User;
  6. use LedgerSMB::Auth;
  7. use strict;
  8. sub __default {
  9. my ($request) = @_;
  10. my $locale;
  11. $locale = LedgerSMB::Locale->get_handle(${LedgerSMB::Sysconfig::language})
  12. or $request->error( __FILE__ . ':' . __LINE__ .
  13. ": Locale not loaded: $!\n" );
  14. my $template = LedgerSMB::Template->new(
  15. user =>$request->{_user},
  16. locale => $locale,
  17. path => 'UI',
  18. template => 'login',
  19. format => 'HTML'
  20. );
  21. $template->render($request);
  22. }
  23. sub authenticate {
  24. my ($request) = @_;
  25. if (!$request->{dbh}){
  26. if (!$request->{company}){
  27. $request->{company} = $LedgerSMB::Sysconfig::default_db;
  28. }
  29. $request->_db_init;
  30. }
  31. my $path = $ENV{SCRIPT_NAME};
  32. $path =~ s|[^/]*$||;
  33. if ($request->{dbh} || $request->{log_out}){
  34. print "Content-Type: text/html\n";
  35. print "Set-Cookie: LedgerSMB=Login; path=$path\n";
  36. print "Status: 200 Success\n\n";
  37. if ($request->{log_out}){
  38. exit;
  39. }
  40. }
  41. else {
  42. print "WWW-Authenticate: Basic realm=\"LedgerSMB\"\n";
  43. print "Status: 401 Unauthorized\n\n";
  44. print "Please enter your credentials.\n";
  45. exit;
  46. }
  47. }
  48. sub login {
  49. my ($request) = @_;
  50. if (!$request->{_user}){
  51. __default($request);
  52. }
  53. require "scripts/menu.pl";
  54. LedgerSMB::Scripts::menu::root_doc($request);
  55. }
  56. sub logout {
  57. my ($request) = @_;
  58. $request->{callback} = "";
  59. $request->{endsession} = 1;
  60. LedgerSMB::Auth::session_destroy($request);
  61. print "Location: login.pl\n";
  62. print "Content-type: text/html\n\n";
  63. exit;
  64. }
  65. 1;