BigW Consortium Gitlab

group_members_controller.rb 2.42 KB
Newer Older
1
class Groups::GroupMembersController < Groups::ApplicationController
2
  # Authorize
3
  before_action :authorize_admin_group_member!, except: [:index, :leave]
4

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

    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)
16 17

    @group_member = @group.group_members.new
18
  end
19 20

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

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

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

29
    return render_403 unless can?(current_user, :update_group_member, @group_member)
30

31
    @group_member.update_attributes(member_params)
32 33 34
  end

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

37 38 39 40 41 42 43
    return render_403 unless can?(current_user, :destroy_group_member, @group_member)

    @group_member.destroy

    respond_to do |format|
      format.html { redirect_to group_group_members_path(@group), notice: 'User was successfully removed from group.' }
      format.js { render nothing: true }
44 45 46
    end
  end

47
  def resend_invite
48
    redirect_path = group_group_members_path(@group)
49

50
    @group_member = @group.group_members.find(params[:id])
51

52 53 54
    if @group_member.invite?
      @group_member.resend_invite

55
      redirect_to redirect_path, notice: 'The invitation was successfully resent.'
56
    else
57
      redirect_to redirect_path, alert: 'The invitation has already been accepted.'
58 59 60
    end
  end

61
  def leave
62
    @group_member = @group.group_members.find_by(user_id: current_user)
63

64 65
    if can?(current_user, :destroy_group_member, @group_member)
      @group_member.destroy
66

67
      redirect_to(dashboard_groups_path, notice: "You left #{group.name} group.")
68
    else
69 70 71 72 73
      if @group.last_owner?(current_user)
        redirect_to(dashboard_groups_path, alert: "You can not leave #{group.name} group because you're the last owner. Transfer or delete the group.")
      else
        return render_403
      end
74 75 76
    end
  end

77 78
  protected

79
  def member_params
80
    params.require(:group_member).permit(:access_level, :user_id)
81
  end
82
end