OSDN Git Service

t#32326:fix 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       if user_signed_in?
33         @user = current_user
34         @author = @user.author
35         @artist = if @author and @author.artist?
36           @author.artist
37         else
38           nil
39         end
40       end
41       @admin = if admin_signed_in?
42         current_admin
43       else
44         nil
45       end
46       @demand_user = if demand_user_signed_in?
47         current_demand_user
48       else
49         nil
50       end
51     end
52   end
53   
54   def authenticate_reader
55     authenticate_user! unless (@user || @admin)
56   end
57   
58   def authenticate_user
59     authenticate_user! unless (@user)
60   end
61   
62   def authenticate_resource_reader
63     authenticate_user! unless (@user || @admin || @demand_user)
64   end
65   
66   def authenticate_author
67     if @author
68       true
69     else
70       respond_to do |format|
71         format.html { redirect_to main_app.new_author_path, :status => :found }
72         format.js { render "authors/new" }
73         format.json { 
74           raise ActiveRecord::Forbidden
75         }
76       end
77       false
78     end
79   end
80       
81   def authenticate_artist
82     if @artist
83       true
84     else
85       respond_to do |format|
86         format.html { redirect_to main_app.new_artist_path, :status => :found }
87         format.js { render "artists/new" }
88         format.json { 
89           raise ActiveRecord::Forbidden
90         }
91       end
92       false
93     end
94   end
95   
96   def set_image(file)
97     if file.respond_to?(:read)
98       file.read
99     else
100       Base64.decode64(file.to_s.gsub(' ', '+'))
101     end
102   end
103   
104   def ymd_to_time ymd_str
105     return nil if ymd_str.blank?
106     date = nil
107     begin
108       date = Time.parse(ymd_str[0..3] + '/' + ymd_str[4..5] + '/' + ymd_str[6..7])
109     rescue
110       date = nil
111     end
112     date
113   end
114   
115   def export_url demander_url, action, token, date
116     u = demander_url + (demander_url[-1] == '/' ? '' : '/')
117     prm = '?auth_token=' + token
118     prm = prm + '&date=' + date.strftime("%Y%m%d") unless date.blank?
119     u = URI.join(u, action + '.json' + prm)
120     u.to_s
121   end
122   
123   def export_from_provider url
124     res = nil
125     begin
126       json = RestClient.get url
127       res = JSON.parse json
128     rescue
129     end
130     res
131   end
132   
133   def export_by action, provider_status, ymd
134     t = ymd_to_time ymd
135     url = export_url provider_status.provider.demander_url, action, provider_status.token, t
136     export_from_provider(url)
137   end
138   
139     rescue_from Pettanr::NotWork, :with => :render_not_work
140     def render_not_work(exception = nil)
141       if exception
142         logger.info "Rendering , :: #{exception.message}"
143       end
144       respond_to do |format|
145         format.html { 
146           render :file => "#{Rails.root}/public/not_work.html", :layout => false
147         }
148         format.json { 
149           render :text => "400 Not work", :status => 400
150         }
151       end
152     end
153     
154   if Rails.env == 'production'
155     rescue_from ActiveRecord::RecordNotFound, :with => :render_404
156     rescue_from ActiveRecord::Forbidden, :with => :render_403
157     
158     private
159     def render_404(exception = nil)
160       if exception
161         logger.info "Rendering 404: #{exception.message}"
162       end
163       respond_to do |format|
164         format.html { 
165           render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
166         }
167         format.json { 
168           render :text => "404 Not found", :status => 404
169         }
170       end
171     end
172     
173     def render_403(exception = nil)
174       if exception
175         logger.info "Rendering 403: #{exception.message}"
176       end
177       respond_to do |format|
178         format.html { 
179           render :file => "#{Rails.root}/public/403.html", :status => 404, :layout => false
180         }
181         format.json { 
182           render :text => "403 Forbidden", :status => 403
183         }
184       end
185     end
186   end
187 end