BigW Consortium Gitlab

members.rb 2.79 KB
Newer Older
1 2 3
module Emails
  module Members
    extend ActiveSupport::Concern
4
    include MembersHelper
5 6

    included do
7
      helper_method :member_source, :member
8 9
    end

10 11
    def member_access_requested_email(member_source_type, member_id)
      @member_source_type = member_source_type
12 13
      @member_id = member_id

14
      admins = member_source.members.owners_and_masters.pluck(:notification_email)
15 16 17
      # A project in a group can have no explicit owners/masters, in that case
      # we fallbacks to the group's owners/masters.
      if admins.empty? && member_source.respond_to?(:group) && member_source.group
18
        admins = member_source.group.members.owners_and_masters.pluck(:notification_email)
19
      end
20 21

      mail(to: admins,
22
           subject: subject("Request to join the #{member_source.human_name} #{member_source.model_name.singular}"))
23 24
    end

25 26
    def member_access_granted_email(member_source_type, member_id)
      @member_source_type = member_source_type
27 28 29
      @member_id = member_id

      mail(to: member.user.notification_email,
30
           subject: subject("Access to the #{member_source.human_name} #{member_source.model_name.singular} was granted"))
31 32
    end

33 34 35 36
    def member_access_denied_email(member_source_type, source_id, user_id)
      @member_source_type = member_source_type
      @member_source = member_source_class.find(source_id)
      requester = User.find(user_id)
37

38 39
      mail(to: requester.notification_email,
           subject: subject("Access to the #{member_source.human_name} #{member_source.model_name.singular} was denied"))
40 41
    end

42 43
    def member_invited_email(member_source_type, member_id, token)
      @member_source_type = member_source_type
44 45 46 47
      @member_id = member_id
      @token = token

      mail(to: member.invite_email,
48
           subject: subject("Invitation to join the #{member_source.human_name} #{member_source.model_name.singular}"))
49 50
    end

51 52
    def member_invite_accepted_email(member_source_type, member_id)
      @member_source_type = member_source_type
53
      @member_id = member_id
54
      return unless member.created_by
55

56
      mail(to: member.created_by.notification_email,
57 58 59
           subject: subject('Invitation accepted'))
    end

60 61
    def member_invite_declined_email(member_source_type, source_id, invite_email, created_by_id)
      return unless created_by_id
62

63 64
      @member_source_type = member_source_type
      @member_source = member_source_class.find(source_id)
65
      @invite_email = invite_email
66
      inviter = User.find(created_by_id)
67

68
      mail(to: inviter.notification_email,
69 70 71 72
           subject: subject('Invitation declined'))
    end

    def member
73
      @member ||= Member.find(@member_id)
74 75
    end

76 77
    def member_source
      @member_source ||= member.source
78 79 80 81
    end

    private

82 83
    def member_source_class
      @member_source_type.classify.constantize
84 85 86
    end
  end
end