BigW Consortium Gitlab

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

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
  before_action :group_projects, only: [:projects, :activity, :issues, :merge_requests]
16
  before_action :group_merge_requests, only: [:merge_requests]
17
  before_action :event_filter, only: [:activity]
18

19 20
  before_action :user_actions, only: [:show, :subgroups]

21 22
  layout :determine_layout

23
  def index
24
    redirect_to(current_user ? dashboard_groups_path : explore_groups_path)
25 26
  end

27 28 29 30 31
  def new
    @group = Group.new
  end

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

Felipe Artur committed
34
    if @group.persisted?
Z.J. van de Weg committed
35 36 37
      notice = if @group.chat_team.present?
                 "Group '#{@group.name}' and its Mattermost team were successfully created."
               else
Z.J. van de Weg committed
38
                 "Group '#{@group.name}' was successfully created."
Z.J. van de Weg committed
39 40 41
               end

      redirect_to @group, notice: notice
42 43 44 45
    else
      render action: "new"
    end
  end
46

47
  def show
48
    setup_projects
49

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

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

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

66
  def subgroups
67 68
    return not_found unless Group.supports_nested_groups?

69
    @nested_groups = GroupsFinder.new(current_user, parent: group).execute
70 71 72
    @nested_groups = @nested_groups.search(params[:filter_groups]) if params[:filter_groups].present?
  end

73
  def activity
74
    respond_to do |format|
75 76
      format.html

77 78 79 80 81 82 83
      format.json do
        load_events
        pager_json("events/_events", @events.count)
      end
    end
  end

84 85 86
  def edit
  end

87
  def projects
88
    @projects = @group.projects.with_statistics.page(params[:page])
89 90
  end

91
  def update
92
    if Groups::UpdateService.new(@group, current_user, group_params).execute
93
      redirect_to edit_group_path(@group), notice: "Group '#{@group.name}' was successfully updated."
94
    else
95
      @group.restore_path!
James Lopez committed
96

97
      render action: "edit"
98 99 100 101
    end
  end

  def destroy
102
    Groups::DestroyService.new(@group, current_user).async_execute
103

104
    redirect_to root_path, alert: "Group '#{@group.name}' was scheduled for deletion."
105 106
  end

107 108
  protected

109
  def setup_projects
110 111 112 113
    set_non_archived_param
    params[:sort] ||= 'latest_activity_desc'
    @sort = params[:sort]

114 115 116 117
    options = {}
    options[:only_owned] = true if params[:shared] == '0'
    options[:only_shared] = true if params[:shared] == '1'

118
    @projects = GroupProjectsFinder.new(params: params, group: group, options: options, current_user: current_user).execute
119
    @projects = @projects.includes(:namespace)
120
    @projects = @projects.page(params[:page]) if params[:name].blank?
121 122
  end

123
  def authorize_create_group!
124
    unless can?(current_user, :create_group)
125 126 127 128
      return render_404
    end
  end

129
  def determine_layout
130
    if [:new, :create].include?(action_name.to_sym)
131
      'application'
132 133
    elsif [:edit, :update, :projects].include?(action_name.to_sym)
      'group_settings'
134
    else
135
      'group'
136 137
    end
  end
138

139
  def group_params
140 141 142 143 144
    params.require(:group).permit(group_params_ce)
  end

  def group_params_ce
    [
145
      :avatar,
146
      :description,
147 148
      :lfs_enabled,
      :name,
149 150 151
      :path,
      :public,
      :request_access_enabled,
152
      :share_with_group_lock,
153
      :visibility_level,
154
      :parent_id,
155
      :create_chat_team,
156 157 158
      :chat_team_name,
      :require_two_factor_authentication,
      :two_factor_grace_period
159
    ]
160
  end
161 162

  def load_events
163
    @events = Event.in_projects(@projects)
164 165 166
    @events = event_filter.apply_filter(@events).with_associations
    @events = @events.limit(20).offset(params[:offset] || 0)
  end
167 168 169 170 171 172

  def user_actions
    if current_user
      @notification_setting = current_user.notification_settings_for(group)
    end
  end
173 174 175 176 177 178 179 180

  def build_canonical_path(group)
    return group_path(group) if action_name == 'show' # root group path
    
    params[:id] = group.to_param

    url_for(params)
  end
181
end