Changeset 1182

Show
Ignore:
Timestamp:
07/11/08 12:38:16 (6 months ago)
Author:
alban
Message:

Mise en place d'un users/recover_password. Fixes #11

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/app/controllers/application.rb

    r986 r1182  
    22# Likewise, all the methods added will be available for all controllers. 
    33class ApplicationController < ActionController::Base 
    4         before_filter :check_authentication, :except => [:signup, :signin, :welcome, :index, :confirm, :play, :playlist, :feed, :begin, :complete
     4        before_filter :check_authentication, :except => [:signup, :signin, :welcome, :index, :confirm, :play, :playlist, :feed, :begin, :complete, :recover_password
    55 
    66        private 
     
    99                        redirect_to :controller => "users", :action => "signin" 
    1010                end 
    11         end  
     11        end 
    1212end 
  • trunk/app/controllers/users_controller.rb

    r1130 r1182  
    22        open_id_consumer :required => [:email, :nickname], :optional => [:fullname] 
    33        layout 'documents' 
    4          
     4 
    55        def index 
    66                welcome 
    77                render(:action => "welcome") 
    88        end 
    9          
     9 
    1010        def welcome 
    1111                # could be remove if no one find something to put in 
     
    1919                @tag = user.tags.uniq[0..15] 
    2020        end 
    21          
     21 
    2222        def tags 
    2323          user = User.find(session[:user]) 
     
    2727                @tag = user.tags.uniq[0..15] 
    2828        end 
    29          
     29 
    3030        def tag 
    3131          user = User.find(session[:user]) 
     
    3434                @subscription = user.find_subscriptions(:tag => params[:name], :limit => 5) 
    3535        end 
    36          
     36 
    3737        def find 
    3838          user = User.find(session[:user]) 
    39            
     39 
    4040                @document = user.documents.find_by_keywords(params[:keywords]) 
    4141                @subscription = user.find_subscriptions(:keywords => params[:keywords]) 
    4242        end 
    43          
     43 
    4444        def options 
    4545                @user = User.find(session[:user]) 
     
    5353                end 
    5454        end 
    55          
     55 
    5656        def signin 
    5757                @user = User.new 
     
    6767                end 
    6868        end 
    69          
     69 
    7070  def signup 
    7171    @user = User.new(params[:user]) 
     
    8181        flash[:failure] = "Votre compte n'a pas été crée" 
    8282                  end 
    83     end   
     83    end 
    8484  end 
    85    
     85 
     86  def recover_password 
     87    @email = params[:email] 
     88 
     89    if request.post? 
     90      user = User.find_by_email(@email) 
     91      unless user.nil? 
     92        user.change_password 
     93        flash[:success] = "Votre nouveau de passe a été envoyé à #{user.email}" 
     94        redirect_to :action => :signin 
     95      else 
     96        flash[:failure] = "Aucun compte AudioBank ne correspond à cet email" 
     97      end 
     98    end 
     99  end 
     100 
    86101  def confirm 
    87102        @user = User.find(params[:id]) 
    88103        if @user.hashcode == params[:confirm] && !@user.confirmed? 
    89104                @user.update_attribute(:confirmed, true) 
    90                  
     105 
    91106                begin 
    92107                User.find(@user.id).subscriptions.build(:author => User.find(1), :document => Document.find(1)).save 
     
    94109            logger.error("no welcome document found") 
    95110          end 
    96            
     111 
    97112                flash[:success] = "Bienvenue !" 
    98113            session[:user] = @user.id 
     
    118133      when OpenID::FAILURE 
    119134        flash[:failure] = "Votre identification a échoué" 
    120         
     135 
    121136      when OpenID::SUCCESS 
    122137        @user = User.find_or_initialize_by_openid_url(open_id_response.identity_url) 
     
    130145        flash[:success] = "Bienvenue !" 
    131146        session[:user] = @user.id 
    132      
     147 
    133148      when OpenID::CANCEL 
    134149        flash[:failure] = "Votre identification a été annulé" 
    135      
     150 
    136151      else 
    137152        flash[:failure] = "Voter identification a échoué : #{open_id_response.status}" 
     
    144159                flash[:success] = "A bientÃŽt !" 
    145160                redirect_to :action => "welcome" 
    146         end  
     161        end 
    147162end 
  • trunk/app/models/mailer.rb

    r1140 r1182  
    99    @body["user"] = user 
    1010  end 
    11    
     11 
     12  def new_password(user, new_password) 
     13    @recipients = user.email 
     14    @from = "AudioBank <audiobank@tryphon.org>" 
     15    @subject = "[AudioBank] Votre mot de passe" 
     16    @body = { :new_password => new_password, :user => user } 
     17  end 
     18 
    1219  def document_ready(document) 
    1320    @recipients = document.author.email 
     
    1623    @body = { :document => document, :user => document.author } 
    1724  end 
    18    
     25 
    1926  def document_shared(user, subscriptions) 
    2027    return if subscriptions.empty? 
    21      
     28 
    2229    @recipients = user.email 
    2330    @from = "AudioBank <audiobank@tryphon.org>" 
    24     @subject =  
    25       "[AudioBank] " +  
     31    @subject = 
     32      "[AudioBank] " + 
    2633      (subscriptions.size == 1 ? "nouvelle souscription" : "nouvelles souscriptions") 
    2734    @body = { :subscriptions => subscriptions, :user => user } 
    2835  end 
    29    
     36 
    3037end 
  • trunk/app/models/user.rb

    r1180 r1182  
    137137        end 
    138138 
     139        def change_password 
     140          generated_password = new_password 
     141          update_attribute(:password, generated_password) 
     142          Mailer.deliver_new_password(self, generated_password) 
     143        end 
     144 
     145        def new_password 
     146          random_sha = Digest::SHA256.hexdigest rand.to_s 
     147          random_sha[-10..-1] 
     148        end 
     149 
    139150        def match_name?(input) 
    140151          (self.name.downcase.include?(input) or (not self.username.nil? and self.username.downcase.include?(input))) 
  • trunk/app/views/users/signin.rhtml

    r986 r1182  
    66 <%= password_field("user", "password") %></p> 
    77 <%= submit_tag "Se connecter" %> 
     8 ou 
     9 <%= link_to "rappel de votre mot de passe &#8250;", :controller => :users, :action => :recover_password, :class => "blue" %> 
    810<% end %> 
     11 
    912<% form_tag(:action => :begin) do %> 
    1013 <p><label for="openid_url">OpenId :</label><br/>