Attention ! Gitlab fournissant maintenant nativement des certificats Let’s Encrypt aux domaines personnalisés des Gitlab Pages, nous avons coupé notre service qui le faisait automatiquement pour vous.

Il est impératif, pour que votre domaine personnalisé continue à avoir un certificat Let’s Encrypt à jour, d’activer la fonctionnalité native dans les paramètres de votre projet. Cette activation remplacera votre certificat actuel par un nouveau certificat Let’s Encrypt géré par Gitlab.

Voir les détails sur https://docs.framasoft.org/fr/gitlab/gitlab-pages-le.html

Verified Commit 8be7cd2d authored by Luc Didry's avatar Luc Didry

Refactor DB abstraction layer

parent d30cb602
Pipeline #73132 passed with stages
in 6 minutes and 59 seconds
......@@ -42,9 +42,9 @@ Have a look at Lstu::DB::Ban::SQLite's code: it's simple and may be more underst
=item B<Arguments> : any of the attribute above
=item B<Purpose> : construct a new db accessor object. If the C<ip> attribute is provided, it have to load the informations from the database
=item B<Purpose> : construct a new Lstu::DB::Ban object. If the C<ip> attribute is provided, it have to load the informations from the database
=item B<Returns> : the db accessor object
=item B<Returns> : the Lstu::DB::Ban object
=item B<Info> : the app argument is used by Lstu::DB::Ban to choose which db accessor will be used, you don't need to use it in new(), but you can use it to access helpers or configuration settings in the other subroutines
......@@ -142,24 +142,51 @@ sub is_blacklisted {
eg: C<WHERE ip = ? AND until E<gt> ? AND strike E<gt>= ?', $c->ip, time, $argument>
=item B<Returns> : the db accessor object if the ip is banned, undef otherwise
=item B<Returns> : the Lstu::DB::Ban object if the ip is banned, undef otherwise
=item B<Info> : if the IP is whitelisted (see C<is_whitelisted> above), it must return undef
=back
=cut
sub is_banned {
my $c = shift;
my $ban_min_strike = shift;
return undef if $c->is_whitelisted;
if ($c->is_blacklisted) {
$c->until(time + 3600);
$c->strike(1 + $c->app->config('ban_min_strike'));
return $c;
}
my $h = $c->app->dbi->db->query('SELECT * FROM ban WHERE ip = ? AND until > ? AND strike >= ?', $c->ip, time, $ban_min_strike)->hashes;
if ($h->size) {
$c->until($h->first->{until});
$c->strike($h->first->{strike});
$c->record(1);
return $c;
} else {
return undef;
}
}
=head2 increment_ban_delay
=over 1
=item B<Usage> : C<$c-E<gt>increment_ban_delay(3600)>
=item B<Arguments> : an integer. This number is a penalty (in second) that will be added to the C<until> attribute of the db accessor object
=item B<Arguments> : an integer. This number is a penalty (in second) that will be added to the C<until> attribute of the Lstu::DB::Ban object
=item B<Purpose> : add penalty to the C<until> attribute of the db accessor object, increment the C<strike> attribute by one and write the db accessor object's attribute to the database.
=item B<Purpose> : add penalty to the C<until> attribute of the Lstu::DB::Ban object, increment the C<strike> attribute by one and write the Lstu::DB::Ban object's attribute to the database.
Update the database record if one already exists, create one otherwise.
=item B<Returns> : the db accessor object
=item B<Returns> : the Lstu::DB::Ban object
=back
......@@ -179,6 +206,14 @@ eg: C<WHERE until E<lt> ?, time>
=back
=cut
sub clear {
my $c = shift;
$c->app->dbi->db->query('DELETE FROM ban WHERE until < ?', time);
}
=head2 delete_all
=over 1
......@@ -195,4 +230,39 @@ eg: C<WHERE until E<lt> ?, time>
=cut
sub delete_all {
my $c = shift;
$c->app->dbi->db->query('DELETE FROM ban');
}
=head2 _slurp
=over 1
=item B<Usage> : C<$c-E<gt>_slurp>
=item B<Arguments> : none
=item B<Purpose> : put a database record's columns into the Lstu::DB::Ban object's attributes
=item B<Returns> : the Lstu::DB::Ban object
=back
=cut
sub _slurp {
my $c = shift;
my $h = $c->app->dbi->db->query('SELECT * FROM ban WHERE ip = ?', $c->ip)->hashes;
if ($h->size) {
$c->until($h->first->{until});
$c->strike($h->first->{strike});
$c->record(1);
}
return $c;
}
1;
......@@ -14,31 +14,6 @@ sub new {
return $c;
}
sub is_banned {
my $c = shift;
my $ban_min_strike = shift;
return undef if $c->is_whitelisted;
if ($c->is_blacklisted) {
$c->until(time + 3600);
$c->strike(1 + $c->app->config('ban_min_strike'));
return $c;
}
my $h = $c->app->mysql->db->query('SELECT * FROM ban WHERE ip = ? AND until > ? AND strike >= ?', $c->ip, time, $ban_min_strike)->hashes;
if ($h->size) {
$c->until($h->first->{until});
$c->strike($h->first->{strike});
$c->record(1);
return $c;
} else {
return undef;
}
}
sub increment_ban_delay {
my $c = shift;
my $penalty = shift;
......@@ -49,10 +24,10 @@ sub increment_ban_delay {
strike => 1
};
if ($c->record) {
$c->app->mysql->db->query('UPDATE ban SET until = ?, strike = strike + 1 WHERE ip = ?', $until, $c->ip);
$h = $c->app->mysql->db->query('SELECT strike FROM ban WHERE ip = ?', $c->ip)->hashes->first;
$c->app->dbi->db->query('UPDATE ban SET until = ?, strike = strike + 1 WHERE ip = ?', $until, $c->ip);
$h = $c->app->dbi->db->query('SELECT strike FROM ban WHERE ip = ?', $c->ip)->hashes->first;
} else {
$c->app->mysql->db->query('INSERT INTO ban (ip, until, strike) VALUES (?, ?, 1)', $c->ip, $until);
$c->app->dbi->db->query('INSERT INTO ban (ip, until, strike) VALUES (?, ?, 1)', $c->ip, $until);
$c->record(1);
}
......@@ -62,29 +37,4 @@ sub increment_ban_delay {
return $c;
}
sub clear {
my $c = shift;
$c->app->mysql->db->query('DELETE FROM ban WHERE until < ?', time);
}
sub delete_all {
my $c = shift;
$c->app->mysql->db->query('DELETE FROM ban');
}
sub _slurp {
my $c = shift;
my $h = $c->app->mysql->db->query('SELECT * FROM ban WHERE ip = ?', $c->ip)->hashes;
if ($h->size) {
$c->until($h->first->{until});
$c->strike($h->first->{strike});
$c->record(1);
}
return $c;
}
1;
......@@ -14,31 +14,6 @@ sub new {
return $c;
}
sub is_banned {
my $c = shift;
my $ban_min_strike = shift;
return undef if $c->is_whitelisted;
if ($c->is_blacklisted) {
$c->until(time + 3600);
$c->strike(1 + $c->app->config('ban_min_strike'));
return $c;
}
my $h = $c->app->pg->db->query('SELECT * FROM ban WHERE ip = ? AND until > ? AND strike >= ?', $c->ip, time, $ban_min_strike)->hashes;
if ($h->size) {
$c->until($h->first->{until});
$c->strike($h->first->{strike});
$c->record(1);
return $c;
} else {
return undef;
}
}
sub increment_ban_delay {
my $c = shift;
my $penalty = shift;
......@@ -49,9 +24,9 @@ sub increment_ban_delay {
strike => 1
};
if ($c->record) {
$h = $c->app->pg->db->query('UPDATE ban SET until = ?, strike = strike + 1 WHERE ip = ? RETURNING strike', $until, $c->ip)->hashes->first;
$h = $c->app->dbi->db->query('UPDATE ban SET until = ?, strike = strike + 1 WHERE ip = ? RETURNING strike', $until, $c->ip)->hashes->first;
} else {
$c->app->pg->db->query('INSERT INTO ban (ip, until, strike) VALUES (?, ?, 1)', $c->ip, $until);
$c->app->dbi->db->query('INSERT INTO ban (ip, until, strike) VALUES (?, ?, 1)', $c->ip, $until);
$c->record(1);
}
......@@ -61,29 +36,4 @@ sub increment_ban_delay {
return $c;
}
sub clear {
my $c = shift;
$c->app->pg->db->query('DELETE FROM ban WHERE until < ?', time);
}
sub delete_all {
my $c = shift;
$c->app->pg->db->query('DELETE FROM ban');
}
sub _slurp {
my $c = shift;
my $h = $c->app->pg->db->query('SELECT * FROM ban WHERE ip = ?', $c->ip)->hashes;
if ($h->size) {
$c->until($h->first->{until});
$c->strike($h->first->{strike});
$c->record(1);
}
return $c;
}
1;
......@@ -14,31 +14,6 @@ sub new {
return $c;
}
sub is_banned {
my $c = shift;
my $ban_min_strike = shift;
return undef if $c->is_whitelisted;
if ($c->is_blacklisted) {
$c->until(time + 3600);
$c->strike(1 + $c->app->config('ban_min_strike'));
return $c;
}
my $h = $c->app->sqlite->db->query('SELECT * FROM ban WHERE ip = ? AND until > ? AND strike >= ?', $c->ip, time, $ban_min_strike)->hashes;
if ($h->size) {
$c->until($h->first->{until});
$c->strike($h->first->{strike});
$c->record(1);
return $c;
} else {
return undef;
}
}
sub increment_ban_delay {
my $c = shift;
my $penalty = shift;
......@@ -49,10 +24,10 @@ sub increment_ban_delay {
strike => 1
};
if ($c->record) {
$c->app->sqlite->db->query('UPDATE ban SET until = ?, strike = strike + 1 WHERE ip = ?', $until, $c->ip)->hashes->first;
$h = $c->app->sqlite->db->query('SELECT strike FROM ban WHERE ip = ?', $c->ip)->hashes->first;
$c->app->dbi->db->query('UPDATE ban SET until = ?, strike = strike + 1 WHERE ip = ?', $until, $c->ip)->hashes->first;
$h = $c->app->dbi->db->query('SELECT strike FROM ban WHERE ip = ?', $c->ip)->hashes->first;
} else {
$c->app->sqlite->db->query('INSERT INTO ban (ip, until, strike) VALUES (?, ?, 1)', $c->ip, $until);
$c->app->dbi->db->query('INSERT INTO ban (ip, until, strike) VALUES (?, ?, 1)', $c->ip, $until);
$c->record(1);
}
......@@ -62,29 +37,4 @@ sub increment_ban_delay {
return $c;
}
sub clear {
my $c = shift;
$c->app->sqlite->db->query('DELETE FROM ban WHERE until < ?', time);
}
sub delete_all {
my $c = shift;
$c->app->sqlite->db->query('DELETE FROM ban');
}
sub _slurp {
my $c = shift;
my $h = $c->app->sqlite->db->query('SELECT * FROM ban WHERE ip = ?', $c->ip)->hashes;
if ($h->size) {
$c->until($h->first->{until});
$c->strike($h->first->{strike});
$c->record(1);
}
return $c;
}
1;
......@@ -39,9 +39,9 @@ Have a look at Lstu::DB::Session::SQLite's code: it's simple and may be more und
=item B<Arguments> : any of the attribute above
=item B<Purpose> : construct a new db accessor object. If the C<token> attribute is provided, it have to load the informations from the database
=item B<Purpose> : construct a new Lstu::DB::Session object. If the C<token> attribute is provided, it have to load the informations from the database
=item B<Returns> : the db accessor object
=item B<Returns> : the Lstu::DB::Session object
=item B<Info> : the app argument is used by Lstu::DB::Session to choose which db accessor will be used, you don't need to use it in new(), but you can use it to access helpers or configuration settings in the other subroutines
......@@ -97,10 +97,12 @@ sub to_hash {
=item B<Purpose> : delete the session record from the database
=item B<Returns> : the db accessor object
=item B<Returns> : the Lstu::DB::Session object
=back
=cut
=head2 write
=over 1
......@@ -111,10 +113,25 @@ sub to_hash {
=item B<Purpose> : create or update the object in the database
=item B<Returns> : the db accessor object
=item B<Returns> : the Lstu::DB::Session object
=back
=cut
sub write {
my $c = shift;
if ($c->record) {
$c->app->dbi->db->query('UPDATE sessions SET until = ? WHERE token = ?', $c->until, $c->token);
} else {
$c->app->dbi->db->query('INSERT INTO sessions (token, until) VALUES (?, ?)', $c->token, $c->until);
$c->record(1);
}
return $c;
}
=head2 clear
=over 1
......@@ -131,6 +148,14 @@ eg: C<WHERE until E<lt> ?, time>
=back
=cut
sub clear {
my $c = shift;
$c->app->dbi->db->query('DELETE FROM sessions WHERE until < ?', time);
}
=head2 delete_all
=over 1
......@@ -147,4 +172,39 @@ eg: C<WHERE until E<lt> ?, time>
=cut
sub delete_all {
my $c = shift;
$c->app->dbi->db->query('DELETE FROM sessions');
}
=head2 _slurp
=over 1
=item B<Usage> : C<$c-E<gt>_slurp>
=item B<Arguments> : none
=item B<Purpose> : put a database record's columns into the Lstu::DB::Session object's attributes
=item B<Returns> : the Lstu::DB::Session object
=back
=cut
sub _slurp {
my $c = shift;
my $h = $c->app->dbi->db->query('SELECT * FROM sessions WHERE token = ?', $c->token)->hashes;
if ($h->size) {
$c->token($h->first->{token});
$c->until($h->first->{until});
$c->record(1);
}
return $c;
}
1;
......@@ -17,8 +17,8 @@ sub new {
sub delete {
my $c = shift;
$c->app->mysql->db->query('DELETE FROM sessions WHERE token = ?', $c->token);
my $h = $c->app->mysql->db->query('SELECT * FROM sessions WHERE token = ?', $c->token)->hashes;
$c->app->dbi->db->query('DELETE FROM sessions WHERE token = ?', $c->token);
my $h = $c->app->dbi->db->query('SELECT * FROM sessions WHERE token = ?', $c->token)->hashes;
if ($h->size) {
# We found the session, it hasn't been deleted
return 0;
......@@ -29,42 +29,4 @@ sub delete {
}
}
sub write {
my $c = shift;
if ($c->record) {
$c->app->mysql->db->query('UPDATE sessions SET until = ? WHERE token = ?', $c->until, $c->token);
} else {
$c->app->mysql->db->query('INSERT INTO sessions (token, until) VALUES (?, ?)', $c->token, $c->until);
$c->record(1);
}
return $c;
}
sub clear {
my $c = shift;
$c->app->mysql->db->query('DELETE FROM sessions WHERE until < ?', time);
}
sub delete_all {
my $c = shift;
$c->app->mysql->db->query('DELETE FROM sessions');
}
sub _slurp {
my $c = shift;
my $h = $c->app->mysql->db->query('SELECT * FROM sessions WHERE token = ?', $c->token)->hashes;
if ($h->size) {
$c->token($h->first->{token});
$c->until($h->first->{until});
$c->record(1);
}
return $c;
}
1;
......@@ -17,7 +17,7 @@ sub new {
sub delete {
my $c = shift;
my $h = $c->app->pg->db->query('DELETE FROM sessions WHERE token = ? RETURNING *', $c->token)->hashes;
my $h = $c->app->dbi->db->query('DELETE FROM sessions WHERE token = ? RETURNING *', $c->token)->hashes;
if ($h->size) {
$c = Lstu::DB::Session->new(app => $c->app);
}
......@@ -25,42 +25,4 @@ sub delete {
return $h->size;
}
sub write {
my $c = shift;
if ($c->record) {
$c->app->pg->db->query('UPDATE sessions SET until = ? WHERE token = ?', $c->until, $c->token);
} else {
$c->app->pg->db->query('INSERT INTO sessions (token, until) VALUES (?, ?)', $c->token, $c->until);
$c->record(1);
}
return $c;
}
sub clear {
my $c = shift;
$c->app->pg->db->query('DELETE FROM sessions WHERE until < ?', time);
}
sub delete_all {
my $c = shift;
$c->app->pg->db->query('DELETE FROM sessions');
}
sub _slurp {
my $c = shift;
my $h = $c->app->pg->db->query('SELECT * FROM sessions WHERE token = ?', $c->token)->hashes;
if ($h->size) {
$c->token($h->first->{token});
$c->until($h->first->{until});
$c->record(1);
}
return $c;
}
1;
......@@ -17,8 +17,8 @@ sub new {
sub delete {
my $c = shift;
$c->app->sqlite->db->query('DELETE FROM sessions WHERE token = ?', $c->token);
my $h = $c->app->sqlite->db->query('SELECT * FROM sessions WHERE token = ?', $c->token)->hashes;
$c->app->dbi->db->query('DELETE FROM sessions WHERE token = ?', $c->token);
my $h = $c->app->dbi->db->query('SELECT * FROM sessions WHERE token = ?', $c->token)->hashes;
if ($h->size) {
# We found the session, it hasn't been deleted
return 0;
......@@ -29,42 +29,4 @@ sub delete {
}
}
sub write {
my $c = shift;
if ($c->record) {
$c->app->sqlite->db->query('UPDATE sessions SET until = ? WHERE token = ?', $c->until, $c->token);
} else {
$c->app->sqlite->db->query('INSERT INTO sessions (token, until) VALUES (?, ?)', $c->token, $c->until);
$c->record(1);
}
return $c;
}
sub clear {
my $c = shift;
$c->app->sqlite->db->query('DELETE FROM sessions WHERE until < ?', time);
}
sub delete_all {
my $c = shift;
$c->app->sqlite->db->query('DELETE FROM sessions');
}
sub _slurp {
my $c = shift;
my $h = $c->app->sqlite->db->query('SELECT * FROM sessions WHERE token = ?', $c->token)->hashes;
if ($h->size) {
$c->token($h->first->{token});
$c->until($h->first->{until});
$c->record(1);
}
return $c;
}
1;
......@@ -48,9 +48,9 @@ Have a look at Lstu::DB::URL::SQLite's code: it's simple and may be more underst
=item B<Arguments> : any of the attribute above
=item B<Purpose> : construct a new db accessor object. If the C<short> or the C<url> attribute is provided, it have to load the informations from the database. In the case of multiple records for the same C<url>, choose the first.
=item B<Purpose> : construct a new Lstu::DB::URL object. If the C<short> or the C<url> attribute is provided, it have to load the informations from the database. In the case of multiple records for the same C<url>, choose the first.
=item B<Returns> : the db accessor object
=item B<Returns> : the Lstu::DB::URL object
=item B<Info> : the app argument is used by Lstu::DB::URL to choose which db accessor will be used, you don't need to use it in new(), but you can use it to access helpers or configuration settings in the other subroutines
......@@ -100,9 +100,9 @@ sub to_hash {
=item B<Arguments> : none
=item B<Purpose> : increment the C<counter> attribute of the db accessor object and update the database record
=item B<Purpose> : increment the C<counter> attribute of the Lstu::DB::URL object and update the database record
=item B<Returns> : the db accessor object
=item B<Returns> : the Lstu::DB::URL object
=back
......@@ -116,10 +116,25 @@ sub to_hash {
=item B<Purpose> : create or update the object in the database
=item B<Returns> : the db accessor object
=item B<Returns> : the Lstu::DB::URL object
=back
=cut
sub write {
my $c = shift;
if ($c->record) {
$c->app->dbi->db->query('UPDATE lstu SET url = ?, counter = ?, timestamp = ?, created_by = ? WHERE short = ?', $c->url, $c->counter, $c->timestamp, $c->created_by, $c->short);
} else {
$c->app->dbi->db->query('INSERT INTO lstu (short, url, counter, timestamp, created_by) VALUES (?, ?, ?, ?, ?)', $c->short, $c->url, $c->counter, $c->timestamp, $c->created_by);
$c->record(1);
}
return $c;
}
=head2 delete
=over 1
......@@ -150,6 +165,17 @@ eg: COUNT(short) WHERE short = ?, $argument
=back
=cut
sub exist {
my $c = shift;
my $short = shift;
return undef unless $short;
return $c->app->dbi->db->query('SELECT count(short) AS count FROM lstu WHERE short = ?', $short)->hashes->first->{count};
}
=head2 choose_empty
=over 1
......@@ -164,6 +190,22 @@ eg: COUNT(short) WHERE short = ?, $argument
=back
=cut
sub choose_empty {
my $c = shift;
my $h = $c->app->dbi->db->query('SELECT * FROM lstu WHERE url IS NULL')->hashes->shuffle;
if ($h->size) {
$c->short($h->first->{short});
$c->record(1);
return $c;
} else {
return undef;
}
}
=head2 count_empty
=over 1
......@@ -180,6 +222,14 @@ eg: C<COUNT(short) WHERE url IS NULL>
=back
=cut
sub count_empty {
my $c = shift;
return $c->app->dbi->db->query('SELECT count(short) AS count FROM lstu WHERE url IS NULL')->hashes->first->{count};
}
=head2 paginate
=over 1
......@@ -204,6 +254,16 @@ eg: SELECT * WHERE url IS NOT NULL ORDER BY counter DESC LIMIT ? OFFSET ?, $page
=back
=cut
sub paginate {
my $c = shift;
my $page = shift;
my $page_offset = shift;
return @{$c->app->dbi->db->query('SELECT * FROM lstu WHERE url IS NOT NULL ORDER BY counter DESC LIMIT ? offset ?', $page_offset, $page * $page_offset)->hashes->to_array};
}
=head2 get_a_lot
=over 1
......@@ -218,6 +278,20 @@ eg: SELECT * WHERE url IS NOT NULL ORDER BY counter DESC LIMIT ? OFFSET ?, $page
=back
=cut
sub get_a_lot {