BigW Consortium Gitlab

groups_controller.rb 3.45 KB
Newer Older
1
class GroupsController < Groups::ApplicationController
2
  skip_before_action :authenticate_user!, only: [:show, :issues, :merge_requests]
3
  respond_to :html
4
  before_action :group, except: [:new, :create]
5

6
  # Authorize
7
  before_action :authorize_read_group!, except: [:show, :new, :create]
8 9
  before_action :authorize_admin_group!, only: [:edit, :update, :destroy, :projects]
  before_action :authorize_create_group!, only: [:new, :create]
10 11

  # Load group projects
12 13
  before_action :load_projects, except: [:new, :create, :projects, :edit, :update]
  before_action :event_filter, only: :show
14

15 16
  layout :determine_layout

17
  def index
18
    redirect_to(current_user ? dashboard_groups_path : explore_groups_path)
19 20
  end

21 22 23 24 25
  def new
    @group = Group.new
  end

  def create
26
    @group = Group.new(group_params)
27
    @group.name = @group.path.dup unless @group.name
28 29

    if @group.save
30
      @group.add_owner(current_user)
31
      redirect_to @group, notice: "Group '#{@group.name}' was successfully created."
32 33 34 35
    else
      render action: "new"
    end
  end
36

37
  def show
38
    @last_push = current_user.recent_push if current_user
39
    @projects = @projects.includes(:namespace)
40 41 42

    respond_to do |format|
      format.html
43 44

      format.json do
45
        load_events
46 47 48
        pager_json("events/_events", @events.count)
      end

49 50 51 52
      format.atom do
        load_events
        render layout: false
      end
53 54 55 56
    end
  end

  def merge_requests
57
    @merge_requests = get_merge_requests_collection
58
    @merge_requests = @merge_requests.page(params[:page]).per(PER_PAGE)
59
    @merge_requests = @merge_requests.preload(:author, :target_project)
60 61 62
  end

  def issues
63
    @issues = get_issues_collection
64
    @issues = @issues.page(params[:page]).per(PER_PAGE)
65
    @issues = @issues.preload(:author, :project)
66 67 68 69 70 71 72

    respond_to do |format|
      format.html
      format.atom { render layout: false }
    end
  end

73 74 75
  def edit
  end

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

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

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

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

94 95 96
  protected

  def group
skv committed
97
    @group ||= Group.find_by(path: params[:id])
98 99
  end

100
  def load_projects
101
    @projects ||= ProjectsFinder.new.execute(current_user, group: group).sorted_by_activity.non_archived
102 103 104
  end

  def project_ids
105
    @projects.pluck(:id)
106
  end
107 108 109

  # Dont allow unauthorized access to group
  def authorize_read_group!
110
    unless @group and (@projects.present? or can?(current_user, :read_group, @group))
111 112 113 114 115
      if current_user.nil?
        return authenticate_user!
      else
        return render_404
      end
116 117
    end
  end
118 119

  def authorize_create_group!
120 121 122 123 124
    unless can?(current_user, :create_group, nil)
      return render_404
    end
  end

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

135 136 137
  def group_params
    params.require(:group).permit(:name, :description, :path, :avatar)
  end
138 139 140 141 142 143

  def load_events
    @events = Event.in_projects(project_ids)
    @events = event_filter.apply_filter(@events).with_associations
    @events = @events.limit(20).offset(params[:offset] || 0)
  end
144
end