debian-mirror-gitlab/app/controllers/concerns/membership_actions.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

168 lines
4.7 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
module MembershipActions
2018-11-18 11:00:15 +05:30
include MembersPresentation
extend ActiveSupport::Concern
2018-03-27 19:54:05 +05:30
def update
update_params = params.require(root_params_key).permit(:access_level, :expires_at)
2023-04-23 21:23:45 +05:30
member = members_and_requesters.find(params[:id])
2021-03-11 19:13:27 +05:30
result = Members::UpdateService
2018-03-27 19:54:05 +05:30
.new(current_user, update_params)
.execute(member)
2023-03-17 16:20:25 +05:30
member = result[:members].first
2021-03-11 19:13:27 +05:30
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
2021-01-03 14:25:43 +05:30
else
2021-03-11 19:13:27 +05:30
render json: { message: result[:message] }, status: :unprocessable_entity
2017-08-17 22:00:37 +05:30
end
end
def destroy
2023-04-23 21:23:45 +05:30
member = members_and_requesters.find(params[:id])
2021-04-29 21:17:54 +05:30
skip_subresources = !ActiveRecord::Type::Boolean.new.cast(params.delete(:remove_sub_memberships))
2020-07-28 23:09:34 +05:30
# !! 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-04-29 21:17:54 +05:30
Members::DestroyService.new(current_user).execute(member, skip_subresources: skip_subresources, unassign_issuables: unassign_issuables)
2017-08-17 22:00:37 +05:30
respond_to do |format|
format.html do
2019-07-07 11:18:12 +05:30
message =
2023-03-04 22:38:38 +05:30
case membershipable
when Namespace
if skip_subresources
_("User was successfully removed from group.")
2019-07-07 11:18:12 +05:30
else
2023-03-04 22:38:38 +05:30
_("User was successfully removed from group and any subgroups and projects.")
2019-07-07 11:18:12 +05:30
end
2023-03-04 22:38:38 +05:30
else
_("User was successfully removed from project.")
2019-07-07 11:18:12 +05:30
end
2019-01-03 12:48:30 +05:30
2023-07-09 08:55:56 +05:30
redirect_to members_page_url, notice: message, status: :see_other
2017-08-17 22:00:37 +05:30
end
format.js { head :ok }
end
end
def request_access
2020-05-30 21:06:31 +05:30
access_requester = membershipable.request_access(current_user)
2020-05-30 21:06:31 +05:30
if access_requester.persisted?
redirect_to polymorphic_path(membershipable),
2023-05-27 22:25:52 +05:30
notice: _('Your request for access has been queued for review.')
2020-05-30 21:06:31 +05:30
else
redirect_to polymorphic_path(membershipable),
2023-05-27 22:25:52 +05:30
alert: format(_("Your request for access could not be processed: %{error_message}"), error_message: access_requester.errors.full_messages.to_sentence)
2020-05-30 21:06:31 +05:30
end
end
def approve_access_request
2023-04-23 21:23:45 +05:30
access_requester = requesters.find(params[:id])
2018-03-27 19:54:05 +05:30
Members::ApproveAccessRequestService
.new(current_user, params)
.execute(access_requester)
2017-08-17 22:00:37 +05:30
redirect_to members_page_url
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
def leave
2023-04-23 21:23:45 +05:30
member = members_and_requesters.find_by!(user_id: current_user.id)
2018-03-27 19:54:05 +05:30
Members::DestroyService.new(current_user).execute(member)
2016-08-24 12:49:21 +05:30
notice =
2016-11-03 12:29:30 +05:30
if member.request?
2023-03-17 16:20:25 +05:30
format(_("Your access request to the %{source_type} has been withdrawn."), source_type: source_type)
else
2023-03-17 16:20:25 +05:30
format(_("You left the \"%{membershipable_human_name}\" %{source_type}."), membershipable_human_name: membershipable.human_name, source_type: source_type)
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
respond_to do |format|
format.html do
2021-07-02 01:05:55 +05:30
redirect_path = member.request? ? member.source : [:dashboard, membershipable.class.to_s.tableize.to_sym]
2017-09-10 17:25:29 +05:30
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
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-03-27 19:54:05 +05:30
def resend_invite
2021-01-03 14:25:43 +05:30
member = membershipable_members.find(params[:id])
2018-03-27 19:54:05 +05:30
if member.invite?
member.resend_invite
2019-07-07 11:18:12 +05:30
redirect_to members_page_url, notice: _('The invitation was successfully resent.')
2018-03-27 19:54:05 +05:30
else
2019-07-07 11:18:12 +05:30
redirect_to members_page_url, alert: _('The invitation has already been accepted.')
2018-03-27 19:54:05 +05:30
end
end
protected
def membershipable
raise NotImplementedError
end
2017-08-17 22:00:37 +05:30
2021-01-03 14:25:43 +05:30
def membershipable_members
raise NotImplementedError
end
2018-03-27 19:54:05 +05:30
def root_params_key
2021-09-04 01:27:46 +05:30
raise NotImplementedError
2018-03-27 19:54:05 +05:30
end
2017-08-17 22:00:37 +05:30
def members_page_url
2021-09-04 01:27:46 +05:30
raise NotImplementedError
2017-08-17 22:00:37 +05:30
end
def source_type
2021-09-04 01:27:46 +05:30
raise NotImplementedError
end
def plain_source_type
raise NotImplementedError
2017-08-17 22:00:37 +05:30
end
2020-01-01 13:55:28 +05:30
2023-04-23 21:23:45 +05:30
def members_and_requesters
membershipable.members_and_requesters
end
def requesters
membershipable.requesters
end
2022-07-23 23:45:48 +05:30
def requested_relations(inherited_permissions = :with_inherited_permissions)
case params[inherited_permissions].presence
2020-01-01 13:55:28 +05:30
when 'exclude'
[:direct]
when 'only'
[:inherited]
else
2022-10-11 01:57:18 +05:30
if Feature.enabled?(:webui_members_inherited_users, current_user)
[:inherited, :direct, :shared_from_groups]
else
[:inherited, :direct]
end
2020-01-01 13:55:28 +05:30
end
end
end
2021-06-08 01:23:25 +05:30
MembershipActions.prepend_mod_with('MembershipActions')