diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2a918d8..54d6aa0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,39 +1,45 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception attr_writer :breadcrumb helper_method :admin? def admin? - params.key?(:admin) + not params.key?(:user) + end + + def admin_only! + return if admin? + + redirect_to root_path, alert: 'Admin only area!' end private def group @group ||= group_scope.find(params[:group_id] || params[:id]) end def domain @domain ||= domain_scope.find(params[:domain_id] || params[:id]) end def record @record ||= record_scope.find(params[:record_id] || params[:id]) end def group_scope - @group_scope ||= current_user.groups + @group_scope ||= admin? ? Group.all : current_user.groups end def domain_scope - @domain_scope ||= Domain.where(group: group_scope) + @domain_scope ||= admin? ? Domain.all : Domain.where(group: group_scope) end def record_scope @record_scope ||= domain.records end end diff --git a/app/views/shared/_nav.html.erb b/app/views/shared/_nav.html.erb index 03e23cc..cd40a8e 100644 --- a/app/views/shared/_nav.html.erb +++ b/app/views/shared/_nav.html.erb @@ -1,35 +1,38 @@ diff --git a/config/routes.rb b/config/routes.rb index 7ef49f0..11badcc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,31 +1,35 @@ Rails.application.routes.draw do # Override devise user removal devise_scope :users do delete :users, to: redirect('/') end devise_for :users root to: redirect('/domains') resources :groups, only: [:show] do get :search_member, to: 'groups#search_member', on: :member post :members, to: 'groups#create_member', as: :create_member, on: :member delete 'member/:user_id', to: 'groups#destroy_member', as: :destroy_member, on: :member end resources :domains do resources :records, except: [:index, :show] do # Reuse records#update instead of introducing new controller actions # # rubocop:disable Style/AlignHash put :disable, to: 'records#update', on: :member, defaults: { record: { disabled: true } } put :enable, to: 'records#update', on: :member, defaults: { record: { disabled: false } } # rubocop:enable Style/AlignHash end end + + # Admin + namespace :admin do + end end