1 | package LATMOS::Accounts::Bases::Sql; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | |
---|
7 | use base qw(LATMOS::Accounts::Bases); |
---|
8 | use LATMOS::Accounts::Log; |
---|
9 | use DBI; |
---|
10 | |
---|
11 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
12 | |
---|
13 | =head1 NAME |
---|
14 | |
---|
15 | LATMOS::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 | |
---|
25 | Account base access over standard unix file format. |
---|
26 | |
---|
27 | =head1 FUNCTIONS |
---|
28 | |
---|
29 | =cut |
---|
30 | |
---|
31 | =head2 new(%options) |
---|
32 | |
---|
33 | Create a new LATMOS::Ad object for windows AD $domain. |
---|
34 | |
---|
35 | domain / server: either the Ad domain or directly the server |
---|
36 | |
---|
37 | ldap_args is an optionnal list of arguments to pass to L<Net::LDAP>. |
---|
38 | |
---|
39 | =cut |
---|
40 | |
---|
41 | sub new { |
---|
42 | my ($class, %options) = @_; |
---|
43 | |
---|
44 | my $base = { |
---|
45 | db_conn => $options{db_conn}, |
---|
46 | }; |
---|
47 | |
---|
48 | bless($base, $class); |
---|
49 | } |
---|
50 | |
---|
51 | sub DESTROY { |
---|
52 | my ($self) = @_; |
---|
53 | $self->{_db} && $self->{_db}->rollback; |
---|
54 | } |
---|
55 | |
---|
56 | sub 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 SESSION CHARACTERISTICS AS TRANSACTION |
---|
76 | ISOLATION LEVEL SERIALIZABLE)); |
---|
77 | $self->{_db}->do(q{set DATESTYLE to 'DMY'}); |
---|
78 | $self->log(LA_DEBUG, 'New connection to DB'); |
---|
79 | return $self->{_db}; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | =head2 load |
---|
84 | |
---|
85 | Read file and load data into memory |
---|
86 | |
---|
87 | =cut |
---|
88 | |
---|
89 | sub load { |
---|
90 | my ($self) = @_; |
---|
91 | return $self->db ? 1 : 0; |
---|
92 | } |
---|
93 | |
---|
94 | sub commit { |
---|
95 | my ($self) = @_; |
---|
96 | if ($ENV{LA_NO_COMMIT}) { |
---|
97 | $self->log(LA_DEBUG, 'DB::COMMIT (ignore due to LA_NO_COMMIT)'); |
---|
98 | return 1; |
---|
99 | } else { |
---|
100 | $self->log(LA_DEBUG, 'DB::COMMIT'); |
---|
101 | } |
---|
102 | $self->{__cache} = undef; |
---|
103 | $self->db->commit; |
---|
104 | } |
---|
105 | |
---|
106 | sub rollback { |
---|
107 | my ($self) = @_; |
---|
108 | if ($ENV{LA_NO_COMMIT}) { |
---|
109 | $self->log(LA_DEBUG, 'DB::ROLLBACK (ignore due to LA_NO_COMMIT)'); |
---|
110 | return 1 |
---|
111 | } else { |
---|
112 | $self->log(LA_DEBUG, 'DB::ROLLBACK'); |
---|
113 | } |
---|
114 | $self->{__cache} = undef; |
---|
115 | $self->db->rollback; |
---|
116 | } |
---|
117 | |
---|
118 | sub list_supported_objects { |
---|
119 | my ($self, @otype) = @_; |
---|
120 | $self->SUPER::list_supported_objects(qw(site), @otype); |
---|
121 | } |
---|
122 | |
---|
123 | sub current_rev { |
---|
124 | my ($self) = @_; |
---|
125 | my $sth = $self->db->prepare_cached( |
---|
126 | q{select max(rev) from revisions} |
---|
127 | ); |
---|
128 | $sth->execute; |
---|
129 | my $res = $sth->fetchrow_hashref; |
---|
130 | $sth->finish; |
---|
131 | return ($res->{max}); |
---|
132 | } |
---|
133 | |
---|
134 | sub get_global_value { |
---|
135 | my ($self, $varname) = @_; |
---|
136 | |
---|
137 | my $sth = $self->db->prepare_cached(q{ |
---|
138 | select val from settings where varname = ? |
---|
139 | }); |
---|
140 | $sth->execute($varname); |
---|
141 | my $res = $sth->fetchrow_hashref; |
---|
142 | $sth->finish; |
---|
143 | $res->{val} |
---|
144 | } |
---|
145 | |
---|
146 | sub set_global_value { |
---|
147 | my ($self, $varname, $value) = @_; |
---|
148 | my $sth = $self->db->prepare(q{ |
---|
149 | update settings set val = ? where varname = ? |
---|
150 | }); |
---|
151 | $sth->execute($value, $varname) == 0 and do { |
---|
152 | my $sth2 = $self->db->prepare(q{ |
---|
153 | insert into settings (val, varname) values (?,?) |
---|
154 | }); |
---|
155 | $sth2->execute($value, $varname); |
---|
156 | }; |
---|
157 | } |
---|
158 | |
---|
159 | sub find_next_expire_users { |
---|
160 | my ($self, $expire) = @_; |
---|
161 | |
---|
162 | my $sth= $self->db->prepare(q{ |
---|
163 | select name from "user" where |
---|
164 | expire < now() + ?::interval |
---|
165 | and expire > now() |
---|
166 | and expire is not null |
---|
167 | } . ($self->{wexported} ? '' : 'and exported = true') . q{ |
---|
168 | order by expire |
---|
169 | } |
---|
170 | ); |
---|
171 | $sth->execute($expire || '1 month'); |
---|
172 | my @users; |
---|
173 | while (my $res = $sth->fetchrow_hashref) { |
---|
174 | push(@users, $res->{name}); |
---|
175 | } |
---|
176 | @users |
---|
177 | } |
---|
178 | |
---|
179 | sub find_expired_users { |
---|
180 | my ($self, $expire) = @_; |
---|
181 | |
---|
182 | my $sth= $self->db->prepare(q{ |
---|
183 | select name from "user" where |
---|
184 | expire < now() - ?::interval |
---|
185 | and expire is not null |
---|
186 | } . ($self->{wexported} ? '' : 'and exported = true') . q{ |
---|
187 | order by expire |
---|
188 | } |
---|
189 | ); |
---|
190 | $sth->execute($expire || '1 second'); |
---|
191 | my @users; |
---|
192 | while (my $res = $sth->fetchrow_hashref) { |
---|
193 | push(@users, $res->{name}); |
---|
194 | } |
---|
195 | @users |
---|
196 | } |
---|
197 | |
---|
198 | 1; |
---|
199 | |
---|
200 | __END__ |
---|
201 | |
---|
202 | =head1 SEE ALSO |
---|
203 | |
---|
204 | =head1 AUTHOR |
---|
205 | |
---|
206 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
207 | |
---|
208 | =head1 COPYRIGHT AND LICENSE |
---|
209 | |
---|
210 | Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS |
---|
211 | |
---|
212 | This library is free software; you can redistribute it and/or modify |
---|
213 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
214 | at your option, any later version of Perl 5 you may have available. |
---|
215 | |
---|
216 | |
---|
217 | =cut |
---|