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

randx's avatar
randx committed
6
  respond_to :html
7 8 9

  skip_before_action :authenticate_user!, only: [:index, :show, :issues, :merge_requests]
  before_action :group, except: [:index, :new, :create]
randx's avatar
randx committed
10

11
  # Authorize
12
  before_action :authorize_read_group!, except: [:index, :show, :new, :create, :autocomplete]
13 14
  before_action :authorize_admin_group!, only: [:edit, :update, :destroy, :projects]
  before_action :authorize_create_group!, only: [:new, :create]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
15 16

  # Load group projects
17
  before_action :load_projects, except: [:index, :new, :create, :projects, :edit, :update, :autocomplete]
18
  before_action :event_filter, only: [:activity]
19

20 21
  layout :determine_layout

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

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
26 27 28 29 30
  def new
    @group = Group.new
  end

  def create
31
    @group = Group.new(group_params)
32
    @group.name = @group.path.dup unless @group.name
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
33 34

    if @group.save
35
      @group.add_owner(current_user)
36
      redirect_to @group, notice: "Group '#{@group.name}' was successfully created."
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
37 38 39 40
    else
      render action: "new"
    end
  end
41

randx's avatar
randx committed
42
  def show
43
    @last_push = current_user.recent_push if current_user
44
    @projects = @projects.includes(:namespace)
45 46
    @projects = filter_projects(@projects)
    @projects = @projects.sort(@sort = params[:sort])
Josh Frye's avatar
Josh Frye committed
47
    @projects = @projects.page(params[:page]).per(PER_PAGE) if params[:filter_projects].blank?
randx's avatar
randx committed
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
randx's avatar
randx committed
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 80 81 82
  def projects
    @projects = @group.projects.page(params[:page])
  end

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

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

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

randx's avatar
randx committed
97 98 99
  protected

  def group
skv's avatar
skv committed
100
    @group ||= Group.find_by(path: params[:id])
James Lopez's avatar
James Lopez committed
101
    @group || render_404
randx's avatar
randx committed
102 103
  end

104
  def load_projects
105
    @projects ||= ProjectsFinder.new.execute(current_user, group: group).sorted_by_activity
randx's avatar
randx committed
106 107
  end

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
  def group_params
136
    params.require(:group).permit(:name, :description, :path, :avatar, :public)
137
  end
138 139

  def load_events
140
    @events = Event.in_projects(@projects)
141 142 143
    @events = event_filter.apply_filter(@events).with_associations
    @events = @events.limit(20).offset(params[:offset] || 0)
  end
randx's avatar
randx committed
144
end