BigW Consortium Gitlab

groups_controller.rb 3.45 KB
Newer Older
1
class GroupsController < Groups::ApplicationController
2
  include FilterProjects
3 4 5
  include IssuesAction
  include MergeRequestsAction

6
  respond_to :html
7

8
  before_action :authenticate_user!, only: [:new, :create]
9
  before_action :group, except: [:index, :new, :create]
10

11
  # Authorize
12 13
  before_action :authorize_admin_group!, only: [:edit, :update, :destroy, :projects]
  before_action :authorize_create_group!, only: [:new, :create]
14 15

  # Load group projects
16
  before_action :group_projects, only: [:show, :projects, :activity, :issues, :merge_requests]
17
  before_action :event_filter, only: [:activity]
18

19 20
  layout :determine_layout

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

25 26 27 28 29
  def new
    @group = Group.new
  end

  def create
Felipe Artur committed
30
    @group = Groups::CreateService.new(current_user, group_params).execute
31

Felipe Artur committed
32
    if @group.persisted?
33
      redirect_to @group, notice: "Group '#{@group.name}' was successfully created."
34 35 36 37
    else
      render action: "new"
    end
  end
38

39
  def show
40 41 42 43
    if current_user
      @last_push            = current_user.recent_push
      @notification_setting = current_user.notification_settings_for(group)
    end
44

45 46
    @nested_groups = group.children

47
    setup_projects
48

49 50
    respond_to do |format|
      format.html
51 52

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

58 59 60 61
      format.atom do
        load_events
        render layout: false
      end
62 63 64
    end
  end

65
  def activity
66
    respond_to do |format|
67 68
      format.html

69 70 71 72 73 74 75
      format.json do
        load_events
        pager_json("events/_events", @events.count)
      end
    end
  end

76 77 78
  def edit
  end

79
  def projects
80
    @projects = @group.projects.with_statistics.page(params[:page])
81 82
  end

83
  def update
84
    if Groups::UpdateService.new(@group, current_user, group_params).execute
85
      redirect_to edit_group_path(@group), notice: "Group '#{@group.name}' was successfully updated."
86
    else
87
      @group.restore_path!
James Lopez committed
88

89
      render action: "edit"
90 91 92 93
    end
  end

  def destroy
94
    DestroyGroupService.new(@group, current_user).async_execute
95

96
    redirect_to root_path, alert: "Group '#{@group.name}' was scheduled for deletion."
97 98
  end

99 100
  protected

101 102 103 104 105 106 107 108 109 110
  def setup_projects
    @projects = @projects.includes(:namespace)
    @projects = @projects.sorted_by_activity
    @projects = filter_projects(@projects)
    @projects = @projects.sort(@sort = params[:sort])
    @projects = @projects.page(params[:page]) if params[:filter_projects].blank?

    @shared_projects = GroupProjectsFinder.new(group, only_shared: true).execute(current_user)
  end

111
  def authorize_create_group!
112 113 114 115 116
    unless can?(current_user, :create_group, nil)
      return render_404
    end
  end

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

127
  def group_params
128 129 130 131 132
    params.require(:group).permit(group_params_ce)
  end

  def group_params_ce
    [
133
      :avatar,
134
      :description,
135 136
      :lfs_enabled,
      :name,
137 138 139
      :path,
      :public,
      :request_access_enabled,
140 141
      :share_with_group_lock,
      :visibility_level
142
    ]
143
  end
144 145

  def load_events
146
    @events = Event.in_projects(@projects)
147 148 149
    @events = event_filter.apply_filter(@events).with_associations
    @events = @events.limit(20).offset(params[:offset] || 0)
  end
150
end