2018-09-14 01:42:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-02 12:05:06 -04:00
|
|
|
module MembershipActions
|
2018-07-16 12:18:52 -04:00
|
|
|
include MembersPresentation
|
2016-06-02 12:05:06 -04:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
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])
|
2021-01-28 16:09:04 -05:00
|
|
|
result = Members::UpdateService
|
2018-02-16 09:10:22 -05:00
|
|
|
.new(current_user, update_params)
|
2018-02-16 06:00:25 -05:00
|
|
|
.execute(member)
|
|
|
|
|
2021-01-28 16:09:04 -05:00
|
|
|
member = result[:member]
|
|
|
|
|
|
|
|
member_data = if member.expires?
|
|
|
|
{
|
|
|
|
expires_soon: member.expires_soon?,
|
|
|
|
expires_at_formatted: member.expires_at.to_time.in_time_zone.to_s(:medium)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
render json: member_data
|
2020-10-06 14:08:49 -04:00
|
|
|
else
|
2021-01-28 16:09:04 -05:00
|
|
|
render json: { message: result[:message] }, status: :unprocessable_entity
|
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])
|
2021-03-23 08:09:33 -04:00
|
|
|
skip_subresources = !ActiveRecord::Type::Boolean.new.cast(params.delete(:remove_sub_memberships))
|
2020-06-23 11:08:41 -04:00
|
|
|
# !! is used in case unassign_issuables contains empty string which would result in nil
|
|
|
|
unassign_issuables = !!ActiveRecord::Type::Boolean.new.cast(params.delete(:unassign_issuables))
|
|
|
|
|
2021-03-23 08:09:33 -04:00
|
|
|
Members::DestroyService.new(current_user).execute(member, skip_subresources: skip_subresources, unassign_issuables: unassign_issuables)
|
2017-04-17 10:44:30 -04:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
2019-03-27 12:52:52 -04:00
|
|
|
message =
|
|
|
|
begin
|
|
|
|
case membershipable
|
|
|
|
when Namespace
|
2021-03-23 08:09:33 -04:00
|
|
|
if skip_subresources
|
|
|
|
_("User was successfully removed from group.")
|
|
|
|
else
|
|
|
|
_("User was successfully removed from group and any subgroups and projects.")
|
|
|
|
end
|
2019-03-27 12:52:52 -04:00
|
|
|
else
|
|
|
|
_("User was successfully removed from project.")
|
|
|
|
end
|
|
|
|
end
|
2018-11-16 10:09:32 -05:00
|
|
|
|
2017-04-17 10:44:30 -04:00
|
|
|
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
|
2020-05-27 17:08:05 -04:00
|
|
|
access_requester = membershipable.request_access(current_user)
|
2016-06-02 12:05:06 -04:00
|
|
|
|
2020-05-27 17:08:05 -04:00
|
|
|
if access_requester.persisted?
|
|
|
|
redirect_to polymorphic_path(membershipable),
|
|
|
|
notice: _('Your request for access has been queued for review.')
|
|
|
|
else
|
|
|
|
redirect_to polymorphic_path(membershipable),
|
2022-02-28 04:15:16 -05:00
|
|
|
alert: _("Your request for access could not be processed: %{error_message}") %
|
|
|
|
{ error_message: access_requester.errors.full_messages.to_sentence }
|
2020-05-27 17:08:05 -04:00
|
|
|
end
|
2016-06-02 12:05:06 -04:00
|
|
|
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
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-06-02 12:05:06 -04:00
|
|
|
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?
|
2019-03-27 12:52:52 -04:00
|
|
|
_("Your access request to the %{source_type} has been withdrawn.") % { source_type: source_type }
|
2016-06-02 12:05:06 -04:00
|
|
|
else
|
2019-03-27 12:52:52 -04:00
|
|
|
_("You left the \"%{membershipable_human_name}\" %{source_type}.") % { membershipable_human_name: membershipable.human_name, source_type: 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
|
2021-05-24 14:10:28 -04:00
|
|
|
redirect_path = member.request? ? member.source : [:dashboard, membershipable.class.to_s.tableize.to_sym]
|
2017-06-06 06:35:22 -04:00
|
|
|
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-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-06-02 12:05:06 -04:00
|
|
|
|
2018-02-16 09:10:22 -05:00
|
|
|
def resend_invite
|
2020-10-16 05:09:06 -04:00
|
|
|
member = membershipable_members.find(params[:id])
|
2018-02-16 09:10:22 -05:00
|
|
|
|
|
|
|
if member.invite?
|
|
|
|
member.resend_invite
|
|
|
|
|
2019-03-27 12:52:52 -04:00
|
|
|
redirect_to members_page_url, notice: _('The invitation was successfully resent.')
|
2018-02-16 09:10:22 -05:00
|
|
|
else
|
2019-03-27 12:52:52 -04:00
|
|
|
redirect_to members_page_url, alert: _('The invitation has already been accepted.')
|
2018-02-16 09:10:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-02 12:05:06 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def membershipable
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2017-04-17 10:44:30 -04:00
|
|
|
|
2020-10-16 05:09:06 -04:00
|
|
|
def membershipable_members
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2018-02-16 09:10:22 -05:00
|
|
|
def root_params_key
|
2021-06-15 08:10:11 -04:00
|
|
|
raise NotImplementedError
|
2018-02-16 09:10:22 -05:00
|
|
|
end
|
|
|
|
|
2017-04-17 10:44:30 -04:00
|
|
|
def members_page_url
|
2021-06-15 08:10:11 -04:00
|
|
|
raise NotImplementedError
|
2017-04-17 10:44:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def source_type
|
2021-06-15 08:10:11 -04:00
|
|
|
raise NotImplementedError
|
2017-04-17 10:44:30 -04:00
|
|
|
end
|
2019-12-13 10:08:02 -05:00
|
|
|
|
2021-06-14 14:10:28 -04:00
|
|
|
def plain_source_type
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2022-05-30 05:09:35 -04:00
|
|
|
def requested_relations(inherited_permissions = :with_inherited_permissions)
|
|
|
|
case params[inherited_permissions].presence
|
2019-12-13 10:08:02 -05:00
|
|
|
when 'exclude'
|
|
|
|
[:direct]
|
|
|
|
when 'only'
|
|
|
|
[:inherited]
|
|
|
|
else
|
2022-08-25 23:12:38 -04:00
|
|
|
if Feature.enabled?(:webui_members_inherited_users, current_user)
|
|
|
|
[:inherited, :direct, :shared_from_groups]
|
|
|
|
else
|
|
|
|
[:inherited, :direct]
|
|
|
|
end
|
2019-12-13 10:08:02 -05:00
|
|
|
end
|
|
|
|
end
|
2016-06-02 12:05:06 -04:00
|
|
|
end
|
2021-05-03 20:10:16 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
MembershipActions.prepend_mod_with('MembershipActions')
|