OSDN Git Service

t#32471:add profiles
[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 format_prof format
97     format.prof { 
98       render :template => 'system/prof', :locals => {
99         :item => @item, :roles => [@user, @admin]
100       }
101     }
102   end
103   
104   def set_image(file)
105     if file.respond_to?(:read)
106       file.read
107     else
108       Base64.decode64(file.to_s.gsub(' ', '+'))
109     end
110   end
111   
112   def ymd_to_time ymd_str
113     return nil if ymd_str.blank?
114     date = nil
115     begin
116       date = Time.parse(ymd_str[0..3] + '/' + ymd_str[4..5] + '/' + ymd_str[6..7])
117     rescue
118       date = nil
119     end
120     date
121   end
122   
123   def export_url demander_url, action, token, date
124     u = demander_url + (demander_url[-1] == '/' ? '' : '/')
125     prm = '?auth_token=' + token
126     prm = prm + '&date=' + date.strftime("%Y%m%d") unless date.blank?
127     u = URI.join(u, action + '.json' + prm)
128     u.to_s
129   end
130   
131   def export_from_provider url
132     res = nil
133     begin
134       json = RestClient.get url
135       res = JSON.parse json
136     rescue
137     end
138     res
139   end
140   
141   def export_by action, provider_status, ymd
142     t = ymd_to_time ymd
143     url = export_url provider_status.provider.demander_url, action, provider_status.token, t
144     export_from_provider(url)
145   end
146   
147     rescue_from Pettanr::NotWork, :with => :render_not_work
148     def render_not_work(exception = nil)
149       if exception
150         logger.info "Rendering , :: #{exception.message}"
151       end
152       respond_to do |format|
153         format.html { 
154           render :file => "#{Rails.root}/public/not_work.html", :layout => false
155         }
156         format.json { 
157           render :text => "400 Not work", :status => 400
158         }
159       end
160     end
161     
162   if Rails.env == 'production'
163     rescue_from ActiveRecord::RecordNotFound, :with => :render_404
164     rescue_from ActiveRecord::Forbidden, :with => :render_403
165     
166     private
167     def render_404(exception = nil)
168       if exception
169         logger.info "Rendering 404: #{exception.message}"
170       end
171       respond_to do |format|
172         format.html { 
173           render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
174         }
175         format.json { 
176           render :text => "404 Not found", :status => 404
177         }
178       end
179     end
180     
181     def render_403(exception = nil)
182       if exception
183         logger.info "Rendering 403: #{exception.message}"
184       end
185       respond_to do |format|
186         format.html { 
187           render :file => "#{Rails.root}/public/403.html", :status => 404, :layout => false
188         }
189         format.json { 
190           render :text => "403 Forbidden", :status => 403
191         }
192       end
193     end
194   end
195 end