diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index cfc884634..87375c34d 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -6,6 +6,8 @@ class WikiController < ApplicationController load_resource :project + before_filter :authorize_read_actions, :only => [:index, :show, :git, :compare, :compare_wiki, :history, :wiki_history, :search, :pages] + before_filter :authorize_write_actions, :only => [:edit, :update, :new, :create, :destroy, :revert, :revert_wiki, :preview] before_filter :get_wiki def index @@ -55,14 +57,14 @@ class WikiController < ApplicationController def update if can? :write, @project @name = CGI.unescape(params[:id]) - page = @wiki.page(@name) + @page = @wiki.page(@name) name = params[:rename] || @name committer = Gollum::Committer.new(@wiki, commit) - commit = {:committer => committer} + commit_arg = {:committer => committer} - update_wiki_page(@wiki, page, params[:content], commit, name, params[:format]) - update_wiki_page(@wiki, page.footer, params[:footer], commit) if params[:footer] - update_wiki_page(@wiki, page.sidebar, params[:sidebar], commit) if params[:sidebar] + update_wiki_page(@wiki, @page, params[:content], commit_arg, name, params[:format]) + update_wiki_page(@wiki, @page.footer, params[:footer], commit_arg) if params[:footer] + update_wiki_page(@wiki, @page.sidebar, params[:sidebar], commit_arg) if params[:sidebar] committer.commit @@ -185,7 +187,8 @@ class WikiController < ApplicationController else # if revert wasn't successful then redirect back to comparsion. # if second commit version is missed, then second version is - # params[:sha1] and first version is previous version related to params[:sha1] + # params[:sha1] and first version is parent of params[:sha1] + # (see Gollum::Wiki#revert_page) sha2, sha1 = sha1, "#{sha1}^" if !sha2 @versions = [sha1, sha2] diffs = @wiki.repo.diff(@versions.first, @versions.last, @page.path) @@ -198,6 +201,26 @@ class WikiController < ApplicationController end end + def revert_wiki + if can? :write, @project + sha1 = params[:sha1] + sha2 = params[:sha2] + if @wiki.revert_commit(sha1, sha2, commit) + flash[:notice] = t("flash.wiki.revert_success") + redirect_to project_wiki_index_path(@project) + else + sha2, sha1 = sha1, "#{sha1}^" if !sha2 + @versions = [sha1, sha2] + diffs = @wiki.repo.diff(@versions.first, @versions.last) + @diffs = [diffs.first] + flash[:error] = t("flash.wiki.patch_does_not_apply") + render :compare + end + else + redirect_to forbidden_path + end + end + def preview if can? :write, @project @name = params['page'] @@ -274,11 +297,14 @@ class WikiController < ApplicationController else # msg = "#{!!@wiki.page(@name) ? 'Updated page' : 'Created page'} #{@name}" msg = case action_name.to_s - when 'create' then 'Created page ' - when 'update' then 'Updated page ' - when 'revert' then 'Reverted page ' - end + @name.to_s + when 'create' then "Created page #{@name.to_s}" + when 'update' then "Updated page #{@name.to_s}" + when 'revert' then "Reverted page #{@name.to_s}" + when 'revert_wiki' then "Reverted wiki" + end + msg += " (#{params['format']})" if params['format'] end + msg = 'Unhandled action' if !msg || msg.empty? { :message => msg } end @@ -288,6 +314,7 @@ class WikiController < ApplicationController def show_or_create_page if @page + puts @page.format @content = @page.formatted_data @editable = can?(:write, @project) render :show @@ -301,5 +328,13 @@ class WikiController < ApplicationController redirect_to forbidden_path end end + + def authorize_read_actions + puts "authorize action #{action_name}" + end + + def authorize_write_actions + puts "authorize action #{action_name}" + end end diff --git a/app/helpers/wiki_helper.rb b/app/helpers/wiki_helper.rb index 6bfbe25aa..1f89e2d02 100644 --- a/app/helpers/wiki_helper.rb +++ b/app/helpers/wiki_helper.rb @@ -4,7 +4,7 @@ module WikiHelper if name revert_page_project_wiki_path(project, CGI.escape(name), first, second) else - revert_page_project_wiki_index_path(project, first, second) + revert_project_wiki_index_path(project, first, second) end end diff --git a/app/views/wiki/_compare.html.haml b/app/views/wiki/_compare.html.haml index 83d1b7959..7787f65e5 100644 --- a/app/views/wiki/_compare.html.haml +++ b/app/views/wiki/_compare.html.haml @@ -1,10 +1,13 @@ +- revert_button = capture do + = link_to t("layout.wiki.revert_page#{action_name == 'revert' ? '' : 's'}"), '#', :class => "gollum-revert-button" + #compare-content - if action_name != 'revert' %ul.actions %li.minibutton = form_tag revert_path(@project, @versions[0][0..6], @versions[1][0..6], @name), :name => "gollum-revert", :id => "gollum-revert-form" do - = link_to t("layout.wiki.revert_changes"), '#', :class => "gollum-revert-button" + = revert_button = render :partial => 'diff_data', :collection => @diffs, :as => :diff .spacer @@ -13,5 +16,5 @@ %ul.actions - if action_name != 'revert' %li.minibutton - = link_to t("layout.wiki.revert_changes"), '#', :class => "gollum-revert-button" + = revert_button %li.minibutton= link_to t("layout.wiki.back_to_top"), '#wiki' diff --git a/config/locales/en.yml b/config/locales/en.yml index 02168c8b4..d11fe3a04 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -214,7 +214,8 @@ en: history_for: History for editing_page: Editing create_page: Create Page - revert_changes: Revert Page + revert_page: Revert Page + revert_pages: Revert Pages all_pages_in: "All pages in %{ref}:" search_results_for: "Search results for %{query}:" preview: Preview diff --git a/config/locales/ru.yml b/config/locales/ru.yml index 0034846c7..fb935406a 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -214,7 +214,8 @@ ru: history_for: История для editing_page: Редактирование страницы create_page: Создать страницу - revert_changes: Откатить изменения + revert_page: Откатить изменения + revert_pages: Откатить изменения all_pages_in: "Все страницы в %{ref}:" search_results_for: "Результаты поиска для %{query}:" preview: Предпросмотр diff --git a/config/routes.rb b/config/routes.rb index 8a6c0107e..5695db689 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -86,7 +86,7 @@ Rosa::Application.routes.draw do collection do match '_history' => 'wiki#wiki_history', :as => :history, :via => :get match '_access' => 'wiki#git', :as => :git, :via => :get - match '_revert/:sha1/:sha2' => 'wiki#revert', :as => :revert_page, :via => [:get, :post] + match '_revert/:sha1/:sha2' => 'wiki#revert_wiki', :as => :revert, :via => [:get, :post] match '_compare' => 'wiki#compare_wiki', :as => :compare, :via => :post match '_compare/*versions' => 'wiki#compare_wiki', :as => :compare_versions, :via => :get post :preview