BigW Consortium Gitlab

wikis_controller.rb 3.31 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 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 committed
10 11
  end

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

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 27 28 29 30
      if file.on_disk?
        send_file file.on_disk_path, disposition: 'inline'
      else
        send_data(
          file.raw_data,
          type: file.mime_type,
          disposition: 'inline',
          filename: file.name
        )
      end
31
    else
32
      return render('empty') unless can?(current_user, :create_wiki, @project)
33 34
      @page = WikiPage.new(@project_wiki)
      @page.title = params[:id]
35 36

      render 'edit'
Valery Sizov committed
37 38 39 40
    end
  end

  def edit
41
    @page = @project_wiki.find_page(params[:id])
42 43 44
  end

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

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

Dongqing Hu committed
50
    if @page.valid?
51
      redirect_to(
52
        project_wiki_path(@project, @page),
53 54
        notice: 'Wiki was successfully updated.'
      )
55 56 57
    else
      render 'edit'
    end
Valery Sizov committed
58 59 60
  end

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

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

  def history
74
    @page = @project_wiki.find_page(params[:id])
75

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

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

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

93
  def git_access
94 95
  end

96
  def preview_markdown
97 98 99 100 101 102 103 104
    result = PreviewMarkdownService.new(@project, current_user, params).execute

    render json: {
      body: view_context.markdown(result[:text], pipeline: :wiki, project_wiki: @project_wiki, page_slug: params[:id]),
      references: {
        users: result[:users]
      }
    }
Valery Sizov committed
105
  end
106 107 108

  private

109 110
  def load_project_wiki
    @project_wiki = ProjectWiki.new(@project, current_user)
111 112

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

  def wiki_params
122
    params.require(:wiki).permit(:title, :content, :format, :message)
123
  end
Valery Sizov committed
124
end