socialforge/app/controllers/account_controller.rb

432 lines
14 KiB
Ruby
Raw Normal View History

# 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
# Login request and validation
def login
if request.get?
2015-08-25 16:39:22 +08:00
@login = params[:login] || true
if User.current.logged?
2015-08-29 15:25:58 +08:00
redirect_to user_path(User.current)
2015-08-25 14:24:20 +08:00
else
render :layout => 'login'
end
else
authenticate_user
end
end
2015-08-26 16:50:26 +08:00
# 服务协议
2015-08-26 16:31:28 +08:00
def agreement
2015-08-26 16:50:26 +08:00
render :layout => 'static_base'
2015-08-26 16:31:28 +08:00
end
# Log out current user and redirect to welcome page
def logout
if User.current.anonymous?
2015-08-25 16:39:22 +08:00
redirect_to signin_path
elsif request.post?
logout_user
2015-08-25 16:39:22 +08:00
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
2015-08-29 15:25:58 +08:00
(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?
2015-08-27 15:15:06 +08:00
redirect_to signin_path
return
end
@user = @token.user
unless @user && @user.active?
2015-08-27 15:15:06 +08:00
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
2015-08-27 14:11:56 +08:00
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)
2015-08-26 17:52:23 +08:00
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
2015-03-31 11:42:36 +08:00
Mailer.run.lost_password(token)
flash[:notice] = l(:notice_account_lost_email_sent)
2015-08-26 17:52:23 +08:00
redirect_to lost_password_path
return
end
end
2015-08-26 17:52:23 +08:00
render :layout => 'static_base'
end
end
# User self-registration
def register
2015-08-29 15:25:58 +08:00
(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
end
else
2014-12-06 11:27:52 +08:00
us = UsersService.new
@user = us.register user_params.merge(:should_confirmation_password => true)
case Setting.self_registration
when '1'
2014-12-04 15:53:47 +08:00
#register_by_email_activation(@user)
unless @user.new_record?
flash[:notice] = l(:notice_account_register_done)
render action: 'email_valid', locals: {:mail => @user.mail}
2014-12-04 15:53:47 +08:00
end
when '3'
2014-12-04 15:53:47 +08:00
#register_automatically(@user)
2015-08-25 14:24:20 +08:00
if !@user.new_record?
2014-12-06 11:27:52 +08:00
self.logged_user = @user
2014-12-04 15:53:47 +08:00
flash[:notice] = l(:notice_account_activated)
redirect_to my_account_url
2015-08-25 14:24:20 +08:00
else
redirect_to signin_path
2014-12-04 15:53:47 +08:00
end
else
2014-12-04 15:53:47 +08:00
#register_manually_by_administrator(@user)
unless @user.new_record?
account_pending
end
2014-12-06 11:27:52 +08:00
end
end
end
end
#should_confirmation_password是否验证密码
def create_and_save_user login,password,email,password_confirmation,should_confirmation_password
2014-12-03 11:16:14 +08:00
@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?
2014-12-03 11:16:14 +08:00
@user.password = password
else
@user.password = ""
2014-12-03 11:16:14 +08:00
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
2015-08-29 15:25:58 +08:00
(redirect_to(signin_path); return) unless Setting.self_registration? && params[:token].present?
token = Token.find_token('register', params[:token].to_s)
2015-08-29 15:25:58 +08:00
(redirect_to(signin_path); return) unless token and !token.expired?
user = token.user
2015-08-29 15:25:58 +08:00
(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
end
2014-12-03 17:28:19 +08:00
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?('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 email_valid
end
def resendmail
user = User.find(params[:user]) if params[:user]
token = Token.new(:user => user, :action => "register")
if token.save
2015-03-31 11:42:36 +08:00
Mailer.run.register(token)
else
yield if block_given?
end
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
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
2015-08-29 15:25:58 +08:00
(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*/
2014-11-23 09:36:47 +08:00
#根据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)
2014-11-23 09:36:47 +08:00
else
if last_login_on == ''
redirect_to my_account_url
else
2014-11-23 09:36:47 +08:00
#by young
#redirect_back_or_default my_page_path
redirect_back_or_default User.current
#redirect_to my_account_url
2014-11-23 09:36:47 +08:00
#redirect_to User.current
end
end
end
def set_autologin_cookie(user)
2015-05-05 11:45:58 +08:00
token = Token.get_or_create_permanent_login_token(user)
cookie_options = {
:value => token.value,
2015-05-15 10:27:26 +08:00
: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.now[:error] = l(:notice_account_invalid_creditentials)
2015-08-25 14:24:20 +08:00
render :layout => 'login'
end
def invalid_credentials_new
logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
flash.now[:error] = l(:notice_account_invalid_creditentials_new)
2015-08-25 14:24:20 +08:00
render :layout => 'login'
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)
2015-03-31 11:42:36 +08:00
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
2015-03-31 11:42:36 +08:00
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