ShopOrder: billing_email zusätzlich als invoice_mail ...
[kivitendo-erp.git] / SL / Auth / DB.pm
1 package SL::Auth::DB;
2
3 use strict;
4
5 use Carp;
6 use Scalar::Util qw(weaken);
7
8 use SL::Auth::Constants qw(:all);
9 use SL::Auth::Password;
10 use SL::DBUtils;
11
12 sub new {
13   $main::lxdebug->enter_sub();
14
15   my $type = shift;
16   my $self = {};
17
18   $self->{auth} = shift;
19   weaken $self->{auth};
20
21   bless $self, $type;
22
23   $main::lxdebug->leave_sub();
24
25   return $self;
26 }
27
28 sub reset {
29   # nothing to do here
30 }
31
32 sub authenticate {
33   $main::lxdebug->enter_sub();
34
35   my $self       = shift;
36   my $login      = shift;
37   my $password   = shift;
38
39   my $stored_password = $self->{auth}->get_stored_password($login);
40
41   # Empty password hashes in the database mean just that -- empty
42   # passwords. Hash it for easier comparison.
43   $stored_password    = SL::Auth::Password->hash(password => $stored_password) unless $stored_password;
44   my ($algorithm)     = SL::Auth::Password->parse($stored_password);
45   my $hashed_password = SL::Auth::Password->hash(password => $password, algorithm => $algorithm, login => $login, stored_password => $stored_password);
46
47   $main::lxdebug->leave_sub();
48
49   return $hashed_password eq $stored_password ? OK : ERR_PASSWORD;
50 }
51
52 sub can_change_password {
53   return 1;
54 }
55
56 sub requires_cleartext_password {
57   return 0;
58 }
59
60 sub change_password {
61   $main::lxdebug->enter_sub();
62
63   my $self       = shift;
64   my $login      = shift;
65   my $password   = shift;
66   my $is_crypted = shift;
67
68   my $dbh        = $self->{auth}->dbconnect();
69
70   if (!$dbh) {
71     $main::lxdebug->leave_sub();
72     return ERR_BACKEND;
73   }
74
75   $password = SL::Auth::Password->hash(login => $login, password => $password) unless $is_crypted;
76
77   do_query($main::form, $dbh, qq|UPDATE auth."user" SET password = ? WHERE login = ?|, $password, $login);
78
79   $dbh->commit();
80
81   $main::lxdebug->leave_sub();
82
83   return 1;
84 }
85
86 sub verify_config {
87   return 1;
88 }
89
90 1;