From 97e1ce858e18b020f78ce8b0980f36a27c0b056b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=83=8A=E5=9D=A4?= Date: Mon, 23 Nov 2015 14:13:41 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E6=B3=A8=E5=86=8C=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index dbcde11..2f405da 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,10 +1,19 @@ #encoding: utf-8 class UsersController < ApplicationController def register + @user = User.new + render 'register', layout: 'register' end def register_confirm + @user = User.new params.require(:user).permit(:username,:em ail,:password,:password_confirmation) + if @user.save + to_login @user + redirect_to root_path + else + render 'register', layout: 'register' + end end @@ -25,4 +34,4 @@ class UsersController < ApplicationController def to_login(user) end -end \ No newline at end of file +end From 9dd05e6bd67a124b1984f50cfd4aacddce43f59f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=83=8A=E5=9D=A4?= Date: Tue, 24 Nov 2015 14:13:14 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 2f405da..3164455 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -18,20 +18,37 @@ class UsersController < ApplicationController end def login - + return redirect_to(login_path(from: referer)) unless pa rams[:from].present? + @user = User.new + render 'login', layout: 'register' end def login_confirm + @user = User.find_by username: params[:user][:username] + if @user && @user.check_password(params[:user][:password]) + to_login @user + @user.update_attribute :last_login_time, DateTime.now + redirect_to (params[:from].present? ? params[:from] : ro ot_path) + else + flash[:error] = '用户名或密码错误' + render 'login', layout: 'register' + end + rescue + flash[:error] = '用户名或密码错误' + render 'login', layout: 'register' end def logout + session[:user_id] = nil + redirect_to referer end protected def to_login(user) + session[:user_id] = user.id end end From 790ed1b7916049fc681fc514175c0cebda6f9597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=83=8A=E5=9D=A4?= Date: Sun, 29 Nov 2015 14:12:37 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E5=AE=8C=E6=88=90user=E7=9A=84=E5=AD=97?= =?UTF-8?q?=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/user.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index e029173..fdbe3e4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,5 +9,27 @@ class User < ActiveRecord::Base validates :email, format: {with: /\A[a-zA-Z0-9\-]+@[a-zA-Z0-9-]+\.(org|com|cn|io|net|cc|me)\z/}, uniqueness: true validates :password, length: {minimum: 6}, confirmation: true, if: :need_valid_password? validates :nick_name, length: {minimum: 2, maximum: 20}, uniqueness: true, if: 'nick_name.present?' + def nickname + self.nick_name || self.username + end + def set_admin + self.admin = 0 unless self.admin.present? + end + + def check_password(password) + self.authenticate(password) + end + + def update_last_reply_time + self.update_attribute last_reply_time: DateTime.now + end + + def can_reply? + (DateTime.now.to_i - self.last_reply_time.to_i) > 60 + end + + def need_valid_password? + new_record? || password.present? + end end From 384ddb394a57e7767017013f4e442e56af6c7bef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=83=8A=E5=9D=A4?= Date: Sat, 5 Dec 2015 14:11:50 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E5=AE=8C=E6=88=90user=E8=A1=A8=E5=8D=95?= =?UTF-8?q?=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/users/_form.html.haml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100755 app/views/users/_form.html.haml diff --git a/app/views/users/_form.html.haml b/app/views/users/_form.html.haml new file mode 100755 index 0000000..7859877 --- /dev/null +++ b/app/views/users/_form.html.haml @@ -0,0 +1,14 @@ +- method = action == 'update' ? 'put' : 'post' +- submit_url = case action; when 'register'; register_confirm_users_path; when 'login'; login_confirm_users_path; when 'update'; user_path(@user); end += form_for @user, method: method, url: submit_url, role: 'form' do |f| + .form-field.form-group + = f.text_field :username, placeholder: '用户名', class: 'form-control username', required: true + .form-field.form-group + = f.text_field :email, placeholder: '邮箱', class: 'form-control email', required: true + .form-field.form-group + = f.password_field :password, placeholder: '密码,最少6位', class: 'form-control password', required: true + .form-field.form-group + = f.password_field :password_confirmation, placeholder: '确认密码', class: 'form-control password', required: true + = submit_tag '注册', class: 'btn btn-primary btn-lg' + .action-wrapper + = link_to '已有账号?', login_path \ No newline at end of file From ac45f25328d96010ad9a0f901485bcc8d68eefc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=83=8A=E5=9D=A4?= Date: Sun, 20 Dec 2015 14:11:03 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E5=AE=8C=E6=88=90user=20=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/users/。 | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 app/views/users/。 diff --git a/app/views/users/。 b/app/views/users/。 new file mode 100755 index 0000000..0340601 --- /dev/null +++ b/app/views/users/。 @@ -0,0 +1,28 @@ +%section.content + .form-unit + = link_to root_path, class: 'brand' do + %h1 Blog + %h3 用户登录 + - errors = @user ? @user.errors.full_messages : [] + - if errors.any? + .alert.alert-danger + %a.close{"data-dismiss"=>"alert"} × + %span + = errors.first + - if flash[:success].present? + .alert.alert-success + %a.close{"data-dismiss"=>"success"} × + %span + = flash[:success] + - elsif flash[:error].present? + .alert.alert-danger + %a.close{"data-dismiss"=>"alert"} × + %span= flash[:error] + = form_for :user, url: login_confirm_users_path, role: 'form' do |f| + .form-field.form-group + = f.text_field :username, placeholder: '用户名', class: 'form-control username', required: true + .form-field.form-group + = f.password_field :password, placeholder: '密码', class: 'form-control password', required: true + = submit_tag '登录', class: 'btn btn-primary btn-lg' + .action-wrapper + = link_to '没有账号?', register_path \ No newline at end of file From b0deb43b22d5e87dd51b9c69822761ffc2fc7413 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=83=8A=E5=9D=A4?= Date: Sun, 27 Dec 2015 14:09:07 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E5=AE=8C=E6=88=90user=20=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/users/{。 => login.html.haml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename app/views/users/{。 => login.html.haml} (100%) diff --git a/app/views/users/。 b/app/views/users/login.html.haml similarity index 100% rename from app/views/users/。 rename to app/views/users/login.html.haml From 7d2162ea105c37b4f1760c776870e6d82e35fbe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=83=8A=E5=9D=A4?= Date: Wed, 30 Dec 2015 14:10:27 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E5=AE=8C=E6=88=90user=20=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/users/register.html.haml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100755 app/views/users/register.html.haml diff --git a/app/views/users/register.html.haml b/app/views/users/register.html.haml new file mode 100755 index 0000000..e3485df --- /dev/null +++ b/app/views/users/register.html.haml @@ -0,0 +1,12 @@ +%section.content + .form-unit + = link_to root_path, class: 'brand' do + %h1 Blog + %h3 注册账号 + - errors = @user.errors.full_messages + - if errors.any? + .alert.alert-danger + %a.close{"data-dismiss"=>"alert"} × + %span + = errors.first + = render partial: 'form', locals: {action: 'register'} \ No newline at end of file From 6effa4a88776ef96329400ee913a45f64925d582 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=83=8A=E5=9D=A4?= Date: Mon, 4 Jan 2016 13:54:35 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E6=96=B0=E5=A2=9Euser=20controller?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 0 -> 8196 bytes app/.DS_Store | Bin 0 -> 8196 bytes app/controllers/users_controller.rb | 6 +- test/controllers/users_controller_test.rb | 64 ++++++++++++++++++++++ 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 .DS_Store create mode 100644 app/.DS_Store create mode 100644 test/controllers/users_controller_test.rb diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..52f3d37e42a0071a7b193b73cc7a2ef7a7db84fb GIT binary patch literal 8196 zcmeI1Piz!b9LIm(-2yYizqhrOWevD#ttF&NK|~t~?6#|c6r|YJwuqG7oe?HXXV#tB z-3FuCo6%rU6Nwr*aM8fg#OPTsTr^Qh)OgY8$(tt=_2BQ#n?g(7s~UMPdGGh$@6XKa zcRrcPn>be%Vus&YcI`9N}JBs&zuXXoSR<8(qnSv_h6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aT&|_99QrqzS57Ch<0##TK894T;;2^QY`CFNr~CpNiltRo0Yqx zQv7kV6kxJ_TSsT4$dQ{Xx@Hk?)svEt)`-&1iZ+xwR}1ofmTCF{YiF~ipJC}ai%Lsk z<8|eoNq0#R2}5^#(!B=jTqIQ2+LP{+Di$qzRH@T8#Rfgfx|E6*t$K9GVm*$VqD_wu zTUqrh5~TY*>6~c@+#Jith#fS+=jpbahDFiy63E7?5W_mz2!^pKAX7Upd81%s|Y* z-N^u2b5prdez#^vp3Jo~T%Y4Ai}YL4s6x0=={TcG#~E+_VaV4Rc9k*N2$wXnh0C4% P5D=gLY6;@z9%SG@clu&F literal 0 HcmV?d00001 diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 3164455..c3ef57b 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -7,7 +7,7 @@ class UsersController < ApplicationController end def register_confirm - @user = User.new params.require(:user).permit(:username,:em ail,:password,:password_confirmation) + @user = User.new params.require(:user).permit(:username,:email,:password,:password_confirmation) if @user.save to_login @user redirect_to root_path @@ -18,7 +18,7 @@ class UsersController < ApplicationController end def login - return redirect_to(login_path(from: referer)) unless pa rams[:from].present? + return redirect_to(login_path(from: referer)) unless params[:from].present? @user = User.new render 'login', layout: 'register' end @@ -28,7 +28,7 @@ class UsersController < ApplicationController if @user && @user.check_password(params[:user][:password]) to_login @user @user.update_attribute :last_login_time, DateTime.now - redirect_to (params[:from].present? ? params[:from] : ro ot_path) + redirect_to (params[:from].present? ? params[:from] : root_path) else flash[:error] = '用户名或密码错误' render 'login', layout: 'register' diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb new file mode 100644 index 0000000..7b3bfd4 --- /dev/null +++ b/test/controllers/users_controller_test.rb @@ -0,0 +1,64 @@ +require 'test_helper' +require 'users_controller' + +class UserControllerTest < ActionController::TestCase + setup do + @controller = UsersController.new + @user = User.new + @user.username = 'user1' + @user.email = '123456677@qq.com' + @user.password = '1234567890' + @user.password_confirmation = '1234567890' + @user.admin = 1 + @user.save + end + + test "register" do + get :register + assert_response :success + assert_template :register + assert_template layout: "layouts/register" + end + + test "register_confim_exist_user" do + post :register_confirm, :user => {:username => 'user1', :email => '1234566@qq.com', :password => 'password123', :password_confirmation => 'password123'} + + assert_template :register + assert_template layout: "layouts/register" + end + + test "register_confim_no_valid" do + post :register_confirm, :user => {:username => 'usertest', :email => '1234566@qq.com'} + assert_response :success + assert_template :register + assert_template layout: "layouts/register" + end + + test "register_confim_new_user" do + post :register_confirm, :user => {:username => 'test1', :email => '234566@qq.com', :password => 'password123', :password_confirmation => 'password123'} + assert_not_nil session[:user_id] + assert_redirected_to root_path + end + + test "login" do + get :login, :from => "test" + assert_response :success + assert_template :login + end + + test "logout" do + get :logout + assert_nil session[:user_id] + end + + test "login_confim" do + post :login_confirm, :user => {:username => 'user1', :password => '1234567890'} + assert_redirected_to root_path + end + + test "login_confim_wrong" do + post :login_confirm, :user => {:username => 'user1', :password => '123456000000'} + assert_response :success + assert_equal '用户名或密码错误', flash[:error] + end +end