BigW Consortium Gitlab

project_members_controller.rb 3.46 KB
Newer Older
1 2
class Projects::ProjectMembersController < Projects::ApplicationController
  # Authorize
3
  before_action :authorize_admin_project_member!, except: [:leave, :index]
4 5 6

  def index
    @project_members = @project.project_members
7
    @project_members = @project_members.non_invite unless can?(current_user, :admin_project, @project)
8 9 10 11 12 13 14 15 16 17 18

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

    @project_members = @project_members.order('access_level DESC')

    @group = @project.group
    if @group
      @group_members = @group.group_members
19
      @group_members = @group_members.non_invite unless can?(current_user, :admin_group, @group)
20 21 22 23 24

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

26
      @group_members = @group_members.order('access_level DESC')
27 28 29
    end

    @project_member = @project.project_members.new
30
    @project_group_links = @project.project_group_links
31 32 33
  end

  def create
34
    @project.team.add_users(params[:user_ids].split(','), params[:access_level], current_user)
35 36 37 38 39

    redirect_to namespace_project_project_members_path(@project.namespace, @project)
  end

  def update
40
    @project_member = @project.project_members.find(params[:id])
41 42 43

    return render_403 unless can?(current_user, :update_project_member, @project_member)

44 45 46 47
    @project_member.update_attributes(member_params)
  end

  def destroy
48
    @project_member = @project.project_members.find(params[:id])
49 50 51

    return render_403 unless can?(current_user, :destroy_project_member, @project_member)

52 53 54 55
    @project_member.destroy

    respond_to do |format|
      format.html do
56
        redirect_to namespace_project_project_members_path(@project.namespace, @project)
57
      end
58
      format.js { head :ok }
59 60 61
    end
  end

62
  def resend_invite
63 64
    redirect_path = namespace_project_project_members_path(@project.namespace, @project)

65
    @project_member = @project.project_members.find(params[:id])
66

67 68 69
    if @project_member.invite?
      @project_member.resend_invite

70
      redirect_to redirect_path, notice: 'The invitation was successfully resent.'
71
    else
72
      redirect_to redirect_path, alert: 'The invitation has already been accepted.'
73 74 75
    end
  end

76
  def leave
77
    @project_member = @project.project_members.find_by(user_id: current_user)
78

79 80
    if can?(current_user, :destroy_project_member, @project_member)
      @project_member.destroy
81

82 83
      respond_to do |format|
        format.html { redirect_to dashboard_projects_path, notice: "You left the project." }
84
        format.js { head :ok }
85 86 87 88 89 90 91 92
      end
    else
      if current_user == @project.owner
        message = 'You can not leave your own project. Transfer or delete the project.'
        redirect_back_or_default(default: { action: 'index' }, options: { alert: message })
      else
        render_403
      end
93 94 95 96
    end
  end

  def apply_import
97
    source_project = Project.find(params[:source_project_id])
98

99 100
    if can?(current_user, :read_project_member, source_project)
      status = @project.team.import(source_project, current_user)
101 102
      notice = status ? "Successfully imported" : "Import failed"
    else
103
      return render_404
104
    end
105 106 107 108 109 110 111 112 113 114 115

    redirect_to(namespace_project_project_members_path(project.namespace, project),
                notice: notice)
  end

  protected

  def member_params
    params.require(:project_member).permit(:user_id, :access_level)
  end
end