Auth Konstanten ausgelagert in ein eigenes Package.
[kivitendo-erp.git] / SL / Auth / DB.pm
1 package SL::Auth::DB;
2
3 use SL::Auth::Constants qw(:all);
4 use SL::DBUtils;
5
6 use strict;
7
8 sub new {
9   $main::lxdebug->enter_sub();
10
11   my $type = shift;
12   my $self = {};
13
14   $self->{auth} = shift;
15
16   bless $self, $type;
17
18   $main::lxdebug->leave_sub();
19
20   return $self;
21 }
22
23 sub authenticate {
24   $main::lxdebug->enter_sub();
25
26   my $self       = shift;
27   my $login      = shift;
28   my $password   = shift;
29   my $is_crypted = shift;
30
31   my $dbh        = $self->{auth}->dbconnect();
32
33   if (!$dbh) {
34     $main::lxdebug->leave_sub();
35     return ERR_BACKEND;
36   }
37
38   my $query             = qq|SELECT password FROM auth."user" WHERE login = ?|;
39   my ($stored_password) = $dbh->selectrow_array($query, undef, $login);
40
41   $password        = crypt $password, substr($login, 0, 2)        if (!$password || !$is_crypted);
42   $stored_password = crypt $stored_password, substr($login, 0, 2) if (!$stored_password);
43
44   $main::lxdebug->leave_sub();
45
46   return $password eq $stored_password ? OK : ERR_PASSWORD;
47 }
48
49 sub can_change_password {
50   return 1;
51 }
52
53 sub change_password {
54   $main::lxdebug->enter_sub();
55
56   my $self       = shift;
57   my $login      = shift;
58   my $password   = shift;
59   my $is_crypted = shift;
60
61   my $dbh        = $self->{auth}->dbconnect();
62
63   if (!$dbh) {
64     $main::lxdebug->leave_sub();
65     return ERR_BACKEND;
66   }
67
68   $password = crypt $password, substr($login, 0, 2) if (!$is_crypted);
69
70   do_query($main::form, $dbh, qq|UPDATE auth."user" SET password = ? WHERE login = ?|, $password, $login);
71
72   $dbh->commit();
73
74   $main::lxdebug->leave_sub();
75
76   return 1;
77 }
78
79 sub verify_config {
80   return 1;
81 }
82
83 1;