2016-06-02 12:05:06 -04:00
|
|
|
module MembershipActions
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2017-04-17 07:45:14 -04:00
|
|
|
def create
|
2017-06-06 10:55:12 -04:00
|
|
|
create_params = params.permit(:user_ids, :access_level, :expires_at)
|
2018-02-16 09:10:22 -05:00
|
|
|
result = Members::CreateService.new(current_user, create_params).execute(membershipable)
|
2017-04-17 07:45:14 -04:00
|
|
|
|
2017-06-06 10:55:12 -04:00
|
|
|
if result[:status] == :success
|
2018-02-16 09:10:22 -05:00
|
|
|
redirect_to members_page_url, notice: 'Users were successfully added.'
|
2017-04-17 07:45:14 -04:00
|
|
|
else
|
2018-02-16 09:10:22 -05:00
|
|
|
redirect_to members_page_url, alert: result[:message]
|
2017-04-17 07:45:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-16 06:00:25 -05:00
|
|
|
def update
|
2018-02-16 09:10:22 -05:00
|
|
|
update_params = params.require(root_params_key).permit(:access_level, :expires_at)
|
2018-02-16 06:00:25 -05:00
|
|
|
member = membershipable.members_and_requesters.find(params[:id])
|
2018-02-16 09:10:22 -05:00
|
|
|
member = Members::UpdateService
|
|
|
|
.new(current_user, update_params)
|
2018-02-16 06:00:25 -05:00
|
|
|
.execute(member)
|
|
|
|
.present(current_user: current_user)
|
|
|
|
|
|
|
|
respond_to do |format|
|
2018-02-16 09:10:22 -05:00
|
|
|
format.js { render 'shared/members/update', locals: { member: member } }
|
2018-02-16 06:00:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-17 10:44:30 -04:00
|
|
|
def destroy
|
2017-10-11 10:47:08 -04:00
|
|
|
member = membershipable.members_and_requesters.find(params[:id])
|
2018-02-16 09:10:22 -05:00
|
|
|
Members::DestroyService.new(current_user).execute(member)
|
2017-04-17 10:44:30 -04:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
message = "User was successfully removed from #{source_type}."
|
|
|
|
redirect_to members_page_url, notice: message
|
|
|
|
end
|
|
|
|
|
|
|
|
format.js { head :ok }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-02 12:05:06 -04:00
|
|
|
def request_access
|
|
|
|
membershipable.request_access(current_user)
|
|
|
|
|
|
|
|
redirect_to polymorphic_path(membershipable),
|
|
|
|
notice: 'Your request for access has been queued for review.'
|
|
|
|
end
|
|
|
|
|
|
|
|
def approve_access_request
|
2017-10-11 10:47:08 -04:00
|
|
|
access_requester = membershipable.requesters.find(params[:id])
|
|
|
|
Members::ApproveAccessRequestService
|
2018-02-16 09:10:22 -05:00
|
|
|
.new(current_user, params)
|
2017-10-11 10:47:08 -04:00
|
|
|
.execute(access_requester)
|
2016-06-02 12:05:06 -04:00
|
|
|
|
2017-04-17 10:44:30 -04:00
|
|
|
redirect_to members_page_url
|
2016-06-02 12:05:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def leave
|
2017-10-11 10:47:08 -04:00
|
|
|
member = membershipable.members_and_requesters.find_by!(user_id: current_user.id)
|
2018-02-16 09:10:22 -05:00
|
|
|
Members::DestroyService.new(current_user).execute(member)
|
2016-06-17 08:06:55 -04:00
|
|
|
|
2016-06-17 12:59:33 -04:00
|
|
|
notice =
|
2016-09-09 12:51:31 -04:00
|
|
|
if member.request?
|
2016-06-17 12:59:33 -04:00
|
|
|
"Your access request to the #{source_type} has been withdrawn."
|
2016-06-02 12:05:06 -04:00
|
|
|
else
|
2016-07-28 13:31:17 -04:00
|
|
|
"You left the \"#{membershipable.human_name}\" #{source_type}."
|
2016-06-02 12:05:06 -04:00
|
|
|
end
|
2017-04-17 10:44:30 -04:00
|
|
|
|
2017-06-06 06:35:22 -04:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
redirect_path = member.request? ? member.source : [:dashboard, membershipable.class.to_s.tableize]
|
|
|
|
redirect_to redirect_path, notice: notice
|
|
|
|
end
|
2016-06-17 12:59:33 -04:00
|
|
|
|
2017-06-06 10:01:42 -04:00
|
|
|
format.json { render json: { notice: notice } }
|
2017-06-06 06:35:22 -04:00
|
|
|
end
|
2016-06-02 12:05:06 -04:00
|
|
|
end
|
|
|
|
|
2018-02-16 09:10:22 -05:00
|
|
|
def resend_invite
|
|
|
|
member = membershipable.members.find(params[:id])
|
|
|
|
|
|
|
|
if member.invite?
|
|
|
|
member.resend_invite
|
|
|
|
|
|
|
|
redirect_to members_page_url, notice: 'The invitation was successfully resent.'
|
|
|
|
else
|
|
|
|
redirect_to members_page_url, alert: 'The invitation has already been accepted.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-02 12:05:06 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def membershipable
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2017-04-17 10:44:30 -04:00
|
|
|
|
2018-02-16 09:10:22 -05:00
|
|
|
def root_params_key
|
|
|
|
case membershipable
|
|
|
|
when Namespace
|
|
|
|
:group_member
|
|
|
|
when Project
|
|
|
|
:project_member
|
|
|
|
else
|
|
|
|
raise "Unknown membershipable type: #{membershipable}!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-17 10:44:30 -04:00
|
|
|
def members_page_url
|
2018-02-16 09:10:22 -05:00
|
|
|
case membershipable
|
|
|
|
when Namespace
|
|
|
|
polymorphic_url([membershipable, :members])
|
|
|
|
when Project
|
2017-07-07 10:40:41 -04:00
|
|
|
project_project_members_path(membershipable)
|
2017-04-17 10:44:30 -04:00
|
|
|
else
|
2018-02-16 09:10:22 -05:00
|
|
|
raise "Unknown membershipable type: #{membershipable}!"
|
2017-04-17 10:44:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def source_type
|
|
|
|
@source_type ||= membershipable.class.to_s.humanize(capitalize: false)
|
|
|
|
end
|
2016-06-02 12:05:06 -04:00
|
|
|
end
|