BigW Consortium Gitlab

groups_controller.rb 3.32 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
    setup_projects
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
60 61 62
    end
  end

63
  def activity
64
    respond_to do |format|
65 66
      format.html

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

74 75 76
  def edit
  end

77 78 79 80
  def projects
    @projects = @group.projects.page(params[:page])
  end

81
  def update
82
    if Groups::UpdateService.new(@group, current_user, group_params).execute
83
      redirect_to edit_group_path(@group), notice: "Group '#{@group.name}' was successfully updated."
84 85 86 87 88 89
    else
      render action: "edit"
    end
  end

  def destroy
90
    DestroyGroupService.new(@group, current_user).async_execute
91

92
    redirect_to root_path, alert: "Group '#{@group.name}' was scheduled for deletion."
93 94
  end

95 96
  protected

97 98 99 100 101 102 103 104 105 106
  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

107
  def authorize_create_group!
108 109 110 111 112
    unless can?(current_user, :create_group, nil)
      return render_404
    end
  end

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

123
  def group_params
124
    params.require(:group).permit(
125
      :avatar,
126
      :description,
127 128
      :lfs_enabled,
      :name,
129 130 131
      :path,
      :public,
      :request_access_enabled,
132 133
      :share_with_group_lock,
      :visibility_level
134
    )
135
  end
136 137

  def load_events
138
    @events = Event.in_projects(@projects)
139 140 141
    @events = event_filter.apply_filter(@events).with_associations
    @events = @events.limit(20).offset(params[:offset] || 0)
  end
142
end