BigW Consortium Gitlab

groups_controller.rb 3.07 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
    @last_push = current_user.recent_push if current_user
41

42
    @projects = @projects.includes(:namespace)
43
    @projects = @projects.sorted_by_activity
44 45
    @projects = filter_projects(@projects)
    @projects = @projects.sort(@sort = params[:sort])
46
    @projects = @projects.page(params[:page]) if params[:filter_projects].blank?
47

Douwe Maan committed
48
    @shared_projects = GroupProjectsFinder.new(group, only_shared: true).execute(current_user)
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 activity
67
    respond_to do |format|
68 69
      format.html

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

77 78 79
  def edit
  end

80
  def projects
Douwe Maan committed
81
    @projects = @group.projects.page(params[:page])
82 83
  end

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

  def destroy
93
    DestroyGroupService.new(@group, current_user).execute
94

95
    redirect_to root_path, alert: "Group '#{@group.name}' was successfully deleted."
96 97
  end

98 99
  protected

100
  def authorize_create_group!
101 102 103 104 105
    unless can?(current_user, :create_group, nil)
      return render_404
    end
  end

106
  def determine_layout
107
    if [:new, :create].include?(action_name.to_sym)
108
      'application'
109 110
    elsif [:edit, :update, :projects].include?(action_name.to_sym)
      'group_settings'
111
    else
112
      'group'
113 114
    end
  end
115

116
  def group_params
117
    params.require(:group).permit(:name, :description, :path, :avatar, :public, :visibility_level, :share_with_group_lock)
118
  end
119 120

  def load_events
121
    @events = Event.in_projects(@projects)
122 123 124
    @events = event_filter.apply_filter(@events).with_associations
    @events = @events.limit(20).offset(params[:offset] || 0)
  end
125
end