wikis_controller.rb 3.29 KB
Newer Older
1
class Projects::WikisController < Projects::ApplicationController
2
  before_action :authorize_read_wiki!
3
  before_action :authorize_create_wiki!, only: [:edit, :create, :history]
4 5
  before_action :authorize_admin_wiki!, only: :destroy
  before_action :load_project_wiki
6

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

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

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

21 22 23 24 25 26
      send_data(
        file.raw_data,
        type: file.mime_type,
        disposition: 'inline',
        filename: file.name
      )
27
    else
28
      return render('empty') unless can?(current_user, :create_wiki, @project)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
29 30
      @page = WikiPage.new(@project_wiki)
      @page.title = params[:id]
31 32

      render 'edit'
Valery Sizov's avatar
Valery Sizov committed
33 34 35 36
    end
  end

  def edit
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
37
    @page = @project_wiki.find_page(params[:id])
38 39 40
  end

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

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

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

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

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

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

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
75
    unless @page
Vinnie Okada's avatar
Vinnie Okada committed
76
      redirect_to(
77
        project_wiki_path(@project, :home),
Vinnie Okada's avatar
Vinnie Okada committed
78 79
        notice: "Page not found"
      )
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
80
    end
Valery Sizov's avatar
Valery Sizov committed
81
  end
82

Valery Sizov's avatar
Valery Sizov committed
83
  def destroy
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
84
    @page = @project_wiki.find_page(params[:id])
85
    WikiPages::DestroyService.new(@project, current_user).execute(@page)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
86

87
    redirect_to project_wiki_path(@project, :home),
88 89
                status: 302,
                notice: "Page was successfully deleted"
90
  end
Valery Sizov's avatar
Valery Sizov committed
91

92
  def git_access
93 94
  end

95
  def preview_markdown
96
    result = PreviewMarkdownService.new(@project, current_user, params).execute
97 98

    render json: {
99
      body: view_context.markdown(result[:text], pipeline: :wiki, project_wiki: @project_wiki, page_slug: params[:id]),
100
      references: {
101
        users: result[:users]
102 103 104 105
      }
    }
  end

106 107
  private

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
108 109
  def load_project_wiki
    @project_wiki = ProjectWiki.new(@project, current_user)
110 111

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

  def wiki_params
121
    params.require(:wiki).permit(:title, :content, :format, :message, :last_commit_sha)
122
  end
Valery Sizov's avatar
Valery Sizov committed
123
end