Use contracted member services and show only manageable projects in user administration (#9076)
* Reuse contracted services in memberships controller * Show only manageable projects * Show only manageable projects in select box * Reject empty values in role_ids * Add spec for when user has no manage_members permission * Also handle delete through service * Extend spec for when user has no manage_members permission * Extend Members SetAttributesSpec * Make manageable_Members an instance method and add spec * hide buttons if user is not allowed to use them * Rename manageable_projects to assignable_projects * Build a membership for the contract * Fix url for global_roles to work with update and create methods Co-authored-by: ulferts <jens.ulferts@googlemail.com>pull/9080/head
parent
4a54e9b41a
commit
e9f1781d2b
@ -1,43 +1,52 @@ |
|||||||
module IndividualPrincipals |
module IndividualPrincipals |
||||||
module MembershipControllerMethods |
module MembershipControllerMethods |
||||||
def update |
extend ActiveSupport::Concern |
||||||
update_or_create(request.patch?, :notice_successful_update) |
|
||||||
|
included do |
||||||
|
before_action :find_membership, only: %i[update destroy] |
||||||
end |
end |
||||||
|
|
||||||
def create |
def create |
||||||
update_or_create(request.post?, :notice_successful_create) |
membership_params = permitted_params.membership.merge(principal: @individual_principal) |
||||||
end |
call = ::Members::CreateService |
||||||
|
.new(user: current_user) |
||||||
|
.call(membership_params) |
||||||
|
|
||||||
def destroy |
respond_with_service_call call, message: :notice_successful_create |
||||||
@membership = @individual_principal.memberships.find(params[:id]) |
end |
||||||
tab = redirected_to_tab(@membership) |
|
||||||
|
|
||||||
if @membership.deletable? && request.delete? |
def update |
||||||
@membership.destroy |
call = ::Members::UpdateService |
||||||
@membership = nil |
.new(model: @membership, user: current_user) |
||||||
|
.call(permitted_params.membership) |
||||||
|
|
||||||
flash[:notice] = I18n.t(:notice_successful_delete) |
respond_with_service_call call, message: :notice_successful_update |
||||||
end |
end |
||||||
|
|
||||||
redirect_to edit_polymorphic_path(@individual_principal, tab: tab) |
def destroy |
||||||
|
call = ::Members::DeleteService |
||||||
|
.new(model: @membership, user: current_user) |
||||||
|
.call |
||||||
|
|
||||||
|
respond_with_service_call call, message: :notice_successful_delete |
||||||
end |
end |
||||||
|
|
||||||
private |
private |
||||||
|
|
||||||
def update_or_create(save_record, message) |
def find_membership |
||||||
@membership = params[:id].present? ? Member.find(params[:id]) : Member.new(principal: @individual_principal, project: nil) |
@membership = Member.visible(current_user).find(params[:id]) |
||||||
|
rescue ActiveRecord::RecordNotFound |
||||||
result = ::Members::EditMembershipService |
render_404 |
||||||
.new(@membership, save: save_record, current_user: current_user) |
end |
||||||
.call(attributes: permitted_params.membership) |
|
||||||
|
|
||||||
if result.success? |
def respond_with_service_call(call, message:) |
||||||
|
if call.success? |
||||||
flash[:notice] = I18n.t(message) |
flash[:notice] = I18n.t(message) |
||||||
else |
else |
||||||
flash[:error] = result.errors.full_messages.join("\n") |
flash[:error] = call.errors.full_messages.join("\n") |
||||||
end |
end |
||||||
|
|
||||||
redirect_to edit_polymorphic_path(@individual_principal, tab: redirected_to_tab(@membership)) |
redirect_to edit_polymorphic_path(@individual_principal, tab: redirected_to_tab(call.result)) |
||||||
end |
end |
||||||
end |
end |
||||||
end |
end |
||||||
|
Loading…
Reference in new issue