/
application_controller.rb
437 lines (365 loc) · 14.7 KB
/
application_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
PROFILER_SESSIONS_FILE = 'used_tags.txt'
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
include HtmlCleaner
before_filter :sanitize_params
# Authlogic login helpers
helper_method :current_user
helper_method :current_admin
helper_method :logged_in?
helper_method :logged_in_as_admin?
# Title helpers
helper_method :process_title
# clear out the flash-being-set
before_filter :clear_flash_cookie
def clear_flash_cookie
cookies.delete(:flash_is_set)
end
after_filter :check_for_flash
def check_for_flash
cookies[:flash_is_set] = 1 unless flash.empty?
end
# So if there is not a user_credentials cookie and the user appears to be logged in then
# redirect to the logout page
before_filter :logout_if_not_user_credentials
def logout_if_not_user_credentials
if logged_in? && cookies[:user_credentials]==nil && controller_name != "user_sessions"
logger.error "Forcing logout"
@user_session = UserSession.find
if @user_session
@user_session.destroy
end
redirect_to '/lost_cookie' and return
end
end
# mark the flash as being set (called when flash is set)
def set_flash_cookie(key=nil, msg=nil)
cookies[:flash_is_set] = 1
end
# aliasing setflash for set_flash_cookie
# def setflash (this is here in case someone is grepping for the definition of the method)
alias :setflash :set_flash_cookie
def current_user
@current_user ||= current_user_session && current_user_session.record
end
protected
def record_not_found (exception)
@message=exception.message
respond_to do |f|
f.html{ render :template => "errors/404", :status => 404 }
end
end
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_admin_session
return @current_admin_session if defined?(@current_admin_session)
@current_admin_session = AdminSession.find
end
def current_admin
@current_admin = current_admin_session && current_admin_session.record
end
def logged_in?
current_user.nil? ? false : true
end
def logged_in_as_admin?
current_admin.nil? ? false : true
end
def guest?
!(logged_in? || logged_in_as_admin?)
end
def process_title(string)
string = string.humanize.titleize
string = string.sub("Faq", "FAQ")
string = string.sub("Tos", "TOS")
string = string.sub("Dmca", "DMCA")
return string
end
public
before_filter :fetch_admin_settings
def fetch_admin_settings
if Rails.env.development?
@admin_settings = AdminSetting.first
else
@admin_settings = Rails.cache.fetch("admin_settings"){AdminSetting.first}
end
end
before_filter :load_admin_banner
def load_admin_banner
if Rails.env.development?
@admin_banner = AdminBanner.where(:active => true).last
else
@admin_banner = Rails.cache.fetch("admin_banner"){AdminBanner.where(:active => true).last}
end
end
# store previous page in session to make redirecting back possible
# if already redirected once, don't redirect again.
before_filter :store_location
def store_location
if session[:return_to] == "redirected"
Rails.logger.debug "Return to back would cause infinite loop"
session.delete(:return_to)
else
session[:return_to] = request.fullpath
Rails.logger.debug "Return to: #{session[:return_to]}"
end
end
# Redirect to the URI stored by the most recent store_location call or
# to the passed default.
def redirect_back_or_default(default = root_path)
back = session[:return_to]
session.delete(:return_to)
if back
Rails.logger.debug "Returning to #{back}"
session[:return_to] = "redirected"
redirect_to(back) and return
else
Rails.logger.debug "Returning to default (#{default})"
redirect_to(default) and return
end
end
# Filter method - keeps users out of admin areas
def admin_only
logged_in_as_admin? || admin_only_access_denied
end
# Filter method to prevent admin users from accessing certain actions
def users_only
logged_in? || access_denied
end
# Filter method - requires user to have opendoors privs
def opendoors_only
(logged_in? && permit?("opendoors")) || access_denied
end
# Redirect as appropriate when an access request fails.
#
# The default action is to redirect to the login screen.
#
# Override this method in your controllers if you want to have special
# behavior in case the user is not authorized
# to access the requested action. For example, a popup window might
# simply close itself.
def access_denied(options ={})
store_location
if logged_in?
destination = options[:redirect].blank? ? user_path(current_user) : options[:redirect]
flash[:error] = ts "Sorry, you don't have permission to access the page you were trying to reach."
redirect_to destination
else
destination = options[:redirect].blank? ? new_user_session_path : options[:redirect]
flash[:error] = ts "Sorry, you don't have permission to access the page you were trying to reach. Please log in."
redirect_to destination
end
false
end
def admin_only_access_denied
flash[:error] = ts("I'm sorry, only an admin can look at that area.")
redirect_to root_path
false
end
# Filter method - prevents users from logging in as admin
def user_logout_required
if logged_in?
flash[:notice] = 'Please log out of your user account first!'
redirect_to root_path
end
end
# Prevents admin from logging in as users
def admin_logout_required
if logged_in_as_admin?
flash[:notice] = 'Please log out of your admin account first!'
redirect_to root_path
end
end
# Hide admin banner via cookies
before_filter :hide_banner
def hide_banner
if params[:hide_banner]
session[:hide_banner] = true
end
end
# Store the current user as a class variable in the User class,
# so other models can access it with "User.current_user"
before_filter :set_current_user
def set_current_user
User.current_user = logged_in_as_admin? ? current_admin : current_user
@current_user = current_user
unless current_user.nil?
@current_user_subscriptions_count, @current_user_visible_work_count, @current_user_bookmarks_count, @current_user_owned_collections_count, @current_user_challenge_signups_count, @current_user_offer_assignments, @current_user_unposted_works_size=
Rails.cache.fetch("user_menu_counts_#{current_user.id}",
expires_in: 2.hours,
race_condition_ttl: 5) { "#{current_user.subscriptions.count}, #{current_user.visible_work_count}, #{current_user.bookmarks.count}, #{current_user.owned_collections.count}, #{current_user.challenge_signups.count}, #{current_user.offer_assignments.undefaulted.count + current_user.pinch_hit_assignments.undefaulted.count}, #{current_user.unposted_works.size}" }.split(",").map(&:to_i)
end
end
def load_collection
@collection = Collection.find_by_name(params[:collection_id]) if params[:collection_id]
end
def collection_maintainers_only
logged_in? && @collection && @collection.user_is_maintainer?(current_user) || access_denied
end
def collection_owners_only
logged_in? && @collection && @collection.user_is_owner?(current_user) || access_denied
end
def not_allowed(fallback=nil)
flash[:error] = ts("Sorry, you're not allowed to do that.")
redirect_to (fallback || root_path) rescue redirect_to '/'
end
@over_anon_threshold = true if @over_anon_threshold.nil?
def get_page_title(fandom, author, title, options = {})
# truncate any piece that is over 15 chars long to the nearest word
if options[:truncate]
fandom = fandom.gsub(/^(.{15}[\w.]*)(.*)/) {$2.empty? ? $1 : $1 + '...'}
author = author.gsub(/^(.{15}[\w.]*)(.*)/) {$2.empty? ? $1 : $1 + '...'}
title = title.gsub(/^(.{15}[\w.]*)(.*)/) {$2.empty? ? $1 : $1 + '...'}
end
@page_title = ""
if logged_in? && !current_user.preference.try(:work_title_format).blank?
@page_title = current_user.preference.work_title_format
@page_title.gsub!(/FANDOM/, fandom)
@page_title.gsub!(/AUTHOR/, author)
@page_title.gsub!(/TITLE/, title)
else
@page_title = title + " - " + author + " - " + fandom
end
@page_title += " [#{ArchiveConfig.APP_NAME}]" unless options[:omit_archive_name]
@page_title.html_safe
end
# Define media for fandoms menu
before_filter :set_media
def set_media
uncategorized = Media.uncategorized
@menu_media = Media.by_name - [Media.find_by_name(ArchiveConfig.MEDIA_NO_TAG_NAME), uncategorized] + [uncategorized]
end
### GLOBALIZATION ###
# before_filter :load_locales
# before_filter :set_preferred_locale
# I18n.backend = I18nDB::Backend::DBBased.new
# I18n.record_missing_keys = false # if you want to record missing translations
protected
def load_locales
@loaded_locales ||= Locale.order(:iso)
end
# Sets the locale
def set_preferred_locale
# Loading the current locale
if session[:locale] && @loaded_locales.detect { |loc| loc.iso == session[:locale]}
set_locale session[:locale].to_sym
else
set_locale Locale.find_main_cached.iso.to_sym
end
@current_locale = Locale.find_by_iso(I18n.locale.to_s)
end
### -- END GLOBALIZATION -- ###
public
#### -- AUTHORIZATION -- ####
# It is just much easier to do this here than to try to stuff variable values into a constant in environment.rb
before_filter :set_redirects
def set_redirects
@logged_in_redirect = url_for(current_user) if current_user.is_a?(User)
@logged_out_redirect = url_for({:controller => 'session', :action => 'new'})
end
def is_registered_user?
logged_in? || logged_in_as_admin?
end
def is_admin?
logged_in_as_admin?
end
def see_adult?
params[:anchor] = "comments" if (params[:show_comments] && params[:anchor].blank?)
Rails.logger.debug "Added anchor #{params[:anchor]}"
return true if session[:adult] || logged_in_as_admin?
return false unless current_user
return true if current_user.is_author_of?(@work)
return true if current_user.preference && current_user.preference.adult
return false
end
def use_caching?
%w(staging production).include?(Rails.env) && @admin_settings.enable_test_caching?
end
protected
# Prevents banned and suspended users from adding/editing content
def check_user_status
if current_user.is_a?(User) && (current_user.suspended? || current_user.banned?)
if current_user.suspended?
flash[:error] = t('suspension_notice', default: "Your account has been suspended until %{suspended_until}. You may not add or edit content until your suspension has been resolved. Please <a href=\"#{new_abuse_report_path}\">contact Abuse</a> for more information.", suspended_until: localize(current_user.suspended_until)).html_safe
else
flash[:error] = t('ban_notice', default: "Your account has been banned. You are not permitted to add or edit archive content. Please <a href=\"#{new_abuse_report_path}\">contact Abuse</a> for more information.").html_safe
end
redirect_to current_user
end
end
# Does the current user own a specific object?
def current_user_owns?(item)
!item.nil? && current_user.is_a?(User) && (item.is_a?(User) ? current_user == item : current_user.is_author_of?(item))
end
# Make sure a specific object belongs to the current user and that they have permission
# to view, edit or delete it
def check_ownership
access_denied(:redirect => @check_ownership_of) unless current_user_owns?(@check_ownership_of)
end
def check_ownership_or_admin
return true if logged_in_as_admin?
access_denied(:redirect => @check_ownership_of) unless current_user_owns?(@check_ownership_of)
end
# Make sure the user is allowed to see a specific page
# includes a special case for restricted works and series, since we want to encourage people to sign up to read them
def check_visibility
if @check_visibility_of.respond_to?(:restricted) && @check_visibility_of.restricted && User.current_user.nil?
store_location
redirect_to login_path(restricted: true)
elsif @check_visibility_of.is_a? Skin
access_denied unless logged_in_as_admin? || current_user_owns?(@check_visibility_of) || @check_visibility_of.official?
else
is_hidden = (@check_visibility_of.respond_to?(:visible) && !@check_visibility_of.visible) ||
(@check_visibility_of.respond_to?(:visible?) && !@check_visibility_of.visible?) ||
(@check_visibility_of.respond_to?(:hidden_by_admin?) && @check_visibility_of.hidden_by_admin?)
can_view_hidden = logged_in_as_admin? || current_user_owns?(@check_visibility_of)
access_denied if (is_hidden && !can_view_hidden)
end
end
# Make sure user is allowed to access tag wrangling pages
def check_permission_to_wrangle
if @admin_settings.tag_wrangling_off? && !logged_in_as_admin?
flash[:error] = "Wrangling is disabled at the moment. Please check back later."
redirect_to root_path
else
logged_in_as_admin? || permit?("tag_wrangler") || access_denied
end
end
private
# With thanks from here: http://blog.springenwerk.com/2008/05/set-date-attribute-from-dateselect.html
def convert_date(hash, date_symbol_or_string)
attribute = date_symbol_or_string.to_s
return Date.new(hash[attribute + '(1i)'].to_i, hash[attribute + '(2i)'].to_i, hash[attribute + '(3i)'].to_i)
end
public
def valid_sort_column(param, model='work')
allowed = []
if model.to_s.downcase == 'work'
allowed = ['author', 'title', 'date', 'created_at', 'word_count', 'hit_count']
elsif model.to_s.downcase == 'tag'
allowed = ['name', 'created_at', 'suggested_fandoms', 'taggings_count']
elsif model.to_s.downcase == 'collection'
allowed = ['collections.title', 'collections.created_at']
elsif model.to_s.downcase == 'prompt'
allowed = %w(fandom created_at prompter)
elsif model.to_s.downcase == 'claim'
allowed = %w(created_at claimer)
end
!param.blank? && allowed.include?(param.to_s.downcase)
end
def set_sort_order
# sorting
@sort_column = (valid_sort_column(params[:sort_column],"prompt") ? params[:sort_column] : 'id')
@sort_direction = (valid_sort_direction(params[:sort_direction]) ? params[:sort_direction] : 'DESC')
if !params[:sort_direction].blank? && !valid_sort_direction(params[:sort_direction])
params[:sort_direction] = 'DESC'
end
@sort_order = @sort_column + " " + @sort_direction
end
def valid_sort_direction(param)
!param.blank? && ['asc', 'desc'].include?(param.to_s.downcase)
end
#### -- AUTHORIZATION -- ####
protect_from_forgery
end