orbit-basic/lib/tasks/mid_site_sync.rake

107 lines
5.8 KiB
Ruby

# encoding: utf-8
namespace :mid_site do
attr_from_mid = %w{nccu_id psn_nam ut_chi_m eml_adr off_tel_ext sta_num}
officer_posgrp_code = %w{02 06 10 05} #from RSS2
admin_role = nil
sub_role = nil
task :sync => :environment do
users_from_mid = MID_CLIENT.query("SELECT #{attr_from_mid.join(',')} FROM rss_pautlst_ut WHERE posgrp_cod IN (#{officer_posgrp_code.join(',')})")
remote_list = users_from_mid.collect{|t| t["nccu_id"]}
#remove delete user sho has been deleted at remote first
local_need_remove = User.all.collect{|t| t.nccu_id rescue nil}.uniq.delete_if {|x| x == nil} - remote_list
local_need_remove.each{|user_id| (User.find user_id).destroy}
#starting update user
users_from_mid.each do |mid_user|
local_user = User.find_or_initialize_by(:nccu_id => mid_user["nccu_id"])
local_user.update_attributes(mid_user)
local_user.save!
end
end
task :install_admin => :before_instll_admin do
admins_nccu_id = '2772'
# admin_role = Role.find_or_create_by( key: 'administrator')
# sub_role = admin_role.sub_roles.find_or_create_by(:key => 'computer_center')
info_profile = Info.first(conditions: {:key => 'profile'})
user_from_mid = MID_CLIENT.query("SELECT #{attr_from_mid.join(',')} FROM rss_pautlst_ut WHERE posgrp_cod IN (#{officer_posgrp_code.join(',')}) AND nccu_id = '#{admins_nccu_id}' limit 1")
admin_at_mid = user_from_mid.first
user_first_name = admin_at_mid["psn_nam"].size > 3 ? admin_at_mid["psn_nam"][3..-1] : admin_at_mid["psn_nam"][1..-1]
user_last_name = admin_at_mid["psn_nam"].size > 3 ? admin_at_mid["psn_nam"][1..2] : admin_at_mid["psn_nam"][0]
local_user = User.find_or_initialize_by(:nccu_id => admin_at_mid["nccu_id"])
local_user.update_attributes(:email => admin_at_mid["eml_adr"], :admin => true, :role_id => admin_role.id, :sub_role_ids => [sub_role.id])
# local_user.role = admin_role
# local_user.sub_roles <<
AttributeValue.create( :user => local_user, :attribute_field => info_profile.attribute_fields[0], :key => 'first_name', :en => user_first_name, :zh_tw => user_first_name )
AttributeValue.create( :user_id => local_user.id, :attribute_field_id => info_profile.attribute_fields[1].id, :key => 'last_name', :en => user_last_name, :zh_tw => user_last_name )
#AttributeValue.create( :user_id => local_user.id, :attribute_field_id => sr_1_2.attribute_fields[0].id, :key => 'field', :en => 'Computer Architecture', :zh_tw => '計算機系統結構' )
#AttributeValue.create( :user_id => local_user.id, :attribute_field_id => sr_1_2.attribute_fields[1].id, :key => 'department', :en => user_from_mid["ut_chi_m"], :zh_tw => user_from_mid["ut_chi_m"] )
#AttributeValue.create( :user_id => local_user.id, :attribute_field_id => sr_1_2.attribute_fields[2].id, :key => 'speciality', :en => 'HSR', :zh_tw => '高鐵' )
end
task :install_test => :before_instll_admin do
admins_nccu_id = '139716'
# admin_role = Role.find_or_create_by( key: 'administrator')
# sub_role = admin_role.sub_roles.find_or_create_by(:key => 'computer_center')
info_profile = Info.first(conditions: {:key => 'profile'})
user_from_mid = MID_CLIENT.query("SELECT #{attr_from_mid.join(',')} FROM rss_pautlst_ut WHERE nccu_id = '#{admins_nccu_id}' limit 1")
admin_at_mid = user_from_mid.first
user_first_name = admin_at_mid["psn_nam"].size > 3 ? admin_at_mid["psn_nam"][3..-1] : admin_at_mid["psn_nam"][1..-1]
user_last_name = admin_at_mid["psn_nam"].size > 3 ? admin_at_mid["psn_nam"][1..2] : admin_at_mid["psn_nam"][0]
local_user = User.find_or_initialize_by(:nccu_id => admin_at_mid["nccu_id"])
local_user.update_attributes(:email => admin_at_mid["eml_adr"], :admin => true, :role_id => admin_role.id, :sub_role_ids => [sub_role.id])
AttributeValue.create( :user => local_user, :attribute_field => info_profile.attribute_fields[0], :key => 'first_name', :en => user_first_name, :zh_tw => user_first_name )
AttributeValue.create( :user_id => local_user.id, :attribute_field_id => info_profile.attribute_fields[1].id, :key => 'last_name', :en => user_last_name, :zh_tw => user_last_name )
end
task :before_instll_admin => :environment do
var_1 = I18nVariable.find_or_create_by( :document_class => 'Role', :key => 'administrator', :en => 'Administrator', :zh_tw => '管理員' )
var_1_1 = I18nVariable.find_or_create_by( :document_class => 'SubRole', :key => 'computer_center', :en => 'Computer Center', :zh_tw => '計算機中心', :parent_id => var_1.id )
admin_role = Role.find_or_create_by( :key => 'administrator',:built_in => true)
admin_role.i18n_variable =var_1
admin_role.save!
sub_role = admin_role.sub_roles.find_or_create_by(:key => 'computer_center', :built_in => true)
sub_role.i18n_variable =var_1_1
sub_role.save!
#var_1_1_1 = I18nVariable.create!( :document_class => 'Attribute', :key => 'field', :en => 'Field', :zh_tw => '領域', :parent_id => var_1_1.id )
#var_1_1_2 = I18nVariable.create!( :document_class => 'Attribute', :key => 'department', :en => 'Department', :zh_tw => '學系', :parent_id => var_1_1.id )
end
task :clean_local_account => :environment do
User.remote_account.each{|user| user.destroy}
end
task :claen_cc => [:clean_i18n_vars,:clean_admin_role_and_sub_role] do
end
task :clean_i18n_vars => :environment do
i18ns = I18nVariable.any_in( key: ['administrator','computer_center'])
i18ns.each { |var| var.destroy }
end
task :clean_admin_role_and_sub_role => :environment do
data = Role.any_in( key: ['administrator','computer_center'])
data.each { |var| var.destroy }
data = SubRole.any_in( key: ['administrator','computer_center'])
data.each { |var| var.destroy }
end
end