File: service_desk_setting.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (121 lines) | stat: -rw-r--r-- 3,492 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# frozen_string_literal: true

class ServiceDeskSetting < ApplicationRecord
  include Gitlab::Utils::StrongMemoize

  CUSTOM_EMAIL_VERIFICATION_SUBADDRESS = '+verify'

  attribute :custom_email_enabled, default: false

  belongs_to :project

  validates :project_id, presence: true
  validate :valid_issue_template
  validate :valid_project_key
  validate :custom_email_enabled_state
  validates :outgoing_name, length: { maximum: 255 }, allow_blank: true
  validates :project_key,
    length: { maximum: 255 },
    allow_blank: true,
    format: { with: /\A[a-z0-9_]+\z/, message: ->(setting, data) { _("can contain only lowercase letters, digits, and '_'.") } }

  # Don't use Devise.email_regexp or URI::MailTo::EMAIL_REGEXP to be a bit more restrictive
  # on the format of an email. For example because we don't want to allow `+` and other
  # subaddress delimeters in the local part.
  validates :custom_email,
    length: { maximum: 255 },
    uniqueness: true,
    allow_nil: true,
    format: Gitlab::Email::ServiceDesk::CustomEmail::EMAIL_REGEXP_WITH_ANCHORS

  validates :custom_email_credential,
    presence: true,
    if: :needs_custom_email_credentials?
  validates :custom_email,
    presence: true,
    devise_email: true,
    if: :needs_custom_email_credentials?

  scope :with_project_key, ->(key) { where(project_key: key) }

  def custom_email_credential
    project&.service_desk_custom_email_credential
  end

  def custom_email_verification
    project&.service_desk_custom_email_verification
  end

  def custom_email_address_for_verification
    return unless custom_email.present?

    custom_email.sub("@", "#{CUSTOM_EMAIL_VERIFICATION_SUBADDRESS}@")
  end

  def issue_template_content
    strong_memoize(:issue_template_content) do
      next unless issue_template_key.present?

      TemplateFinder.new(
        :issues, project,
        name: issue_template_key,
        source_template_project: source_template_project
      ).execute.content
    rescue ::Gitlab::Template::Finders::RepoTemplateFinder::FileNotFoundError
    end
  end

  def issue_template_missing?
    issue_template_key.present? && !issue_template_content.present?
  end

  def valid_issue_template
    if issue_template_missing?
      errors.add(:issue_template_key, 'is empty or does not exist')
    end
  end

  def valid_project_key
    if projects_with_same_slug_and_key_exists?
      errors.add(:project_key, 'already in use for another service desk address.')
    end
  end

  def custom_email_enabled_state
    return unless custom_email_enabled?

    if custom_email_verification.blank? || !custom_email_verification.finished?
      errors.add(:custom_email_enabled, 'cannot be enabled until verification process has finished.')
    end
  end

  def tickets_confidential_by_default?
    # Tickets in public projects should always be confidential by default
    return true if project.public?

    self[:tickets_confidential_by_default]
  end

  private

  def source_template_project
    nil
  end

  def projects_with_same_slug_and_key_exists?
    return false unless project_key

    settings = self.class.with_project_key(project_key).where.not(project_id: project_id).preload(:project)
    project_slug = self.project.full_path_slug

    settings.any? do |setting|
      setting.project.full_path_slug == project_slug
    end
  end

  def needs_custom_email_credentials?
    custom_email_enabled? || custom_email_verification.present?
  end
end

ServiceDeskSetting.prepend_mod