Ignore:
Timestamp:
07/30/12 01:04:24 (12 years ago)
Author:
nanardon
Message:

confirm subscription by mail

Account are not immediatelly created, the request is stored and validation code
is sent by mail

File:
1 edited

Legend:

Unmodified
Added
Removed
  • web/lib/Sophie/Controller/Login.pm

    r460 r461  
    5656    my ($self, $c, $mail) = @_; 
    5757 
    58     my $valid_code = join('', map { printf("%02x", rand(256)) } (0 .. 15)); 
     58    my $valid_code = join('', map { sprintf("%02x", rand(256)) } (0 .. 15)); 
    5959 
    6060    $c->model('Base::AccountRequest')->create({ 
    6161            mail => $mail, 
    6262            valid_code => $valid_code, 
     63            ip_address => $c->req->address, 
    6364    }); 
     65    $c->model('Base')->storage->dbh->commit; 
     66 
     67    return $valid_code; 
    6468} 
    6569 
     
    6771    my ($self, $c) = @_; 
    6872 
    69     warn $c->req->param('valid'); 
    70     warn $c->session->{valid_create_user}; 
    7173    if ((my $valid = $c->req->param('valid')) && $c->req->param('username')) { 
     74       # create a login request 
     75       my $valid_code = $c->forward('create_request', [ $c->req->param('username') ],); 
     76 
    7277       if ($valid == $c->session->{valid_create_user}) { 
    73            my $res = $c->forward('/admin/create_user', 
    74                [ 
    75                    $c->req->param('username'), 
    76                    $c->req->param('password'), 
    77                ] 
    78             ); 
    79             if ($res) { 
    80                $c->res->redirect($c->uri_for('/login', 
    81                    { username => $c->req->param('username') } 
    82                )); 
    83            } 
    84        } 
     78           $c->stash->{email} = { 
     79               header => [ 
     80                   to      => $c->req->param('username'), 
     81                   from    => 'sophie@zarb.org', 
     82                   subject => 'Sophie.zarb.org confirm request', 
     83               ], 
     84               body    => " 
     85Someone, hopefully you, request an account on Sophe web site. 
     86 
     87To complete your subscription follow the link bellow: 
     88 
     89" . $c->uri_for('/login/confirm', { id => $valid_code }) . " 
     90 
     91If this is an error, simply ignore this mail. 
     92 
     93", 
     94           }; 
     95           $c->forward( $c->view('Email') ); 
     96        } 
    8597    } 
    8698    my $aa = (0 .. 9)[rand(9)]; 
     
    88100    $c->stash->{valid} = "$aa + $bb"; 
    89101    $c->session->{valid_create_user} = $aa + $bb; 
     102} 
     103 
     104sub confirm :Local { 
     105    my ($self, $c) = @_; 
     106 
     107    my $reqid = $c->req->param('id'); 
     108 
     109    my $request = $c->model('Base::AccountRequest')->find( 
     110        { 
     111            valid_code => $reqid, 
     112        }); 
     113    if (!$request) { 
     114        # ERR 
     115    } 
     116    $c->stash->{email} = $request->mail; 
     117 
     118    if ($c->req->param('password')) { 
     119       my $res = $c->forward('/admin/create_user', 
     120           [ 
     121               $request->mail, 
     122               $c->req->param('password'), 
     123           ] 
     124        ); 
     125        if ($res) { 
     126           $request->delete; 
     127           $c->model('Base')->storage->dbh->commit; 
     128           # TODO authenticate user directly 
     129           $c->res->redirect($c->uri_for('/login', 
     130               { username => $request->mail } 
     131           )); 
     132       } 
     133    } 
    90134} 
    91135 
Note: See TracChangeset for help on using the changeset viewer.