| 1 | # ABSTRACT: Dancer::Plugin::Auth::RBAC authentication via SQLite! | ||
| 2 | |||
| 3 | package Dancer::Plugin::Auth::RBAC::Credentials::SQLite; | ||
| 4 | BEGIN { | ||
| 5 | $Dancer::Plugin::Auth::RBAC::Credentials::SQLite::VERSION = '1.110720'; | ||
| 6 | } | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use base qw/Dancer::Plugin::Auth::RBAC::Credentials/; | ||
| 11 | use Dancer::Plugin::Database; | ||
| 12 | |||
| 13 | |||
| 14 | sub authorize { | ||
| 15 | |||
| 16 | my ($self, $options, @arguments) = @_; | ||
| 17 | my ($login, $password) = @arguments; | ||
| 18 | |||
| 19 | my $settings = $Dancer::Plugin::Auth::RBAC::settings; | ||
| 20 | |||
| 21 | if ($login) { | ||
| 22 | |||
| 23 | # authorize a new account using supplied credentials | ||
| 24 | |||
| 25 | unless ($password) { | ||
| 26 | $self->errors('login and password are required'); | ||
| 27 | return 0; | ||
| 28 | } | ||
| 29 | |||
| 30 | my $dbh = database($options->{handle}); | ||
| 31 | my $sth = $dbh->prepare( | ||
| 32 | 'SELECT * FROM users WHERE login = ? AND password = ?', | ||
| 33 | ); $sth->execute($login, $password) if $sth; | ||
| 34 | |||
| 35 | my $accounts = $sth->fetchrow_hashref; | ||
| 36 | |||
| 37 | if (defined $accounts) { | ||
| 38 | |||
| 39 | my $session_data = { | ||
| 40 | id => $accounts->{id}, | ||
| 41 | name => $accounts->{name}, | ||
| 42 | login => $accounts->{login}, | ||
| 43 | roles => [ | ||
| 44 | map { $_ =~ s/^\s+|\s+$//; $_ } | ||
| 45 | split /\,/, $accounts->{roles} | ||
| 46 | ], | ||
| 47 | error => [] | ||
| 48 | }; | ||
| 49 | return $self->credentials($session_data); | ||
| 50 | |||
| 51 | } | ||
| 52 | else { | ||
| 53 | $self->errors('login and/or password is invalid'); | ||
| 54 | return 0; | ||
| 55 | } | ||
| 56 | |||
| 57 | } | ||
| 58 | else { | ||
| 59 | |||
| 60 | # check if current user session is authorized | ||
| 61 | |||
| 62 | my $user = $self->credentials; | ||
| 63 | if (($user->{id} || $user->{login}) && !@{$user->{error}}) { | ||
| 64 | |||
| 65 | return $user; | ||
| 66 | |||
| 67 | } | ||
| 68 | else { | ||
| 69 | $self->errors('you are not authorized', 'your session may have ended'); | ||
| 70 | return 0; | ||
| 71 | } | ||
| 72 | |||
| 73 | } | ||
| 74 | return 0; | ||
| 75 | } | ||
| 76 | |||
| 77 | 1; | ||
| 78 | |||
| 79 | __END__ |