From e067486581bac1ea7b6ca5084e637d09153cdf07 Mon Sep 17 00:00:00 2001 From: Claudius Nicolae Date: Tue, 5 Mar 2013 12:06:00 +0200 Subject: [PATCH] audit --- Gemfile | 1 + Gemfile.lock | 5 ++++ app/models/domain.rb | 3 +++ app/models/permission.rb | 1 + app/models/record.rb | 1 + app/models/user.rb | 4 ++- config/initializers/rails_admin.rb | 3 ++- db/migrate/20130305075639_install_audited.rb | 28 ++++++++++++++++++++ 8 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20130305075639_install_audited.rb diff --git a/Gemfile b/Gemfile index 9614e34..a277327 100644 --- a/Gemfile +++ b/Gemfile @@ -36,6 +36,7 @@ gem 'seedbank', '~> 0.1.3' gem 'rails_admin', '~> 0.0.5' gem 'rails-settings-cached', '~> 0.2.2' gem 'unicorn', '~> 4.5.0' +gem "audited-activerecord", '~> 3.0' # Gems used only for assets and not required # in production environments by default. diff --git a/Gemfile.lock b/Gemfile.lock index 21f2707..86725ee 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,6 +56,10 @@ GEM acts_as_nested_interval (0.0.10) rails (~> 3.2.1) arel (3.0.2) + audited (3.0.0) + audited-activerecord (3.0.0) + activerecord (~> 3.0) + audited (= 3.0.0) bbenezech-nested_form (0.0.6) bcrypt-ruby (3.0.1) bootstrap-sass (2.1.0.1) @@ -333,6 +337,7 @@ DEPENDENCIES active-model-email-validator (~> 1.0.2) active_scaffold (~> 3.2.16) acts_as_nested_interval (~> 0.0.7) + audited-activerecord (~> 3.0) bootstrap-sass (~> 2.1.0.0) cancan (= 1.6.7) capistrano (~> 2.9.0) diff --git a/app/models/domain.rb b/app/models/domain.rb index 79a6f5d..1ffd3c2 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -2,11 +2,14 @@ class Domain < ActiveRecord::Base self.inheritance_column = :sti_disabled nilify_blanks stampable + audited # optional IP for create form, results in a type A record attr_accessor :ip attr_accessor :domain_ownership_failed + attr_accessible :name, :ip, :soa_record, :ns_records, :apply_subdomains + belongs_to :user, :inverse_of => :domain has_many :records, :inverse_of => :domain, :dependent => :destroy has_many :permissions, :inverse_of => :domain, :dependent => :destroy diff --git a/app/models/permission.rb b/app/models/permission.rb index 62fbcc6..0edb89a 100644 --- a/app/models/permission.rb +++ b/app/models/permission.rb @@ -1,5 +1,6 @@ class Permission < ActiveRecord::Base stampable + audited belongs_to :domain, :inverse_of => :permissions belongs_to :user, :inverse_of => :permissions diff --git a/app/models/record.rb b/app/models/record.rb index 3f61135..dddfcd0 100644 --- a/app/models/record.rb +++ b/app/models/record.rb @@ -1,5 +1,6 @@ class Record < ActiveRecord::Base stampable + audited belongs_to :domain, :inverse_of => :records belongs_to :user, :inverse_of => :records diff --git a/app/models/user.rb b/app/models/user.rb index 77500e3..93205fd 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,6 +2,7 @@ class User < ActiveRecord::Base include SentientModel model_stamper stampable + audited # Include default devise modules. Others available are: # :token_authenticatable, :encryptable, :timeoutable and :omniauthable @@ -17,7 +18,8 @@ class User < ActiveRecord::Base validates :first_name, :last_name, :presence => true # Setup accessible (or protected) attributes for your model - attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name + attr_accessible :email, :password, :password_confirmation, :remember_me, + :first_name, :last_name has_many :domains, :inverse_of => :user, :dependent => :destroy has_many :records, :inverse_of => :user, :dependent => :destroy diff --git a/config/initializers/rails_admin.rb b/config/initializers/rails_admin.rb index 0dc9a33..d8216fe 100644 --- a/config/initializers/rails_admin.rb +++ b/config/initializers/rails_admin.rb @@ -34,7 +34,8 @@ RailsAdmin.config do |config| # config.excluded_models = [A, AAAA, Admin, CNAME, Domain, MX, NS, Permission, Record, SOA, SRV, TXT, User] # Add models here if you want to go 'whitelist mode': - # config.included_models = [A, AAAA, Admin, CNAME, Domain, MX, NS, Permission, Record, SOA, SRV, TXT, User] + config.included_models = [A, AAAA, Admin, CNAME, Domain, MX, NS, Permission, + Record, SOA, SRV, TXT, User, Audited::Adapters::ActiveRecord::Audit] # Application wide tried label methods for models' instances # config.label_methods << :description # Default is [:name, :title] diff --git a/db/migrate/20130305075639_install_audited.rb b/db/migrate/20130305075639_install_audited.rb new file mode 100644 index 0000000..fb2a927 --- /dev/null +++ b/db/migrate/20130305075639_install_audited.rb @@ -0,0 +1,28 @@ +class InstallAudited < ActiveRecord::Migration + def self.up + create_table :audits, :force => true do |t| + t.column :auditable_id, :integer + t.column :auditable_type, :string + t.column :associated_id, :integer + t.column :associated_type, :string + t.column :user_id, :integer + t.column :user_type, :string + t.column :username, :string + t.column :action, :string + t.column :audited_changes, :text + t.column :version, :integer, :default => 0 + t.column :comment, :string + t.column :remote_address, :string + t.column :created_at, :datetime + end + + add_index :audits, [:auditable_id, :auditable_type], :name => 'auditable_index' + add_index :audits, [:associated_id, :associated_type], :name => 'associated_index' + add_index :audits, [:user_id, :user_type], :name => 'user_index' + add_index :audits, :created_at + end + + def self.down + drop_table :audits + end +end