From 3480fa25456ed148125657e551ff76dd4e6f4d2c Mon Sep 17 00:00:00 2001 From: "Matthew K. Fu JuYuan" Date: Thu, 24 May 2012 17:01:00 +0800 Subject: [PATCH] Fix login and label. Check sync jobs and update info function. --- app/controllers/sessions_controller.rb | 4 +- app/jobs/backup_server.rb | 8 ++++ app/jobs/restart_server.rb | 8 ---- app/jobs/sync_db.rb | 8 ++++ app/views/devise/sessions/new.html.erb | 6 +-- config/locales/zh_tw.yml | 2 +- config/resque_schedule.yml | 8 ++-- lib/tasks/mid_site_sync.rake | 55 ++++++++++++++++---------- 8 files changed, 61 insertions(+), 38 deletions(-) create mode 100644 app/jobs/backup_server.rb delete mode 100644 app/jobs/restart_server.rb diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 74dce24a..a6925aa4 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -15,10 +15,10 @@ class SessionsController < Devise::SessionsController if $nccu_ldap_connection.bind logger.info "=LDAP Binded password ok..." result =check_auth_with_ldap(login_uid,login_password) - if result && login_password!='' + if result #&& login_password!='' logger.info "==LDAP password passed..." nccu_id = get_nccu_id_from_mid_site(login_uid) - resource = nccu_id.nil? ? nil : (User.first(conditions:{ nccu_id: nccu_id })) + resource = nccu_id.nil? ? nil : (User.first(conditions:{ nccu_ldap_uid: nccu_id })) # resource = env['warden'].authenticate!(:check_nccu_ldap) # resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new") set_flash_message(:notice, :signed_in) if is_navigational_format? diff --git a/app/jobs/backup_server.rb b/app/jobs/backup_server.rb new file mode 100644 index 00000000..f76eaac8 --- /dev/null +++ b/app/jobs/backup_server.rb @@ -0,0 +1,8 @@ +class BackupServer + @queue = :high + + def self.perform() + CronMail.time_check("Going to backup Orbit").deliver + # %x[touch #{Rails.root}/tmp/restart] + end +end diff --git a/app/jobs/restart_server.rb b/app/jobs/restart_server.rb deleted file mode 100644 index 53167fea..00000000 --- a/app/jobs/restart_server.rb +++ /dev/null @@ -1,8 +0,0 @@ -class RestartServer - @queue = :high - - def self.perform() - CronMail.time_check("Going to restarting Orbit").deliver - %x[touch #{Rails.root}/tmp/restart] - end -end diff --git a/app/jobs/sync_db.rb b/app/jobs/sync_db.rb index d4bda790..2c550d06 100644 --- a/app/jobs/sync_db.rb +++ b/app/jobs/sync_db.rb @@ -4,6 +4,7 @@ class SyncDb def self.perform() self.start_sync + self.set_admin end def self.start_sync @@ -12,4 +13,11 @@ class SyncDb %x[rake #{task} --trace >> #{Rails.root}/log/rake.log] puts "SyncDb Synced" end + + def self.set_admin + task = 'mid_site:install_admin' + args = [] + %x[rake #{task} --trace >> #{Rails.root}/log/rake.log] + puts "Admin done" + end end diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index 13c428ab..4eee59d8 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -11,12 +11,12 @@

You need to sign in or sign up before continuing.

- <%= f.label :nccu_ldap_uid %> - <%= f.text_field :nccu_ldap_uid, :placeholder => t("nccu"), :style => "width: 330px;" %> + <%= f.label :nccu_ldap_uid ,t("nccu_c.nccu_ldap_uid")%> + <%= f.text_field :nccu_ldap_uid, :placeholder => t("nccu_c.nccu_ldap_uid"), :style => "width: 330px;" %> Please correct the error
- <%= f.label :password %> + <%= f.label :password,t("password") %> <%= f.password_field :password, :placeholder => t(:dots), :style => "width: 330px;" %> Please correct the error <%= link_to t(:forgot_password), new_user_password_path, :class => 'pull-right forgot hide' %> diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index a4516ee5..0b06f844 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -27,7 +27,7 @@ zh_tw: logout: 登出 nccu: 政大 nccu_c: - nccu_ldap_uid: 政大LDAP帳號 + nccu_ldap_uid: iNCCU帳號 no_: "No" nothing: 無 or_lower: 或 diff --git a/config/resque_schedule.yml b/config/resque_schedule.yml index 60194101..27af98fa 100644 --- a/config/resque_schedule.yml +++ b/config/resque_schedule.yml @@ -10,11 +10,11 @@ nccu_daily_ldap_sync: args: description: Sync NCCU middle site with Orbit for LDAP data -nccu_daily_restart: - cron: * * */3 * * * - class: RestartServer +nccu_daily_backup: + cron: * * 3 * * * + class: BackupServer args: - description: Restart NCCU site + description: Backup NCCU site nccu_claender_sync: cron: * * [0,12] * * * diff --git a/lib/tasks/mid_site_sync.rake b/lib/tasks/mid_site_sync.rake index ae6877d2..87979b66 100644 --- a/lib/tasks/mid_site_sync.rake +++ b/lib/tasks/mid_site_sync.rake @@ -35,18 +35,31 @@ namespace :mid_site do #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 desc "Going to delete User with IDs: #{local_need_remove.inspect}" - local_need_remove = User.excludes(nccu_id: test_account_ldap_id ).collect{|t| t.nccu_id rescue nil}.uniq.delete_if {|x| x == nil} - remote_list + local_need_remove = User.excludes(nccu_ldap_uid: test_account_ldap_id ).collect{|t| t.nccu_ldap_uid rescue nil}.uniq.delete_if {|x| x == nil} - remote_list desc "Deletion completed! \n" #starting update user desc "Updating and Newing Users" + users_from_mid.each do |mid_user| ut_item = ut_data_from_mid.find{|ut_data| ut_data["ut_cod"] == mid_user["ut_cod"]} up_ut_item = ut_data_from_mid.find{|ut_data| ut_data["ut_cod"] == mid_user["up_ut_cod"] } local_user = User.find_or_initialize_by(:nccu_ldap_uid => mid_user["nccu_id"]) - AttributeValue.find_or_create_by( :user_id => local_user.id, :attribute_field_id => info_profile.attribute_fields[0].id, :key => 'first_name', :en => '', :zh_tw => (mid_user["psn_nam"].size <= 3? mid_user["psn_nam"][1..2]:mid_user["psn_nam"][2..-1]) ) - AttributeValue.find_or_create_by( :user_id => local_user.id, :attribute_field_id => info_profile.attribute_fields[1].id, :key => 'last_name', :en => '', :zh_tw => (mid_user["psn_nam"].size <= 3? mid_user["psn_nam"][0]:mid_user["psn_nam"][0..1]) ) + users_name_from_mid = $mid_site_connection.query("SELECT eng_nam_l,eng_nam_f,chi_nam_l,chi_nam_f FROM rss_zzperson_view WHERE nccu_id =#{mid_user["nccu_id"]}").first + + first_name = AttributeValue.find_or_create_by( :user_id => local_user.id, :attribute_field_id => info_profile.attribute_fields[0].id, :key => 'first_name') + last_name = AttributeValue.find_or_create_by( :user_id => local_user.id, :attribute_field_id => info_profile.attribute_fields[1].id, :key => 'last_name') + + first_name['en'] = users_name_from_mid["eng_nam_f"] + first_name['zh_tw'] = users_name_from_mid["chi_nam_f"] + first_name.save + + last_name['en'] = users_name_from_mid["eng_nam_l"] + last_name['zh_tw'] = users_name_from_mid["chi_nam_l"] + last_name.save + + user_model_mapper = Hash[:ut_cod => mid_user["ut_cod"] ,:up_ut_cod => mid_user["up_ut_cod"],:email => mid_user["eml_adr"],:off_tel_ext => mid_user["off_tel_ext"],:sta_num => mid_user["sta_num"]] user_model_mapper[:admin] = false local_user.update_attributes(user_model_mapper) @@ -66,27 +79,29 @@ namespace :mid_site do end end + + task :install_admin => :before_instll_admin do admins_nccu_id = '2772' info_profile = Info.first(conditions: {:key => 'profile'}) - - # admin_role = Role.find_or_create_by( key: 'administrator') - # sub_role = admin_role.sub_roles.find_or_create_by(:key => 'computer_center') - + user_from_mid = $mid_site_connection.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 => '高鐵' ) + local_user = User.find_or_initialize_by(:nccu_ldap_uid => 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]) + users_name_from_mid = $mid_site_connection.query("SELECT eng_nam_l,eng_nam_f,chi_nam_l,chi_nam_f FROM rss_zzperson_view WHERE nccu_id =#{admin_at_mid["nccu_id"]}").first + + first_name = AttributeValue.find_or_create_by( :user_id => local_user.id, :attribute_field_id => info_profile.attribute_fields[0].id, :key => 'first_name') + last_name = AttributeValue.find_or_create_by( :user_id => local_user.id, :attribute_field_id => info_profile.attribute_fields[1].id, :key => 'last_name') + + first_name['en'] = users_name_from_mid["eng_nam_f"] + first_name['zh_tw'] = users_name_from_mid["chi_nam_f"] + first_name.save + + last_name['en'] = users_name_from_mid["eng_nam_l"] + last_name['zh_tw'] = users_name_from_mid["chi_nam_l"] + last_name.save end @@ -100,7 +115,7 @@ namespace :mid_site do 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 = User.find_or_initialize_by(:nccu_ldap_uid => 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_id => local_user.id, :attribute_field_id => info_profile.attribute_fields[0].id, :key => 'first_name', :en => user_first_name, :zh_tw => user_first_name ) @@ -141,7 +156,7 @@ namespace :mid_site do end task :clean_ldap_users => :environment do - User.where(:nccu_ldap_id.ne => nil ).each {|t| t.destroy} + User.where(:nccu_ldap_uid.ne => nil ).each {|t| t.destroy} end task :clean_admin_role_and_sub_role => :environment do