Commit e50556fc authored by Douwe Maan's avatar Douwe Maan

Let invites be declined.

parent 8949af0c
class InvitesController < ApplicationController class InvitesController < ApplicationController
before_filter :member before_filter :member
skip_before_filter :authenticate_user!, only: :decline
respond_to :html respond_to :html
...@@ -31,6 +32,32 @@ class InvitesController < ApplicationController ...@@ -31,6 +32,32 @@ class InvitesController < ApplicationController
end end
end end
def decline
if member.decline_invite!
case member.source
when Project
project = member.source
source = "project #{project.name_with_namespace}"
when Group
group = member.source
source = "group #{group.name}"
else
source = "who knows what"
end
path =
if current_user
dashboard_path
else
new_user_session_path
end
redirect_to path, notice: "You have declined the invite to join #{source}."
else
redirect_to :back, alert: "The invite could not be declined."
end
end
private private
def member def member
......
...@@ -27,5 +27,18 @@ module Emails ...@@ -27,5 +27,18 @@ module Emails
mail(to: @group_member.created_by.notification_email, mail(to: @group_member.created_by.notification_email,
subject: subject("Invite accepted")) subject: subject("Invite accepted"))
end end
def group_invite_declined_email(group_id, invite_email, access_level, created_by_id)
return if created_by_id.nil?
@group = Group.find(group_id)
@created_by = User.find(created_by_id)
@access_level = access_level
@invite_email = invite_email
@target_url = group_url(@group)
mail(to: @created_by.notification_email,
subject: subject("Invite declined"))
end
end end
end end
...@@ -28,6 +28,19 @@ module Emails ...@@ -28,6 +28,19 @@ module Emails
subject: subject("Invite accepted")) subject: subject("Invite accepted"))
end end
def project_invite_declined_email(project_id, invite_email, access_level, created_by_id)
return if created_by_id.nil?
@project = Project.find(project_id)
@created_by = User.find(created_by_id)
@access_level = access_level
@invite_email = invite_email
@target_url = namespace_project_url(@project.namespace, @project)
mail(to: @created_by.notification_email,
subject: subject("Invite declined"))
end
def project_was_moved_email(project_id, user_id) def project_was_moved_email(project_id, user_id)
@current_user = @user = User.find user_id @current_user = @user = User.find user_id
@project = Project.find project_id @project = Project.find project_id
......
...@@ -76,6 +76,16 @@ class Member < ActiveRecord::Base ...@@ -76,6 +76,16 @@ class Member < ActiveRecord::Base
saved saved
end end
def decline_invite!
return false unless invite?
destroyed = self.destroy
after_decline_invite if destroyed
destroyed
end
def generate_invite_token def generate_invite_token
raw, enc = Devise.token_generator.generate(self.class, :invite_token) raw, enc = Devise.token_generator.generate(self.class, :invite_token)
@raw_invite_token = raw @raw_invite_token = raw
...@@ -116,6 +126,10 @@ class Member < ActiveRecord::Base ...@@ -116,6 +126,10 @@ class Member < ActiveRecord::Base
post_create_hook post_create_hook
end end
def after_decline_invite
# override in subclass
end
def system_hook_service def system_hook_service
SystemHooksService.new SystemHooksService.new
end end
......
...@@ -66,4 +66,10 @@ class GroupMember < Member ...@@ -66,4 +66,10 @@ class GroupMember < Member
super super
end end
def after_decline_invite
notification_service.decline_group_invite(self)
super
end
end end
...@@ -167,6 +167,12 @@ class ProjectMember < Member ...@@ -167,6 +167,12 @@ class ProjectMember < Member
super super
end end
def after_decline_invite
notification_service.decline_project_invite(self)
super
end
def event_service def event_service
EventCreateService.new EventCreateService.new
end end
......
...@@ -173,6 +173,10 @@ class NotificationService ...@@ -173,6 +173,10 @@ class NotificationService
mailer.project_invite_accepted_email(project_member.id) mailer.project_invite_accepted_email(project_member.id)
end end
def decline_project_invite(project_member)
mailer.project_invite_declined_email(project_member.project.id, project_member.invite_email, project_member.access_level, project_member.created_by_id)
end
def new_project_member(project_member) def new_project_member(project_member)
mailer.project_access_granted_email(project_member.id) mailer.project_access_granted_email(project_member.id)
end end
...@@ -189,6 +193,10 @@ class NotificationService ...@@ -189,6 +193,10 @@ class NotificationService
mailer.group_invite_accepted_email(group_member.id) mailer.group_invite_accepted_email(group_member.id)
end end
def decline_group_invite(group_member)
mailer.group_invite_declined_email(group_member.group.id, group_member.invite_email, group_member.access_level, group_member.created_by_id)
end
def new_group_member(group_member) def new_group_member(group_member)
mailer.group_access_granted_email(group_member.id) mailer.group_access_granted_email(group_member.id)
end end
......
...@@ -26,3 +26,4 @@ ...@@ -26,3 +26,4 @@
- else - else
.actions .actions
= link_to "Accept invitation", accept_invite_url(@token), method: :post, class: "btn btn-success" = link_to "Accept invitation", accept_invite_url(@token), method: :post, class: "btn btn-success"
= link_to "Decline", decline_invite_url(@token), method: :post, class: "btn btn-danger prepend-left-10"
%p
#{@invite_email}
has declined your invitation to join group
= link_to @group.name, group_url(@group)
as #{Gitlab::Access.options_with_owner.key(@access_level)}.
<%= @invite_email %> has declined your invitation to join group <%= @group.name %> as <%= Gitlab::Access.options_with_owner.key(@access_level) %>.
<%= group_url(@group) %>
...@@ -9,4 +9,6 @@ ...@@ -9,4 +9,6 @@
%p %p
= link_to 'Accept invitation', invite_url(@token) = link_to 'Accept invitation', invite_url(@token)
or
= link_to 'decline', decline_invite_url(@token)
You have been invited <%= "by #{@group_member.created_by.name} " if @group_member.created_by %>to join group <%= @group.name %> as <%= @group_member.human_access %>. You have been invited <%= "by #{@group_member.created_by.name} " if @group_member.created_by %>to join group <%= @group.name %> as <%= @group_member.human_access %>.
Accept invitation: <%= invite_url(@token) %> Accept invitation: <%= invite_url(@token) %>
Decline invitation: <%= decline_invite_url(@token) %>
<%= @project_member.invite_email %>, now known as <%= @project_member.user.name %>, has accepted your invitation to join project <%= @group.name_with_namespace %> as <%= @project_member.human_access %>. <%= @project_member.invite_email %>, now known as <%= @project_member.user.name %>, has accepted your invitation to join project <%= @project.name_with_namespace %> as <%= @project_member.human_access %>.
<%= namespace_project_url(@project.namespace, @project) %> <%= namespace_project_url(@project.namespace, @project) %>
%p
#{@invite_email}
has declined your invitation to join project
= link_to @project.name_with_namespace, namespace_project_url(@project.namespace, @project)
as #{Gitlab::Access.options_with_owner.key(@access_level)}.
<%= @invite_email %> has declined your invitation to join project <%= @project.name_with_namespace %> as <%= Gitlab::Access.options_with_owner.key(@access_level) %>.
<%= namespace_project_url(@project.namespace, @project) %>
...@@ -9,3 +9,5 @@ ...@@ -9,3 +9,5 @@
%p %p
= link_to 'Accept invitation', invite_url(@token) = link_to 'Accept invitation', invite_url(@token)
or
= link_to 'decline', decline_invite_url(@token)
You have been invited <%= "by #{@project_member.created_by.name} " if @project_member.created_by %>to join project <%= @project.name_with_namespace %> as <%= @project_member.human_access %>. You have been invited <%= "by #{@project_member.created_by.name} " if @project_member.created_by %>to join project <%= @project.name_with_namespace %> as <%= @project_member.human_access %>.
Accept invitation: <%= invite_url(@token) %> Accept invitation: <%= invite_url(@token) %>
Decline invitation: <%= decline_invite_url(@token) %>
...@@ -60,6 +60,7 @@ Gitlab::Application.routes.draw do ...@@ -60,6 +60,7 @@ Gitlab::Application.routes.draw do
resources :invites, only: [:show], constraints: { id: /[A-Za-z0-9_-]+/ } do resources :invites, only: [:show], constraints: { id: /[A-Za-z0-9_-]+/ } do
member do member do
post :accept post :accept
match :decline, via: [:get, :post]
end end
end end
......
...@@ -88,6 +88,23 @@ describe Member do ...@@ -88,6 +88,23 @@ describe Member do
end end
end end
describe "#decline_invite!" do
let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) }
it "destroys the member" do
member.decline_invite!
expect(member).to be_destroyed
end
it "calls #after_decline_invite" do
expect(member).to receive(:after_decline_invite)
member.decline_invite!
end
end
describe "#generate_invite_token" do describe "#generate_invite_token" do
let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) } let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment