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

184 lines
5.1 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
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
2019-07-07 11:18:12 +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])
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)
2021-03-11 19:13:27 +05:30
member = result[:member]
member_data = if member.expires?
{
expires_in: helpers.distance_of_time_in_words_to_now(member.expires_at),
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
2018-03-27 19:54:05 +05:30
member = membershipable.members_and_requesters.find(params[:id])
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))
Members::DestroyService.new(current_user).execute(member, 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 =
begin
case membershipable
when Namespace
_("User was successfully removed from group and any subresources.")
else
_("User was successfully removed from project.")
end
end
2019-01-03 12:48:30 +05:30
2017-08-17 22:00:37 +05:30
redirect_to members_page_url, notice: message
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),
notice: _('Your request for access has been queued for review.')
else
redirect_to polymorphic_path(membershipable),
alert: _("Your request for access could not be processed: %{error_meesage}") %
{ error_meesage: access_requester.errors.full_messages.to_sentence }
end
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)
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
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-08-24 12:49:21 +05:30
notice =
2016-11-03 12:29:30 +05:30
if member.request?
2019-07-07 11:18:12 +05:30
_("Your access request to the %{source_type} has been withdrawn.") % { source_type: source_type }
else
2019-07-07 11:18:12 +05:30
_("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
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
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
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
2019-07-07 11:18:12 +05:30
@source_type ||=
begin
case membershipable
when Namespace
_("group")
when Project
_("project")
else
raise "Unknown membershipable type: #{membershipable}!"
end
end
2017-08-17 22:00:37 +05:30
end
2020-01-01 13:55:28 +05:30
def requested_relations
case params[:with_inherited_permissions].presence
when 'exclude'
[:direct]
when 'only'
[:inherited]
else
[:inherited, :direct]
end
end
end