groups_controller.rb 3.5 KB
Newer Older
V
Valery Sizov 已提交
1
class GroupsController < Groups::ApplicationController
2 3 4
  include IssuesAction
  include MergeRequestsAction

R
randx 已提交
5
  respond_to :html
6 7 8

  skip_before_action :authenticate_user!, only: [:index, :show, :issues, :merge_requests]
  before_action :group, except: [:index, :new, :create]
R
randx 已提交
9

10
  # Authorize
11
  before_action :authorize_read_group!, except: [:index, :show, :new, :create, :autocomplete]
12 13
  before_action :authorize_admin_group!, only: [:edit, :update, :destroy, :projects]
  before_action :authorize_create_group!, only: [:new, :create]
D
Dmitriy Zaporozhets 已提交
14 15

  # Load group projects
16
  before_action :load_projects, except: [:index, :new, :create, :projects, :edit, :update, :autocomplete]
17
  before_action :event_filter, only: [:show, :events]
18

19 20
  layout :determine_layout

21
  def index
22
    redirect_to(current_user ? dashboard_groups_path : explore_groups_path)
23 24
  end

D
Dmitriy Zaporozhets 已提交
25 26 27 28 29
  def new
    @group = Group.new
  end

  def create
30
    @group = Group.new(group_params)
31
    @group.name = @group.path.dup unless @group.name
D
Dmitriy Zaporozhets 已提交
32 33

    if @group.save
34
      @group.add_owner(current_user)
35
      redirect_to @group, notice: "Group '#{@group.name}' was successfully created."
D
Dmitriy Zaporozhets 已提交
36 37 38 39
    else
      render action: "new"
    end
  end
40

R
randx 已提交
41
  def show
42
    @last_push = current_user.recent_push if current_user
43
    @projects = @projects.includes(:namespace)
44
    @projects = @projects.search(params[:filter_projects]) if params[:filter_projects].present?
J
Josh Frye 已提交
45
    @projects = @projects.page(params[:page]).per(PER_PAGE) if params[:filter_projects].blank?
R
randx 已提交
46 47 48

    respond_to do |format|
      format.html
49 50

      format.json do
51 52 53
        render json: {
          html: view_to_html_string("dashboard/projects/_projects", locals: { projects: @projects })
        }
54 55
      end

56 57 58 59
      format.atom do
        load_events
        render layout: false
      end
R
randx 已提交
60 61 62
    end
  end

63 64 65 66 67 68 69 70 71
  def events
    respond_to do |format|
      format.json do
        load_events
        pager_json("events/_events", @events.count)
      end
    end
  end

72 73 74
  def edit
  end

D
Dmitriy Zaporozhets 已提交
75 76 77 78
  def projects
    @projects = @group.projects.page(params[:page])
  end

79
  def update
80
    if @group.update_attributes(group_params)
81
      redirect_to edit_group_path(@group), notice: "Group '#{@group.name}' was successfully updated."
82 83 84 85 86 87
    else
      render action: "edit"
    end
  end

  def destroy
88
    DestroyGroupService.new(@group, current_user).execute
89

90
    redirect_to root_path, alert: "Group '#{@group.name}' was successfully deleted."
91 92
  end

R
randx 已提交
93 94 95
  protected

  def group
S
skv 已提交
96
    @group ||= Group.find_by(path: params[:id])
J
James Lopez 已提交
97
    @group || render_404
R
randx 已提交
98 99
  end

D
Dmitriy Zaporozhets 已提交
100
  def load_projects
101
    @projects ||= ProjectsFinder.new.execute(current_user, group: group).sorted_by_activity.non_archived
R
randx 已提交
102 103
  end

104 105
  # Dont allow unauthorized access to group
  def authorize_read_group!
D
Dmitriy Zaporozhets 已提交
106
    unless @group and (@projects.present? or can?(current_user, :read_group, @group))
107 108 109 110 111
      if current_user.nil?
        return authenticate_user!
      else
        return render_404
      end
112 113
    end
  end
114 115

  def authorize_create_group!
116 117 118 119 120
    unless can?(current_user, :create_group, nil)
      return render_404
    end
  end

121
  def determine_layout
122
    if [:new, :create].include?(action_name.to_sym)
123
      'application'
124 125
    elsif [:edit, :update, :projects].include?(action_name.to_sym)
      'group_settings'
126
    else
127
      'group'
128 129
    end
  end
130

131
  def group_params
132
    params.require(:group).permit(:name, :description, :path, :avatar, :public)
133
  end
134 135

  def load_events
136
    @events = Event.in_projects(@projects)
137 138 139
    @events = event_filter.apply_filter(@events).with_associations
    @events = @events.limit(20).offset(params[:offset] || 0)
  end
R
randx 已提交
140
end