From 89284c4c9ff5a4a9f1a7b90dfcf43c4404cd9cb8 Mon Sep 17 00:00:00 2001 From: Shubham Sangamnerkar Date: Fri, 8 Feb 2019 04:05:55 +0530 Subject: [PATCH] Added functionality to verify email from token (#3992) Minor changes Redirection of routes Added verification link in the welcome email Update is_verified only if user is not already verified Tests added fixed tests --- app/controllers/users_controller.rb | 15 +++++++++++++++ app/views/welcome_mailer/notify_newcomer.html.erb | 7 +++++++ config/routes.rb | 1 + test/functional/users_controller_test.rb | 14 ++++++++++++++ 4 files changed, 37 insertions(+) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index abce2029c26..ea059d152ca 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -372,6 +372,21 @@ def shortlink end end + def verify_email + decrypted_user_id = User.validate_token(params[:token]) + action_msg = "Email verification failed" + if decrypted_user_id != 0 + user_obj = User.find(decrypted_user_id) + if user_obj.is_verified + action_msg = "Email already verified" + else + user_obj.update_column(:is_verified, true) + action_msg = "Successfully verified email" + end + end + redirect_to "/login", flash: { notice: action_msg } + end + private def set_user diff --git a/app/views/welcome_mailer/notify_newcomer.html.erb b/app/views/welcome_mailer/notify_newcomer.html.erb index 941c7cf8698..73cfa55adfd 100644 --- a/app/views/welcome_mailer/notify_newcomer.html.erb +++ b/app/views/welcome_mailer/notify_newcomer.html.erb @@ -18,6 +18,13 @@ Looking forward to hearing from you soon, + Please click on the link below to verify your email. + + <% verifaction_link="#{ActionMailer::Base.default_url_options[:host]}/verify/#{@user.generate_token}" %> + <%= link_to "#{verifaction_link}", verifaction_link%> + + In case you are not able to click the link, try copy pasting the link in the browser. + The Public Lab Community <% end %> diff --git a/config/routes.rb b/config/routes.rb index e1f81259f9c..8db4b421b9e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -52,6 +52,7 @@ get 'people/:tagname' => 'users#list' get 'signup' => 'users#new' get 'home' => 'home#front' + get 'verify/:token' => 'users#verify_email' resources :relationships, only: [:create, :destroy] get '/wiki/:id/comments', to: 'wiki#comments' diff --git a/test/functional/users_controller_test.rb b/test/functional/users_controller_test.rb index 4f6df1b8008..950893609be 100644 --- a/test/functional/users_controller_test.rb +++ b/test/functional/users_controller_test.rb @@ -282,4 +282,18 @@ def setup assert_equal [], UserTag.where(uid: users(:bob).id, value: "notify-comment-indirect:false") assert_equal [], UserTag.where(uid: users(:bob).id, value: 'digest:digest') end + + test 'upon verification redirection to login takes place' do + test_user = users(:admin) + email_verification_token = test_user.generate_token + get :verify_email, params: { token: email_verification_token } + assert_redirected_to "/login" + end + + test 'upon verification the is_verified field gets updated appropriately' do + test_user = users(:admin) + email_verification_token = test_user.generate_token + get :verify_email, params: { token: email_verification_token } + assert_equal "Successfully verified email", flash[:notice] + end end