group_members_controller.rb 2.07 KB
Newer Older
1
class Groups::GroupMembersController < Groups::ApplicationController
2
  include MembershipActions
3

4
  # Authorize
5
  before_action :authorize_admin_group_member!, except: [:index, :leave, :request_access]
6

7 8 9
  def index
    @project = @group.projects.find(params[:project_id]) if params[:project_id]
    @members = @group.group_members
10
    @members = @members.non_pending unless can?(current_user, :admin_group, @group)
11 12 13 14 15 16 17

    if params[:search].present?
      users = @group.users.search(params[:search]).to_a
      @members = @members.where(user_id: users)
    end

    @members = @members.order('access_level DESC').page(params[:page]).per(50)
18 19

    @group_member = @group.group_members.new
20
  end
21 22

  def create
23
    @group.add_users(params[:user_ids].split(','), params[:access_level], current_user)
24

25
    redirect_to group_group_members_path(@group), notice: 'Users were successfully added.'
26 27 28
  end

  def update
29
    @group_member = @group.group_members.find(params[:id])
30

31
    return render_403 unless can?(current_user, :update_group_member, @group_member)
32

33
    @group_member.update_attributes(member_params)
34 35 36
  end

  def destroy
37
    @group_member = @group.group_members.find(params[:id])
38

39 40
    return render_403 unless can?(current_user, :destroy_group_member, @group_member)

41
    @group_member.destroy
42 43 44

    respond_to do |format|
      format.html { redirect_to group_group_members_path(@group), notice: 'User was successfully removed from group.' }
45
      format.js { head :ok }
46 47 48
    end
  end

49
  def resend_invite
Douwe Maan's avatar
Douwe Maan committed
50
    redirect_path = group_group_members_path(@group)
51

52
    @group_member = @group.group_members.find(params[:id])
Douwe Maan's avatar
Douwe Maan committed
53

54 55 56
    if @group_member.invite?
      @group_member.resend_invite

Douwe Maan's avatar
Douwe Maan committed
57
      redirect_to redirect_path, notice: 'The invitation was successfully resent.'
58
    else
Douwe Maan's avatar
Douwe Maan committed
59
      redirect_to redirect_path, alert: 'The invitation has already been accepted.'
60 61 62
    end
  end

63
  protected
64

65 66
  def member_params
    params.require(:group_member).permit(:access_level, :user_id)
67 68
  end

69 70
  # MembershipActions concern
  alias_method :membershipable, :group
71

72 73
  def cannot_leave?
    @group.last_owner?(current_user)
74
  end
75
end