source: LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql.pm @ 413

Last change on this file since 413 was 413, checked in by nanardon, 15 years ago

In SQL base, allow to have a peer of public/private RSA key, then when user set it password it is stored in it clear form but encrypt using public key
The la-encrypt-passwd tools allow to

  • generate or regenerate a new key
  • read password
  • read password to propagate it into another base

Notice the private key is protected by a passwphrase prompt when setting it

  • Property svn:keywords set to Id Rev
File size: 3.5 KB
Line 
1package LATMOS::Accounts::Bases::Sql;
2
3use 5.010000;
4use strict;
5use warnings;
6
7use base qw(LATMOS::Accounts::Bases);
8use LATMOS::Accounts::Log;
9use DBI;
10
11our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
12
13=head1 NAME
14
15LATMOS::Ad - Perl extension for blah blah blah
16
17=head1 SYNOPSIS
18
19  use LATMOS::Accounts::Bases;
20  my $base = LATMOS::Accounts::Bases->new('unix');
21  ...
22
23=head1 DESCRIPTION
24
25Account base access over standard unix file format.
26
27=head1 FUNCTIONS
28
29=cut
30
31=head2 new(%options)
32
33Create a new LATMOS::Ad object for windows AD $domain.
34
35domain / server: either the Ad domain or directly the server
36
37ldap_args is an optionnal list of arguments to pass to L<Net::LDAP>.
38
39=cut
40
41sub new {
42    my ($class, %options) = @_;
43   
44    my $base = {
45        db_conn => $options{db_conn},
46    };
47
48    bless($base, $class);
49}
50
51sub DESTROY {
52    my ($self) = @_;
53    $self->{_db} && $self->{_db}->rollback;
54}
55
56sub db {
57    my ($self) = @_;
58
59    if ($self->{_db} && $self->{_db}->ping) {
60        return $self->{_db};
61    } else {
62        $self->{_db} = DBI->connect_cached(
63            'dbi:Pg:' . $self->{db_conn},
64            undef, undef,
65            {
66                RaiseError => 1,
67                AutoCommit => 0,
68                PrintWarn => 1,
69                PrintError => 1,
70            }
71        ) or do {
72            $self->log(LA_ERR, "Cannot connect to database");   
73            return;
74        };
75        $self->{_db}->do(q{set DATESTYLE to 'DMY'});
76        $self->log(LA_DEBUG, 'New connection to DB');
77        return $self->{_db};
78    }
79}
80
81=head2 load
82
83Read file and load data into memory
84
85=cut
86
87sub load {
88    my ($self) = @_;
89    return $self->db ? 1 : 0;   
90}
91
92sub commit {
93    my ($self) = @_;
94    if ($ENV{LA_NO_COMMIT}) {
95        $self->log(LA_DEBUG, 'DB::COMMIT (ignore due to LA_NO_COMMIT)');
96        return 1;
97    } else {
98        $self->log(LA_DEBUG, 'DB::COMMIT');
99    }
100    $self->db->commit;
101}
102
103sub rollback {
104    my ($self) = @_;
105    if ($ENV{LA_NO_COMMIT}) {
106        $self->log(LA_DEBUG, 'DB::ROLLBACK (ignore due to LA_NO_COMMIT)');
107        return 1
108    } else {
109        $self->log(LA_DEBUG, 'DB::ROLLBACK');
110    }
111    $self->db->rollback;
112}
113
114sub list_supported_objects {
115    my ($self, @otype) = @_;
116    $self->SUPER::list_supported_objects(qw(site), @otype);
117}
118
119sub current_rev {
120    my ($self) = @_;
121    my $sth = $self->db->prepare_cached(
122        q{select max(rev) from revisions}
123    );
124    $sth->execute;
125    my $res = $sth->fetchrow_hashref;
126    $sth->finish;
127    return ($res->{max});
128} 
129
130sub get_global_value {
131    my ($self, $varname) = @_;
132
133    my $sth = $self->db->prepare_cached(q{
134        select val from settings where varname = ?
135        });
136    $sth->execute($varname);
137    my $res = $sth->fetchrow_hashref;
138    $sth->finish;
139    $res->{val}
140}
141
142sub set_global_value {
143    my ($self, $varname, $value) = @_;
144    my $sth = $self->db->prepare(q{
145        update settings set val = ? where varname = ?
146        });
147    $sth->execute($value, $varname) == 0 and do {
148        my $sth2 = $self->db->prepare(q{
149            insert into settings (val, varname) values (?,?)
150            });
151        $sth2->execute($value, $varname);
152    };
153}
154
1551;
156
157__END__
158
159=head1 SEE ALSO
160
161=head1 AUTHOR
162
163Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
164
165=head1 COPYRIGHT AND LICENSE
166
167Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS
168
169This library is free software; you can redistribute it and/or modify
170it under the same terms as Perl itself, either Perl version 5.10.0 or,
171at your option, any later version of Perl 5 you may have available.
172
173
174=cut
Note: See TracBrowser for help on using the repository browser.