Admin.pm 3.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
package Lstu::Controller::Admin;
use Mojo::Base 'Mojolicious::Controller';
use LstuModel;

sub login {
    my $c    = shift;
    my $pwd  = $c->param('adminpwd');
    my $act  = $c->param('action');

11
    $c->cleaning;
12

13 14
    my $ip = $c->ip;

15
    my @banned = LstuModel::Ban->select('WHERE ip = ? AND until > ? AND strike >= ?', $ip, time, $c->config('ban_min_strike'));
16 17
    if (scalar @banned) {
        my $penalty = 3600;
18
        if ($banned[0]->strike >= 2 * $c->config('ban_min_strike')) {
19 20 21 22 23 24 25 26
            $penalty = 3600 * 24 * 30; # 30 days of banishing
        }
        $banned[0]->update(
            strike => $banned[0]->strike + 1,
            until  => time + $penalty
        );
        $c->flash('msg'    => $c->l('Too many bad passwords. You\'re banned.'));
        $c->flash('banned' => 1);
27 28
        $c->redirect_to('stats');
    } else {
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        if (defined($c->config('adminpwd')) && defined($pwd) && $pwd eq $c->config('adminpwd')) {
            my $token = $c->shortener(32);

            LstuModel::Sessions->create(
                token => $token,
                until => time + 3600
            );
            $c->session('token' => $token);
            $c->redirect_to('stats');
        } elsif (defined($act) && $act eq 'logout') {
            LstuModel::Sessions->delete_where('token = ?', $c->session->{token});
            delete $c->session->{token};
            $c->redirect_to('stats');
        } else {
            my @bans = LstuModel::Ban->select('WHERE ip = ?', $ip);

            if (scalar @bans) {
                $bans[0]->update(
                    strike => $bans[0]->strike + 1,
                    until  => time + 3600
                );
            } else {
                LstuModel::Ban->create(
                    ip     => $ip,
                    strike => 1,
                    until  => time + 3600
                );
            }
            $c->flash('msg' => $c->l('Bad password'));
            $c->redirect_to('stats');
        }
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    }
}

sub delete {
    my $c = shift;
    my $short = $c->param('short');

    if (defined($c->session('token')) && LstuModel::Sessions->count('WHERE token = ?', $c->session('token'))) {
        my @urls = LstuModel::Lstu->select('WHERE short = ?', $short);
        if (scalar(@urls)) {
            my $deleted = LstuModel::Lstu->delete_where('short = ?', $short);
            $c->respond_to(
                json => { json => { success => Mojo::JSON->true, deleted => $deleted } },
                any  => sub {
                    my $c = shift;
                    $c->redirect_to('stats');
                }
            );
        } else {
            my $msg = $c->l('The shortened URL %1 doesn\'t exist.', $c->url_for('/')->to_abs.$short);
            $c->respond_to(
                json => { json => { success => Mojo::JSON->false, msg => $msg } },
                any  => sub {
                    my $c = shift;
                    $c->flash('msg' => $msg);
                    $c->redirect_to('stats');
                }
            );
        }
    } else {
        $c->flash('msg' => $c->l('Bad password'));
        $c->redirect_to('stats');
    }
}

1;