From bfdbdf338c2d37d9c1434396e2ab46dc17881549 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Sat, 20 Oct 2018 14:39:26 -0500 Subject: [PATCH 1/2] add rails_admin for site_administrators --- Gemfile | 3 ++ Gemfile.lock | 31 ++++++++++++++++++++ config/initializers/rails_admin.rb | 45 ++++++++++++++++++++++++++++++ config/routes.rb | 3 +- 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 config/initializers/rails_admin.rb diff --git a/Gemfile b/Gemfile index db0af898..b6d09f91 100644 --- a/Gemfile +++ b/Gemfile @@ -72,6 +72,9 @@ gem 'delayed_job_active_record' # Exports gem 'csv' +# Admin +gem 'rails_admin', '~> 1.3' + # Tech debt & hacks gem 'binding_of_caller' # see has_changelog.rb diff --git a/Gemfile.lock b/Gemfile.lock index 758790bd..5f9fdb64 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -177,6 +177,8 @@ GEM ffi (1.9.25) filesize (0.1.1) flamegraph (0.9.5) + font-awesome-rails (4.7.0.4) + railties (>= 3.2, < 6.0) formatador (0.2.5) friendly_id (5.2.4) activerecord (>= 4.0.0) @@ -200,6 +202,9 @@ GEM guard-rubocop (1.3.0) guard (~> 2.0) rubocop (~> 0.20) + haml (5.0.4) + temple (>= 0.8.0) + tilt hashdiff (0.3.7) html-pipeline (2.8.0) activesupport (>= 2) @@ -212,6 +217,12 @@ GEM nokogiri (>= 1.6) jaro_winkler (1.4.0) jmespath (1.4.0) + jquery-rails (4.3.3) + rails-dom-testing (>= 1, < 3) + railties (>= 4.2.0) + thor (>= 0.14, < 2.0) + jquery-ui-rails (6.0.1) + railties (>= 3.2.16) json (1.8.6) kaminari (1.1.1) activesupport (>= 4.1.0) @@ -263,6 +274,7 @@ GEM multipart-post (2.0.0) mustache (1.0.5) nenv (0.3.0) + nested_form (0.3.2) newrelic_rpm (5.2.0.345) nio4r (2.3.1) nokogiri (1.8.4) @@ -305,6 +317,9 @@ GEM rack (2.0.5) rack-mini-profiler (1.0.0) rack (>= 1.2.0) + rack-pjax (1.0.0) + nokogiri (~> 1.5) + rack (>= 1.1) rack-test (1.0.0) rack (>= 1.0, < 3) rack-timeout (0.5.1) @@ -338,6 +353,19 @@ GEM rails_12factor (0.0.3) rails_serve_static_assets rails_stdout_logging + rails_admin (1.4.2) + builder (~> 3.1) + coffee-rails (~> 4.0) + font-awesome-rails (>= 3.0, < 5) + haml (>= 4.0, < 6) + jquery-rails (>= 3.0, < 5) + jquery-ui-rails (>= 5.0, < 7) + kaminari (>= 0.14, < 2.0) + nested_form (~> 0.3) + rack-pjax (>= 0.7) + rails (>= 4.0, < 6) + remotipart (~> 1.3) + sass-rails (>= 4.0, < 6) rails_serve_static_assets (0.0.5) rails_stdout_logging (0.0.5) railties (5.2.0) @@ -354,6 +382,7 @@ GEM ffi (>= 0.5.0, < 2) redcarpet (3.4.0) ref (2.0.0) + remotipart (1.4.2) responders (2.4.0) actionpack (>= 4.2.0, < 5.3) railties (>= 4.2.0, < 5.3) @@ -444,6 +473,7 @@ GEM stripe_event (2.1.1) activesupport (>= 3.1) stripe (>= 2.8, < 4.0) + temple (0.8.0) terrapin (0.6.0) climate_control (>= 0.0.3, < 1.0) therubyracer (0.12.3) @@ -547,6 +577,7 @@ DEPENDENCIES rails-perftest rails-ujs rails_12factor + rails_admin (~> 1.3) redcarpet rmagick rspec-prof diff --git a/config/initializers/rails_admin.rb b/config/initializers/rails_admin.rb new file mode 100644 index 00000000..6f8eaef5 --- /dev/null +++ b/config/initializers/rails_admin.rb @@ -0,0 +1,45 @@ +RailsAdmin.config do |config| + + ### Popular gems integration + + ## == Devise == + # config.authenticate_with do + # warden.authenticate! scope: :user + # end + # config.current_user_method(&:current_user) + + ## == Cancan == + # config.authorize_with :cancan + + ## == Pundit == + # config.authorize_with :pundit + + ## == PaperTrail == + # config.audit_with :paper_trail, 'User', 'PaperTrail::Version' # PaperTrail >= 3.0.0 + + ### More at https://github.com/sferik/rails_admin/wiki/Base-configuration + + ## == Gravatar integration == + ## To disable Gravatar integration in Navigation Bar set to false + # config.show_gravatar = true + + config.actions do + dashboard # mandatory + index # mandatory + new + export + bulk_delete + show + edit + delete + show_in_app + + ## With an audit adapter, you can add: + # history_index + # history_show + end + + config.authorize_with do + redirect_to main_app.root_path unless user_signed_in? && current_user.site_administrator? + end +end diff --git a/config/routes.rb b/config/routes.rb index 353bd86e..f4fc0232 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -163,13 +163,14 @@ Rails.application.routes.draw do end get 'search/', to: 'search#results' - scope 'admin' do + scope 'admin_stats' do get '/', to: 'admin#dashboard', as: :admin_dashboard get '/content_type/:type', to: 'admin#content_type', as: :admin_content_type get '/attributes', to: 'admin#attributes', as: :admin_attributes get '/masquerade/:user_id', to: 'admin#masquerade', as: :masquerade end + mount RailsAdmin::Engine => '/admin', as: 'rails_admin' scope 'export' do get '/', to: 'export#index', as: :notebook_export From a2f85fe512f8559b5023c8fc4732909a9e12c800 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Sat, 20 Oct 2018 15:01:17 -0500 Subject: [PATCH 2/2] limit rails_admin to just users --- config/initializers/rails_admin.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/config/initializers/rails_admin.rb b/config/initializers/rails_admin.rb index 6f8eaef5..634c9eb7 100644 --- a/config/initializers/rails_admin.rb +++ b/config/initializers/rails_admin.rb @@ -42,4 +42,13 @@ RailsAdmin.config do |config| config.authorize_with do redirect_to main_app.root_path unless user_signed_in? && current_user.site_administrator? end + + config.included_models = ["User"] + # Todo whitelist the fields we want to show for each model + # config.model 'User' do + # list do + # field :name + # field :created_at + # end + # end end