2016-06-16 23:09:34 +05:30
|
|
|
module MembershipActions
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def create
|
2017-09-10 17:25:29 +05:30
|
|
|
create_params = params.permit(:user_ids, :access_level, :expires_at)
|
2018-03-27 19:54:05 +05:30
|
|
|
result = Members::CreateService.new(current_user, create_params).execute(membershipable)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
if result[:status] == :success
|
2018-03-27 19:54:05 +05:30
|
|
|
redirect_to members_page_url, notice: 'Users were successfully added.'
|
2017-08-17 22:00:37 +05:30
|
|
|
else
|
2018-03-27 19:54:05 +05:30
|
|
|
redirect_to members_page_url, alert: result[:message]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
update_params = params.require(root_params_key).permit(:access_level, :expires_at)
|
|
|
|
member = membershipable.members_and_requesters.find(params[:id])
|
|
|
|
member = Members::UpdateService
|
|
|
|
.new(current_user, update_params)
|
|
|
|
.execute(member)
|
|
|
|
.present(current_user: current_user)
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.js { render 'shared/members/update', locals: { member: member } }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2018-03-27 19:54:05 +05:30
|
|
|
member = membershipable.members_and_requesters.find(params[:id])
|
|
|
|
Members::DestroyService.new(current_user).execute(member)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
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-16 23:09:34 +05:30
|
|
|
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
|
2018-03-27 19:54:05 +05:30
|
|
|
access_requester = membershipable.requesters.find(params[:id])
|
|
|
|
Members::ApproveAccessRequestService
|
|
|
|
.new(current_user, params)
|
|
|
|
.execute(access_requester)
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
redirect_to members_page_url
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def leave
|
2018-03-27 19:54:05 +05:30
|
|
|
member = membershipable.members_and_requesters.find_by!(user_id: current_user.id)
|
|
|
|
Members::DestroyService.new(current_user).execute(member)
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
notice =
|
2016-11-03 12:29:30 +05:30
|
|
|
if member.request?
|
2016-08-24 12:49:21 +05:30
|
|
|
"Your access request to the #{source_type} has been withdrawn."
|
2016-06-16 23:09:34 +05:30
|
|
|
else
|
2016-11-03 12:29:30 +05:30
|
|
|
"You left the \"#{membershipable.human_name}\" #{source_type}."
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
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-08-24 12:49:21 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
format.json { render json: { notice: notice } }
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
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-16 23:09:34 +05:30
|
|
|
protected
|
|
|
|
|
|
|
|
def membershipable
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def root_params_key
|
|
|
|
case membershipable
|
|
|
|
when Namespace
|
|
|
|
:group_member
|
|
|
|
when Project
|
|
|
|
:project_member
|
|
|
|
else
|
|
|
|
raise "Unknown membershipable type: #{membershipable}!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def members_page_url
|
2018-03-27 19:54:05 +05:30
|
|
|
case membershipable
|
|
|
|
when Namespace
|
|
|
|
polymorphic_url([membershipable, :members])
|
|
|
|
when Project
|
2017-09-10 17:25:29 +05:30
|
|
|
project_project_members_path(membershipable)
|
2017-08-17 22:00:37 +05:30
|
|
|
else
|
2018-03-27 19:54:05 +05:30
|
|
|
raise "Unknown membershipable type: #{membershipable}!"
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def source_type
|
|
|
|
@source_type ||= membershipable.class.to_s.humanize(capitalize: false)
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|