summaryrefslogtreecommitdiff
path: root/scripts/login.pl
blob: 537d8b617295c97d33eb149d9a74a520b644319b (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. # this is kind of silly, as it doesn't check if someone IS trying to log in.
  9. # If one looks at the login template (get_password.html), it does not post
  10. # to any action, so this code will always get called, thereby preventing
  11. # anyone from actually logging in.
  12. sub __default {
  13. my ($request) = @_;
  14. my $locale;
  15. $locale = LedgerSMB::Locale->get_handle(${LedgerSMB::Sysconfig::language})
  16. or $request->error( __FILE__ . ':' . __LINE__ .
  17. ": Locale not loaded: $!\n" );
  18. my $template = LedgerSMB::Template->new(
  19. user =>$request->{_user},
  20. locale => $locale,
  21. path => 'UI',
  22. template => 'login',
  23. format => 'HTML'
  24. );
  25. $template->render($request);
  26. }
  27. # Directly printing like this is made of fail.
  28. sub authenticate {
  29. my ($request) = @_;
  30. if (!$request->{dbh}){
  31. if (!$request->{company}){
  32. $request->{company} = $LedgerSMB::Sysconfig::default_db;
  33. }
  34. $request->_db_init;
  35. }
  36. my $path = $ENV{SCRIPT_NAME};
  37. $path =~ s|[^/]*$||;
  38. if ($request->{dbh} && $request->{next}) {
  39. print "Content-Type: text/html\n";
  40. print "Set-Cookie: LedgerSMB=Login; path=$path\n";
  41. print "Status: 302 Found\n";
  42. print "Location: ".$path.$request->{next}."\n";
  43. print "\n";
  44. exit;
  45. }
  46. elsif ($request->{dbh} || $request->{log_out}){
  47. print "Content-Type: text/html\n";
  48. print "Set-Cookie: LedgerSMB=Login; path=$path\n";
  49. print "Status: 200 Success\n\n";
  50. if ($request->{log_out}){
  51. exit;
  52. }
  53. }
  54. else {
  55. print "WWW-Authenticate: Basic realm=\"LedgerSMB\"\n";
  56. print "Status: 401 Unauthorized\n\n";
  57. print "Please enter your credentials.\n";
  58. exit;
  59. }
  60. }
  61. sub login {
  62. my ($request) = @_;
  63. if (!$request->{_user}){
  64. __default($request);
  65. }
  66. require "scripts/menu.pl";
  67. LedgerSMB::Scripts::menu::root_doc($request);
  68. }
  69. sub logout {
  70. my ($request) = @_;
  71. $request->{callback} = "";
  72. $request->{endsession} = 1;
  73. LedgerSMB::Auth::session_destroy($request);
  74. print "Location: login.pl\n";
  75. print "Content-type: text/html\n\n";
  76. exit;
  77. }
  78. sub continue {
  79. my ($request) = @_;
  80. if ($request->{next} && $request->{password}) {
  81. $request->{user} = "admin";
  82. if (&authenticate($request)) {
  83. # LedgerSMB::Handler::call_script();
  84. }
  85. }
  86. else {
  87. # well, wtf? This is kind of useless.
  88. $request->error("Cannot continue to a Nonexistent page.");
  89. }
  90. }
  91. 1;