Changeset 1640 for trunk


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

Add basic functions for anonymous password reset

Location:
trunk/LATMOS-Accounts
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/MANIFEST

    r1549 r1640  
    3232bin/la-sql-log 
    3333bin/la-sql-rename-host 
     34bin/la-sql-reset-passwd 
    3435bin/la-sql-rev 
    3536bin/la-sql-runstat 
     
    176177templates/mail/account_expire.mail 
    177178templates/mail/account_expired_reminder.mail 
     179templates/mail/passwordreset.mail 
    178180testdata/acls1 
    179181testdata/acls2 
  • trunk/LATMOS-Accounts/bin/la-sql-upgrade.in

    r1490 r1640  
    883883 
    884884        ], 
    885     } 
     885    }, 
     886    { 
     887        ver => 17, 
     888        sql => [ 
     889            q{ 
     890            CREATE TABLE passwordreset 
     891            ( 
     892            -- Hérité(e) from table revisions:  rev integer NOT NULL DEFAULT nextval('revisions_rev_seq'::regclass), 
     893            -- Hérité(e) from table revisions:  date timestamp with time zone NOT NULL DEFAULT now(), 
     894            -- Hérité(e) from table revisions:  "create" timestamp with time zone NOT NULL DEFAULT now(), 
     895            -- Hérité(e) from table revisions:  ikey integer NOT NULL DEFAULT nextval('ikey_seq'::regclass), 
     896            "user" text NOT NULL, 
     897            id text NOT NULL, 
     898            CONSTRAINT passwordreset_pkey PRIMARY KEY (id), 
     899            CONSTRAINT password_reset_user_fkey FOREIGN KEY ("user") 
     900            REFERENCES "user" (name) MATCH SIMPLE 
     901            ON UPDATE CASCADE ON DELETE CASCADE 
     902            ) 
     903            INHERITS (revisions) 
     904            WITH ( 
     905            OIDS=FALSE 
     906            ); 
     907            }, 
     908        ], 
     909    }, 
    886910); 
    887911 
  • 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.