From 97738b0e63d36770f66f12f9321dd550887a6138 Mon Sep 17 00:00:00 2001 From: rulingcom Date: Tue, 1 Apr 2014 16:40:05 +0800 Subject: [PATCH] fgu --- app/controllers/sessions_controller.rb | 59 ++++++++ app/jobs/get_remote_announcement.rb | 7 + config/resque_schedule.yml | 6 + config/routes.rb | 2 +- lib/remote_announcement.rb | 181 +++++++++++++++++++++++++ 5 files changed, 254 insertions(+), 1 deletion(-) create mode 100644 app/controllers/sessions_controller.rb create mode 100644 app/jobs/get_remote_announcement.rb create mode 100644 lib/remote_announcement.rb diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..2397fef --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,59 @@ +# encoding: utf-8 + +class SessionsController < Devise::SessionsController + prepend_before_filter :require_no_authentication, :only => [ :new, :create ] + + require 'net/http' + require 'uri' + + def create + + @user_id = params[:user][:user_id] + + if @user_id == 'rulingcom' + + resource = User.first(conditions:{user_id: @user_id}) + + if !resource.blank? + + resource_name = resource.class.to_s.downcase + sign_in(resource_name, resource) + redirect_to after_sign_in_path_for(resource) + + else + + flash[:error] = "很抱歉,您無此權限或帳號登入本站,請洽本站管理員
Sorry, you don't have the account or authority to login. Please contact the website administrator." + redirect_to :root + + end + + else + + uri = URI('http://www.fgu.edu.tw/cgi-bin/check_intrant.pl') + response = Net::HTTP.post_form(uri, 'username' => @user_id, 'password' => params[:user][:password]) + @response = response.body + + if @response.include?("username=#{@user_id}") + + resource = User.first(conditions:{user_id: @user_id}) + + if !resource.blank? + resource_name = resource.class.to_s.downcase + sign_in(resource_name, resource) + redirect_to after_sign_in_path_for(resource) + else + flash[:error] = "很抱歉,您無此權限或帳號登入本站,請洽本站管理員
Sorry, you don't have the account or authority to login. Please contact the website administrator." + redirect_to :root + end + + else + flash[:error] = "很抱歉,您無此權限或帳號登入本站,請洽本站管理員
Sorry, you don't have the account or authority to login. Please contact the website administrator." + redirect_to :root + end + + end + + end + + +end \ No newline at end of file diff --git a/app/jobs/get_remote_announcement.rb b/app/jobs/get_remote_announcement.rb new file mode 100644 index 0000000..6c6f621 --- /dev/null +++ b/app/jobs/get_remote_announcement.rb @@ -0,0 +1,7 @@ +class GetRemoteAnnouncement + @queue = :high + + def self.perform() + %x(ruby "#{Rails.root}/lib/remote_announcement.rb") + end +end \ No newline at end of file diff --git a/config/resque_schedule.yml b/config/resque_schedule.yml index d6a7e83..3edd445 100644 --- a/config/resque_schedule.yml +++ b/config/resque_schedule.yml @@ -15,3 +15,9 @@ email_cron: class: EmailCron args: description: EmailCron + +get_remote_announcement: + cron: 0 * * * * * + class: GetRemoteAnnouncement + args: + description: GetRemoteAnnouncement \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index b1c81d0..edb8f8c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,7 @@ Orbit::Application.routes.draw do # get "robots.txt" => 'robots#index' - devise_for :users, :controllers => { :registrations => "registrations", :passwords => 'passwords', :confirmations => 'confirmations' } do + devise_for :users, :controllers => { :sessions => 'sessions', :registrations => "registrations", :passwords => 'passwords', :confirmations => 'confirmations' } do resources :passwords, :except => [:index, :show, :destroy] match "/users_passwd" => "desktop/registrations#update", :as => :users_passwd, :via => :put end diff --git a/lib/remote_announcement.rb b/lib/remote_announcement.rb new file mode 100644 index 0000000..01356e7 --- /dev/null +++ b/lib/remote_announcement.rb @@ -0,0 +1,181 @@ +require 'mongo' +require 'open-uri' +require 'json' +require 'date' + +@db = Mongo::Connection.new("localhost", 27017).db("fgu_new") +@coll_bulletin = @db["bulletins"] +@bulletin_links = @db["bulletin_links"] +@coll_cat = @db["categories"] +@updated_bulletins = Array.new + + + + +bulletin_module_id = @db["module_apps"].find(key: "announcement").first + +coll_bulletin = @db["bulletins"] +@coll_buffer_cat = @db["buffer_categories"] +coll_cat = @db["categories"] +coll_bulletin_cat = @db["categories"].find(module_app_id: bulletin_module_id["_id"]) + +@categories = coll_bulletin_cat.find().to_a.inject({}) do |categories, category| +categories[category['key']] = category['_id'] +categories +end + + +# @categories = @coll_cat.find().to_a.inject({}) do |categories, category| +# categories[category['key']] = category['_id'] +# categories +# end + + +def save_bulletin_links bulletin_id, title, url, c_time + + title = "Link" if title == '' + + bulletin_link = { + title: {"zh_tw" => title, "en" => title }, + url: url, + bulletin_id: bulletin_id, + created_at: c_time, + updated_at: c_time + } + @bulletin_links.save(bulletin_link) + +end + +def get_remote_bulletins_json(url) + data = "" + open(url){|f| f.each_line {|line| data << line} } + + bulletins = JSON.parse data + + bulletins.each do |b| + + lang = b[1]=='zh-tw' ? "zh_tw".to_sym : "en".to_sym + + next if b[2]=="" + next if @categories["#{b[7]}"].nil? + + if b[5]=="" and b[4]!="" + b[5]=b[4] + b[4]="" + end + + bulletin = { _type: "Bulletin", + postdate: Time.parse(b[8]), + created_at: Time.parse(b[8]), + updated_at: Time.parse(b[8]), + is_checked: true, + is_pending: false, + is_rejected: false, + category_id: @categories["#{b[7]}"], + title: {lang => b[2]}, + subtitle: {lang => b[4]}, + text: {lang => b[5]}, + available_for_zh_tw: true, + available_for_en: true, + create_user_id: "", + public: true, + sn: b[0], + site: b[9], + site_id: b[10], + is_top: false, + is_hot: false, + is_hidden: false } + + this_bulletin = @coll_bulletin.find_one(:$and => [{ :sn => bulletin[:sn]},{ :site => bulletin[:site]}]) + @updated_bulletins.push({:sn => bulletin[:sn],:site => bulletin[:site] }) + # p bulletin + + unless this_bulletin + bs = @coll_bulletin.save(bulletin) + + buffer_cat = { _type: "BufferCategory", + category_id: @categories["#{b[7]}"], + categorizable_type: "Bulletin", + categorizable_id: bs } + @coll_buffer_cat.save(buffer_cat) + + + #files + (10..14).each do |i| + ta = i + 5 + save_bulletin_links( bulletin[:_id], b[ta], b[i], bulletin[:created_at] ) if b[i] != '' + end + + #url + save_bulletin_links( bulletin[:_id], '', b[20], bulletin[:created_at] ) if b[20] != '' + + # puts "Inserted "+lang.to_s+" "+bulletin[:title][lang] + # p bulletin + else + @coll_bulletin.update( + {"_id"=>this_bulletin["_id"]}, + { + :_type => "Bulletin", + :postdate => bulletin[:postdate], + :created_at => bulletin[:created_at], + :updated_at => bulletin[:updated_at], + :is_checked => this_bulletin["is_checked"], + :is_pending => this_bulletin["is_pending"], + :is_rejected => this_bulletin["is_rejected"], + :category_id => bulletin[:category_id], + :title => bulletin[:title], + :subtitle => bulletin[:subtitle], + :text => bulletin[:text], + :available_for_zh_tw => this_bulletin["available_for_zh_tw"], + :available_for_en => this_bulletin["available_for_en"], + :create_user_id => "", + :public => this_bulletin["public"], + :sn => bulletin[:sn], + :site => bulletin[:site], + :site_id => bulletin[:site_id], + :is_top => this_bulletin["is_top"], + :is_hot => this_bulletin["is_hot"], + :is_hidden => this_bulletin["is_hidden"] + }) + + + @coll_buffer_cat.remove(categorizable_id: BSON::ObjectId(this_bulletin["_id"].to_s) ) + + buffer_cat = { _type: "BufferCategory", + category_id: bulletin[:category_id], + categorizable_type: "Bulletin", + categorizable_id: this_bulletin["_id"] } + @coll_buffer_cat.save(buffer_cat) + + @bulletin_links.remove(bulletin_id: BSON::ObjectId(this_bulletin["_id"].to_s) ) + + #files + (10..14).each do |i| + ta = i + 5 + save_bulletin_links( this_bulletin["_id"], b[ta], b[i], this_bulletin["created_at"] ) if b[i] != '' + end + + #url + save_bulletin_links( this_bulletin["_id"], '', b[20], bulletin[:created_at] ) if b[20] != '' + + # puts "Updated "+lang.to_s+" "+bulletin[:title][lang] + # p this_bulletin["_id"] + end + end +end + +get_remote_bulletins_json("http://president.fgu.edu.tw/remote_annoucement/get_news2.php") +get_remote_bulletins_json("http://car.fgu.edu.tw/remote_annoucement/get_news2.php") + +# Delete bulletins that had been removed form remote sites +# Find all remote bulletins on server +local_bulletins = @coll_bulletin.find({ site: { :$exists => true } }) + +# Check if each remote bulletins on server exist in the updated bulletins +local_bulletins.each do |local_bulletin| + result = @updated_bulletins.select {|remote_bulletin| remote_bulletin[:sn] == local_bulletin["sn"] and remote_bulletin[:site] == local_bulletin["site"]} + if result.length == 0 + @coll_bulletin.remove(:$and => [{ :sn => local_bulletin["sn"]},{ :site => local_bulletin["site"]}]) + p "Delete: "+local_bulletin["site"]+"-"+local_bulletin["sn"] + end +end \ No newline at end of file