zy: add mail send code to change password

This commit is contained in:
lovelyzhang 2017-01-05 22:07:22 +08:00
parent 92821cfd87
commit d20cb8ec83
9 changed files with 88 additions and 61 deletions

View File

@ -34,7 +34,7 @@ class UsersController < ApplicationController
if useremail
@user = User.find_by(email: useremail)
if !@user.nil?
# send_code @user
send_code @user
flash[:success] = "验证码已发送至您的邮箱"
redirect_to changepasswd_path(email: @user.email)
else
@ -53,10 +53,21 @@ class UsersController < ApplicationController
@user = User.find_by(email: user_email)
if !@user.nil?
render 'changepasswd'
end
else
flash[:danger] = "用户不存在"
redirect_to login_path
end
else
render html: "hello"
redirect_to login_path
end
else
result = check_change_params(params[:email], params[:verify_code], params[:password], params[:password_confirm])
if result
flash[:success]="密码更新成功"
else
flash[:danger]="密码更新失败"
end
redirect_to login_path
end
end
@ -77,7 +88,24 @@ class UsersController < ApplicationController
user.verify_code = verify_code
user.save
end
end
def check_change_params(email, verify_code, password, password_confirmation)
if password != password_confirmation
return false
end
user = User.find_by(email: email)
if user.nil?
return false
end
if user.verify_code != verify_code
return false
end
hold = Hash.new
hold[:password] = password
hold[:password_confirmation] = password_confirmation
user.update_attributes(hold)
return true
end
end

View File

@ -1,4 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: 'from@example.com'
default from: "ucasguoren@163.com"
layout 'mailer'
end

View File

@ -1,5 +1,5 @@
class UserMailer < ApplicationMailer
default from: "zy_lovestar@163.com"
default from: "ucasguoren@163.com"
def send_verify_code mail_address, code
@verify_code = code

View File

@ -4,79 +4,44 @@
<div class="login-wrapper">
<a href="#"><img width="210" height="70" src="images/logo/guoren.png"/></a>
<%= form_for @user, url: {action: "changepasswd"} do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= form_tag("/changepasswd", method: "post") do %>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-ok"></i></span>
<%= text_field_tag("verif_code", class: 'form-control', placeholder: "请输入邮箱验证码") %>
<span class="input-group-addon"><i class="icon-envelope"></i></span><input class="form-control"
placeholder="请输入邮箱地址"
name="email"
type="email">
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-envelope"></i></span>
<%= f.email_field :email, class: 'form-control', placeholder: "请输入邮箱" %>
<span class="input-group-addon"><i class="icon-envelope-alt"></i></span><input class="form-control"
placeholder="请输入邮箱验证码"
name="verify_code"
type="number">
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-ok"></i></span>
<%= f.password_field :password, class: 'form-control', placeholder: "请输入密码" %>
<span class="input-group-addon"><i class="icon-lock"></i></span><input class="form-control"
placeholder="请输入新密码"
name="password"
type="password">
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-ok"></i></span>
<%= f.password_field :password_confirmation, class: 'form-control', placeholder: "请再次输入密码" %>
<span class="input-group-addon"><i class="icon-lock"></i></span><input class="form-control"
placeholder="请再次输入新密码"
name="password_confirm"
type="password">
</div>
</div>
<%= f.submit "重置密码", class: "btn btn-primary btn-block" %>
<%= submit_tag "重置密码", class: "btn btn-primary btn-block" %>
<% end %>
<!--<%= form_tag("/changepasswd", method: "post") do %>-->
<!--<div class="form-group">-->
<!--<div class="input-group">-->
<!--<span class="input-group-addon"><i class="icon-envelope"></i></span><input class="form-control"-->
<!--placeholder="请输入邮箱地址"-->
<!--name="email"-->
<!--type="email">-->
<!--</div>-->
<!--</div>-->
<!--<div class="form-group">-->
<!--<div class="input-group">-->
<!--<span class="input-group-addon"><i class="icon-envelope-alt"></i></span><input class="form-control"-->
<!--placeholder="请输入邮箱验证码"-->
<!--name="verify_code"-->
<!--type="number">-->
<!--</div>-->
<!--</div>-->
<!--<div class="form-group">-->
<!--<div class="input-group">-->
<!--<span class="input-group-addon"><i class="icon-lock"></i></span><input class="form-control"-->
<!--placeholder="请输入新密码"-->
<!--name="password"-->
<!--type="password">-->
<!--</div>-->
<!--</div>-->
<!--<div class="form-group">-->
<!--<div class="input-group">-->
<!--<span class="input-group-addon"><i class="icon-lock"></i></span><input class="form-control"-->
<!--placeholder="请再次输入新密码"-->
<!--name="password_confirm"-->
<!--type="password">-->
<!--</div>-->
<!--</div>-->
<!--<%= submit_tag "重置密码", class: "btn btn-primary btn-block" %>-->
<!--
<% end %>-->
</div>

33
code.bak Normal file
View File

@ -0,0 +1,33 @@
<%= form_for @user, url: {action: "changepasswd"} do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-lock"></i></span>
<input class="form-control" placeholder="请输入邮箱验证码" name="verify_code" type="text">
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-envelope"></i></span>
<%= f.email_field :email, class: 'form-control', placeholder: "请输入邮箱" %>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-ok"></i></span>
<%= f.password_field :password, class: 'form-control', placeholder: "请输入密码" %>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="icon-ok"></i></span>
<%= f.password_field :password_confirmation, class: 'form-control', placeholder: "请再次输入密码" %>
</div>
</div>
<%= f.submit "重置密码", class: "btn btn-primary btn-block" %>
<% end %>

View File

@ -17,8 +17,8 @@ module GuorenPro
config.action_mailer.smtp_settings = {
:address => "smtp.163.com",
:port => 25,
:user_name => "zy_lovestar@163.com",
:password => "zy_lovestar+=*",
:user_name => "ucasguoren@163.com",
:password => "ucas123",
:authentication => "plain",
:enable_starttls_auto => true
}

View File

@ -57,8 +57,8 @@ Rails.application.configure do
config.action_mailer.smtp_settings = {
:address => "smtp.163.com",
:port => 25,
:user_name => "zy_lovestar@163.com",
:password => "zy_lovestar+=*",
:user_name => "ucasguoren@163.com",
:password => "ucas123",
:authentication => "plain",
:enable_starttls_auto => true
}

View File

@ -10,6 +10,7 @@ Rails.application.routes.draw do
get '/profile', to: 'users#edit'
get '/forgetpasswd', to: 'users#forgetpasswd'
get '/changepasswd', to: 'users#changepasswd'
post '/changepasswd', to: 'users#changepasswd'
get '/main', to: 'main#show'
get '/activity', to: 'main#activity'

Binary file not shown.