2016-09-07 14:19:07 +08:00
|
|
|
|
#coding=utf-8
|
2014-12-17 14:50:57 +08:00
|
|
|
|
class UsersService
|
|
|
|
|
include ApplicationHelper
|
|
|
|
|
include AccountHelper
|
|
|
|
|
include AvatarHelper
|
|
|
|
|
include CoursesHelper
|
2015-01-20 17:42:18 +08:00
|
|
|
|
include ApiHelper
|
2015-05-20 17:27:17 +08:00
|
|
|
|
include WordsHelper
|
2014-12-17 14:50:57 +08:00
|
|
|
|
#将用户注册的功能函数写这里
|
|
|
|
|
#参数约定
|
|
|
|
|
#成功返回注册后的User实例,失败直接抛异常
|
|
|
|
|
|
|
|
|
|
def register(params)
|
|
|
|
|
@user = User.new
|
|
|
|
|
@user.admin = false
|
|
|
|
|
@user.register
|
|
|
|
|
@user.login = params[:login]
|
|
|
|
|
@user.mail = params[:mail]
|
|
|
|
|
password = params[:password]
|
|
|
|
|
password_confirmation = params[:password_confirmation]
|
|
|
|
|
should_confirmation_password = params[: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
|
|
|
|
|
else
|
|
|
|
|
@user.password = ""
|
|
|
|
|
end
|
|
|
|
|
case Setting.self_registration
|
|
|
|
|
when '1'
|
|
|
|
|
@user = email_activation_register(@user)
|
|
|
|
|
when '3'
|
|
|
|
|
@user = automatically_register(@user)
|
|
|
|
|
else
|
|
|
|
|
@user = administrator_manually__register(@user)
|
|
|
|
|
end
|
|
|
|
|
if @user.id != nil
|
|
|
|
|
ue = @user.user_extensions ||= UserExtensions.new
|
|
|
|
|
ue.user_id = @user.id
|
|
|
|
|
ue.save
|
|
|
|
|
end
|
2015-01-20 17:42:18 +08:00
|
|
|
|
@user
|
|
|
|
|
#img_url = url_to_avatar(@user)
|
|
|
|
|
#gender = @user.user_extensions.gender.nil? ? 0 : @user.user_extensions.gender
|
|
|
|
|
#work_unit = get_user_work_unit @user
|
|
|
|
|
#location = get_user_location @user
|
|
|
|
|
#{:id => @user.id, :img_url => img_url, :nickname => @user.login, :gender => gender, :work_unit => work_unit, :mail => @user.mail, :location => location, :brief_introduction => @user.user_extensions.brief_introduction}
|
2014-12-17 14:50:57 +08:00
|
|
|
|
end
|
2015-04-22 16:49:07 +08:00
|
|
|
|
|
|
|
|
|
# 自动注册功能 FOR:邮件邀请
|
2015-10-09 10:33:10 +08:00
|
|
|
|
def register_auto(login, mail, password, first_name, last_name, gender)
|
|
|
|
|
mail_notification = "day"
|
2015-02-11 23:34:10 +08:00
|
|
|
|
@user = User.new
|
|
|
|
|
@user.admin = false
|
|
|
|
|
@user.register
|
|
|
|
|
@user.login = login
|
2015-04-22 16:49:07 +08:00
|
|
|
|
@user.mail = mail
|
2015-10-09 10:33:10 +08:00
|
|
|
|
@user.firstname = first_name
|
|
|
|
|
@user.lastname = last_name
|
|
|
|
|
@user.mail_notification = mail_notification
|
2015-02-11 23:34:10 +08:00
|
|
|
|
password_confirmation = password
|
|
|
|
|
should_confirmation_password = true
|
|
|
|
|
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
|
|
|
|
|
else
|
|
|
|
|
@user.password = ""
|
|
|
|
|
end
|
2015-05-27 17:40:18 +08:00
|
|
|
|
@user = automatically_register_lock(@user)
|
2015-02-11 23:34:10 +08:00
|
|
|
|
if @user.id != nil
|
|
|
|
|
ue = @user.user_extensions ||= UserExtensions.new
|
2015-10-09 10:33:10 +08:00
|
|
|
|
ue.gender = gender
|
2015-02-11 23:34:10 +08:00
|
|
|
|
ue.user_id = @user.id
|
|
|
|
|
ue.save
|
|
|
|
|
end
|
|
|
|
|
@user
|
|
|
|
|
end
|
2015-04-22 16:49:07 +08:00
|
|
|
|
|
2014-12-17 14:50:57 +08:00
|
|
|
|
#显示用户
|
|
|
|
|
#id用户id
|
|
|
|
|
def show_user(params)
|
|
|
|
|
@user = User.find(params[:id])
|
2016-08-09 14:09:13 +08:00
|
|
|
|
img_url = "/images/"+url_to_avatar(@user)
|
2014-12-17 14:50:57 +08:00
|
|
|
|
gender = @user.user_extensions.gender.nil? ? 0 : @user.user_extensions.gender
|
2014-12-23 17:43:53 +08:00
|
|
|
|
work_unit = get_user_work_unit @user
|
|
|
|
|
location = get_user_location @user
|
2015-04-16 15:48:34 +08:00
|
|
|
|
{:id => @user.id, :img_url => img_url,:realname => @user.realname, :nickname => @user.login, :gender => gender, :work_unit => work_unit, :mail => @user.mail, :location => location, :brief_introduction => @user.user_extensions.brief_introduction}
|
2014-12-17 14:50:57 +08:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-12 16:33:31 +08:00
|
|
|
|
#忘记密码
|
|
|
|
|
def lost_password params
|
|
|
|
|
user = ::User.find_by_mail(params[:mail].to_s)
|
|
|
|
|
# user not found or not active
|
|
|
|
|
unless user && user.active?
|
|
|
|
|
raise l(:notice_account_unknown_email,:locale => 'zh')
|
|
|
|
|
end
|
|
|
|
|
# user cannot change its password
|
|
|
|
|
unless user.change_password_allowed?
|
|
|
|
|
raise l(:notice_can_t_change_password,:locale => user.language)
|
|
|
|
|
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)
|
2015-03-12 16:33:31 +08:00
|
|
|
|
return l(:notice_account_lost_email_sent,:locale => user.language)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-17 14:50:57 +08:00
|
|
|
|
#编辑用户
|
|
|
|
|
#gender 1:female 0:male 其他:male
|
|
|
|
|
def edit_user params
|
|
|
|
|
@user = User.find(params[:id])
|
|
|
|
|
fileio = params[:file]
|
|
|
|
|
|
|
|
|
|
@se = @user.extensions
|
|
|
|
|
if @user.user_extensions.identity == 0 || @user.user_extensions.identity == 1
|
|
|
|
|
@se.school_id = params[:occupation]
|
|
|
|
|
elsif @user.user_extensions.identity == 3
|
|
|
|
|
@se.occupation = params[:occupation]
|
|
|
|
|
elsif @user.user_extensions.identity == 2
|
|
|
|
|
@user.firstname = params[:occupation]
|
|
|
|
|
end
|
|
|
|
|
@se.brief_introduction = params[:brief_introduction]
|
|
|
|
|
@se.gender = params[:gender]
|
|
|
|
|
@se.location = params[:province] if params[:province]
|
|
|
|
|
@se.location_city = params[:city] if params[:city]
|
|
|
|
|
raise @se.errors.full_message unless @se.save
|
|
|
|
|
unless fileio.nil?
|
|
|
|
|
file = fileio[:tempfile]
|
|
|
|
|
diskfile=disk_filename(@user.class.to_s, @user.id)
|
|
|
|
|
@image_file = fileio[:name]
|
|
|
|
|
@urlfile='/' << File.join("images", "avatars", avatar_directory(@user.class.to_s), avatar_filename(@user.id, @image_file))
|
|
|
|
|
|
|
|
|
|
path = File.dirname(diskfile)
|
|
|
|
|
unless File.directory?(path)
|
|
|
|
|
FileUtils.mkdir_p(path)
|
|
|
|
|
end
|
|
|
|
|
File.rename(file.path, @urlfile)
|
|
|
|
|
begin
|
|
|
|
|
f = Magick::ImageList.new(diskfile)
|
|
|
|
|
# gif格式不再做大小处理
|
|
|
|
|
if f.format != 'GIF'
|
|
|
|
|
width = 300.0
|
|
|
|
|
proportion = (width/f[0].columns)
|
|
|
|
|
height = (f[0].rows*proportion)
|
|
|
|
|
f.resize_to_fill!(width, height)
|
|
|
|
|
f.write(diskfile)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
rescue Exception => e
|
|
|
|
|
logger.error "[Error] avatar : users_service#edit_user ===> #{e}"
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-01-20 17:42:18 +08:00
|
|
|
|
#img_url = url_to_avatar(@user)
|
|
|
|
|
#gender = @user.user_extensions.gender.nil? ? 0 : @user.user_extensions.gender
|
|
|
|
|
#work_unit = get_user_work_unit @user
|
|
|
|
|
#location = get_user_location @user
|
|
|
|
|
#{:id => @user.id, :img_url => img_url, :nickname => @user.login, :gender => gender, :work_unit => work_unit, :mail => @user.mail, :location => location, :brief_introduction => @user.user_extensions.brief_introduction}
|
|
|
|
|
@user
|
2014-12-23 17:43:53 +08:00
|
|
|
|
end
|
|
|
|
|
|
2015-05-20 17:27:17 +08:00
|
|
|
|
# 获取某个用户的所有留言信息
|
|
|
|
|
def get_all_messages params
|
|
|
|
|
user = User.find(params[:user_id])
|
2015-05-23 17:13:38 +08:00
|
|
|
|
jours = user.journals_for_messages.where('m_parent_id IS NULL').order('created_on DESC').page(params[:page] || 1).per(10)
|
2015-05-20 17:27:17 +08:00
|
|
|
|
jours.update_all(:is_readed => true, :status => false)
|
|
|
|
|
jours.each do |journal|
|
|
|
|
|
fetch_user_leaveWord_reply(journal).update_all(:is_readed => true, :status => false)
|
|
|
|
|
end
|
|
|
|
|
jours
|
|
|
|
|
end
|
|
|
|
|
|
2015-05-21 15:57:35 +08:00
|
|
|
|
# 回复用户
|
|
|
|
|
def reply_user_messages params,current_user
|
2015-05-20 17:27:17 +08:00
|
|
|
|
user = User.find(params[:user_id])
|
2015-05-28 17:51:50 +08:00
|
|
|
|
|
|
|
|
|
m_parent_id = params[:parent_id]
|
2015-05-21 15:57:35 +08:00
|
|
|
|
author_id = current_user.id
|
2015-05-28 17:51:50 +08:00
|
|
|
|
reply_id = params[:ref_user_id]
|
|
|
|
|
ref_message_id = params[:ref_message_id]
|
2015-05-21 15:57:35 +08:00
|
|
|
|
content = params[:content]
|
2015-05-28 17:51:50 +08:00
|
|
|
|
options = {:user_id => author_id, # 作者id
|
2015-05-21 15:57:35 +08:00
|
|
|
|
:status => true,
|
2015-05-28 17:51:50 +08:00
|
|
|
|
:m_parent_id => m_parent_id,# 父留言id
|
|
|
|
|
:m_reply_id => ref_message_id, # 子留言 id
|
|
|
|
|
:reply_id => reply_id, # 被留言用户id
|
2015-05-21 15:57:35 +08:00
|
|
|
|
:notes => content,
|
|
|
|
|
:is_readed => false}
|
2015-05-28 17:51:50 +08:00
|
|
|
|
if(params[:type] == 1)
|
|
|
|
|
user.add_jour(nil, nil,nil,options)
|
|
|
|
|
elsif(params[:type] == 2)
|
|
|
|
|
Course.find(params[:course_id]).journals_for_messages.build(options).save! unless params[:course_id].nil?
|
|
|
|
|
else
|
|
|
|
|
end
|
|
|
|
|
|
2015-05-20 17:27:17 +08:00
|
|
|
|
end
|
2014-12-23 17:43:53 +08:00
|
|
|
|
|
2015-05-26 09:48:22 +08:00
|
|
|
|
# 给用户留言
|
|
|
|
|
def leave_message params,current_user
|
|
|
|
|
obj = User.find(params[:user_id]).add_jour(current_user, params[:content], 0)
|
|
|
|
|
obj
|
|
|
|
|
end
|
2015-01-20 17:42:18 +08:00
|
|
|
|
|
2014-12-17 14:50:57 +08:00
|
|
|
|
|
|
|
|
|
#关注列表
|
|
|
|
|
def user_watcher params
|
|
|
|
|
@user = User.find(params[:id])
|
|
|
|
|
User.watched_by(@user.id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#用户课程列表
|
2014-12-23 15:38:45 +08:00
|
|
|
|
def user_courses_list params,current_user
|
|
|
|
|
@user = User.find(params[:id])
|
|
|
|
|
if !current_user.admin? && !@user.active?
|
2014-12-17 14:50:57 +08:00
|
|
|
|
raise '404'
|
|
|
|
|
return
|
|
|
|
|
end
|
2014-12-23 15:38:45 +08:00
|
|
|
|
if current_user == @user || current_user.admin?
|
2014-12-17 14:50:57 +08:00
|
|
|
|
membership = @user.coursememberships.all
|
|
|
|
|
else
|
2015-01-21 11:28:09 +08:00
|
|
|
|
membership = @user.coursememberships.all(:conditions => Course.visible_condition(current_user))
|
2014-12-17 14:50:57 +08:00
|
|
|
|
end
|
|
|
|
|
membership.sort! {|older, newer| newer.created_on <=> older.created_on }
|
2015-01-06 17:46:16 +08:00
|
|
|
|
course_list = []
|
|
|
|
|
membership.each do |mp|
|
2016-08-09 14:09:13 +08:00
|
|
|
|
course_list << {:course => mp.course,:img_url => "/images/"+url_to_avatar(mp.course),:current_user_is_member => current_user.member_of_course?(mp.course),:current_user_is_teacher => is_course_teacher(current_user,mp.course)}
|
2015-01-06 17:46:16 +08:00
|
|
|
|
end
|
|
|
|
|
course_list
|
2014-12-17 14:50:57 +08:00
|
|
|
|
end
|
2015-12-11 17:56:26 +08:00
|
|
|
|
|
2014-12-17 14:50:57 +08:00
|
|
|
|
#修改密码
|
|
|
|
|
def change_password params
|
|
|
|
|
@current_user = User.find(params[:current_user_id])
|
|
|
|
|
if @current_user.check_password?(params[:password])
|
|
|
|
|
@current_user.password, @current_user.password_confirmation = params[:new_password], params[:new_password_confirmation]
|
|
|
|
|
@current_user.save
|
2015-12-10 15:54:44 +08:00
|
|
|
|
# 修改密码同步gitlab密码修改
|
|
|
|
|
unless @current_user.gid.nil?
|
|
|
|
|
begin
|
|
|
|
|
g = Gitlab.client
|
|
|
|
|
g.edit_user(@current_user.gid, :password => params[:new_password])
|
|
|
|
|
rescue Exception => e
|
|
|
|
|
logger.error "change users password failed! ===> #{e}"
|
|
|
|
|
end
|
|
|
|
|
end
|
2014-12-17 14:50:57 +08:00
|
|
|
|
#raise @current_user.errors.full_message
|
|
|
|
|
#return @current_user
|
|
|
|
|
else
|
2015-04-08 18:20:27 +08:00
|
|
|
|
raise l(:notice_account_wrong_password,:locale => 'zh')
|
2014-12-17 14:50:57 +08:00
|
|
|
|
end
|
|
|
|
|
@current_user
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#搜索用户
|
|
|
|
|
def search_user params
|
2015-04-10 16:43:42 +08:00
|
|
|
|
status = params[:status] || 1
|
2014-12-17 14:50:57 +08:00
|
|
|
|
has = {
|
|
|
|
|
"show_changesets" => true
|
|
|
|
|
}
|
2015-04-10 16:43:42 +08:00
|
|
|
|
scope = User.logged.status(status)
|
2015-06-01 12:39:58 +08:00
|
|
|
|
search_by = params[:search_by] ? params[:search_by] : "0"
|
2015-05-13 09:56:34 +08:00
|
|
|
|
if params[:is_search_assitant].nil?
|
2015-05-18 16:41:04 +08:00
|
|
|
|
#modify by yutao 2015/5/18 没有params[:user_id]参数时去掉"id not in (?)"条件(bug:#2270) start
|
|
|
|
|
#say by yutao: params[:user_id]这个是指谁发起的搜索么? 如果是 这个值貌似应该从session获取 怪怪的赶脚-_-!
|
|
|
|
|
if params[:name].present?
|
|
|
|
|
if !params[:user_id].nil?
|
|
|
|
|
watcher = User.watched_by(params[:user_id])
|
|
|
|
|
watcher.push(params[:user_id])
|
|
|
|
|
scope = scope.where("id not in (?)",watcher)
|
|
|
|
|
end
|
2015-06-05 11:39:55 +08:00
|
|
|
|
#scope = scope.like(params[:name],search_by)
|
|
|
|
|
scope = scope.where("( LOWER(login) LIKE ? or LOWER(concat(lastname, firstname)) LIKE ? or LOWER(mail) LIKE ? )",
|
|
|
|
|
"%#{params[:name]}%","%#{params[:name]}%","%#{params[:name]}%")
|
2015-05-18 16:41:04 +08:00
|
|
|
|
end
|
|
|
|
|
#modify by yutao 2015/5/18 没有params[:user_id]参数时去掉"id not in (?)"条件 end
|
2015-05-13 09:56:34 +08:00
|
|
|
|
else
|
2015-05-15 09:06:36 +08:00
|
|
|
|
teachers = searchTeacherAndAssistant(Course.find(params[:course_id]))
|
|
|
|
|
scope = scope.where("id not in (?)",teachers.map{|t| t.user_id}).like(params[:name],search_by) if params[:name].present?
|
2015-05-13 09:56:34 +08:00
|
|
|
|
end
|
2014-12-17 14:50:57 +08:00
|
|
|
|
scope
|
|
|
|
|
end
|
|
|
|
|
|
2015-05-28 17:51:50 +08:00
|
|
|
|
# 课程留言中与我相关的回复
|
|
|
|
|
def my_course_messages params,current_user
|
|
|
|
|
#找到我所有的课程
|
|
|
|
|
@user = current_user
|
|
|
|
|
if !current_user.admin? && !@user.active?
|
|
|
|
|
raise '404'
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
if current_user == @user || current_user.admin?
|
|
|
|
|
membership = @user.coursememberships.all
|
|
|
|
|
end
|
|
|
|
|
# membership.sort! {|older, newer| newer.created_on <=> older.created_on }
|
|
|
|
|
message_list = []
|
|
|
|
|
membership.each do |mp|
|
|
|
|
|
#课程轮询找到与我相关的回复
|
|
|
|
|
message_list << mp.course.journals_for_messages.where("reply_id = ?",current_user.id)
|
|
|
|
|
end
|
|
|
|
|
message_list
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# 获取与我相关的留言:我的留言,回复我的留言
|
|
|
|
|
def my_personal_messages params,current_user
|
|
|
|
|
jours = current_user.journals_for_messages.where('m_parent_id is null or reply_id = ?',current_user.id)
|
|
|
|
|
jours.update_all(:is_readed => true, :status => false)
|
|
|
|
|
jours
|
|
|
|
|
end
|
|
|
|
|
|
2015-05-29 14:22:43 +08:00
|
|
|
|
# 所有的与我相关
|
2015-05-28 17:51:50 +08:00
|
|
|
|
def reply_my_messages params,current_user
|
|
|
|
|
jours = my_personal_messages params,current_user
|
|
|
|
|
jours1 = my_course_messages params,current_user
|
|
|
|
|
my_jours = []
|
|
|
|
|
my_jours << jours << jours1
|
|
|
|
|
my_jours.flatten!.sort! {|older, newer| newer.created_on <=> older.created_on }
|
|
|
|
|
my_jours_arr = Kaminari.paginate_array(my_jours, total_count: my_jours.count).page(params[:page] || 1).per(10)
|
|
|
|
|
my_jours_arr
|
|
|
|
|
end
|
|
|
|
|
|
2016-09-05 11:02:34 +08:00
|
|
|
|
def wechat_unbind uw
|
2016-09-06 14:41:22 +08:00
|
|
|
|
user = uw.user
|
|
|
|
|
|
|
|
|
|
#发重新绑定的微信模版消息
|
2016-09-07 14:19:07 +08:00
|
|
|
|
|
|
|
|
|
type = "login"
|
|
|
|
|
title = "尊敬的用户,您已解除绑定。"
|
|
|
|
|
key1 = "个人原因"
|
|
|
|
|
remark = "点击进入重新绑定。"
|
|
|
|
|
|
2016-09-06 14:41:22 +08:00
|
|
|
|
ws = WechatService.new
|
2016-09-07 14:19:07 +08:00
|
|
|
|
ws.rebind_notice user.id, type, user.id, title, key1,format_time(Time.now), remark
|
2016-09-06 14:41:22 +08:00
|
|
|
|
|
2016-09-05 11:02:34 +08:00
|
|
|
|
uw.user_id = nil
|
|
|
|
|
uw.delete
|
2016-09-06 14:41:22 +08:00
|
|
|
|
|
2016-09-05 11:02:34 +08:00
|
|
|
|
end
|
2016-07-02 23:46:45 +08:00
|
|
|
|
|
2014-12-17 14:50:57 +08:00
|
|
|
|
end
|