SQL.pm 659 Bytes
Newer Older
Francesc Guasch's avatar
Francesc Guasch committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package Ravada::Auth::SQL;

use warnings;
use strict;

use Digest::SHA qw(sha1_hex);

our $CON;

sub init {
    my ($config, $con) = @_;
    $CON = $con;
}

sub add_user {
    my ($login,$password) = @_;
    my $sth = $CON->dbh->prepare(
            "INSERT INTO users (name,password) VALUES(?,?)");

20
    $sth->execute($login,sha1_hex($password));
Francesc Guasch's avatar
Francesc Guasch committed
21
22
23
24
25
26
27
28
    $sth->finish;
}

sub login {
    my ($login,$password) = @_;

    my $sth = $CON->dbh->prepare(
       "SELECT name FROM users WHERE name=? AND password=?");
29
    $sth->execute($login, sha1_hex($password));
Francesc Guasch's avatar
Francesc Guasch committed
30
31
32
33
34
35
36
37
    my ($found) = $sth->fetchrow;
    $sth->finish;
    return if !$found;
    return $found;
}

1;