From 2a7e0365b895bb786885e3783415c6cc716bf75c Mon Sep 17 00:00:00 2001 From: z9hang Date: Thu, 4 Dec 2014 15:53:47 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B3=A8=E5=86=8C=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/account_controller.rb | 108 ++++++++++++++------------ app/helpers/account_helper.rb | 41 ++++++++++ app/services/users_service.rb | 22 ++++-- 3 files changed, 113 insertions(+), 58 deletions(-) diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb index 2ca202191..3de865d6a 100644 --- a/app/controllers/account_controller.rb +++ b/app/controllers/account_controller.rb @@ -101,33 +101,22 @@ class AccountController < ApplicationController # User self-registration def register - # @root_path="/home/pdl/redmine-2.3.2-0/apache2/" -# - #@cache_identityy = params[:identity]||"" #身份 - @cache_no = params[:no]||"" #学号 - @cache_technical_title = params[:technical_title]||"" #教师职称 - @cache_province = params[:province]||"" #省份 - @cache_city = params[:city]||"" #城市 - @cache_enterprise_name = params[:enterprise_name]||"" #企业 - - firstname_code = "" - lastname_code = "" (redirect_to(home_url); return) unless Setting.self_registration? || session[:auth_source_registration] if request.get? session[:auth_source_registration] = nil @user = User.new(:language => current_language.to_s) else user_params = params[:user] || {} - @user = User.new - @user.safe_attributes = user_params - if params[:identity] == "2" # 2 企业 - firstname_code = @user.firstname - lastname_code = @user.lastname - @user.firstname = params[:enterprise_name] - @user.lastname = l(:field_enterprise) - end - @user.admin = false - @user.register + #@user = User.new + #@user.safe_attributes = user_params + #if params[:identity] == "2" # 2 企业 + # firstname_code = @user.firstname + # lastname_code = @user.lastname + # @user.firstname = params[:enterprise_name] + # @user.lastname = l(:field_enterprise) + #end + #@user.admin = false + #@user.register if session[:auth_source_registration] @user.activate @user.login = session[:auth_source_registration][:login] @@ -139,42 +128,59 @@ class AccountController < ApplicationController redirect_to my_account_path end else - @user.login = params[:user][:login] - unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank? - @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation] - end - - - - case Setting.self_registration + us = UsersService.new + @user = us.register user_params.merge(:should_confirmation_password => true) + case Setting.self_registration when '1' - register_by_email_activation(@user) + #register_by_email_activation(@user) + unless @user.new_record? + render action: 'email_valid', locals: {:mail => user.mail} + end when '3' - register_automatically(@user) + #register_automatically(@user) + unless @user.new_record? + self.logged_user = user + flash[:notice] = l(:notice_account_activated) + redirect_to my_account_url + end else - register_manually_by_administrator(@user) - end - - #added by bai - if @user.id != nil - ue = @user.user_extensions ||= UserExtensions.new - #ue = UserExtensions.create(:identity => params[:identity].to_i,:technical_title => params[:technical_title], :gender => params[:gender].to_i, :user_id => @user.id, :student_id => ) - ue.identity = params[:identity].to_i - ue.technical_title = params[:technical_title] - ue.gender = params[:gender].to_i - ue.user_id = @user.id - ue.student_id = params[:no] - ue.location = params[:province] if params[:province] != nil - ue.location_city = params[:city] if params[:city] != nil - ue.save - end + #register_manually_by_administrator(@user) + unless @user.new_record? + account_pending + end + end + # @user.login = params[:user][:login] + # unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank? + # @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation] + # end + # + # + # + # case Setting.self_registration + # when '1' + # register_by_email_activation(@user) + # when '3' + # register_automatically(@user) + # else + # register_manually_by_administrator(@user) + # end + # + # #added by bai + # if @user.id != nil + # ue = @user.user_extensions ||= UserExtensions.new + # #ue = UserExtensions.create(:identity => params[:identity].to_i,:technical_title => params[:technical_title], :gender => params[:gender].to_i, :user_id => @user.id, :student_id => ) + # ue.identity = params[:identity].to_i + # ue.technical_title = params[:technical_title] + # ue.gender = params[:gender].to_i + # ue.user_id = @user.id + # ue.student_id = params[:no] + # ue.location = params[:province] if params[:province] != nil + # ue.location_city = params[:city] if params[:city] != nil + # ue.save + # end end end - if params[:identity] == "2" - @user.firstname = firstname_code - @user.lastname = lastname_code - end end # Token based account activation diff --git a/app/helpers/account_helper.rb b/app/helpers/account_helper.rb index 8beeac363..3be096492 100644 --- a/app/helpers/account_helper.rb +++ b/app/helpers/account_helper.rb @@ -18,4 +18,45 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. module AccountHelper + + def email_activation_register(user, &block) + token = Token.new(:user => user, :action => "register") + if user.save and token.save + UserStatus.create(:user_id => user.id, :changsets_count => 0, :watchers_count => 0) + Mailer.register(token).deliver + flash[:notice] = l(:notice_account_register_done) + #render action: 'email_valid', locals: {:mail => user.mail} + else + yield if block_given? + end + user + end + + def automatically_register(user, &block) + # Automatic activation + user.activate + user.last_login_on = Time.now + if user.save + UserStatus.create(:user_id => user.id, :changsets_count => 0, :watchers_count => 0) + #self.logged_user = user + #flash[:notice] = l(:notice_account_activated) + #redirect_to my_account_url + else + yield if block_given? + end + user + end + + def administrator_manually__register(user, &block) + if user.save + UserStatus.create(:user_id => user.id ,:changsets_count => 0, :watchers_count => 0) + # Sends an email to the administrators + Mailer.account_activation_request(user).deliver + #account_pending + else + yield if block_given? + end + user + end + end diff --git a/app/services/users_service.rb b/app/services/users_service.rb index aa2c6bf71..f36cf3836 100644 --- a/app/services/users_service.rb +++ b/app/services/users_service.rb @@ -1,24 +1,32 @@ class UsersService - + include AccountHelper #将用户注册的功能函数写这里 #参数约定 #成功返回注册后的User实例,失败直接抛异常 + def register(params) @user = User.new @user.admin = false @user.register @user.login = params[:login] - @user.mail = params[:email] - unless password.blank? - @user.password = params[:password] + @user.mail = params[:mail] + password = params[:password] + password_confirmation = params[:password_confirmation] + should_confirmation_password = params[:should_confirmation_password] + if !password.blank? && !password_confirmation && should_confirmation_password + @user.password,@user.password_confirmation = password,password_confirmation + elsif !password.blank? && !should_confirmation_password + @user.password = password + else + @user.password = "" end case Setting.self_registration when '1' - register_by_email_activation(@user) + @user = email_activation_register(@user) when '3' - register_automatically(@user) + @user = automatically_register(@user) else - register_manully_by_administrator(@user) + @user = administrator_manually__register(@user) end if @user.id != nil ue = @user.user_extensions ||= UserExtensions.new From fa1e7c9a0458b68819e0f562e4d7616d0c8ea67c Mon Sep 17 00:00:00 2001 From: z9hang Date: Sat, 6 Dec 2014 11:27:52 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=AF=86=E7=A0=81?= =?UTF-8?q?=E9=95=BF=E5=BA=A6=E5=88=A4=E6=96=AD=E9=94=99=E8=AF=AF=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile.lock | 11 ------ app/controllers/account_controller.rb | 50 +++------------------------ app/services/users_service.rb | 2 +- 3 files changed, 6 insertions(+), 57 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 58f069ac1..43252793e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -122,7 +122,6 @@ GEM htmlentities (4.3.2) i18n (0.6.1) ice_nine (0.11.0) - iconv (1.0.4) journey (1.0.4) jquery-rails (2.0.3) railties (>= 3.1.0, < 5.0) @@ -172,8 +171,6 @@ GEM win32console (~> 1.3) pry-nav (0.2.3) pry (~> 0.9.10) - puma (2.10.1) - rack (>= 1.1, < 2.0) rack (1.4.5) rack-accept (0.4.5) rack (>= 0.4) @@ -222,7 +219,6 @@ GEM sass-rails rmagick (2.13.2) ruby-openid (2.1.8) - ruby-prof (0.15.2) rubyzip (1.1.6) sass (3.3.10) sass-rails (3.2.6) @@ -273,8 +269,6 @@ GEM win32console (1.3.2-x86-mingw32) xpath (2.0.0) nokogiri (~> 1.3) - zip-zip (0.3) - rubyzip (>= 1.0.0) PLATFORMS ruby @@ -297,7 +291,6 @@ DEPENDENCIES guard-test (~> 1.0.0) htmlentities i18n (~> 0.6.0) - iconv jquery-rails (~> 2.0.2) kaminari mocha (~> 1.1.0) @@ -307,15 +300,12 @@ DEPENDENCIES paperclip (~> 3.5.4) pry pry-nav - puma rack-mini-profiler! rack-openid rails (= 3.2.13) rich (= 1.4.6) rmagick (>= 2.0.0) ruby-openid (~> 2.1.4) - ruby-prof (~> 0.15.1) - rubyzip sass-rails (~> 3.2.3) seems_rateable! selenium-webdriver (~> 2.42.0) @@ -323,4 +313,3 @@ DEPENDENCIES spork-testunit (~> 0.0.8) therubyracer uglifier (>= 1.0.3) - zip-zip diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb index 3de865d6a..6102160b3 100644 --- a/app/controllers/account_controller.rb +++ b/app/controllers/account_controller.rb @@ -107,16 +107,6 @@ class AccountController < ApplicationController @user = User.new(:language => current_language.to_s) else user_params = params[:user] || {} - #@user = User.new - #@user.safe_attributes = user_params - #if params[:identity] == "2" # 2 企业 - # firstname_code = @user.firstname - # lastname_code = @user.lastname - # @user.firstname = params[:enterprise_name] - # @user.lastname = l(:field_enterprise) - #end - #@user.admin = false - #@user.register if session[:auth_source_registration] @user.activate @user.login = session[:auth_source_registration][:login] @@ -128,9 +118,9 @@ class AccountController < ApplicationController redirect_to my_account_path end else - us = UsersService.new - @user = us.register user_params.merge(:should_confirmation_password => true) - case Setting.self_registration + us = UsersService.new + @user = us.register user_params.merge(:should_confirmation_password => true) + case Setting.self_registration when '1' #register_by_email_activation(@user) unless @user.new_record? @@ -139,7 +129,7 @@ class AccountController < ApplicationController when '3' #register_automatically(@user) unless @user.new_record? - self.logged_user = user + self.logged_user = @user flash[:notice] = l(:notice_account_activated) redirect_to my_account_url end @@ -148,37 +138,7 @@ class AccountController < ApplicationController unless @user.new_record? account_pending end - end - # @user.login = params[:user][:login] - # unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank? - # @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation] - # end - # - # - # - # case Setting.self_registration - # when '1' - # register_by_email_activation(@user) - # when '3' - # register_automatically(@user) - # else - # register_manually_by_administrator(@user) - # end - # - # #added by bai - # if @user.id != nil - # ue = @user.user_extensions ||= UserExtensions.new - # #ue = UserExtensions.create(:identity => params[:identity].to_i,:technical_title => params[:technical_title], :gender => params[:gender].to_i, :user_id => @user.id, :student_id => ) - # ue.identity = params[:identity].to_i - # ue.technical_title = params[:technical_title] - # ue.gender = params[:gender].to_i - # ue.user_id = @user.id - # ue.student_id = params[:no] - # ue.location = params[:province] if params[:province] != nil - # ue.location_city = params[:city] if params[:city] != nil - # ue.save - # end - + end end end end diff --git a/app/services/users_service.rb b/app/services/users_service.rb index f36cf3836..0f79e8308 100644 --- a/app/services/users_service.rb +++ b/app/services/users_service.rb @@ -13,7 +13,7 @@ class UsersService password = params[:password] password_confirmation = params[:password_confirmation] should_confirmation_password = params[:should_confirmation_password] - if !password.blank? && !password_confirmation && should_confirmation_password + if !password.blank? && !password_confirmation.blank? && should_confirmation_password @user.password,@user.password_confirmation = password,password_confirmation elsif !password.blank? && !should_confirmation_password @user.password = password