Ignore:
Timestamp:
01/18/16 23:32:48 (8 years ago)
Author:
nanardon
Message:

Add basic functions for anonymous password reset

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/User.pm

    r1636 r1640  
    1515use LATMOS::Accounts::I18N; 
    1616use Date::Calc; 
     17use LATMOS::Accounts::Mail; 
    1718 
    1819our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; 
     
    14961497} 
    14971498 
     1499=head2 GenPasswordResetId 
     1500 
     1501Return a new id allowing passowrd reset 
     1502 
     1503=cut 
     1504 
     1505sub GenPasswordResetId { 
     1506    my ($self) = @_; 
     1507 
     1508    my $id = LATMOS::Accounts::Utils::genpassword(length => 32); 
     1509 
     1510    my $sth = $self->base->db->prepare_cached(q{ 
     1511        INSERT INTO passwordreset (id, "user") values (?,?) 
     1512    }); 
     1513 
     1514    if ($sth->execute($id, $self->id)) { 
     1515        return $id; 
     1516    } else { 
     1517        return; 
     1518    } 
     1519} 
     1520 
     1521=head2 SendPasswordReset($url) 
     1522 
     1523Generate a password reset Id and the to the user. 
     1524 
     1525C<$url> is the URL where the password can changed (printf forward, the %s is 
     1526replaced by the request id) 
     1527 
     1528=cut 
     1529 
     1530sub SendPasswordReset { 
     1531    my ($self, $url) = @_; 
     1532 
     1533    my $id = $self->GenPasswordResetId; 
     1534 
     1535    my $mail = $self->get_attributes('mail') or do { 
     1536        $self->base->log(LA_ERR, "Cannot sent reset password mail: no mail found"); 
     1537        return; 
     1538    }; 
     1539 
     1540    my %mail = ( 
     1541        Subject => 'LATMOS: pasword reset', 
     1542        'X-LATMOS-Reason' => 'Account destruction', 
     1543        to => $mail, 
     1544    ); 
     1545 
     1546    my $vars = { 
     1547        url => sprintf($url, $id), 
     1548    }; 
     1549    $vars->{id} =  $id; 
     1550    $vars->{obj} = $self; 
     1551 
     1552    my $lamail = LATMOS::Accounts::Mail->new( 
     1553        $self->base->la, 
     1554        'passwordreset.mail', 
     1555    ); 
     1556 
     1557    if ($lamail->process(\%mail, $vars)) { 
     1558        $self->base->log(LA_NOTICE, 
     1559            "Reset password sent to %s for user %s", 
     1560            $mail{to}, 
     1561            $self->id, 
     1562        ); 
     1563    } 
     1564} 
     1565 
     1566=head2 CheckPasswordResetId($id) 
     1567 
     1568Return True if the reset password ID can be found and is less than one day old 
     1569 
     1570=cut 
     1571 
     1572sub CheckPasswordResetId { 
     1573    my ($self, $id) = @_; 
     1574 
     1575    my $sth = $self->base->db->prepare_cached(q{ 
     1576        SELECT * FROM passwordreset WHERE 
     1577            "user" = ? and 
     1578            id = ? and 
     1579            "create" >= now() - '1 days'::interval 
     1580    }); 
     1581    $sth->execute($self->id, $id); 
     1582 
     1583    my $res = $sth->fetchrow_hashref; 
     1584    $sth->finish; 
     1585 
     1586    return $res ? 1 : 0; 
     1587} 
     1588 
    149815891; 
    14991590 
Note: See TracChangeset for help on using the changeset viewer.