965 lines
33 KiB
Ruby
965 lines
33 KiB
Ruby
#coding=utf-8
|
||
# Redmine - project management software
|
||
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
||
#
|
||
# This program is free software; you can redistribute it and/or
|
||
# modify it under the terms of the GNU General Public License
|
||
# as published by the Free Software Foundation; either version 2
|
||
# of the License, or (at your option) any later version.
|
||
#
|
||
# This program is distributed in the hope that it will be useful,
|
||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
# GNU General Public License for more details.
|
||
#
|
||
# You should have received a copy of the GNU General Public License
|
||
# along with this program; if not, write to the Free Software
|
||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
||
class AccountController < ApplicationController
|
||
helper :custom_fields
|
||
include CustomFieldsHelper
|
||
|
||
# prevents login action to be filtered by check_if_login_required application scope filter
|
||
skip_before_filter :check_if_login_required
|
||
skip_before_filter :check_authentication, :only => [:login, :logout, :user_join, :avatar, :authentication, :professional_certification, :security_settings, :change_psd]
|
||
before_filter :auth_login1, :only => [:avatar, :authentication, :professional_certification, :security_settings, :change_psd]
|
||
skip_before_filter :verify_authenticity_token, :only =>[:codepedia_login]
|
||
before_filter :require_login, only: [:avatar, :authentication, :professional_certification, :security_settings, :change_psd, :user_auth, :apply_auth, :apply_pro_certification, :check_student_id]
|
||
include ApplicationHelper
|
||
# Login request and validation
|
||
|
||
def login
|
||
if params[:type] == "activated"
|
||
@message = l(:notice_account_activated)
|
||
elsif params[:type] == "expired"
|
||
@message = l(:notice_account_expired)
|
||
end
|
||
|
||
if request.get?
|
||
@login = params[:login] || true
|
||
if User.current.logged?
|
||
# 判断用户基本资料是否完善,不完善讲强制完善基本资料,完善进入主页
|
||
user = UserExtensions.where(:user_id => User.current.id).first
|
||
if user.gender.nil? || user.school_id.nil? || User.current.lastname.nil?
|
||
redirect_to my_account_path(:tip => 1)
|
||
elsif user.identity == 3 && user.school_id.nil?
|
||
redirect_to my_account_path(:tip => 1)
|
||
else
|
||
redirect_to user_path(User.current)
|
||
end
|
||
else
|
||
render :layout => 'login_bigdata'
|
||
end
|
||
else
|
||
authenticate_user
|
||
end
|
||
end
|
||
|
||
def codepedia_login
|
||
logger.info("codepedia_login#########################################")
|
||
logger.info("#{params}")
|
||
user, last_login_on = User.try_to_login(params[:username], params[:password])
|
||
logger.info(user)
|
||
if user.blank?
|
||
render :json => { status: 0 }
|
||
else
|
||
render :json => { status: 1, user: user}
|
||
end
|
||
end
|
||
|
||
def user_join
|
||
if params[:type] == "activated"
|
||
@message = l(:notice_account_activated)
|
||
elsif params[:type] == "expired"
|
||
@message = l(:notice_account_expired)
|
||
end
|
||
|
||
if request.get?
|
||
@login = params[:login] || true
|
||
if User.current.logged?
|
||
# 判断用户基本资料是否完善,不完善讲强制完善基本资料,完善进入主页
|
||
user = UserExtensions.where(:user_id => User.current.id).first
|
||
if user.gender.nil? || user.school_id.nil? || User.current.lastname.nil?
|
||
redirect_to my_account_path(:tip => 1)
|
||
elsif user.identity == 3 && user.school_id.nil?
|
||
redirect_to my_account_path(:tip => 1)
|
||
else
|
||
redirect_to user_path(User.current)
|
||
end
|
||
else
|
||
render :layout => 'login_bigdata'
|
||
end
|
||
else
|
||
authenticate_user
|
||
end
|
||
end
|
||
|
||
# 服务协议
|
||
def agreement
|
||
render :layout => 'base_edu'
|
||
end
|
||
|
||
def about_us
|
||
render :layout => 'static_base'
|
||
end
|
||
|
||
# Log out current user and redirect to welcome page
|
||
def logout
|
||
if User.current.anonymous?
|
||
redirect_to signin_path
|
||
else
|
||
UserActions.create(:action_id => User.current.id, :action_type => "Logout", :user_id => User.current.id)
|
||
logout_user
|
||
# 记录用户登出行为
|
||
redirect_to signin_path
|
||
end
|
||
# display the logout form
|
||
end
|
||
|
||
def heartbeat
|
||
render :json => session[:user_id]
|
||
end
|
||
|
||
# Lets user choose a new password
|
||
def lost_password
|
||
(redirect_to(signin_path); return) unless Setting.lost_password?
|
||
if params[:token]
|
||
@token = Token.find_token("recovery", params[:token].to_s)
|
||
if @token.nil? || @token.expired?
|
||
redirect_to signin_path
|
||
return
|
||
end
|
||
@user = @token.user
|
||
unless @user && @user.active?
|
||
redirect_to signin_path
|
||
return
|
||
end
|
||
if request.post?
|
||
@user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
|
||
if @user.save
|
||
@token.destroy
|
||
flash[:notice] = l(:notice_account_password_updated)
|
||
redirect_to signin_url
|
||
return
|
||
end
|
||
end
|
||
render :template => "account/password_recovery"
|
||
return
|
||
else
|
||
if request.post?
|
||
user = User.find_by_mail(params[:mail].to_s)
|
||
# user not found or not active
|
||
unless user && user.active?
|
||
flash.now[:error] = l(:notice_account_unknown_email)
|
||
render :layout => 'static_base'
|
||
return
|
||
end
|
||
# user cannot change its password
|
||
unless user.change_password_allowed?
|
||
flash.now[:error] = l(:notice_can_t_change_password)
|
||
return
|
||
end
|
||
# create a new token for password recovery
|
||
token = Token.new(:user => user, :action => "recovery")
|
||
if token.save
|
||
Mailer.run.lost_password(token)
|
||
flash[:notice] = l(:notice_account_lost_email_sent)
|
||
redirect_to lost_password_path
|
||
return
|
||
end
|
||
end
|
||
render :layout => 'login_bigdata'
|
||
end
|
||
end
|
||
|
||
# User self-registration
|
||
def register
|
||
(redirect_to(signin_path); 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] || {}
|
||
if session[:auth_source_registration]
|
||
@user.activate
|
||
@user.login = session[:auth_source_registration][:login]
|
||
@user.auth_source_id = session[:auth_source_registration][:auth_source_id]
|
||
if @user.save
|
||
session[:auth_source_registration] = nil
|
||
self.logged_user = @user
|
||
flash[:notice] = l(:notice_account_activated)
|
||
redirect_to my_account_path(:tip => 1)
|
||
end
|
||
else
|
||
us = UsersService.new
|
||
@user = us.register user_params.merge(:should_confirmation_password => false)
|
||
case Setting.self_registration
|
||
when '1'
|
||
#register_by_email_activation(@user)
|
||
unless @user.new_record?
|
||
if params[:user][:mail]
|
||
redirect_to account_email_valid_path(:mail => @user.mail, :user_id => @user.id)
|
||
else
|
||
self.logged_user = @user
|
||
redirect_to my_account_url(:tip=>1)
|
||
end
|
||
# flash[:notice] = l(:notice_account_register_done)
|
||
# render action: 'email_valid', locals: {:mail => @user.mail}
|
||
end
|
||
when '3'
|
||
#register_automatically(@user)
|
||
if !@user.new_record?
|
||
self.logged_user = @user
|
||
flash[:notice] = l(:notice_account_activated)
|
||
redirect_to my_account_url(:tip=>1)
|
||
else
|
||
redirect_to signin_path
|
||
end
|
||
else
|
||
#register_manually_by_administrator(@user)
|
||
unless @user.new_record?
|
||
account_pending
|
||
end
|
||
end
|
||
if params[:user][:mail].nil?
|
||
reward_grade(@user, @user.id, 'Phone', 500)
|
||
else
|
||
reward_grade(@user, @user.id, 'Mail', 500)
|
||
end
|
||
end
|
||
end
|
||
end
|
||
|
||
#should_confirmation_password是否验证密码
|
||
def create_and_save_user login,password,email,password_confirmation,should_confirmation_password
|
||
@user = User.new
|
||
@user.admin = false
|
||
@user.register
|
||
@user.login = login
|
||
@user.mail = email
|
||
if should_confirmation_password && !password.blank? && !password_confirmation.blank?
|
||
@user.password,@user.password_confirmation = password,password_confirmation
|
||
elsif !should_confirmation_password && !password.blank?
|
||
@user.password = password
|
||
else
|
||
@user.password = ""
|
||
end
|
||
case Setting.self_registration
|
||
when '1'
|
||
register_by_email_activation(@user)
|
||
when '3'
|
||
register_automatically(@user)
|
||
else
|
||
register_manually_by_administrator(@user)
|
||
end
|
||
if @user.id != nil
|
||
ue = @user.user_extensions ||= UserExtensions.new
|
||
ue.user_id = @user.id
|
||
ue.save
|
||
end
|
||
@user
|
||
end
|
||
|
||
# Token based account activation
|
||
def activate
|
||
(redirect_to(signin_path); return) unless Setting.self_registration? && params[:token].present?
|
||
token = Token.find_token('register', params[:token].to_s)
|
||
type = l(:notice_account_expired) if (token && token.expired?)
|
||
(redirect_to(signin_path(:type => "expired")); return) unless token and !token.expired?
|
||
user = token.user
|
||
(redirect_to(signin_path); return) unless user.registered?
|
||
user.activate
|
||
if user.save
|
||
token.destroy
|
||
flash[:notice] = l(:notice_account_activated)
|
||
end
|
||
redirect_to signin_url(:type => "activated")
|
||
end
|
||
|
||
def api_register login,password,email
|
||
users_service = UsersService.new
|
||
users_service.register({login: login, password: password, email: eamil})
|
||
end
|
||
|
||
def valid_ajax
|
||
req = Hash.new(false)
|
||
req[:message] = ''
|
||
|
||
valid_attr = params[:valid]
|
||
valid_value = params[:value]
|
||
|
||
faker = User.new
|
||
|
||
if valid_attr.eql?('login')
|
||
faker.login = valid_value
|
||
faker.valid?
|
||
req[:valid] = faker.errors[:login].blank?
|
||
req[:message] = faker.errors[:login]
|
||
end
|
||
|
||
if valid_attr.eql?('phone')
|
||
faker.phone = valid_value
|
||
faker.valid?
|
||
req[:valid] = faker.errors[:phone].blank?
|
||
req[:message] = ""
|
||
end
|
||
|
||
if valid_attr.eql?('mail')
|
||
faker.mail = valid_value
|
||
faker.valid?
|
||
req[:valid] = faker.errors[:mail].blank?
|
||
req[:message] = faker.errors[:mail]
|
||
end
|
||
|
||
req[:message] = l(:modal_valid_passing) if req[:message].blank?
|
||
render :json => req
|
||
end
|
||
|
||
# 手机号或邮箱是否已注册
|
||
def valid_register_user
|
||
req = Hash.new(false)
|
||
req[:message] = ''
|
||
|
||
valid_attr = params[:valid]
|
||
valid_value = params[:value]
|
||
|
||
if valid_attr.eql?('phone')
|
||
user = User.where(:phone => valid_value).first
|
||
req[:valid] = !user.nil?
|
||
req[:message] = user.nil? ? "该手机号未注册" : ""
|
||
elsif valid_attr.eql?('mail')
|
||
user = User.where(:mail => valid_value).first
|
||
req[:valid] = !user.nil?
|
||
req[:message] = user.nil? ? "该邮箱未注册" : ""
|
||
end
|
||
|
||
render :json => req
|
||
end
|
||
|
||
# 验证码是否有效
|
||
def valid_verification_code
|
||
req = Hash.new(false)
|
||
req[:valid] = false
|
||
type = params[:type].to_i
|
||
if type == 1 || type == 2 || type == 4 || params[:phone] =~ /^1\d{10}$/
|
||
code = VerificationCode.where(:phone => params[:phone], :code => params[:code], :code_type => (params[:type].to_i != 1 && params[:type].to_i != 2 && params[:type].to_i != 4) ? 2 : params[:type].to_i ).last
|
||
else
|
||
code = VerificationCode.where(:email => params[:phone], :code => params[:code], :code_type => params[:type].to_i).last
|
||
end
|
||
req[:valid] = !code.nil? && (Time.now.to_i - code.created_at.to_i) <= 10*60
|
||
render :json => req
|
||
end
|
||
|
||
# 发送验证码:type 1:注册手机验证码 2:找回密码手机验证码 3:找回密码邮箱验证码 4:绑定手机 5:绑定邮箱
|
||
def get_verification_code
|
||
code = %W(0 1 2 3 4 5 6 7 8 9)
|
||
type = params[:type].to_i
|
||
req = Hash.new(false)
|
||
req[:status] = 0
|
||
req[:msg] = ''
|
||
if type == 1
|
||
if User.where(:phone => params[:value]).count > 0
|
||
req[:status] = 2 #已注册
|
||
else
|
||
begin
|
||
verification_code = code.sample(6).join
|
||
status = Trustie::Sms.send(mobile: params[:value], code: verification_code)
|
||
if status
|
||
VerificationCode.create(:phone => params[:value], :status => 1, :code_type => 1, :code => verification_code)
|
||
end
|
||
rescue => e
|
||
Rails.logger.error "发送验证码出错: #{e}"
|
||
end
|
||
req[:status] = 1
|
||
end
|
||
elsif type == 2 || type == 3
|
||
if params[:value] =~ /^[a-zA-Z0-9]+([._\\]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/
|
||
if User.where(:mail => params[:value]).count == 0
|
||
req[:status] = 2 #未注册
|
||
else
|
||
begin
|
||
verification_code = code.sample(6).join
|
||
user = User.where(:mail => params[:value]).first
|
||
token = Token.new(:user => user, :action => "recovery")
|
||
if token.save
|
||
Mailer.run.lost_password(token, verification_code)
|
||
VerificationCode.create(:email => params[:value], :status => 1, :code_type => 3, :code => verification_code)
|
||
end
|
||
rescue => e
|
||
Rails.logger.error "发送验证码出错: #{e}"
|
||
end
|
||
req[:status] = 3
|
||
req[:link] = params[:value].split("@")[1]
|
||
end
|
||
elsif params[:value] =~ /^1\d{10}$/
|
||
if User.where(:phone => params[:value]).count == 0
|
||
req[:status] = 2
|
||
else
|
||
begin
|
||
verification_code = code.sample(6).join
|
||
status = Trustie::Sms.send(mobile: params[:value], code: verification_code)
|
||
if status
|
||
VerificationCode.create(:phone => params[:value], :status => 1, :code_type => 2, :code => verification_code)
|
||
end
|
||
rescue => e
|
||
Rails.logger.error "发送验证码出错: #{e}"
|
||
end
|
||
req[:status] = 1
|
||
end
|
||
else
|
||
req[:status] = 2
|
||
end
|
||
else
|
||
if params[:value] =~ /^[a-zA-Z0-9]+([._\\]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/
|
||
if User.where(:mail => params[:value]).count > 0
|
||
req[:status] = 2 #已绑定
|
||
req[:msg] = '该邮箱已被绑定'
|
||
else
|
||
begin
|
||
verification_code = code.sample(6).join
|
||
user = User.current
|
||
token = Token.new(:user => user, :action => "bind")
|
||
if token.save
|
||
Mailer.run.bind_email(token, verification_code, params[:value])
|
||
VerificationCode.create(:email => params[:value], :status => 1, :code_type => 5, :code => verification_code)
|
||
end
|
||
rescue => e
|
||
Rails.logger.error "发送验证码出错: #{e}"
|
||
end
|
||
req[:status] = 3
|
||
req[:link] = params[:value].split("@")[1]
|
||
end
|
||
elsif params[:value] =~ /^1\d{10}$/
|
||
if User.where(:phone => params[:value]).count > 0
|
||
req[:status] = 2
|
||
req[:msg] = '该手机号已被绑定'
|
||
else
|
||
begin
|
||
verification_code = code.sample(6).join
|
||
status = Trustie::Sms.send(mobile: params[:value], code: verification_code)
|
||
if status
|
||
VerificationCode.create(:phone => params[:value], :status => 1, :code_type => 4, :code => verification_code)
|
||
end
|
||
rescue => e
|
||
Rails.logger.error "发送验证码出错: #{e}"
|
||
end
|
||
req[:status] = 1
|
||
end
|
||
else
|
||
req[:status] = 2
|
||
end
|
||
|
||
end
|
||
render :json => req
|
||
end
|
||
|
||
def avatar
|
||
@user = params[:user_id].nil? ? User.current : User.find(params[:user_id])
|
||
@setting_type = 2
|
||
render :layout => 'base_edu_account'
|
||
end
|
||
|
||
# 实名认证
|
||
def authentication
|
||
@user = User.current
|
||
@apply_user_auth = ApplyUserAuthentication.where(:user_id => @user.id, :auth_type => 1, :status => [0, 2]).order("created_at asc").last
|
||
@setting_type = 3
|
||
render :layout => 'base_edu_account'
|
||
end
|
||
|
||
# 职业认证
|
||
def professional_certification
|
||
@user = User.current
|
||
@apply_user_auth = ApplyUserAuthentication.where(:user_id => @user.id, :auth_type => 2, :status => [0, 2]).order("created_at asc").last
|
||
@setting_type = 4
|
||
render :layout => 'base_edu_account'
|
||
end
|
||
|
||
def apply_auth
|
||
@user = User.current
|
||
@user.lastname = params[:lastname]
|
||
@user.firstname = ""
|
||
@user.ID_number = params[:ID_number]
|
||
@se = @user.extensions
|
||
@se.gender = params[:sex]
|
||
|
||
if @user.save && @se.save
|
||
@user.update_attributes(:authentication => 0)
|
||
diskfile1 = disk_auth_filename('UserAuthentication', @user.id, 'ID')
|
||
diskfileID = diskfile1 + 'temp'
|
||
begin
|
||
FileUtils.mv diskfileID, diskfile1, force: true if File.exist? diskfileID
|
||
ensure
|
||
File.delete(diskfileID) if File.exist?(diskfileID)
|
||
end
|
||
if File.exist?(diskfile1)
|
||
ApplyUserAuthentication.create(:user_id => @user.id, :status => 0, :auth_type => 1)
|
||
end
|
||
end
|
||
redirect_to authentication_account_path
|
||
end
|
||
|
||
def apply_pro_certification
|
||
@user = User.current
|
||
@se = @user.extensions
|
||
@se.school_id = params[:occupation]
|
||
@se.department_id = params[:department_id]
|
||
@se.identity = params[:identity].to_i if params[:identity]
|
||
if @se.identity == 0
|
||
@se.technical_title = params[:te_technical_title] if params[:te_technical_title]
|
||
@se.student_id = nil
|
||
elsif @se.identity == 1
|
||
@se.student_id = params[:no] if params[:no]
|
||
@se.technical_title = nil
|
||
elsif @se.identity == 2
|
||
@se.technical_title = params[:pro_technical_title] if params[:pro_technical_title]
|
||
@se.student_id = nil
|
||
end
|
||
if @user.save && @se.save
|
||
@user.update_attributes(:professional_certification => 0)
|
||
if @se.identity == 1
|
||
ApplyUserAuthentication.create(:user_id => @user.id, :status => 0, :auth_type => 2)
|
||
else
|
||
diskfile2 = disk_auth_filename('UserAuthentication', @user.id, 'PRO')
|
||
diskfilePRO = diskfile2 + 'temp'
|
||
begin
|
||
FileUtils.mv diskfilePRO, diskfile2, force: true if File.exist? diskfilePRO
|
||
ensure
|
||
File.delete(diskfilePRO) if File.exist?(diskfilePRO)
|
||
end
|
||
if File.exist?(diskfile2)
|
||
ApplyUserAuthentication.create(:user_id => @user.id, :status => 0, :auth_type => 2)
|
||
end
|
||
end
|
||
end
|
||
redirect_to professional_certification_account_path
|
||
end
|
||
|
||
# 修改资料时判断学号是否已使用
|
||
def check_user_student_id
|
||
data = {result:0, account:""}
|
||
if params[:student_id] && params[:school_id]
|
||
if UserExtensions.where("student_id = '#{params[:student_id]}' and school_id = #{params[:school_id]} and user_id != #{User.current.id}").count > 0
|
||
user = User.where(:id => UserExtensions.where("student_id = '#{params[:student_id]}' and school_id = #{params[:school_id]} and user_id != #{User.current.id}").map(&:user_id)).first
|
||
data[:account] = user.mail.blank? ? user.user_phone : user.user_mail
|
||
else
|
||
data[:result] = 1
|
||
end
|
||
end
|
||
render :json => data
|
||
end
|
||
|
||
# 实名认证时判断学号是否已使用
|
||
def check_student_id
|
||
data = {result:0, account:""}
|
||
if params[:student_id] && params[:school_id]
|
||
auth_count = User.where(:id => UserExtensions.where(:student_id => params[:student_id], :school_id => params[:school_id]).map(&:user_id), :professional_certification => 1).count
|
||
apply_count = ApplyUserAuthentication.where(:status => 0, :user_id => User.where(:id => UserExtensions.where(:student_id => params[:student_id], :school_id => params[:school_id]).map(&:user_id), :professional_certification => 0).map(&:id)).count
|
||
if auth_count == 0 && apply_count == 0
|
||
data[:result] = 1
|
||
else
|
||
user = auth_count != 0 ? User.where(:id => UserExtensions.where(:student_id => params[:student_id], :school_id => params[:school_id]).map(&:user_id)).first : ApplyUserAuthentication.where(:status => 0, :user_id => User.where(:id => UserExtensions.where(:student_id => params[:student_id], :school_id => params[:school_id]).map(&:user_id), :professional_certification => 0).map(&:id)).first.user
|
||
if user != User.current
|
||
data[:account] = user.mail.blank? ? user.user_phone : user.user_mail
|
||
else
|
||
data[:result] = 1
|
||
end
|
||
end
|
||
end
|
||
render :json => data
|
||
end
|
||
|
||
# 实名认证时判断证件号码是否已使用
|
||
def check_id_number
|
||
data = {result:0, account:""}
|
||
if params[:id_number]
|
||
auth_count = User.where(:ID_number => params[:id_number], :authentication => 1).count
|
||
apply_count = ApplyUserAuthentication.where(:status => 0, :user_id => User.where(:ID_number => params[:id_number], :authentication => 0).map(&:id)).count
|
||
if auth_count == 0 && apply_count == 0
|
||
data[:result] = 1
|
||
else
|
||
user = auth_count != 0 ? User.where(:ID_number => params[:id_number], :authentication => 1).first : ApplyUserAuthentication.where(:status => 0, :user_id => User.where(:ID_number => params[:id_number], :authentication => 0).map(&:id)).first.user
|
||
if user != User.current
|
||
data[:account] = user.mail.blank? ? user.user_phone : user.user_mail
|
||
else
|
||
data[:result] = 1
|
||
end
|
||
end
|
||
end
|
||
render :json => data
|
||
end
|
||
|
||
def cancel_pro_apply
|
||
@apply_user_auth = ApplyUserAuthentication.where(:user_id => User.current.id, :auth_type => params[:auth_type], :status => 0).update_all(:status => 3)
|
||
|
||
if params[:auth_type] == "1"
|
||
redirect_to authentication_account_path
|
||
else
|
||
redirect_to professional_certification_account_path
|
||
end
|
||
end
|
||
|
||
def apply_trail
|
||
apply_action = ApplyAction.where(:user_id => User.current.id, :container_type => "TrialAuthorization", :status => 0).first
|
||
school_ids = School.where(:auto_users_trial => 1).map(&:id)
|
||
if User.current.user_extensions.identity == 1 && !User.current.user_extensions.student_id.nil? && User.current.user_extensions.student_id != "" && !User.current.user_extensions.school.nil? && school_ids.include?(User.current.user_extensions.school_id)
|
||
User.current.update_attributes(:certification => 1)
|
||
@tip = "申请成功,我们将在一分钟内完成审核"
|
||
if apply_action.blank?
|
||
ApplyAction.create(:user_id => User.current.id, :status => 1, :container_type => "TrialAuthorization", :apply_reason => params[:apply_reason])
|
||
else
|
||
apply_action.update_attributes(:status => 1)
|
||
end
|
||
else
|
||
@tip = "申请成功,我们将在一个工作日内完成审核"
|
||
ApplyAction.create(:user_id => User.current.id, :status => 0, :container_type => "TrialAuthorization", :apply_reason => params[:apply_reason])
|
||
end
|
||
|
||
respond_to do |format|
|
||
format.js
|
||
end
|
||
end
|
||
|
||
def user_auth
|
||
@user = User.current
|
||
diskfile1 = disk_auth_filename('UserAuthentication', @user.id, 'ID')
|
||
diskfile2 = disk_auth_filename('UserAuthentication', @user.id, 'PRO')
|
||
diskfileID = diskfile1 + 'temp'
|
||
diskfilePRO = diskfile2 + 'temp'
|
||
begin
|
||
FileUtils.mv diskfileID, diskfile1, force: true if File.exist? diskfileID
|
||
FileUtils.mv diskfilePRO, diskfile2, force: true if File.exist? diskfilePRO
|
||
ensure
|
||
File.delete(diskfileID) if File.exist?(diskfileID)
|
||
File.delete(diskfilePRO) if File.exist?(diskfilePRO)
|
||
end
|
||
if File.exist?(diskfile1) && File.exist?(diskfile2)
|
||
ApplyUserAuthentication.create(:user_id => @user.id, :status => 0)
|
||
end
|
||
|
||
redirect_to authentication_account_path
|
||
end
|
||
|
||
def security_settings
|
||
@user = User.current
|
||
@setting_type = 5
|
||
render :layout => 'base_edu_account'
|
||
end
|
||
|
||
def change_psd
|
||
@user = User.current
|
||
@setting_type = 6
|
||
render :layout => 'base_edu_account'
|
||
end
|
||
|
||
# 修改密码时判断密码是否输入正确
|
||
def valid_psd
|
||
@user = User.current
|
||
req = Hash.new(false)
|
||
req[:valid] = false
|
||
req[:valid] = @user.check_password?(params[:value])
|
||
render :json => req
|
||
end
|
||
|
||
def change_or_bind
|
||
@user = User.current
|
||
@type = params[:type]
|
||
@setting_type = 5
|
||
render :layout => 'base_edu_account'
|
||
end
|
||
|
||
def bind_email_or_phone
|
||
@user = User.current
|
||
begin
|
||
ActiveRecord::Base.transaction do
|
||
if params[:type] == "phone"
|
||
@user.update_attributes!(:phone => params[:value])
|
||
reward_grade(@user, @user.id, 'Phone', 500)
|
||
else
|
||
@user.update_attributes!(:mail => params[:value])
|
||
reward_grade(@user, @user.id, 'Mail', 500)
|
||
end
|
||
end
|
||
rescue
|
||
raise ActiveRecord::Rollback
|
||
end
|
||
redirect_to security_settings_path
|
||
end
|
||
|
||
def wechat_bind
|
||
respond_to do |format|
|
||
format.html { render :layout => "login_bigdata"}
|
||
end
|
||
end
|
||
|
||
def reset_psd
|
||
if request.get?
|
||
@user = User.where("phone = '#{params[:value]}' or mail = '#{params[:value]}'").first
|
||
if @user
|
||
respond_to do |format|
|
||
format.html { render :layout => "login_bigdata"}
|
||
end
|
||
else
|
||
redirect_to signin_path
|
||
return
|
||
end
|
||
else
|
||
@user = User.find params[:user]
|
||
if @user
|
||
@user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
|
||
if @user.save
|
||
Token.where(:user_id => @user, :action => "recovery").destroy_all
|
||
respond_to do |format|
|
||
format.js
|
||
end
|
||
else
|
||
redirect_to signin_path
|
||
return
|
||
end
|
||
else
|
||
redirect_to signin_path
|
||
return
|
||
end
|
||
end
|
||
end
|
||
|
||
def email_valid
|
||
begin
|
||
@mail_type = params[:mail].split("@")[1]
|
||
@user = User.find(params[:user_id])
|
||
rescue
|
||
return render_404
|
||
end
|
||
respond_to do |format|
|
||
format.html { render :layout => "login_bigdata"}
|
||
format.js
|
||
end
|
||
end
|
||
|
||
def resendmail
|
||
result = {:status=>1, :email=>""}
|
||
user = User.find(params[:user]) if params[:user]
|
||
result[:email] = user.mail
|
||
token = Token.new(:user => user, :action => "register")
|
||
if token.save
|
||
# Mailer.run.register(token)
|
||
Mailer.register(token).deliver
|
||
else
|
||
yield if block_given?
|
||
result[:status] = 0
|
||
end
|
||
render :json => result
|
||
end
|
||
|
||
def change_email
|
||
user = User.find params[:user_id].to_i
|
||
user.update_attributes(:mail => params[:value])
|
||
token = Token.create(:user => user, :action => "register")
|
||
Mailer.run.register(token)
|
||
result = {:email => user.mail, :email_link => user.mail.split("@")[1]}
|
||
|
||
render :json => result
|
||
end
|
||
|
||
def email_activation
|
||
|
||
end
|
||
private
|
||
|
||
def authenticate_user
|
||
if Setting.openid? && using_open_id?
|
||
open_id_authenticate(params[:openid_url])
|
||
else
|
||
password_authentication
|
||
end
|
||
end
|
||
|
||
def password_authentication
|
||
user, last_login_on = User.try_to_login(params[:username], params[:password])
|
||
|
||
if user.nil?
|
||
invalid_credentials
|
||
elsif user.status == 2
|
||
@user = user
|
||
invalid_credentials_new
|
||
elsif user.new_record?
|
||
onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id })
|
||
else
|
||
# Valid user
|
||
successful_authentication(user, last_login_on)
|
||
end
|
||
end
|
||
|
||
def open_id_authenticate(openid_url)
|
||
back_url = signin_url(:autologin => params[:autologin])
|
||
|
||
authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => back_url, :method => :post) do |result, identity_url, registration|
|
||
if result.successful?
|
||
user = User.find_or_initialize_by_identity_url(identity_url)
|
||
if user.new_record?
|
||
# Self-registration off
|
||
(redirect_to(signin_path); return) unless Setting.self_registration?
|
||
|
||
# Create on the fly
|
||
user.login = registration['nickname'] unless registration['nickname'].nil?
|
||
user.mail = registration['email'] unless registration['email'].nil?
|
||
user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
|
||
user.random_password
|
||
user.register
|
||
|
||
case Setting.self_registration
|
||
when '1'
|
||
register_by_email_activation(user) do
|
||
onthefly_creation_failed(user)
|
||
end
|
||
when '3'
|
||
register_automatically(user) do
|
||
onthefly_creation_failed(user)
|
||
end
|
||
else
|
||
register_manually_by_administrator(user) do
|
||
onthefly_creation_failed(user)
|
||
end
|
||
end
|
||
else
|
||
# Existing record
|
||
if user.active?
|
||
successful_authentication(user)
|
||
else
|
||
account_pending
|
||
end
|
||
end
|
||
end
|
||
end
|
||
end
|
||
|
||
def successful_authentication(user, last_login_on)
|
||
logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}"
|
||
# Valid user
|
||
self.logged_user = user
|
||
# generate a key and set cookie if autologin
|
||
if params[:autologin] && Setting.autologin?
|
||
set_autologin_cookie(user)
|
||
end
|
||
call_hook(:controller_account_success_authentication_after, {:user => user })
|
||
|
||
code = /\d*/
|
||
# 记录用户登录行为
|
||
UserActions.create(:action_id => User.current.id, :action_type => "Login", :user_id => User.current.id)
|
||
#根据home_url生产正则表达式
|
||
eval("code = " + "/^" + home_url.gsub(/\//,"\\\/") + "\\\/*(welcome)?\\\/*(\\\/index\\\/*.*)?\$/")
|
||
if (code=~params[:back_url] || params[:back_url].to_s.include?('lost_password')) && last_login_on != ''
|
||
redirect_to user_activities_path(user,host: Setting.host_user)
|
||
else
|
||
if last_login_on == ''
|
||
redirect_to my_account_url
|
||
else
|
||
#by young
|
||
#redirect_back_or_default my_page_path
|
||
# 基本资料不完善的用户,将强制用户完善基本资料。
|
||
user = UserExtensions.where(:user_id => User.current.id).first
|
||
if user.gender.nil? || user.school_id.nil? || User.current.lastname.nil?
|
||
redirect_to my_account_path(:tip => 1)
|
||
elsif user.identity == 3 && user.school_id.nil?
|
||
redirect_to my_account_path(:tip => 1)
|
||
else
|
||
redirect_back_or_default User.current
|
||
#redirect_to my_account_url
|
||
#redirect_to User.current
|
||
end
|
||
end
|
||
end
|
||
end
|
||
|
||
def set_autologin_cookie(user)
|
||
token = Token.get_or_create_permanent_login_token(user)
|
||
cookie_options = {
|
||
:value => token.value,
|
||
:expires => 1.month.from_now,
|
||
:path => (Redmine::Configuration['autologin_cookie_path'] || '/'),
|
||
:secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false),
|
||
:httponly => true
|
||
}
|
||
if Redmine::Configuration['cookie_domain'].present?
|
||
cookie_options = cookie_options.merge(domain: Redmine::Configuration['cookie_domain'])
|
||
end
|
||
cookies[autologin_cookie_name] = cookie_options
|
||
end
|
||
|
||
# Onthefly creation failed, display the registration form to fill/fix attributes
|
||
def onthefly_creation_failed(user, auth_source_options = { })
|
||
@user = user
|
||
session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
|
||
render :action => 'register'
|
||
end
|
||
|
||
def invalid_credentials
|
||
logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
|
||
flash[:error] = l(:notice_account_invalid_creditentials)
|
||
# render :layout => 'login'
|
||
redirect_to signin_path(:login=>true)
|
||
end
|
||
|
||
def invalid_credentials_new
|
||
logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
|
||
# flash[:error] = l(:notice_account_invalid_creditentials_new)
|
||
# render signin_path(:login=>true)
|
||
render :action => 'email_activation',:layout => 'login_bigdata'
|
||
end
|
||
|
||
# Register a user for email activation.
|
||
#
|
||
# Pass a block for behavior when a user fails to save
|
||
def register_by_email_activation(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.run.register(token)
|
||
|
||
|
||
flash[:notice] = l(:notice_account_register_done)
|
||
|
||
render action: 'email_valid', locals: {:mail => user.mail}
|
||
else
|
||
yield if block_given?
|
||
end
|
||
end
|
||
|
||
# Automatically register a user
|
||
#
|
||
# Pass a block for behavior when a user fails to save
|
||
def register_automatically(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
|
||
end
|
||
|
||
# Manual activation by the administrator
|
||
#
|
||
# Pass a block for behavior when a user fails to save
|
||
def register_manually_by_administrator(user, &block)
|
||
if user.save
|
||
UserStatus.create(:user_id => user.id ,:changsets_count => 0, :watchers_count => 0)
|
||
# Sends an email to the administrators
|
||
Mailer.run.account_activation_request(user)
|
||
account_pending
|
||
else
|
||
yield if block_given?
|
||
end
|
||
end
|
||
|
||
def account_pending
|
||
flash[:notice] = l(:notice_account_pending)
|
||
redirect_to signin_url
|
||
end
|
||
end
|