OSDN Git Service

temp
[pettanr/pettanr.git] / app / controllers / application_controller.rb
1 class ApplicationController < ActionController::Base
2   protect_from_forgery
3   layout :devise_layout if MagicNumber['test_layout']
4   before_filter :bf
5   
6   def devise_layout
7     if devise_controller?
8       case resource_name
9       when :admin
10         'guest'
11       when :user
12         'guest'
13       when :demand_user
14         'guest'
15       end
16     else
17       'application'
18     end
19   end
20   
21   def bf
22     @server_result = {
23       :location => {:controller => params[:controller], :action => params[:action]}
24     }
25     @server_result[:location][:id] = params[:id] if params[:id]
26     if Admin.count.to_i == 0 or License.count.to_i == 0
27       if params[:controller] == 'system' and params[:action] == 'start'
28       else
29         redirect_to :controller => '/system', :action => 'start'
30       end
31     else
32       user = if user_signed_in?
33         current_user
34       else
35         nil
36       end
37       author = if user
38         user.author
39       else
40         nil
41       end
42       artist = if user
43         user.artist
44       else
45         nil
46       end
47       admin = if admin_signed_in?
48         current_admin
49       else
50         nil
51       end
52       demand_user = if demand_user_signed_in?
53         current_demand_user
54       else
55         nil
56       end
57       @operators = Operator.new [user, author, artist, admin, demand_user]
58     end
59   end
60   
61   def authenticate_reader
62     authenticate_user! unless @operators.reader?
63   end
64   
65   def authenticate_user
66     authenticate_user! unless @operators.user?
67   end
68   
69   def authenticate_resource_reader
70     authenticate_user! unless @operators.resource_reader?
71   end
72   
73   def authenticate_author
74     if @operators.author
75       true
76     else
77       respond_to do |format|
78         format.html { redirect_to main_app.new_author_path, :status => :found }
79         format.js { render "authors/new" }
80         format.json { 
81           raise ActiveRecord::Forbidden
82         }
83       end
84       false
85     end
86   end
87       
88   def authenticate_artist
89     if @operators.artist
90       true
91     else
92       respond_to do |format|
93         format.html { redirect_to main_app.new_artist_path, :status => :found }
94         format.js { render "artists/new" }
95         format.json { 
96           raise ActiveRecord::Forbidden
97         }
98       end
99       false
100     end
101   end
102   
103   def set_image(file)
104     if file.respond_to?(:read)
105       file.read
106     else
107       Base64.decode64(file.to_s.gsub(' ', '+'))
108     end
109   end
110   
111   def ymd_to_time ymd_str
112     return nil if ymd_str.blank?
113     date = nil
114     begin
115       date = Time.parse(ymd_str[0..3] + '/' + ymd_str[4..5] + '/' + ymd_str[6..7])
116     rescue
117       date = nil
118     end
119     date
120   end
121   
122   def export_url demander_url, action, token, date
123     u = demander_url + (demander_url[-1] == '/' ? '' : '/')
124     prm = '?auth_token=' + token
125     prm = prm + '&date=' + date.strftime("%Y%m%d") unless date.blank?
126     u = URI.join(u, action + '.json' + prm)
127     u.to_s
128   end
129   
130   def export_from_provider url
131     res = nil
132     begin
133       json = RestClient.get url
134       res = JSON.parse json
135     rescue
136     end
137     res
138   end
139   
140   def export_by action, provider_status, ymd
141     t = ymd_to_time ymd
142     url = export_url provider_status.provider.demander_url, action, provider_status.token, t
143     export_from_provider(url)
144   end
145   
146     rescue_from Pettanr::NotWork, :with => :render_not_work
147     def render_not_work(exception = nil)
148       if exception
149         logger.info "Rendering , :: #{exception.message}"
150       end
151       respond_to do |format|
152         format.html { 
153           render :file => "#{Rails.root}/public/not_work.html", :layout => false
154         }
155         format.json { 
156           render :text => "400 Not work", :status => 400
157         }
158       end
159     end
160     
161   if Rails.env == 'production'
162     rescue_from ActiveRecord::RecordNotFound, :with => :render_404
163     rescue_from ActiveRecord::Forbidden, :with => :render_403
164     
165     private
166     def render_404(exception = nil)
167       if exception
168         logger.info "Rendering 404: #{exception.message}"
169       end
170       respond_to do |format|
171         format.html { 
172           render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
173         }
174         format.json { 
175           render :text => "404 Not found", :status => 404
176         }
177       end
178     end
179     
180     def render_403(exception = nil)
181       if exception
182         logger.info "Rendering 403: #{exception.message}"
183       end
184       respond_to do |format|
185         format.html { 
186           render :file => "#{Rails.root}/public/403.html", :status => 404, :layout => false
187         }
188         format.json { 
189           render :text => "403 Forbidden", :status => 403
190         }
191       end
192     end
193   end
194 end