From 21bb1efb46980b71687956bad2ca1e2bb0969e3c Mon Sep 17 00:00:00 2001 From: Dawa Ometto Date: Fri, 3 Apr 2015 17:21:40 +0200 Subject: [PATCH] Block on no-edit without using middleware --- lib/gollum/app.rb | 10 ++++------ lib/gollum/editing_auth.rb | 34 ---------------------------------- 2 files changed, 4 insertions(+), 40 deletions(-) delete mode 100644 lib/gollum/editing_auth.rb diff --git a/lib/gollum/app.rb b/lib/gollum/app.rb index 20deb6e4..cf98690f 100644 --- a/lib/gollum/app.rb +++ b/lib/gollum/app.rb @@ -13,8 +13,6 @@ require 'gollum/views/has_page' require File.expand_path '../helpers', __FILE__ -require 'gollum/editing_auth' - #required to upload bigger binary files Gollum::set_git_timeout(120) Gollum::set_git_max_filesize(190 * 10**6) @@ -51,8 +49,7 @@ module Precious class App < Sinatra::Base register Mustache::Sinatra include Precious::Helpers - use Precious::EditingAuth - + dir = File.dirname(File.expand_path(__FILE__)) # Detect unsupported browsers. @@ -96,6 +93,9 @@ module Precious end before do + settings.wiki_options[:allow_editing] = settings.wiki_options.fetch(:allow_editing, true) + @allow_editing = settings.wiki_options[:allow_editing] + forbid unless @allow_editing || request.request_method == "GET" Precious::App.set(:mustache, {:templates => settings.wiki_options[:template_dir]}) if settings.wiki_options[:template_dir] @base_url = url('/', false).chomp('/') # above will detect base_path when it's used with map in a config.ru @@ -103,8 +103,6 @@ module Precious @css = settings.wiki_options[:css] @js = settings.wiki_options[:js] @mathjax_config = settings.wiki_options[:mathjax_config] - settings.wiki_options[:allow_editing] = settings.wiki_options.fetch(:allow_editing, true) - @allow_editing = settings.wiki_options[:allow_editing] end get '/' do diff --git a/lib/gollum/editing_auth.rb b/lib/gollum/editing_auth.rb deleted file mode 100644 index 8a83159b..00000000 --- a/lib/gollum/editing_auth.rb +++ /dev/null @@ -1,34 +0,0 @@ -module Precious - class EditingAuth < Sinatra::Base - def initialize(app) - @app = app - end - - def call(env) - @env = env - # Blocks all potentially editable pages. Use EditingAuth::whitelist_pages to unblock pages. - unless (env["REQUEST_METHOD"] == "GET") || @app.settings.wiki_options[:allow_editing] - return block unless excluded_page? - end - @app.call(env) - end - - def block - [403, {'Content-Type' => 'text/html', 'Content-Length' => '9'}, ['Forbidden']] - end - - def excluded_page? - return false if env["REQUEST_PATH"].nil? - whitelist_pages.any? do |whitelisted_page| - env["REQUEST_PATH"].include? whitelisted_page - end - end - - private - # List pages paths as str that you want to whitelist. - # Pages will be compared with env["REQUEST_PATH"] using String::include? method. - def whitelist_pages - return ["/compare/"] - end - end -end