OSDN Git Service

work
[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 self.controller
104     Pettanr::Application::manifest.controller_managers[self.model.item_name]
105   end
106   
107   def self.profiler_manager
108     Pettanr::Application::manifest.profiler_managers[self.model.item_name]
109   end
110   
111   def public_list
112     action_name = params[:action]
113     @action = self.class.controller.open(action_name, params, @operators)
114     @items = @action.items 
115     respond_to do |format|
116       format.html {
117         @filer = @action.filer
118         render :template => 'system/filer', :locals => {
119           :filer => @filer
120         }
121       }
122       format.json { render json: @items.to_json(self.class.model.list_json_opt) }
123       format.atom 
124       format.rss
125     end
126   end
127   
128   def my_list params
129     controller_name = params[:controller]
130     action_name = params[:action]
131     controller = Pettanr::Application::manifest.controller_managers[controller_name]
132     @action = controller.open(action_name, params, @operators)
133     @items = @action.items 
134
135     respond_to do |format|
136       format.html {
137         @filer = @action.filer
138         render :template => 'system/filer', :locals => {
139           :filer => @filer
140         }
141       }
142       format.json { render json: @items.to_json(Scroll.list_json_opt) }
143     end
144   end
145   
146   def format_filer format
147     format.html {
148       @paginate = @@model.list_paginate(@page, @page_size)
149       render :template => 'system/filer', :locals => {
150         :items => @items, :model => @@model, 
151         :operators => @operators, :pager => @paginate
152       }
153     }
154   end
155   
156   def format_prof format
157     format.prof { 
158       @profiler = self.class.profiler_manager.open(@item, @operators)
159       render :template => 'system/prof', :locals => {
160         :profiler => @profiler
161       }
162     }
163   end
164   
165   def assist_items item_name, list_name
166     list_manager = Pettanr::Application::manifest.list_managers[item_name]
167     list = list_manager.open list_name, 1, 5, @operators
168     list.items
169   end
170   
171   def assist_filer item_name, items
172     filer_manager = Pettanr::Application::manifest.filer_managers[item_name]
173     filer_manager.open(item_name, items, @operators, nil)
174   end
175   
176   def set_image(file)
177     if file.respond_to?(:read)
178       file.read
179     else
180       Base64.decode64(file.to_s.gsub(' ', '+'))
181     end
182   end
183   
184   def ymd_to_time ymd_str
185     return nil if ymd_str.blank?
186     date = nil
187     begin
188       date = Time.parse(ymd_str[0..3] + '/' + ymd_str[4..5] + '/' + ymd_str[6..7])
189     rescue
190       date = nil
191     end
192     date
193   end
194   
195   def export_url demander_url, action, token, date
196     u = demander_url + (demander_url[-1] == '/' ? '' : '/')
197     prm = '?auth_token=' + token
198     prm = prm + '&date=' + date.strftime("%Y%m%d") unless date.blank?
199     u = URI.join(u, action + '.json' + prm)
200     u.to_s
201   end
202   
203   def export_from_provider url
204     res = nil
205     begin
206       json = RestClient.get url
207       res = JSON.parse json
208     rescue
209     end
210     res
211   end
212   
213   def export_by action, provider_status, ymd
214     t = ymd_to_time ymd
215     url = export_url provider_status.provider.demander_url, action, provider_status.token, t
216     export_from_provider(url)
217   end
218   
219     rescue_from Pettanr::NotWork, :with => :render_not_work
220     def render_not_work(exception = nil)
221       if exception
222         logger.info "Rendering , :: #{exception.message}"
223       end
224       respond_to do |format|
225         format.html { 
226           render :file => "#{Rails.root}/public/not_work.html", :layout => false
227         }
228         format.json { 
229           render :text => "400 Not work", :status => 400
230         }
231       end
232     end
233     
234   if Rails.env == 'production'
235     rescue_from ActiveRecord::RecordNotFound, :with => :render_404
236     rescue_from ActiveRecord::Forbidden, :with => :render_403
237     
238     private
239     def render_404(exception = nil)
240       if exception
241         logger.info "Rendering 404: #{exception.message}"
242       end
243       respond_to do |format|
244         format.html { 
245           render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
246         }
247         format.json { 
248           render :text => "404 Not found", :status => 404
249         }
250       end
251     end
252     
253     def render_403(exception = nil)
254       if exception
255         logger.info "Rendering 403: #{exception.message}"
256       end
257       respond_to do |format|
258         format.html { 
259           render :file => "#{Rails.root}/public/403.html", :status => 404, :layout => false
260         }
261         format.json { 
262           render :text => "403 Forbidden", :status => 403
263         }
264       end
265     end
266   end
267 end