مراية لـ
https://github.com/postalserver/postal.git
تم المزامنة 2026-01-19 22:29:47 +00:00
style(rubocop): fix all safe auto correctable offenses
هذا الالتزام موجود في:
@@ -22,22 +22,22 @@
|
||||
|
||||
class Route < ApplicationRecord
|
||||
|
||||
MODES = ['Endpoint', 'Accept', 'Hold', 'Bounce', 'Reject']
|
||||
MODES = ["Endpoint", "Accept", "Hold", "Bounce", "Reject"]
|
||||
|
||||
include HasUUID
|
||||
|
||||
belongs_to :server
|
||||
belongs_to :domain, :optional => true
|
||||
belongs_to :endpoint, :polymorphic => true, :optional => true
|
||||
has_many :additional_route_endpoints, :dependent => :destroy
|
||||
belongs_to :domain, optional: true
|
||||
belongs_to :endpoint, polymorphic: true, optional: true
|
||||
has_many :additional_route_endpoints, dependent: :destroy
|
||||
|
||||
SPAM_MODES = ['Mark', 'Quarantine', 'Fail']
|
||||
ENDPOINT_TYPES = ['SMTPEndpoint', 'HTTPEndpoint', 'AddressEndpoint']
|
||||
SPAM_MODES = ["Mark", "Quarantine", "Fail"]
|
||||
ENDPOINT_TYPES = ["SMTPEndpoint", "HTTPEndpoint", "AddressEndpoint"]
|
||||
|
||||
validates :name, :presence => true, :format => /\A(([a-z0-9\-\.]*)|(\*)|(__returnpath__))\z/
|
||||
validates :spam_mode, :inclusion => {:in => SPAM_MODES}
|
||||
validates :endpoint, :presence => {:if => proc { self.mode == 'Endpoint' }}
|
||||
validates :domain_id, :presence => {:unless => :return_path?}
|
||||
validates :name, presence: true, format: /\A(([a-z0-9\-.]*)|(\*)|(__returnpath__))\z/
|
||||
validates :spam_mode, inclusion: { in: SPAM_MODES }
|
||||
validates :endpoint, presence: { if: proc { mode == "Endpoint" } }
|
||||
validates :domain_id, presence: { unless: :return_path? }
|
||||
validate :validate_route_is_routed
|
||||
validate :validate_domain_belongs_to_server
|
||||
validate :validate_endpoint_belongs_to_server
|
||||
@@ -47,7 +47,7 @@ class Route < ApplicationRecord
|
||||
|
||||
after_save :save_additional_route_endpoints
|
||||
|
||||
random_string :token, :type => :chars, :length => 8, :unique => true
|
||||
random_string :token, type: :chars, length: 8, unique: true
|
||||
|
||||
def return_path?
|
||||
name == "__returnpath__"
|
||||
@@ -62,12 +62,10 @@ class Route < ApplicationRecord
|
||||
end
|
||||
|
||||
def _endpoint
|
||||
@endpoint ||= begin
|
||||
if self.mode == 'Endpoint'
|
||||
endpoint ? "#{endpoint.class}##{endpoint.uuid}" : nil
|
||||
else
|
||||
self.mode
|
||||
end
|
||||
if mode == "Endpoint"
|
||||
@endpoint ||= endpoint ? "#{endpoint.class}##{endpoint.uuid}" : nil
|
||||
else
|
||||
@endpoint ||= mode
|
||||
end
|
||||
end
|
||||
|
||||
@@ -75,18 +73,17 @@ class Route < ApplicationRecord
|
||||
if value.blank?
|
||||
self.endpoint = nil
|
||||
self.mode = nil
|
||||
else
|
||||
if value =~ /\#/
|
||||
class_name, id = value.split('#', 2)
|
||||
unless ENDPOINT_TYPES.include?(class_name)
|
||||
raise Postal::Error, "Invalid endpoint class name '#{class_name}'"
|
||||
end
|
||||
self.endpoint = class_name.constantize.find_by_uuid(id)
|
||||
self.mode = 'Endpoint'
|
||||
else
|
||||
self.endpoint = nil
|
||||
self.mode = value
|
||||
elsif value =~ /\#/
|
||||
class_name, id = value.split("#", 2)
|
||||
unless ENDPOINT_TYPES.include?(class_name)
|
||||
raise Postal::Error, "Invalid endpoint class name '#{class_name}'"
|
||||
end
|
||||
|
||||
self.endpoint = class_name.constantize.find_by_uuid(id)
|
||||
self.mode = "Endpoint"
|
||||
else
|
||||
self.endpoint = nil
|
||||
self.mode = value
|
||||
end
|
||||
end
|
||||
|
||||
@@ -95,7 +92,7 @@ class Route < ApplicationRecord
|
||||
end
|
||||
|
||||
def wildcard?
|
||||
self.name == '*'
|
||||
name == "*"
|
||||
end
|
||||
|
||||
def additional_route_endpoints_array
|
||||
@@ -107,50 +104,51 @@ class Route < ApplicationRecord
|
||||
end
|
||||
|
||||
def save_additional_route_endpoints
|
||||
if @additional_route_endpoints_array
|
||||
seen = []
|
||||
@additional_route_endpoints_array.each do |item|
|
||||
if existing = additional_route_endpoints.find_by_endpoint(item)
|
||||
seen << existing.id
|
||||
return unless @additional_route_endpoints_array
|
||||
|
||||
seen = []
|
||||
@additional_route_endpoints_array.each do |item|
|
||||
if existing = additional_route_endpoints.find_by_endpoint(item)
|
||||
seen << existing.id
|
||||
else
|
||||
route = additional_route_endpoints.build(_endpoint: item)
|
||||
if route.save
|
||||
seen << route.id
|
||||
else
|
||||
route = additional_route_endpoints.build(:_endpoint => item)
|
||||
if route.save
|
||||
seen << route.id
|
||||
else
|
||||
route.errors.each do |field, message|
|
||||
errors.add :base, message
|
||||
end
|
||||
raise ActiveRecord::RecordInvalid
|
||||
route.errors.each do |field, message|
|
||||
errors.add :base, message
|
||||
end
|
||||
raise ActiveRecord::RecordInvalid
|
||||
end
|
||||
end
|
||||
additional_route_endpoints.where.not(:id => seen).destroy_all
|
||||
end
|
||||
additional_route_endpoints.where.not(id: seen).destroy_all
|
||||
end
|
||||
|
||||
#
|
||||
# This message will create a suitable number of message objects for messages that
|
||||
# are destined for this route. It receives a block which can set the message content
|
||||
# are destined for this route. It receives a block which can set the message content
|
||||
# but most information is specified already.
|
||||
#
|
||||
# Returns an array of created messages.
|
||||
#
|
||||
def create_messages(&block)
|
||||
messages = []
|
||||
message = self.build_message
|
||||
if self.mode == 'Endpoint' && self.server.message_db.schema_version >= 18
|
||||
message.endpoint_type = self.endpoint_type
|
||||
message.endpoint_id = self.endpoint_id
|
||||
message = build_message
|
||||
if mode == "Endpoint" && server.message_db.schema_version >= 18
|
||||
message.endpoint_type = endpoint_type
|
||||
message.endpoint_id = endpoint_id
|
||||
end
|
||||
block.call(message)
|
||||
message.save
|
||||
messages << message
|
||||
|
||||
# Also create any messages for additional endpoints that might exist
|
||||
if self.mode == 'Endpoint' && self.server.message_db.schema_version >= 18
|
||||
self.additional_route_endpoints.each do |endpoint|
|
||||
if mode == "Endpoint" && server.message_db.schema_version >= 18
|
||||
additional_route_endpoints.each do |endpoint|
|
||||
next unless endpoint.endpoint
|
||||
message = self.build_message
|
||||
|
||||
message = build_message
|
||||
message.endpoint_id = endpoint.endpoint_id
|
||||
message.endpoint_type = endpoint.endpoint_type
|
||||
block.call(message)
|
||||
@@ -163,69 +161,67 @@ class Route < ApplicationRecord
|
||||
end
|
||||
|
||||
def build_message
|
||||
message = self.server.message_db.new_message
|
||||
message.scope = 'incoming'
|
||||
message.rcpt_to = self.description
|
||||
message.domain_id = self.domain&.id
|
||||
message.route_id = self.id
|
||||
message = server.message_db.new_message
|
||||
message.scope = "incoming"
|
||||
message.rcpt_to = description
|
||||
message.domain_id = domain&.id
|
||||
message.route_id = id
|
||||
message
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def validate_route_is_routed
|
||||
if self.mode.nil?
|
||||
errors.add :endpoint, "must be chosen"
|
||||
end
|
||||
return unless mode.nil?
|
||||
|
||||
errors.add :endpoint, "must be chosen"
|
||||
end
|
||||
|
||||
def validate_domain_belongs_to_server
|
||||
if self.domain && ![self.server, self.server.organization].include?(self.domain.owner)
|
||||
if domain && ![server, server.organization].include?(domain.owner)
|
||||
errors.add :domain, :invalid
|
||||
end
|
||||
|
||||
if self.domain && !self.domain.verified?
|
||||
errors.add :domain, "has not been verified yet"
|
||||
end
|
||||
return unless domain && !domain.verified?
|
||||
|
||||
errors.add :domain, "has not been verified yet"
|
||||
end
|
||||
|
||||
def validate_endpoint_belongs_to_server
|
||||
if self.endpoint && self.endpoint&.server != self.server
|
||||
errors.add :endpoint, :invalid
|
||||
end
|
||||
return unless endpoint && endpoint&.server != server
|
||||
|
||||
errors.add :endpoint, :invalid
|
||||
end
|
||||
|
||||
def validate_name_uniqueness
|
||||
return if self.server.nil?
|
||||
if self.domain
|
||||
if route = Route.includes(:domain).where(:domains => {:name => self.domain.name}, :name => self.name).where.not(:id => self.id).first
|
||||
return if server.nil?
|
||||
|
||||
if domain
|
||||
if route = Route.includes(:domain).where(domains: { name: domain.name }, name: name).where.not(id: id).first
|
||||
errors.add :name, "is configured on the #{route.server.full_permalink} mail server"
|
||||
end
|
||||
else
|
||||
if route = Route.where(:name => "__returnpath__").where.not(:id => self.id).exists?
|
||||
errors.add :base, "A return path route already exists for this server"
|
||||
end
|
||||
elsif route = Route.where(name: "__returnpath__").where.not(id: id).exists?
|
||||
errors.add :base, "A return path route already exists for this server"
|
||||
end
|
||||
end
|
||||
|
||||
def validate_return_path_route_endpoints
|
||||
if return_path?
|
||||
if self.mode != 'Endpoint' || self.endpoint_type != 'HTTPEndpoint'
|
||||
errors.add :base, "Return path routes must point to an HTTP endpoint"
|
||||
end
|
||||
end
|
||||
return unless return_path?
|
||||
return unless mode != "Endpoint" || endpoint_type != "HTTPEndpoint"
|
||||
|
||||
errors.add :base, "Return path routes must point to an HTTP endpoint"
|
||||
end
|
||||
|
||||
def validate_no_additional_routes_on_non_endpoint_route
|
||||
if self.mode != 'Endpoint' && !self.additional_route_endpoints_array.empty?
|
||||
errors.add :base, "Additional routes are not permitted unless the primary route is an actual endpoint"
|
||||
end
|
||||
return unless mode != "Endpoint" && !additional_route_endpoints_array.empty?
|
||||
|
||||
errors.add :base, "Additional routes are not permitted unless the primary route is an actual endpoint"
|
||||
end
|
||||
|
||||
def self.find_by_name_and_domain(name, domain)
|
||||
route = Route.includes(:domain).where(:name => name, :domains => {:name => domain}).first
|
||||
route = Route.includes(:domain).where(name: name, domains: { name: domain }).first
|
||||
if route.nil?
|
||||
route = Route.includes(:domain).where(:name => '*', :domains => {:name => domain}).first
|
||||
route = Route.includes(:domain).where(name: "*", domains: { name: domain }).first
|
||||
end
|
||||
route
|
||||
end
|
||||
|
||||
المرجع في مشكلة جديدة
حظر مستخدم