diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/concerns/membership_actions.rb | 6 | ||||
-rw-r--r-- | app/models/member.rb | 7 | ||||
-rw-r--r-- | app/models/members/group_member.rb | 12 | ||||
-rw-r--r-- | app/models/members/project_member.rb | 12 | ||||
-rw-r--r-- | app/services/members/destroy_service.rb | 33 | ||||
-rw-r--r-- | app/services/notification_service.rb | 21 |
6 files changed, 45 insertions, 46 deletions
diff --git a/app/controllers/concerns/membership_actions.rb b/app/controllers/concerns/membership_actions.rb index a24273fad0b..b1343576b3c 100644 --- a/app/controllers/concerns/membership_actions.rb +++ b/app/controllers/concerns/membership_actions.rb @@ -23,22 +23,24 @@ module MembershipActions @member = membershipable.members.find_by(user_id: current_user) return render_403 unless @member + @member = Members::DestroyService.new(@member, current_user).execute + source_type = @member.real_source_type.humanize(capitalize: false) - if can?(current_user, action_member_permission(:destroy, @member), @member) + if @member.destroyed? notice = if @member.request? "Your access request to the #{source_type} has been withdrawn." else "You left the \"#{@member.source.human_name}\" #{source_type}." end - @member.destroy redirect_to [:dashboard, @member.real_source_type.tableize], notice: notice else if cannot_leave? alert = "You can not leave the \"#{@member.source.human_name}\" #{source_type}." alert << " Transfer or delete the #{source_type}." + redirect_to polymorphic_url(membershipable), alert: alert else render_403 diff --git a/app/models/member.rb b/app/models/member.rb index 4ee3f1bb5c2..c74a16367db 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -48,7 +48,6 @@ class Member < ActiveRecord::Base after_create :post_create_hook, unless: [:pending?, :importing?] after_update :post_update_hook, unless: [:pending?, :importing?] after_destroy :post_destroy_hook, unless: :pending? - after_destroy :post_decline_request, if: :request? delegate :name, :username, :email, to: :user, prefix: true @@ -188,7 +187,7 @@ class Member < ActiveRecord::Base end def send_request - # override in subclass + notification_service.new_access_request(self) end def post_create_hook @@ -215,10 +214,6 @@ class Member < ActiveRecord::Base post_create_hook end - def post_decline_request - # override in subclass - end - def system_hook_service SystemHooksService.new end diff --git a/app/models/members/group_member.rb b/app/models/members/group_member.rb index 363db877968..2f13d339c89 100644 --- a/app/models/members/group_member.rb +++ b/app/models/members/group_member.rb @@ -33,12 +33,6 @@ class GroupMember < Member super end - def send_request - notification_service.new_group_access_request(self) - - super - end - def post_create_hook notification_service.new_group_member(self) @@ -64,10 +58,4 @@ class GroupMember < Member super end - - def post_decline_request - notification_service.decline_group_access_request(self) - - super - end end diff --git a/app/models/members/project_member.rb b/app/models/members/project_member.rb index 250ee04fd1d..e9d3a82ba15 100644 --- a/app/models/members/project_member.rb +++ b/app/models/members/project_member.rb @@ -111,12 +111,6 @@ class ProjectMember < Member super end - def send_request - notification_service.new_project_access_request(self) - - super - end - def post_create_hook unless owner? event_service.join_project(self.project, self.user) @@ -152,12 +146,6 @@ class ProjectMember < Member super end - def post_decline_request - notification_service.decline_project_access_request(self) - - super - end - def event_service EventCreateService.new end diff --git a/app/services/members/destroy_service.rb b/app/services/members/destroy_service.rb new file mode 100644 index 00000000000..e32eb47b846 --- /dev/null +++ b/app/services/members/destroy_service.rb @@ -0,0 +1,33 @@ +module Members + class DestroyService < BaseService + attr_accessor :member, :current_user + + def initialize(member, user) + @member, @current_user = member, user + end + + def execute + if can?(current_user, "destroy_#{member.type.underscore}".to_sym, member) + member.destroy + + notification_service.decline_access_request(member) if member.request? + end + + member + end + + private + + def abilities + Ability.abilities + end + + def can?(object, action, subject) + abilities.allowed?(object, action, subject) + end + + def notification_service + NotificationService.new + end + end +end diff --git a/app/services/notification_service.rb b/app/services/notification_service.rb index 19832a19b2b..590350a11e5 100644 --- a/app/services/notification_service.rb +++ b/app/services/notification_service.rb @@ -181,15 +181,16 @@ class NotificationService end end - # Project access request - def new_project_access_request(project_member) - mailer.member_access_requested_email(project_member.real_source_type, project_member.id).deliver_later + # Members + def new_access_request(member) + mailer.member_access_requested_email(member.real_source_type, member.id).deliver_later end - def decline_project_access_request(project_member) - mailer.member_access_denied_email(project_member.real_source_type, project_member.project.id, project_member.user.id).deliver_later + def decline_access_request(member) + mailer.member_access_denied_email(member.real_source_type, member.source_id, member.user_id).deliver_later end + # Project invite def invite_project_member(project_member, token) mailer.member_invited_email(project_member.real_source_type, project_member.id, token).deliver_later end @@ -216,15 +217,7 @@ class NotificationService mailer.member_access_granted_email(project_member.real_source_type, project_member.id).deliver_later end - # Group access request - def new_group_access_request(group_member) - mailer.member_access_requested_email(group_member.real_source_type, group_member.id).deliver_later - end - - def decline_group_access_request(group_member) - mailer.member_access_denied_email(group_member.real_source_type, group_member.group.id, group_member.user.id).deliver_later - end - + # Group invite def invite_group_member(group_member, token) mailer.member_invited_email(group_member.real_source_type, group_member.id, token).deliver_later end |