Read about our upcoming Code of Conduct on this issue

projects_controller.rb 3.72 KB
Newer Older
1
class ProjectsController < ApplicationController
2
  skip_before_filter :authenticate_user!, only: [:show]
3
4
  before_filter :project, except: [:new, :create]
  before_filter :repository, except: [:new, :create]
gitlabhq's avatar
gitlabhq committed
5
6

  # Authorize
7
  before_filter :authorize_read_project!, except: [:index, :new, :create]
8
  before_filter :authorize_admin_project!, only: [:edit, :update, :destroy, :transfer, :archive, :unarchive]
9
  before_filter :require_non_empty_project, only: [:blob, :tree, :graph]
gitlabhq's avatar
gitlabhq committed
10

11
  layout 'navless', only: [:new, :create, :fork]
12
  before_filter :set_title, only: [:new, :create]
13

gitlabhq's avatar
gitlabhq committed
14
15
16
17
18
  def new
    @project = Project.new
  end

  def edit
19
    render 'edit', layout: "project_settings"
gitlabhq's avatar
gitlabhq committed
20
21
22
  end

  def create
23
    @project = ::Projects::CreateContext.new(current_user, params[:project]).execute
gitlabhq's avatar
gitlabhq committed
24
25

    respond_to do |format|
26
      flash[:notice] = 'Project was successfully created.' if @project.saved?
27
28
      format.html do
        if @project.saved?
29
          redirect_to @project
30
        else
31
          render "new"
32
        end
gitlabhq's avatar
gitlabhq committed
33
34
35
36
      end
      format.js
    end
  end
gitlabhq's avatar
gitlabhq committed
37

gitlabhq's avatar
gitlabhq committed
38
  def update
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
39
    status = ::Projects::UpdateContext.new(@project, current_user, params).execute
40

gitlabhq's avatar
gitlabhq committed
41
    respond_to do |format|
42
      if status
43
        flash[:notice] = 'Project was successfully updated.'
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
44
        format.html { redirect_to edit_project_path(@project), notice: 'Project was successfully updated.' }
Nihad Abbasov's avatar
Nihad Abbasov committed
45
        format.js
gitlabhq's avatar
gitlabhq committed
46
      else
47
        format.html { render "edit", layout: "project_settings" }
Nihad Abbasov's avatar
Nihad Abbasov committed
48
        format.js
gitlabhq's avatar
gitlabhq committed
49
      end
gitlabhq's avatar
gitlabhq committed
50
    end
51
  end
52

53
54
  def transfer
    ::Projects::TransferContext.new(project, current_user, params).execute
gitlabhq's avatar
gitlabhq committed
55
56
57
  end

  def show
58
    return authenticate_user! unless @project.public? || current_user
59

60
    limit = (params[:limit] || 20).to_i
61
62
63
    @events = @project.events.recent
    @events = event_filter.apply_filter(@events)
    @events = @events.limit(limit).offset(params[:offset] || 0)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
64
65

    respond_to do |format|
Nihad Abbasov's avatar
Nihad Abbasov committed
66
      format.html do
67
        if @project.empty_repo?
68
          render "projects/empty", layout: user_layout
69
        else
70
71
72
          if current_user
            @last_push = current_user.recent_push(@project.id)
          end
73
          render :show, layout: user_layout
74
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
75
      end
76
      format.json { pager_json("events/_events", @events.count) }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
77
    end
78
79
  end

gitlabhq's avatar
gitlabhq committed
80
  def destroy
81
82
    return access_denied! unless can?(current_user, :remove_project, project)

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
83
    project.team.truncate
gitlabhq's avatar
gitlabhq committed
84
85
86
    project.destroy

    respond_to do |format|
randx's avatar
randx committed
87
      format.html { redirect_to root_path }
gitlabhq's avatar
gitlabhq committed
88
89
    end
  end
90
91

  def fork
92
    @forked_project = ::Projects::ForkContext.new(project, current_user).execute
93
94
95

    respond_to do |format|
      format.html do
96
97
        if @forked_project.saved? && @forked_project.forked?
          redirect_to(@forked_project, notice: 'Project was successfully forked.')
98
        else
99
          @title = 'Fork project'
100
          render "fork"
101
102
103
104
105
        end
      end
      format.js
    end
  end
106
107
108
109

  def autocomplete_sources
    @suggestions = {
      emojis: Emoji.names,
110
      issues: @project.issues.select([:iid, :title, :description]),
111
      members: @project.team.members.sort_by(&:username).map { |user| { username: user.username, name: user.name } }
112
113
114
115
116
117
    }

    respond_to do |format|
      format.json { render :json => @suggestions }
    end
  end
118

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  def archive
    return access_denied! unless can?(current_user, :archive_project, project)
    project.archive!

    respond_to do |format|
      format.html { redirect_to @project }
    end
  end

  def unarchive
    return access_denied! unless can?(current_user, :archive_project, project)
    project.unarchive!

    respond_to do |format|
      format.html { redirect_to @project }
    end
  end

137
138
139
140
141
  private

  def set_title
    @title = 'New Project'
  end
142
143
144
145

  def user_layout
    current_user ? "projects" : "public_projects"
  end
gitlabhq's avatar
gitlabhq committed
146
end