project_members_controller.rb 2.45 KB
Newer Older
1
class Projects::ProjectMembersController < Projects::ApplicationController
2
  include MembershipActions
3
  include MembersPresentation
4
  include SortingHelper
5

6
  # Authorize
7
  before_action :authorize_admin_project_member!, except: [:index, :leave, :request_access]
8 9

  def index
10 11 12 13 14 15 16 17 18 19 20 21 22 23
    @sort = params[:sort].presence || sort_value_name
    @group_links = @project.project_group_links

    @skip_groups = @group_links.pluck(:group_id)
    @skip_groups << @project.namespace_id unless @project.personal?
    @skip_groups += @project.group.ancestors.pluck(:id) if @project.group

    @project_members = MembersFinder.new(@project, current_user).execute

    if params[:search].present?
      @project_members = @project_members.joins(:user).merge(User.search(params[:search]))
      @group_links = @group_links.where(group_id: @project.invited_groups.search(params[:search]).select(:id))
    end

24 25
    @project_members = present_members(@project_members.sort(@sort).page(params[:page]))
    @requesters = present_members(AccessRequestsFinder.new(@project).execute(current_user))
26
    @project_member = @project.project_members.new
27 28 29
  end

  def update
30
    @project_member = @project.members_and_requesters.find(params[:id])
31
      .present(current_user: current_user)
32 33 34

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

35 36 37
    @project_member.update_attributes(member_params)
  end

38
  def resend_invite
39
    redirect_path = project_project_members_path(@project)
40

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

43 44 45
    if @project_member.invite?
      @project_member.resend_invite

46
      redirect_to redirect_path, notice: 'The invitation was successfully resent.'
47
    else
48
      redirect_to redirect_path, alert: 'The invitation has already been accepted.'
49 50 51
    end
  end

52 53 54 55
  def import
    @projects = current_user.authorized_projects.order_id_desc
  end

56
  def apply_import
57
    source_project = Project.find(params[:source_project_id])
58

59 60
    if can?(current_user, :read_project_member, source_project)
      status = @project.team.import(source_project, current_user)
61 62
      notice = status ? "Successfully imported" : "Import failed"
    else
63
      return render_404
64
    end
65

66
    redirect_to(project_project_members_path(project),
67 68 69 70 71 72
                notice: notice)
  end

  protected

  def member_params
73
    params.require(:project_member).permit(:user_id, :access_level, :expires_at)
74
  end
75

76 77
  # MembershipActions concern
  alias_method :membershipable, :project
78
end