2020-03-09 00:40:16 +08:00
class UsersController < ApplicationController
2020-06-03 18:23:53 +08:00
before_action :load_user , only : [ :show , :homepage_info , :sync_token , :sync_gitea_pwd , :projects , :watch_users , :fan_users ]
before_action :check_user_exist , only : [ :show , :homepage_info , :projects , :watch_users , :fan_users ]
2020-04-17 10:32:16 +08:00
before_action :require_login , only : % i [ me list projects ]
2020-03-09 00:40:16 +08:00
skip_before_action :check_sign , only : [ :attachment_show ]
def list
scope = User . active . recent . like ( params [ :search ] ) . includes ( :user_extension )
@total_count = scope . size
@users = paginate ( scope )
end
2020-06-03 18:23:53 +08:00
def show
#待办事项,现在未做
@undo_events = 0
#用户的组织数量
# @user_composes_count = @user.composes.size
@user_composes_count = 0
user_projects = User . current . logged? && ( User . current . admin? || User . current . login == @user . login ) ? @user . projects : @user . projects . visible
@projects_common_count = user_projects . common . size
@projects_mirrior_count = user_projects . mirror . size
end
def watch_users
2020-06-08 09:55:54 +08:00
watchers = Watcher . watching_users ( @user . id ) . includes ( :user ) . order ( " watchers.created_at desc " )
2020-06-03 18:23:53 +08:00
if params [ :search ] . present?
search_user_ids = User . where ( id : watchers . pluck ( :watchable_id ) ) . like ( params [ :search ] ) . pluck ( :id )
watchers = watchers . where ( watchable_id : search_user_ids )
end
@watchers_count = watchers . size
@watchers = paginate ( watchers )
end
def fan_users
2020-06-08 09:55:54 +08:00
watchers = @user . watchers . includes ( :user ) . order ( " watchers.created_at desc " )
2020-06-03 18:23:53 +08:00
watchers = watchers . joins ( :user ) . where ( " LOWER(concat(users.lastname, users.firstname, users.login)) LIKE ? " , " % #{ params [ :search ] . split ( " " ) . join ( '|' ) } % " ) if params [ :search ] . present?
@watchers_count = watchers . size
@watchers = paginate ( watchers )
end
2020-03-09 00:40:16 +08:00
def update
@user = User . find params [ :id ]
@user . update! ( user_params )
render_ok
end
def me
@user = current_user
end
# 贴吧获取用户信接口
def get_user_info
begin
@user = current_user
# TODO 等消息上线再打开注释
#@tidding_count = unviewed_tiddings(current_user) if current_user.present?
rescue Exception = > e
uid_logger_error ( e . message )
missing_template
end
end
def attachment_show
file_name = params [ :file_name ]
path = params [ :path ] || edu_setting ( 'attachment_folder' )
send_file " #{ path } / #{ file_name } " , :filename = > " #{ file_name } " ,
:type = > 'game' ,
:disposition = > 'attachment' #inline can open in browser
end
def html_show
@contents = File . read ( " #{ params [ :path ] } " )
respond_to do | format |
format . html { render :layout = > false }
end
end
# Redo: 消息总数缓存
def get_navigation_info
2020-03-14 00:17:18 +08:00
# @old_domain = edu_setting('old_edu_host')
# @user = current_user
# # 新消息数
# @new_message = @user.tidings.where("created_at > '#{@user.click_time}'").count > 0 || @user.private_messages.where("created_at > '#{@user.click_time}'").count > 0
#
# @user_url = "/users/#{@user.login}"
# @career = Career.where(status: true).order("created_at asc").pluck(:id, :name)
# @auth = User.current.ec_school.present? ? "#{@old_domain}/ecs/department?school_id=#{User.current.ec_school}" : nil
2020-03-09 00:40:16 +08:00
end
# 用户回复功能
def reply_message
message = JournalsForMessage . new ( reply_message_params )
message . user_id = current_user . id
message . save!
render_ok ( id : message . id )
end
# 搜索用户具有管理员角色的项目
def search_user_projects
projects = Project . where . not ( status : 9 )
projects = projects . joins ( members : :member_roles ) . where ( member_roles : { role_id : 3 } )
projects = projects . where ( members : { user_id : current_user . id } )
search = params [ :search ] . to_s . strip
projects = projects . where ( 'projects.name LIKE ?' , " % #{ search } % " ) if search . present?
@projects = projects . select ( :id , :name )
end
2020-06-03 18:23:53 +08:00
#TODO 个人主页信息, forge上弃用-hs, 0602
2020-06-02 14:13:40 +08:00
def homepage_info
#待办事项,现在未做
@undo_events = 10
#用户的组织数量
# @user_composes_count = @user.composes.size
@user_composes_count = 10
end
2020-03-09 00:40:16 +08:00
def brief_introduction
content = params [ :content ] . to_s . strip
current_user . user_extension . update! ( brief_introduction : content )
render_ok
end
def attendance
attendance = Users :: AttendanceService . call ( current_user )
render_ok ( grade : current_user . grade , next_gold : attendance . next_gold )
rescue Users :: AttendanceService :: Error = > ex
render_error ( ex . message )
end
2020-04-16 17:24:06 +08:00
# 其他平台登录后, 必须将token同步到forge平台, 实现sso登录功能
def sync_token
return render_error ( '未找相关用户!' ) unless @user
token = Token . get_or_create_permanent_login_token ( @user , 'autologin' )
token . update_column ( :value , params [ :token ] )
render_ok
end
2020-04-17 10:32:16 +08:00
def projects
2020-06-03 18:23:53 +08:00
is_current_admin_user = User . current . logged? && ( current_user & . admin? || current_user . id == @user . id )
scope = Projects :: ListMyQuery . call ( params , @user , is_current_admin_user )
2020-04-17 10:32:16 +08:00
@total_count = scope . size
@projects = paginate ( scope )
end
2020-04-22 15:59:40 +08:00
# TODO 其他平台登录时同步修改gitea平台对应用户的密码
# 该方法主要用于: 别的平台初次部署对接forge平台, 同步用户后, gitea平台对应的用户密码与forge平台用户密码不一致是问题
def sync_gitea_pwd
return render_error ( " 未找到相关的用户 " ) if @user . blank?
2020-04-23 11:21:20 +08:00
flag = sync_pwd_to_gitea! ( @user , { password : params [ :password ] . to_s } )
flag ? render_ok : render_error ( '同步失败!' )
2020-04-22 15:59:40 +08:00
end
2020-04-22 17:13:08 +08:00
# TODO
# 同步trusite平台用户的salt信息, 只需同步一次, 同步完成后, 该方法可以删除
def sync_salt
user = User . find_by_login params [ :login ]
return if user . blank?
user . update_column ( :salt , params [ :salt ] )
render_ok
end
2020-03-09 00:40:16 +08:00
private
def load_user
@user = User . find_by_login ( params [ :id ] ) || User . find_by ( id : params [ :id ] )
end
def user_params
2020-04-15 14:27:58 +08:00
params . require ( :user ) . permit ( :nickname , :lastname , :show_realname , :login , :mail ,
2020-03-09 00:40:16 +08:00
user_extension_attributes : [
:gender , :location , :location_city ,
:occupation , :technical_title ,
2020-04-15 14:27:58 +08:00
:school_id , :department_id , :identity , :student_id , :description ]
2020-03-09 00:40:16 +08:00
)
end
def reply_message_params
normal_status ( - 1 , " 参数不对 " ) if params [ :journals_for_message ] [ :jour_type ] . nil? || params [ :journals_for_message ] [ :jour_id ] . nil? ||
params [ :journals_for_message ] [ :notes ] . nil? || params [ :journals_for_message ] [ :reply_id ] . nil?
params . require ( :journals_for_message ) . permit ( :jour_type , :jour_id , :notes , :m_parent_id , :reply_id )
end
def check_user_exist
return if @user . present?
render_not_found
end
end