wikis_controller.rb 3.23 KB
Newer Older
1
class Projects::WikisController < Projects::ApplicationController
2 3
  include PreviewMarkdown

4
  before_action :authorize_read_wiki!
5
  before_action :authorize_create_wiki!, only: [:edit, :create, :history]
6 7
  before_action :authorize_admin_wiki!, only: :destroy
  before_action :load_project_wiki
8

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
9
  def pages
10
    @wiki_pages = Kaminari.paginate_array(@project_wiki.pages).page(params[:page])
11
    @wiki_entries = WikiPage.group_by_directory(@wiki_pages)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
12 13
  end

Valery Sizov's avatar
Valery Sizov committed
14
  def show
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
15
    @page = @project_wiki.find_page(params[:id], params[:version_id])
16

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
17
    if @page
18
      render 'show'
19
    elsif file = @project_wiki.find_file(params[:id], params[:version_id])
20 21 22
      response.headers['Content-Security-Policy'] = "default-src 'none'"
      response.headers['X-Content-Security-Policy'] = "default-src 'none'"

23 24 25 26 27 28
      send_data(
        file.raw_data,
        type: file.mime_type,
        disposition: 'inline',
        filename: file.name
      )
29
    else
30
      return render('empty') unless can?(current_user, :create_wiki, @project)
31

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
32 33
      @page = WikiPage.new(@project_wiki)
      @page.title = params[:id]
34 35

      render 'edit'
Valery Sizov's avatar
Valery Sizov committed
36 37 38 39
    end
  end

  def edit
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
40
    @page = @project_wiki.find_page(params[:id])
41 42 43
  end

  def update
44
    return render('empty') unless can?(current_user, :create_wiki, @project)
45

46
    @page = @project_wiki.find_page(params[:id])
Dongqing Hu's avatar
Dongqing Hu committed
47
    @page = WikiPages::UpdateService.new(@project, current_user, wiki_params).execute(@page)
48

Dongqing Hu's avatar
Dongqing Hu committed
49
    if @page.valid?
50
      redirect_to(
51
        project_wiki_path(@project, @page),
52 53
        notice: 'Wiki was successfully updated.'
      )
54 55 56
    else
      render 'edit'
    end
57 58 59
  rescue WikiPage::PageChangedError
    @conflict = true
    render 'edit'
Valery Sizov's avatar
Valery Sizov committed
60 61 62
  end

  def create
63
    @page = WikiPages::CreateService.new(@project, current_user, wiki_params).execute
64

65
    if @page.persisted?
Vinnie Okada's avatar
Vinnie Okada committed
66
      redirect_to(
67
        project_wiki_path(@project, @page),
Vinnie Okada's avatar
Vinnie Okada committed
68 69
        notice: 'Wiki was successfully updated.'
      )
70 71
    else
      render action: "edit"
Valery Sizov's avatar
Valery Sizov committed
72 73
    end
  end
Valery Sizov's avatar
Valery Sizov committed
74 75

  def history
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
76
    @page = @project_wiki.find_page(params[:id])
77

78 79 80 81 82
    if @page
      @page_versions = Kaminari.paginate_array(@page.versions(page: params[:page]),
                                               total_count: @page.count_versions)
                               .page(params[:page])
    else
Vinnie Okada's avatar
Vinnie Okada committed
83
      redirect_to(
84
        project_wiki_path(@project, :home),
Vinnie Okada's avatar
Vinnie Okada committed
85 86
        notice: "Page not found"
      )
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
87
    end
Valery Sizov's avatar
Valery Sizov committed
88
  end
89

Valery Sizov's avatar
Valery Sizov committed
90
  def destroy
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
91
    @page = @project_wiki.find_page(params[:id])
92
    WikiPages::DestroyService.new(@project, current_user).execute(@page)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
93

94
    redirect_to project_wiki_path(@project, :home),
95 96
                status: 302,
                notice: "Page was successfully deleted"
97
  end
Valery Sizov's avatar
Valery Sizov committed
98

99
  def git_access
100 101
  end

102 103
  private

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
104 105
  def load_project_wiki
    @project_wiki = ProjectWiki.new(@project, current_user)
106 107

    # Call #wiki to make sure the Wiki Repo is initialized
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
108
    @project_wiki.wiki
109
    @sidebar_wiki_entries = WikiPage.group_by_directory(@project_wiki.pages(limit: 15))
110
  rescue ProjectWiki::CouldNotCreateWikiError
111
    flash[:notice] = "Could not create Wiki Repository at this time. Please try again later."
112
    redirect_to project_path(@project)
113 114 115 116
    return false
  end

  def wiki_params
117
    params.require(:wiki).permit(:title, :content, :format, :message, :last_commit_sha)
118
  end
Valery Sizov's avatar
Valery Sizov committed
119
end