app/controllers/cms/content_controller.rb in browsercms-3.4.2 vs app/controllers/cms/content_controller.rb in browsercms-3.5.0.rc1
- old
+ new
@@ -1,184 +1,172 @@
module Cms
-class ContentController < Cms::ApplicationController
- include Cms::ContentRenderingSupport
+ class ContentController < Cms::ApplicationController
+ include Cms::ContentRenderingSupport
+ include Cms::Attachments::Serving
- skip_before_filter :redirect_to_cms_site
- before_filter :redirect_non_cms_users_to_public_site, :only => [:show, :show_page_route]
- before_filter :construct_path, :only => [:show]
- before_filter :construct_path_from_route, :only => [:show_page_route]
- before_filter :try_to_redirect, :only => [:show]
- before_filter :try_to_stream_file, :only => [:show]
- before_filter :check_access_to_page, :only => [:show, :show_page_route]
+ include Cms::MobileAware
+ helper MobileHelper
+ skip_before_filter :redirect_to_cms_site
+ before_filter :redirect_non_cms_users_to_public_site, :only => [:show, :show_page_route]
+ before_filter :construct_path, :only => [:show]
+ before_filter :construct_path_from_route, :only => [:show_page_route]
+ before_filter :try_to_redirect, :only => [:show]
+ before_filter :try_to_stream_file, :only => [:show]
+ before_filter :check_access_to_page, :only => [:show, :show_page_route]
+ before_filter :select_cache_directory
-
- # ----- Actions --------------------------------------------------------------
- def show
- render_page_with_caching
- end
- def show_page_route
- render_page_with_caching
- end
+ # ----- Actions --------------------------------------------------------------
+ def show
+ render_page_with_caching
+ end
+ def show_page_route
+ render_page_with_caching
+ end
- # Used by the rendering behavior
- def instance_variables_for_rendering
- instance_variables - (@initial_ivars || []) - ["@initial_ivars"]
- end
- protected
+ # Used by the rendering behavior
+ def instance_variables_for_rendering
+ instance_variables - (@initial_ivars || []) - ["@initial_ivars"]
+ end
- # This will assign the value to an instance variable
- def assign(key, value)
- instance_variable_set("@#{key}", value)
- end
+ protected
- private
-
- # This is the method all actions delegate to
- # check_access_to_page will also call this directly
- # if caching is not enabled
- def render_page
- @_page_route.execute(self) if @_page_route
- prepare_connectables_for_render
- render :layout => @page.layout, :action => 'show'
- end
-
- def render_page_with_caching
- render_page
- cache_page if perform_caching
- end
-
- # ----- Before Filters -------------------------------------------------------
- def construct_path
- # @paths = params[:cms_page_path] || params[:path] || []
- @path = "/#{params[:path]}"
- @paths = @path.split("/")
- end
-
- def construct_path_from_route
- @_page_route = PageRoute.find(params[:_page_route_id])
- @path = @_page_route.page.path
- @initial_ivars = instance_variables
- eval @_page_route.code
- end
-
- def redirect_non_cms_users_to_public_site
- @show_toolbar = false
- if perform_caching
- logger.info "Caching is enabled"
- if cms_site?
- logger.info "This is the cms site"
- if current_user.able_to?(:edit_content, :publish_content, :administrate)
- logger.info "User has access to cms"
- @show_toolbar = true
+ # This will assign the value to an instance variable
+ def assign(key, value)
+ instance_variable_set("@#{key}", value)
+ end
+
+ private
+
+ # This is the method all actions delegate to
+ # check_access_to_page will also call this directly
+ # if caching is not enabled
+ def render_page
+ logger.warn "Render page (id: #{@page.id})"
+ @_page_route.execute(self) if @_page_route
+ prepare_connectables_for_render
+ page_layout = determine_page_layout
+ render :layout => page_layout, :action => 'show'
+ end
+
+ def render_page_with_caching
+ render_page
+ cache_page if should_write_to_page_cache?
+ end
+
+ # ----- Before Filters -------------------------------------------------------
+ def construct_path
+ # @paths = params[:cms_page_path] || params[:path] || []
+ @path = "/#{params[:path]}"
+ @paths = @path.split("/")
+ end
+
+ def construct_path_from_route
+ @_page_route = PageRoute.find(params[:_page_route_id])
+ @path = @_page_route.page.path
+ @initial_ivars = instance_variables
+ eval @_page_route.code
+ end
+
+ def redirect_non_cms_users_to_public_site
+ @show_toolbar = false
+ if using_cms_subdomains?
+ logger.debug "Using cms subdomain is enabled"
+ if request_is_for_cms_subdomain?
+ logger.debug "User has required a page on the cms subdomain."
+ if current_user.able_to?(:edit_content, :publish_content, :administrate)
+ logger.debug "User has access to cms"
+ @show_toolbar = true
+ else
+ logger.debug "User does not have access to cms"
+ redirect_to url_without_cms_domain_prefix
+ end
else
- logger.info "User does not have access to cms"
- redirect_to url_without_cms_domain_prefix
+ logger.debug "User has requested a page which is not on the cms domain."
end
else
- logger.info "Not the cms site"
+ logger.debug "Using cms subdomain is disabled"
+ if current_user.able_to?(:edit_content, :publish_content, :administrate)
+ @show_toolbar = true
+ end
end
- else
- logger.info "Caching is disabled"
- if current_user.able_to?(:edit_content, :publish_content, :administrate)
- @show_toolbar = true
+ @show_page_toolbar = @show_toolbar
+ true
+ end
+
+ def try_to_redirect
+ if redirect = Redirect.find_by_from_path(@path)
+ redirect_to redirect.to_path, :status => :moved_permanently
end
end
- @show_page_toolbar = @show_toolbar
- true
- end
-
- def try_to_redirect
- if redirect = Redirect.find_by_from_path(@path)
- redirect_to redirect.to_path, :status=>:moved_permanently
- end
- end
- # Determines if the current request is file that needs to be streamed.
- # Any URL with a . in it is considered a file.
- def is_file?
- split = request.url.split('.')
- ext = split.size > 1 ? split.last.to_s.downcase : nil
- !ext.blank?
- end
+ # Determines if the current request is file that needs to be streamed.
+ # Any URL with a . in it is considered a file.
+ def is_file?
+ split = request.url.split('.')
+ ext = split.size > 1 ? split.last.to_s.downcase : nil
+ !ext.blank?
+ end
- def try_to_stream_file
- if is_file?
-
- #Check access to file
- @attachment = Attachment.find_live_by_file_path(request.fullpath)
- if @attachment
- raise Cms::Errors::AccessDenied unless current_user.able_to_view?(@attachment)
-
- #Construct a path to where this file would be if it were cached
- @file = @attachment.full_file_location
-
- #Stream the file if it exists
- if @path != "/" && File.exists?(@file)
- logger.warn "Sending file #{@file}"
- send_file(@file,
- :filename => @attachment.file_name,
- :type => @attachment.file_type,
- :disposition => "inline"
- )
- end
+ def try_to_stream_file
+ if is_file?
+ @attachment = Attachment.find_live_by_file_path(request.fullpath)
+ send_attachment(@attachment)
end
+
end
-
- end
- def check_access_to_page
- set_page_mode
- if current_user.able_to?(:edit_content, :publish_content, :administrate)
- logger.debug "Displaying draft version of page"
- if page = Page.first(:conditions => {:path => @path})
- @page = page.as_of_draft_version
+ def check_access_to_page
+ set_page_mode
+ if current_user.able_to?(:edit_content, :publish_content, :administrate)
+ logger.debug "Displaying draft version of page"
+ if page = Page.first(:conditions => {:path => @path})
+ @page = page.as_of_draft_version
+ else
+ return render(:layout => 'cms/application',
+ :template => 'cms/content/no_page',
+ :status => :not_found)
+ end
else
- return render(:layout => 'cms/application',
- :template => 'cms/content/no_page',
- :status => :not_found)
+ logger.debug "Displaying live version of page"
+ @page = Page.find_live_by_path(@path)
+ page_not_found unless (@page && !@page.archived?)
end
- else
- logger.info "..... Displaying live version of page"
- @page = Page.find_live_by_path(@path)
- page_not_found unless (@page && !@page.archived?)
+
+ unless current_user.able_to_view?(@page)
+ store_location
+ raise Cms::Errors::AccessDenied
+ end
+
+ # Doing this so if you are logged in, you never see the cached page
+ # We are calling render_page just like the show action does
+ # But since we do it from a before filter, the page doesn't get cached
+ if logged_in?
+ logger.info "Not Caching, user is logged in"
+ render_page
+ elsif !@page.cacheable?
+ logger.info "Not Caching, page cachable is false"
+ render_page
+ elsif params[:cms_cache] == "false"
+ logger.info "Not Caching, cms_cache is false"
+ render_page
+ end
+
end
- unless current_user.able_to_view?(@page)
- store_location
- raise Cms::Errors::AccessDenied
+ # ----- Other Methods --------------------------------------------------------
+
+ def page_not_found
+ raise ActiveRecord::RecordNotFound.new("No page at '#{@path}'")
end
- # Doing this so if you are logged in, you never see the cached page
- # We are calling render_page just like the show action does
- # But since we do it from a before filter, the page doesn't get cached
- if logged_in?
- logger.info "Not Caching, user is logged in"
- render_page
- elsif !@page.cacheable?
- logger.info "Not Caching, page cachable is false"
- render_page
- elsif params[:cms_cache] == "false"
- logger.info "Not Caching, cms_cache is false"
- render_page
+ def set_page_mode
+ @mode = @show_toolbar && current_user.able_to?(:edit_content) ? (params[:mode] || session[:page_mode] || "edit") : "view"
+ session[:page_mode] = @mode
end
- end
-
- # ----- Other Methods --------------------------------------------------------
-
- def page_not_found
- raise ActiveRecord::RecordNotFound.new("No page at '#{@path}'")
- end
- def set_page_mode
- @mode = @show_toolbar && current_user.able_to?(:edit_content) ? (params[:mode] || session[:page_mode] || "edit") : "view"
- session[:page_mode] = @mode
end
-
-
-
-end
end