Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' into rails4
Browse files Browse the repository at this point in the history
Conflicts:
	Gemfile.lock
	app/models/domain.rb
	config/settings.yml
	db/schema.rb
  • Loading branch information
nning committed Jul 2, 2014
2 parents 1161bd1 + 9f8c6b9 commit b454b58
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 3 deletions.
6 changes: 6 additions & 0 deletions app/controllers/admin/domains_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ class Admin::DomainsController < AdminController

actions :all, except: [:show, :index]

# Check if MX records on all managable Domains and redirect to index.
def check_mx_records
Domain.managable(current_mailbox).all.map(&:update_mx_set!)
redirect_to [:admin, :domains]
end

# Create Domain and redirect to index.
def create
super do |success, error|
Expand Down
8 changes: 7 additions & 1 deletion app/models/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Domain < ActiveRecord::Base

before_save -> { name.downcase! }

has_paper_trail class_name: 'Version'
has_paper_trail class_name: 'Version', ignore: :mx_set

searchkick word_middle: [:name, :description]
# Search fields options includable in search on model.
Expand Down Expand Up @@ -63,6 +63,12 @@ def mailboxes_for_select
mailboxes.map { |m| [m.email, m.id] }
end

# Check if MX of Domain is set to default MX for Møil instance.
def update_mx_set!
self.mx_set = (Resolve.address(Resolve.mx(self)) == Resolve.address(Settings.mx_check_domain))
save!
end

# String representation.
def to_s
name
Expand Down
3 changes: 3 additions & 0 deletions app/views/admin/domains/_domain.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
%td= link_to domain.aliases.count, [:admin, domain, :aliases]
%td= link_to domain.permissions.count, [:admin, domain, :permissions]
%td= link_to_alias_or_mailbox domain.catch_all_address
%td
- unless domain.mx_set?
%span.label.label-warning no
%td
- if can? :destroy, domain
= link_to [:admin, domain], method: :delete, data: { confirm: 'Are you really sure? This will delete all mailboxes and aliases of this domain, too.' }, class: 'btn btn-danger btn-xs' do
Expand Down
11 changes: 10 additions & 1 deletion app/views/admin/domains/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
.page-header
%h1 Domains
- if can? :create, :domain
= link_to_create new_admin_domain_path
.btn-toolbar
.btn-group
= link_to_create [:new, :admin, :domain]
%button.btn.btn-default.dropdown-toggle{data: {toggle: 'dropdown'}}
Actions
%span.caret
%ul.dropdown-menu{role: 'menu'}
%li
= link_to 'Check MX records', [:check_mx_records, :admin, :domains], method: :post

%p
Got
Expand All @@ -20,6 +28,7 @@
= icon :certificate
Permissions
%th Catch-All
%th MX?
%th
%tbody
= render collection.all
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
resources :aliases, except: :show
resources :mailboxes, except: :show
resources :permissions, except: :show
post 'check_mx_records', on: :collection
end
resources :versions, only: :index do
get 'updates', on: :collection
Expand Down
2 changes: 2 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ elasticsearch: true

spam_alias_username_length: 6

mx_check_domain: mx.orgizm.net

dirty_rails4_warden_workaround: true
5 changes: 5 additions & 0 deletions db/migrate/20140701123952_add_mx_set_to_domain.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddMxSetToDomain < ActiveRecord::Migration
def change
add_column :domains, :mx_set, :boolean, default: true
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140203233345) do
ActiveRecord::Schema.define(version: 20140701123952) do

create_table "aliases", force: true do |t|
t.string "username"
Expand All @@ -34,6 +34,7 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "quick_access", default: true
t.boolean "mx_set", default: true
end

add_index "domains", ["name"], name: "index_domains_on_name", unique: true
Expand Down
23 changes: 23 additions & 0 deletions lib/resolve.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'resolv'

# Make resolving DNS records a little more handy.
module Resolve
# Resolves A or AAAA record.
def self.address(domain)
Resolv::DNS.new
.getaddress(domain.to_s)
.to_s
rescue Resolv::ResolvError
nil
end

# Resolves MX record.
def self.mx(domain)
Resolv::DNS.new
.getresource(domain.to_s, Resolv::DNS::Resource::IN::MX)
.exchange
.to_s
rescue Resolv::ResolvError
nil
end
end

0 comments on commit b454b58

Please sign in to comment.